🎨 update models

This commit is contained in:
landaiqing
2024-08-09 22:02:44 +08:00
parent 331835f949
commit 6148fc74c6
16 changed files with 202 additions and 38 deletions

View File

@@ -6,6 +6,7 @@ import (
"schisandra-cloud-album/common/result"
"schisandra-cloud-album/model"
"schisandra-cloud-album/service"
"schisandra-cloud-album/utils"
)
var authService = service.Service.AuthService
@@ -28,7 +29,7 @@ func (AuthAPI) GetUserList(c *gin.Context) {
// @Router /api/auth/user/query_by_username [get]
func (AuthAPI) QueryUserByUsername(c *gin.Context) {
username := c.Query("username")
user := authService.QueryUserByName(username)
user := authService.QueryUserByUsername(username)
if reflect.DeepEqual(user, model.ScaAuthUser{}) {
result.FailWithMessage("用户不存在!", c)
return
@@ -83,3 +84,75 @@ func (AuthAPI) QueryUserByPhone(c *gin.Context) {
}
result.OkWithData(user, c)
}
// AccountLogin 账号登录
// @Summary 账号登录
// @Tags 鉴权模块
// @Param account query string true "账号"
// @Param password query string true "密码"
// @Success 200 {string} json
// @Router /api/auth/user/login [post]
func (AuthAPI) AccountLogin(c *gin.Context) {
account := c.PostForm("account")
password := c.PostForm("password")
isPhone := utils.IsPhone(account)
if isPhone {
user := authService.QueryUserByPhone(account)
if reflect.DeepEqual(user, model.ScaAuthUser{}) {
result.FailWithMessage("手机号未注册!", c)
} else {
verify := utils.Verify(password, *user.Password)
if verify {
result.OkWithData(user, c)
} else {
result.FailWithMessage("密码错误!", c)
}
}
}
isEmail := utils.IsEmail(account)
if isEmail {
user := authService.QueryUserByEmail(account)
if reflect.DeepEqual(user, model.ScaAuthUser{}) {
result.FailWithMessage("邮箱未注册!", c)
} else {
verify := utils.Verify(password, *user.Password)
if verify {
result.OkWithData(user, c)
} else {
result.FailWithMessage("密码错误!", c)
}
}
}
isUsername := utils.IsUsername(account)
if isUsername {
user := authService.QueryUserByUsername(account)
if reflect.DeepEqual(user, model.ScaAuthUser{}) {
result.FailWithMessage("用户名未注册!", c)
} else {
verify := utils.Verify(password, *user.Password)
if verify {
result.OkWithData(user, c)
} else {
result.FailWithMessage("密码错误!", c)
}
}
}
}
// Register 用户注册
// @Summary 用户注册
// @Tags 鉴权模块
// @Param user body model.ScaAuthUser true "用户信息"
// @Success 200 {string} json
// @Router /api/auth/user/register [post]
func (AuthAPI) Register(c *gin.Context) {
var user model.ScaAuthUser
_ = c.ShouldBindJSON(&user)
err := authService.AddUser(user)
if err != nil {
result.FailWithMessage("用户注册失败!", c)
return
}
result.OkWithMessage("用户注册成功!", c)
}

View File

@@ -56,6 +56,38 @@ const docTemplate = `{
}
}
},
"/api/auth/user/login": {
"post": {
"tags": [
"鉴权模块"
],
"summary": "账号登录",
"parameters": [
{
"type": "string",
"description": "账号",
"name": "account",
"in": "query",
"required": true
},
{
"type": "string",
"description": "密码",
"name": "password",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
},
"/api/auth/user/query_by_phone": {
"get": {
"tags": [

View File

@@ -45,6 +45,38 @@
}
}
},
"/api/auth/user/login": {
"post": {
"tags": [
"鉴权模块"
],
"summary": "账号登录",
"parameters": [
{
"type": "string",
"description": "账号",
"name": "account",
"in": "query",
"required": true
},
{
"type": "string",
"description": "密码",
"name": "password",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
},
"/api/auth/user/query_by_phone": {
"get": {
"tags": [

View File

@@ -27,6 +27,27 @@ paths:
summary: 删除用户
tags:
- 鉴权模块
/api/auth/user/login:
post:
parameters:
- description: 账号
in: query
name: account
required: true
type: string
- description: 密码
in: query
name: password
required: true
type: string
responses:
"200":
description: OK
schema:
type: string
summary: 账号登录
tags:
- 鉴权模块
/api/auth/user/query_by_phone:
get:
parameters:

4
go.mod
View File

@@ -3,6 +3,7 @@ module schisandra-cloud-album
go 1.22.5
require (
github.com/gin-contrib/cors v1.7.2
github.com/gin-gonic/gin v1.10.0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/redis/go-redis/v9 v9.6.1
@@ -12,6 +13,7 @@ require (
github.com/swaggo/swag v1.16.3
github.com/wenlng/go-captcha-assets v1.0.1
github.com/wenlng/go-captcha/v2 v2.0.0
golang.org/x/crypto v0.25.0
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/mysql v1.5.7
gorm.io/gen v0.3.26
@@ -28,7 +30,6 @@ require (
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/gabriel-vasile/mimetype v1.4.5 // indirect
github.com/gin-contrib/cors v1.7.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
@@ -53,7 +54,6 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/image v0.18.0 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.27.0 // indirect

View File

@@ -1,7 +1,3 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package model
import (

View File

@@ -1,7 +1,3 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package model
import (

View File

@@ -1,7 +1,3 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package model
import (

View File

@@ -1,7 +1,3 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package model
import (
@@ -12,23 +8,23 @@ const TableNameScaAuthUser = "sca_auth_user"
// ScaAuthUser 用户表
type ScaAuthUser struct {
ID int64 `gorm:"column:id;type:bigint(255);primaryKey;autoIncrement:true;comment:自增ID" json:"id"` // 自增ID
ID int64 `gorm:"column:id;type:bigint(255);primaryKey;autoIncrement:true;comment:自增ID" json:"-"` // 自增ID
UUID *string `gorm:"column:uuid;type:varchar(255);comment:唯一ID" json:"uuid"` // 唯一ID
Username *string `gorm:"column:username;type:varchar(32);comment:用户名" json:"username"` // 用户名
Nickname *string `gorm:"column:nickname;type:varchar(32);comment:昵称" json:"nickname"` // 昵称
Email *string `gorm:"column:email;type:varchar(32);comment:邮箱" json:"email"` // 邮箱
Phone *string `gorm:"column:phone;type:varchar(32);comment:电话" json:"phone"` // 电话
Password *string `gorm:"column:password;type:varchar(64);comment:密码" json:"password"` // 密码
Password *string `gorm:"column:password;type:varchar(64);comment:密码" json:"-"` // 密码
Gender *string `gorm:"column:gender;type:varchar(32);comment:性别" json:"gender"` // 性别
Avatar *string `gorm:"column:avatar;type:varchar(255);comment:头像" json:"avatar"` // 头像
Status *int64 `gorm:"column:status;type:tinyint(4);comment:状态 0 正常 1 封禁" json:"status"` // 状态 0 正常 1 封禁
Introduce *string `gorm:"column:introduce;type:varchar(255);comment:介绍" json:"introduce"` // 介绍
ExtJSON *string `gorm:"column:ext_json;type:varchar(255);comment:额外字段" json:"ext_json"` // 额外字段
ExtJSON *string `gorm:"column:ext_json;type:varchar(255);comment:额外字段" json:"-"` // 额外字段
CreatedBy *string `gorm:"column:created_by;type:varchar(32);comment:创建人" json:"created_by"` // 创建人
CreatedTime *time.Time `gorm:"column:created_time;type:datetime;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_time"` // 创建时间
UpdateBy *string `gorm:"column:update_by;type:varchar(32);comment:更新人" json:"update_by"` // 更新人
UpdateTime *time.Time `gorm:"column:update_time;type:datetime;default:CURRENT_TIMESTAMP;comment:更新时间" json:"update_time"` // 更新时间
Deleted *int64 `gorm:"column:deleted;type:int(11);comment:是否删除 0 未删除 1 已删除" json:"deleted"` // 是否删除 0 未删除 1 已删除
Deleted *int64 `gorm:"column:deleted;type:int(11);comment:是否删除 0 未删除 1 已删除" json:"-"` // 是否删除 0 未删除 1 已删除
Blog *string `gorm:"column:blog;type:varchar(255);comment:博客" json:"blog"` // 博客
Location *string `gorm:"column:location;type:varchar(255);comment:地址" json:"location"` // 地址
Company *string `gorm:"column:company;type:varchar(255);comment:公司" json:"company"` // 公司

View File

@@ -1,7 +1,3 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package model
import (

View File

@@ -1,7 +1,3 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package model
import (

View File

@@ -1,7 +1,3 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package model
import (

View File

@@ -14,4 +14,6 @@ func AuthRouter(router *gin.RouterGroup) {
group.GET("/user/query_by_uuid", authApi.QueryUserByUuid)
group.DELETE("/user/delete", authApi.DeleteUser)
group.GET("/user/query_by_phone", authApi.QueryUserByPhone)
group.POST("/user/login", authApi.AccountLogin)
group.POST("/user/register", authApi.Register)
}

View File

@@ -14,8 +14,8 @@ func (AuthService) GetUserList() []*model.ScaAuthUser {
return data
}
// QueryUserByName 根据用户名查询用户
func (AuthService) QueryUserByName(username string) model.ScaAuthUser {
// QueryUserByUsername 根据用户名查询用户
func (AuthService) QueryUserByUsername(username string) model.ScaAuthUser {
authUser := model.ScaAuthUser{}
global.DB.Where("username = ? and deleted = 0", username).First(&authUser)
return authUser

View File

@@ -13,7 +13,7 @@ func Encrypt(val string) (string, error) {
}
// Verify 验证
func Verify(hashedVal, val string) bool {
func Verify(hashedVal string, val string) bool {
// 使用bcrypt库的CompareHashAndPassword函数比较密码
err := bcrypt.CompareHashAndPassword([]byte(hashedVal), []byte(val))
return err == nil

32
utils/match.go Normal file
View File

@@ -0,0 +1,32 @@
package utils
import "regexp"
// IsEmail 判断是否为邮箱
func IsEmail(email string) bool {
// 邮箱的正则表达式
emailRegex := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`
match, _ := regexp.MatchString(emailRegex, email)
return match
}
// IsPhone 判断是否为手机号
func IsPhone(phone string) bool {
// 手机号的正则表达式,这里以中国大陆的手机号为例
phoneRegex := `^1[3-9]\d{9}$`
match, _ := regexp.MatchString(phoneRegex, phone)
return match
}
// IsUsername 用户名的正则表达式
func IsUsername(username string) bool {
/**
1.用户名仅能使用数字,大小写字母和下划线。
2.用户名中的数字必须在最后。 数字可以有零个或多个。
3.用户名不能以数字开头。 用户名字母可以是小写字母和大写字母。
4.用户名长度必须至少为3个字符。 两位用户名只能使用字母最多20个字符
*/
phoneRegex := `^[a-zA-Z_]{2,18}[0-9]*$`
match, _ := regexp.MatchString(phoneRegex, username)
return match
}