From 6148fc74c6b6dbe3a7ce51fd3ee53f2925991111 Mon Sep 17 00:00:00 2001 From: landaiqing <3517283258@qq.com> Date: Fri, 9 Aug 2024 22:02:44 +0800 Subject: [PATCH] :art: update models --- api/auth_api/auth_api.go | 75 ++++++++++++++++++- docs/docs.go | 32 ++++++++ docs/swagger.json | 32 ++++++++ docs/swagger.yaml | 21 ++++++ go.mod | 4 +- ...rmission.gen.go => sca_auth_permission.go} | 4 - ...{sca_auth_role.gen.go => sca_auth_role.go} | 4 - ...ion.gen.go => sca_auth_role_permission.go} | 4 - ...{sca_auth_user.gen.go => sca_auth_user.go} | 12 +-- ..._device.gen.go => sca_auth_user_device.go} | 4 - ...user_role.gen.go => sca_auth_user_role.go} | 4 - ..._social.gen.go => sca_auth_user_social.go} | 4 - router/modules/auth_user_router.go | 2 + service/auth_service/auth_service.go | 4 +- utils/encrypt.go | 2 +- utils/match.go | 32 ++++++++ 16 files changed, 202 insertions(+), 38 deletions(-) rename model/{sca_auth_permission.gen.go => sca_auth_permission.go} (94%) rename model/{sca_auth_role.gen.go => sca_auth_role.go} (91%) rename model/{sca_auth_role_permission.gen.go => sca_auth_role_permission.go} (91%) rename model/{sca_auth_user.gen.go => sca_auth_user.go} (87%) rename model/{sca_auth_user_device.gen.go => sca_auth_user_device.go} (94%) rename model/{sca_auth_user_role.gen.go => sca_auth_user_role.go} (91%) rename model/{sca_auth_user_social.gen.go => sca_auth_user_social.go} (97%) create mode 100644 utils/match.go diff --git a/api/auth_api/auth_api.go b/api/auth_api/auth_api.go index 4c972ba..14342ad 100644 --- a/api/auth_api/auth_api.go +++ b/api/auth_api/auth_api.go @@ -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) +} diff --git a/docs/docs.go b/docs/docs.go index 659cf1e..1e59c81 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -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": [ diff --git a/docs/swagger.json b/docs/swagger.json index 5aeda8f..ddec889 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -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": [ diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 07df8de..76848ab 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -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: diff --git a/go.mod b/go.mod index 2c4b21b..a35a1c0 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/model/sca_auth_permission.gen.go b/model/sca_auth_permission.go similarity index 94% rename from model/sca_auth_permission.gen.go rename to model/sca_auth_permission.go index b1b9588..c5789cb 100644 --- a/model/sca_auth_permission.gen.go +++ b/model/sca_auth_permission.go @@ -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 ( diff --git a/model/sca_auth_role.gen.go b/model/sca_auth_role.go similarity index 91% rename from model/sca_auth_role.gen.go rename to model/sca_auth_role.go index e5a9dd7..be08912 100644 --- a/model/sca_auth_role.gen.go +++ b/model/sca_auth_role.go @@ -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 ( diff --git a/model/sca_auth_role_permission.gen.go b/model/sca_auth_role_permission.go similarity index 91% rename from model/sca_auth_role_permission.gen.go rename to model/sca_auth_role_permission.go index 61db414..534b16a 100644 --- a/model/sca_auth_role_permission.gen.go +++ b/model/sca_auth_role_permission.go @@ -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 ( diff --git a/model/sca_auth_user.gen.go b/model/sca_auth_user.go similarity index 87% rename from model/sca_auth_user.gen.go rename to model/sca_auth_user.go index 66e6eb2..99edd33 100644 --- a/model/sca_auth_user.gen.go +++ b/model/sca_auth_user.go @@ -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"` // 公司 diff --git a/model/sca_auth_user_device.gen.go b/model/sca_auth_user_device.go similarity index 94% rename from model/sca_auth_user_device.gen.go rename to model/sca_auth_user_device.go index 57589a3..1f17b89 100644 --- a/model/sca_auth_user_device.gen.go +++ b/model/sca_auth_user_device.go @@ -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 ( diff --git a/model/sca_auth_user_role.gen.go b/model/sca_auth_user_role.go similarity index 91% rename from model/sca_auth_user_role.gen.go rename to model/sca_auth_user_role.go index 066f379..27aa876 100644 --- a/model/sca_auth_user_role.gen.go +++ b/model/sca_auth_user_role.go @@ -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 ( diff --git a/model/sca_auth_user_social.gen.go b/model/sca_auth_user_social.go similarity index 97% rename from model/sca_auth_user_social.gen.go rename to model/sca_auth_user_social.go index 9a1cbeb..42db95f 100644 --- a/model/sca_auth_user_social.gen.go +++ b/model/sca_auth_user_social.go @@ -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 ( diff --git a/router/modules/auth_user_router.go b/router/modules/auth_user_router.go index 6ad7434..0098963 100644 --- a/router/modules/auth_user_router.go +++ b/router/modules/auth_user_router.go @@ -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) } diff --git a/service/auth_service/auth_service.go b/service/auth_service/auth_service.go index 53724c0..0d56df9 100644 --- a/service/auth_service/auth_service.go +++ b/service/auth_service/auth_service.go @@ -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 diff --git a/utils/encrypt.go b/utils/encrypt.go index 9eb8115..363c7ad 100644 --- a/utils/encrypt.go +++ b/utils/encrypt.go @@ -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 diff --git a/utils/match.go b/utils/match.go new file mode 100644 index 0000000..c954b30 --- /dev/null +++ b/utils/match.go @@ -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 +}