diff --git a/app/hdl/hdl_login.go b/app/hdl/hdl_login.go index aadecd0..400c1fa 100644 --- a/app/hdl/hdl_login.go +++ b/app/hdl/hdl_login.go @@ -9,6 +9,7 @@ import ( "applet/app/svc" "applet/app/svc/sys_cfg" "applet/app/utils" + "applet/app/utils/cache" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" "code.fnuoos.com/EggPlanet/egg_system_rules.git/aliyun" @@ -17,6 +18,7 @@ import ( "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" "fmt" "github.com/gin-gonic/gin" + "github.com/tidwall/gjson" "time" ) @@ -142,6 +144,28 @@ func WechatLogin(c *gin.Context) { e.OutErr(c, err1.Code, err1.Error()) return } + if req.WechatCode == "" { + e.OutErr(c, 400, e.NewErr(400, "获取微信信息失败")) + return + } + //获取openid + wechatUrl := "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code" + wechatUrl = fmt.Sprintf(wechatUrl, svc.GetSysCfgStr("wechat_appid"), svc.GetSysCfgStr("wechat_secret"), req.WechatCode) + wechatResp, err := utils.CurlGet(wechatUrl, nil) + if err != nil { + e.OutErr(c, 400, e.NewErr(400, "获取微信信息失败")) + return + } + req.OpenID = gjson.Get(string(wechatResp), "openid").String() + req.UnionId = gjson.Get(string(wechatResp), "unionid").String() + wechatToken := gjson.Get(string(wechatResp), "access_token").String() + wechatInfoUrl := "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s" + wechatInfoUrl = fmt.Sprintf(wechatInfoUrl, wechatToken, req.OpenID) + wechatInfoResp, _ := utils.CurlGet(wechatInfoUrl, nil) + req.Avatar = gjson.Get(string(wechatInfoResp), "headimgurl").String() + req.Nickname = gjson.Get(string(wechatInfoResp), "nickname").String() + sex := gjson.Get(string(wechatInfoResp), "sex").Int() + req.Sex = utils.Int64ToStr(sex) if req.OpenID == "" { e.OutErr(c, 400, e.NewErr(400, "登录失败")) return @@ -165,9 +189,15 @@ func WechatLogin(c *gin.Context) { } token := "" if userModel != nil { - userModel.OpenId = req.OpenID - userModel.Avatar = req.Avatar - userModel.Nickname = req.Nickname + if userModel.OpenId == "" { + userModel.OpenId = req.OpenID + } + if userModel.Avatar == "" { + userModel.Avatar = req.Avatar + } + if userModel.Nickname == "" { + userModel.Nickname = req.Nickname + } now := time.Now() userModel.LastLoginAt = now.Format("2006-01-02 15:04:05") userModel.LastLoginIp = utils.GetIP(c.Request) @@ -179,6 +209,8 @@ func WechatLogin(c *gin.Context) { e.OutErr(c, e.ERR, err.Error()) return } + } else { + cache.SetEx("wechat:"+req.WechatCode, utils.SerializeStr(req), 3600) } e.OutSuc(c, md.LoginResponse{Token: token}, nil) return @@ -320,6 +352,35 @@ func commReq(c *gin.Context, req md.RegisterReq) { } ip := utils.GetIP(c.Request) + //如果是微信绑定 这里要获取缓存 + if req.WechatCode != "" { + wechatInfo, _ := cache.GetString("wechat:" + req.WechatCode) + req.OpenID = gjson.Get(wechatInfo, "open_id").String() + req.UnionId = gjson.Get(wechatInfo, "union_id").String() + req.Avatar = gjson.Get(wechatInfo, "avatar").String() + req.Nickname = gjson.Get(wechatInfo, "nickname").String() + req.Sex = utils.StrToInt(gjson.Get(wechatInfo, "sex").String()) + 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, _ := newUserDb.UserGetOneByParams(param) + if userModel != nil && req.Mobile != userModel.Phone { + e.OutErr(c, 400, "该手机号已绑定其他微信账号") + return + } + } if user == nil { registerType := func() int { if req.Type == "H5" { @@ -457,10 +518,10 @@ func commReq(c *gin.Context, req md.RegisterReq) { } // 微信 - if req.OpenID != "" { + if req.OpenID != "" && user.OpenId == "" { user.OpenId = req.OpenID } - if req.UnionId != "" { + if req.UnionId != "" && user.UnionId == "" { user.UnionId = req.UnionId } // 登录信息 diff --git a/app/md/md_login.go b/app/md/md_login.go index 0bea4a9..61f52de 100644 --- a/app/md/md_login.go +++ b/app/md/md_login.go @@ -9,9 +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:"验证码"` + WechatCode string `json:"wechat_code"` Type string `json:"type" example:"app:APP注册、h5:H5注册"` InviteCode string `json:"invite_code" example:"邀请码"` OpenID string `json:"open_id" example:"微信openId"` @@ -38,10 +38,12 @@ type DeleteUserReq struct { Code string `json:"code" example:"验证码"` } 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:"昵称"` + WechatCode string `json:"wechat_code"` + OpenID string `json:"open_id" example:"微信openId"` + UnionId string `json:"union_id" example:"微信UnionId"` + Avatar string `json:"avatar" example:"头像"` + Nickname string `json:"nickname" example:"昵称"` + Sex string `json:"sex" ` } type FastLoginReq struct { Token string `json:"token" example:"一键登录的token"` diff --git a/docs/docs.go b/docs/docs.go index 236e1b2..99acd4d 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -5937,6 +5937,9 @@ const docTemplate = `{ "union_id": { "type": "string", "example": "微信UnionId" + }, + "wechat_code": { + "type": "string" } } }, @@ -6552,9 +6555,15 @@ const docTemplate = `{ "type": "string", "example": "微信openId" }, + "sex": { + "type": "string" + }, "union_id": { "type": "string", "example": "微信UnionId" + }, + "wechat_code": { + "type": "string" } } }, diff --git a/docs/swagger.json b/docs/swagger.json index c66f0b9..d0aead7 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -5930,6 +5930,9 @@ "union_id": { "type": "string", "example": "微信UnionId" + }, + "wechat_code": { + "type": "string" } } }, @@ -6545,9 +6548,15 @@ "type": "string", "example": "微信openId" }, + "sex": { + "type": "string" + }, "union_id": { "type": "string", "example": "微信UnionId" + }, + "wechat_code": { + "type": "string" } } }, diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 476e883..9a0e550 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1280,6 +1280,8 @@ definitions: union_id: example: 微信UnionId type: string + wechat_code: + type: string required: - mobile type: object @@ -1703,9 +1705,13 @@ definitions: open_id: example: 微信openId type: string + sex: + type: string union_id: example: 微信UnionId type: string + wechat_code: + type: string type: object md.WithdrawApplyReq: properties: