From 22b4d551241aa9529dc0e029f2d997195f7f3ef6 Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Fri, 22 Nov 2024 16:24:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E7=99=BB=E5=BD=95=20?= =?UTF-8?q?=E6=89=BE=E5=9B=9E=E5=AF=86=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/hdl/hdl_login.go | 212 ++++++++++++++++++++++++++++++++----------- app/hdl/hdl_user.go | 2 + app/md/md_login.go | 17 +++- app/md/md_user.go | 1 + app/router/router.go | 12 ++- 5 files changed, 185 insertions(+), 59 deletions(-) diff --git a/app/hdl/hdl_login.go b/app/hdl/hdl_login.go index d1ede1f..21f1acf 100644 --- a/app/hdl/hdl_login.go +++ b/app/hdl/hdl_login.go @@ -109,6 +109,103 @@ func FastLogin(c *gin.Context) { commReq(c, reqs) } +// WechatLogin +// @Summary 微信登陆 +// @Tags 微信登陆 +// @Description 微信登陆 +// @Accept json +// @Produce json +// @Param req body md.WechatLoginReq true "注册参数" +// @Success 200 {object} md.LoginResponse "登录成功返回" +// @Failure 400 {object} md.Response "具体错误" +// @Router /api/v1/wechatLogin [post] +func WechatLogin(c *gin.Context) { + var req md.WechatLoginReq + err := c.ShouldBindJSON(&req) + if err != nil { + err = svc.HandleValidateErr(err) + err1 := err.(e.E) + e.OutErr(c, err1.Code, err1.Error()) + return + } + if req.OpenID == "" { + e.OutErr(c, 400, e.NewErr(400, "登录失败")) + return + } + param := map[string]interface{}{ + "key": "open_id", + "value": req.OpenID, + } + if req.UnionId != "" { + param = map[string]interface{}{ + "key": "union_id", + "value": req.UnionId, + } + } + // 如果内存缓存中没有,则从 数据库查询 + newUserDb := implement.NewUserDb(db.Db) + userModel, err := newUserDb.UserGetOneByParams(param) + if err != nil { + e.OutErr(c, 400, e.NewErr(400, "登录失败")) + return + } + token := "" + if userModel != nil { + userModel.OpenId = req.OpenID + userModel.Avatar = req.Avatar + userModel.Nickname = req.Nickname + newUserDb.UpdateUser(userModel, "open_id,avatar,nickname") + token, err = svc.HandleLoginToken(userModel) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + } + e.OutSuc(c, md.LoginResponse{Token: token}, nil) + return +} + +// FindPassword +// @Summary 注册 +// @Tags 注册 +// @Description 注册 +// @Accept json +// @Produce json +// @Param req body md.FindPasswordReq true "注册参数" +// @Success 200 string "登录成功返回" +// @Failure 400 {object} md.Response "具体错误" +// @Router /api/v1/findPassword [post] +func FindPassword(c *gin.Context) { + var req md.FindPasswordReq + err := c.ShouldBindJSON(&req) + if err != nil { + err = svc.HandleValidateErr(err) + err1 := err.(e.E) + e.OutErr(c, err1.Code, err1.Error()) + return + } + userDb := implement.NewUserDb(db.Db) + user, err := userDb.UserGetOneByParams(map[string]interface{}{ + "key": "phone", + "value": req.Mobile, + }) + if user == nil { + e.OutErr(c, 400, e.NewErr(400, "手机号不存在")) + return + } + data := svc.AliyunSmsBase(c, "") + //校验短信 + err = aliyun.AliyunCheckSms(data["aliyun_sms_id"], data["aliyun_sms_secret"], req.Mobile, req.Code) + if err != nil { + e.OutErr(c, 400, e.NewErr(400, "验证码错误,请重试")) + return + } + user.Password = utils.Md5(req.Password) + userDb.UpdateUser(user, "password") + e.OutSuc(c, "success", nil) + return +} + // Register // @Summary 注册 // @Tags 注册 @@ -128,7 +225,7 @@ func Register(c *gin.Context) { e.OutErr(c, err1.Code, err1.Error()) return } - data := svc.AliyunSmsBase(c, req.Type) + data := svc.AliyunSmsBase(c, "") //校验短信 err = aliyun.AliyunCheckSms(data["aliyun_sms_id"], data["aliyun_sms_secret"], req.Mobile, req.Code) if err != nil { @@ -137,6 +234,56 @@ func Register(c *gin.Context) { } commReq(c, req) } + +// Login 登陆 +// @Summary 登陆 +// @Tags 登录 +// @Description 登入 +// @Accept json +// @Produce json +// @Param req body md.LoginReq true "用户名密码" +// @Success 200 {object} md.LoginResponse "token" +// @Failure 400 {object} md.Response "具体错误" +// @Router /api/v1/login [post] +func Login(c *gin.Context) { + var req md.LoginReq + err := c.ShouldBindJSON(&req) + if err != nil { + err = validate.HandleValidateErr(err) + err1 := err.(e.E) + e.OutErr(c, err1.Code, err1.Error()) + return + } + userDb := implement.NewUserDb(db.Db) + user, err := userDb.UserGetOneByParams(map[string]interface{}{ + "key": "phone", + "value": req.Phone, + }) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err) + return + } + if user == nil { + e.OutErr(c, e.ERR_NO_DATA, "账号不存在!") + return + } + if utils.Md5(req.PassWord) != user.Password { + e.OutErr(c, e.ERR_INVALID_ARGS, "密码错误") + return + } + //ip := utils.GetIP(c.Request) + token, err := svc.HandleLoginToken(user) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + e.OutSuc(c, md.LoginResponse{ + Token: token, + }, nil) + return +} + +// 公共处理注册 func commReq(c *gin.Context, req md.RegisterReq) { now := time.Now() userDb := implement.NewUserDb(db.Db) @@ -225,6 +372,9 @@ func commReq(c *gin.Context, req md.RegisterReq) { CreateAt: now.Format("2006-01-02 15:04:05"), UpdateAt: now.Format("2006-01-02 15:04:05"), } + if req.Password != "" { + user.Password = utils.Md5(req.Password) + } insertAffected, err1 := userDb.UserInsert(user) if err1 != nil { @@ -252,11 +402,17 @@ func commReq(c *gin.Context, req md.RegisterReq) { if req.Avatar != "" { user.Avatar = req.Avatar } + if req.Password != "" { + user.Password = utils.Md5(req.Password) + } // 微信 - user.OpenId = req.OpenID - user.UnionId = req.UnionId - + if req.OpenID != "" { + user.OpenId = req.OpenID + } + if req.UnionId != "" { + user.UnionId = req.UnionId + } // 登录信息 user.LastLoginAt = now.Format("2006-01-02 15:04:05") user.LastLoginIp = ip @@ -277,51 +433,3 @@ func commReq(c *gin.Context, req md.RegisterReq) { }, nil) return } - -// Login 登陆 -// @Summary 登陆 -// @Tags 登录 -// @Description 登入 -// @Accept json -// @Produce json -// @Param req body md.LoginReq true "用户名密码" -// @Success 200 {object} md.LoginResponse "token" -// @Failure 400 {object} md.Response "具体错误" -// @Router /api/v1/login [post] -func Login(c *gin.Context) { - var req md.LoginReq - err := c.ShouldBindJSON(&req) - if err != nil { - err = validate.HandleValidateErr(err) - err1 := err.(e.E) - e.OutErr(c, err1.Code, err1.Error()) - return - } - userDb := implement.NewUserDb(db.Db) - user, err := userDb.UserGetOneByParams(map[string]interface{}{ - "key": "phone", - "value": req.Phone, - }) - if err != nil { - e.OutErr(c, e.ERR_DB_ORM, err) - return - } - if user == nil { - e.OutErr(c, e.ERR_NO_DATA, "账号不存在!") - return - } - if utils.Md5(req.PassWord) != user.Password { - e.OutErr(c, e.ERR_INVALID_ARGS, "密码错误") - return - } - //ip := utils.GetIP(c.Request) - token, err := svc.HandleLoginToken(user) - if err != nil { - e.OutErr(c, e.ERR, err.Error()) - return - } - e.OutSuc(c, md.LoginResponse{ - Token: token, - }, nil) - return -} diff --git a/app/hdl/hdl_user.go b/app/hdl/hdl_user.go index 30313a5..f21d43a 100644 --- a/app/hdl/hdl_user.go +++ b/app/hdl/hdl_user.go @@ -4,6 +4,7 @@ import ( "applet/app/e" "applet/app/md" "applet/app/svc" + "applet/app/utils" "github.com/gin-gonic/gin" ) @@ -20,6 +21,7 @@ import ( func UserInfo(c *gin.Context) { user := svc.GetUser(c) res := md.UserInfoResp{ + Id: utils.Int64ToStr(user.Id), Phone: user.Phone, Nickname: user.Phone, InviteCode: user.SystemInviteCode, diff --git a/app/md/md_login.go b/app/md/md_login.go index 67886eb..81bcea4 100644 --- a/app/md/md_login.go +++ b/app/md/md_login.go @@ -9,8 +9,9 @@ type SmsSendReq struct { PassToken string `json:"pass_token" example:"阿里云图形验证码对应参数"` } type RegisterReq struct { - Mobile string `json:"mobile" binding:"required"` - Code string `json:"code" example:"验证码"` + Mobile string `json:"mobile" binding:"required"` + Code string `json:"code" example:"验证码"` + Type string `json:"type" example:"app:APP注册、h5:H5注册"` InviteCode string `json:"invite_code" example:"邀请码"` OpenID string `json:"open_id" example:"微信openId"` @@ -18,6 +19,18 @@ type RegisterReq struct { Avatar string `json:"avatar" example:"头像"` Nickname string `json:"nickname" example:"昵称"` Sex int `json:"sex"` //性别(0:未知 1:男 2:女) + Password string `json:"password" example:"密码 如 123"` +} +type FindPasswordReq struct { + Mobile string `json:"mobile" binding:"required"` + Code string `json:"code" example:"验证码"` + Password string `json:"password" example:"密码 如 123"` +} +type WechatLoginReq struct { + OpenID string `json:"open_id" example:"微信openId"` + UnionId string `json:"union_id" example:"微信UnionId"` + Avatar string `json:"avatar" example:"头像"` + Nickname string `json:"nickname" example:"昵称"` } type FastLoginReq struct { Token string `json:"token" example:"一键登录的token"` diff --git a/app/md/md_user.go b/app/md/md_user.go index 7cda52a..71301a3 100644 --- a/app/md/md_user.go +++ b/app/md/md_user.go @@ -1,6 +1,7 @@ package md type UserInfoResp struct { + Id string `json:"id"` Phone string `json:"phone"` Nickname string `json:"nickname"` InviteCode string `json:"invite_code"` diff --git a/app/router/router.go b/app/router/router.go index ebc97bc..6284e44 100644 --- a/app/router/router.go +++ b/app/router/router.go @@ -52,11 +52,13 @@ func route(r *gin.RouterGroup) { r.Any("/aesDecryptByECB", hdl.AesDecryptByECB) r.Use(mw.CheckSign) r.Any("/testCreateSign", hdl.TestCreateSign) - r.POST("/smsSend", hdl.SmsSend) //发送短信 - r.POST("/fastLogin", hdl.FastLogin) //一键登录 - r.POST("/register", hdl.Register) //注册 - r.POST("/login", hdl.Login) //登录 - r.Use(mw.Auth) // 以下接口需要JWT验证 + r.POST("/smsSend", hdl.SmsSend) //发送短信 + r.POST("/fastLogin", hdl.FastLogin) //一键登录 + r.POST("/wechatLogin", hdl.WechatLogin) //微信登录 + r.POST("/register", hdl.Register) //注册 + r.POST("/login", hdl.Login) //登录 + r.POST("/findPassword", hdl.FindPassword) //找回密码 + r.Use(mw.Auth) // 以下接口需要JWT验证 rComm(r.Group("/comm")) r.GET("/userInfo", hdl.UserInfo) //用户基础信息 rHomePage := r.Group("/homePage")