2 Ревизии

Автор SHA1 Съобщение Дата
  huangjiajun fe2eed4388 Merge remote-tracking branch 'origin/master' преди 1 ден
  huangjiajun 22b4d55124 微信登录 преди 1 ден
променени са 5 файла, в които са добавени 185 реда и са изтрити 59 реда
  1. +160
    -52
      app/hdl/hdl_login.go
  2. +2
    -0
      app/hdl/hdl_user.go
  3. +15
    -2
      app/md/md_login.go
  4. +1
    -0
      app/md/md_user.go
  5. +7
    -5
      app/router/router.go

+ 160
- 52
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
}

+ 2
- 0
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,


+ 15
- 2
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"`


+ 1
- 0
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"`


+ 7
- 5
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")


Зареждане…
Отказ
Запис