dengbiao 6 days ago
parent
commit
d1e90ccc27
6 changed files with 101 additions and 13 deletions
  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
  6. +2
    -1
      go.mod

+ 66
- 5
app/hdl/hdl_login.go View File

@@ -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 View File

@@ -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 View File

@@ -5946,6 +5946,9 @@ const docTemplate = `{
"union_id": {
"type": "string",
"example": "微信UnionId"
},
"wechat_code": {
"type": "string"
}
}
},
@@ -6561,9 +6564,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 View File

@@ -5940,6 +5940,9 @@
"union_id": {
"type": "string",
"example": "微信UnionId"
},
"wechat_code": {
"type": "string"
}
}
},
@@ -6555,9 +6558,15 @@
"type": "string",
"example": "微信openId"
},
"sex": {
"type": "string"
},
"union_id": {
"type": "string",
"example": "微信UnionId"
},
"wechat_code": {
"type": "string"
}
}
},


+ 6
- 0
docs/swagger.yaml View File

@@ -1289,6 +1289,8 @@ definitions:
union_id:
example: 微信UnionId
type: string
wechat_code:
type: string
required:
- mobile
type: object
@@ -1712,9 +1714,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:


+ 2
- 1
go.mod View File

@@ -15,7 +15,7 @@ require (
github.com/go-playground/universal-translator v0.18.1
github.com/go-playground/validator/v10 v10.20.0
github.com/go-redis/redis v6.15.9+incompatible
github.com/gomodule/redigo v1.9.2
github.com/gomodule/redigo v2.0.0+incompatible
github.com/jinzhu/copier v0.4.0
github.com/makiuchi-d/gozxing v0.0.0-20210324052758-57132e828831
github.com/qiniu/api.v7/v7 v7.8.2
@@ -37,6 +37,7 @@ require (
code.fnuoos.com/go_rely_warehouse/zyos_go_es.git v1.0.1-0.20241118083738-0f22da9ba0be
code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git v0.0.5
github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible
github.com/gin-contrib/sessions v1.0.1
github.com/go-pay/crypto v0.0.1
github.com/go-pay/gopay v1.5.101
github.com/go-pay/xlog v0.0.2


Loading…
Cancel
Save