|
@@ -109,6 +109,103 @@ func FastLogin(c *gin.Context) { |
|
|
commReq(c, reqs) |
|
|
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 |
|
|
// Register |
|
|
// @Summary 注册 |
|
|
// @Summary 注册 |
|
|
// @Tags 注册 |
|
|
// @Tags 注册 |
|
@@ -128,7 +225,7 @@ func Register(c *gin.Context) { |
|
|
e.OutErr(c, err1.Code, err1.Error()) |
|
|
e.OutErr(c, err1.Code, err1.Error()) |
|
|
return |
|
|
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) |
|
|
err = aliyun.AliyunCheckSms(data["aliyun_sms_id"], data["aliyun_sms_secret"], req.Mobile, req.Code) |
|
|
if err != nil { |
|
|
if err != nil { |
|
@@ -137,6 +234,56 @@ func Register(c *gin.Context) { |
|
|
} |
|
|
} |
|
|
commReq(c, req) |
|
|
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) { |
|
|
func commReq(c *gin.Context, req md.RegisterReq) { |
|
|
now := time.Now() |
|
|
now := time.Now() |
|
|
userDb := implement.NewUserDb(db.Db) |
|
|
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"), |
|
|
CreateAt: now.Format("2006-01-02 15:04:05"), |
|
|
UpdateAt: 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) |
|
|
insertAffected, err1 := userDb.UserInsert(user) |
|
|
if err1 != nil { |
|
|
if err1 != nil { |
|
@@ -252,11 +402,17 @@ func commReq(c *gin.Context, req md.RegisterReq) { |
|
|
if req.Avatar != "" { |
|
|
if req.Avatar != "" { |
|
|
user.Avatar = 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.LastLoginAt = now.Format("2006-01-02 15:04:05") |
|
|
user.LastLoginIp = ip |
|
|
user.LastLoginIp = ip |
|
@@ -277,51 +433,3 @@ func commReq(c *gin.Context, req md.RegisterReq) { |
|
|
}, nil) |
|
|
}, nil) |
|
|
return |
|
|
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 |
|
|
|
|
|
} |
|
|
|