Selaa lähdekoodia

微信登陆

tmp
huangjiajun 1 viikko sitten
vanhempi
commit
2b9589287e
5 muutettua tiedostoa jossa 99 lisäystä ja 12 poistoa
  1. +66
    -5
      app/hdl/hdl_login.go
  2. +9
    -7
      app/md/md_login.go
  3. +9
    -0
      docs/docs.go
  4. +9
    -0
      docs/swagger.json
  5. +6
    -0
      docs/swagger.yaml

+ 66
- 5
app/hdl/hdl_login.go Näytä tiedosto

@@ -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
}
// 登录信息


+ 9
- 7
app/md/md_login.go Näytä tiedosto

@@ -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"`


+ 9
- 0
docs/docs.go Näytä tiedosto

@@ -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"
}
}
},


+ 9
- 0
docs/swagger.json Näytä tiedosto

@@ -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"
}
}
},


+ 6
- 0
docs/swagger.yaml Näytä tiedosto

@@ -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:


Ladataan…
Peruuta
Tallenna