@@ -9,6 +9,7 @@ import ( | |||||
"applet/app/svc" | "applet/app/svc" | ||||
"applet/app/svc/sys_cfg" | "applet/app/svc/sys_cfg" | ||||
"applet/app/utils" | "applet/app/utils" | ||||
"applet/app/utils/cache" | |||||
"code.fnuoos.com/EggPlanet/egg_models.git/src/implement" | "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" | ||||
"code.fnuoos.com/EggPlanet/egg_models.git/src/model" | "code.fnuoos.com/EggPlanet/egg_models.git/src/model" | ||||
"code.fnuoos.com/EggPlanet/egg_system_rules.git/aliyun" | "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" | "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" | ||||
"fmt" | "fmt" | ||||
"github.com/gin-gonic/gin" | "github.com/gin-gonic/gin" | ||||
"github.com/tidwall/gjson" | |||||
"time" | "time" | ||||
) | ) | ||||
@@ -142,6 +144,28 @@ func WechatLogin(c *gin.Context) { | |||||
e.OutErr(c, err1.Code, err1.Error()) | e.OutErr(c, err1.Code, err1.Error()) | ||||
return | 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 == "" { | if req.OpenID == "" { | ||||
e.OutErr(c, 400, e.NewErr(400, "登录失败")) | e.OutErr(c, 400, e.NewErr(400, "登录失败")) | ||||
return | return | ||||
@@ -165,9 +189,15 @@ func WechatLogin(c *gin.Context) { | |||||
} | } | ||||
token := "" | token := "" | ||||
if userModel != nil { | if userModel != nil { | ||||
userModel.OpenId = req.OpenID | if userModel.OpenId == "" { | ||||
userModel.Avatar = req.Avatar | userModel.OpenId = req.OpenID | ||||
userModel.Nickname = req.Nickname | } | ||||
if userModel.Avatar == "" { | |||||
userModel.Avatar = req.Avatar | |||||
} | |||||
if userModel.Nickname == "" { | |||||
userModel.Nickname = req.Nickname | |||||
} | |||||
now := time.Now() | now := time.Now() | ||||
userModel.LastLoginAt = now.Format("2006-01-02 15:04:05") | userModel.LastLoginAt = now.Format("2006-01-02 15:04:05") | ||||
userModel.LastLoginIp = utils.GetIP(c.Request) | userModel.LastLoginIp = utils.GetIP(c.Request) | ||||
@@ -179,6 +209,8 @@ func WechatLogin(c *gin.Context) { | |||||
e.OutErr(c, e.ERR, err.Error()) | e.OutErr(c, e.ERR, err.Error()) | ||||
return | return | ||||
} | } | ||||
} else { | |||||
cache.SetEx("wechat:"+req.WechatCode, utils.SerializeStr(req), 3600) | |||||
} | } | ||||
e.OutSuc(c, md.LoginResponse{Token: token}, nil) | e.OutSuc(c, md.LoginResponse{Token: token}, nil) | ||||
return | return | ||||
@@ -320,6 +352,35 @@ func commReq(c *gin.Context, req md.RegisterReq) { | |||||
} | } | ||||
ip := utils.GetIP(c.Request) | 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 { | if user == nil { | ||||
registerType := func() int { | registerType := func() int { | ||||
if req.Type == "H5" { | 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 | user.OpenId = req.OpenID | ||||
} | } | ||||
if req.UnionId != "" { | if req.UnionId != "" && user.UnionId == "" { | ||||
user.UnionId = req.UnionId | user.UnionId = req.UnionId | ||||
} | } | ||||
// 登录信息 | // 登录信息 | ||||
@@ -9,9 +9,9 @@ type SmsSendReq struct { | |||||
PassToken string `json:"pass_token" example:"阿里云图形验证码对应参数"` | PassToken string `json:"pass_token" example:"阿里云图形验证码对应参数"` | ||||
} | } | ||||
type RegisterReq struct { | type RegisterReq struct { | ||||
Mobile string `json:"mobile" binding:"required"` | Mobile string `json:"mobile" binding:"required"` | ||||
Code string `json:"code" example:"验证码"` | Code string `json:"code" example:"验证码"` | ||||
WechatCode string `json:"wechat_code"` | |||||
Type string `json:"type" example:"app:APP注册、h5:H5注册"` | Type string `json:"type" example:"app:APP注册、h5:H5注册"` | ||||
InviteCode string `json:"invite_code" example:"邀请码"` | InviteCode string `json:"invite_code" example:"邀请码"` | ||||
OpenID string `json:"open_id" example:"微信openId"` | OpenID string `json:"open_id" example:"微信openId"` | ||||
@@ -38,10 +38,12 @@ type DeleteUserReq struct { | |||||
Code string `json:"code" example:"验证码"` | Code string `json:"code" example:"验证码"` | ||||
} | } | ||||
type WechatLoginReq struct { | type WechatLoginReq struct { | ||||
OpenID string `json:"open_id" example:"微信openId"` | WechatCode string `json:"wechat_code"` | ||||
UnionId string `json:"union_id" example:"微信UnionId"` | OpenID string `json:"open_id" example:"微信openId"` | ||||
Avatar string `json:"avatar" example:"头像"` | UnionId string `json:"union_id" example:"微信UnionId"` | ||||
Nickname string `json:"nickname" example:"昵称"` | Avatar string `json:"avatar" example:"头像"` | ||||
Nickname string `json:"nickname" example:"昵称"` | |||||
Sex string `json:"sex" ` | |||||
} | } | ||||
type FastLoginReq struct { | type FastLoginReq struct { | ||||
Token string `json:"token" example:"一键登录的token"` | Token string `json:"token" example:"一键登录的token"` | ||||
@@ -5946,6 +5946,9 @@ const docTemplate = `{ | |||||
"union_id": { | "union_id": { | ||||
"type": "string", | "type": "string", | ||||
"example": "微信UnionId" | "example": "微信UnionId" | ||||
}, | |||||
"wechat_code": { | |||||
"type": "string" | |||||
} | } | ||||
} | } | ||||
}, | }, | ||||
@@ -6561,9 +6564,15 @@ const docTemplate = `{ | |||||
"type": "string", | "type": "string", | ||||
"example": "微信openId" | "example": "微信openId" | ||||
}, | }, | ||||
"sex": { | |||||
"type": "string" | |||||
}, | |||||
"union_id": { | "union_id": { | ||||
"type": "string", | "type": "string", | ||||
"example": "微信UnionId" | "example": "微信UnionId" | ||||
}, | |||||
"wechat_code": { | |||||
"type": "string" | |||||
} | } | ||||
} | } | ||||
}, | }, | ||||
@@ -5940,6 +5940,9 @@ | |||||
"union_id": { | "union_id": { | ||||
"type": "string", | "type": "string", | ||||
"example": "微信UnionId" | "example": "微信UnionId" | ||||
}, | |||||
"wechat_code": { | |||||
"type": "string" | |||||
} | } | ||||
} | } | ||||
}, | }, | ||||
@@ -6555,9 +6558,15 @@ | |||||
"type": "string", | "type": "string", | ||||
"example": "微信openId" | "example": "微信openId" | ||||
}, | }, | ||||
"sex": { | |||||
"type": "string" | |||||
}, | |||||
"union_id": { | "union_id": { | ||||
"type": "string", | "type": "string", | ||||
"example": "微信UnionId" | "example": "微信UnionId" | ||||
}, | |||||
"wechat_code": { | |||||
"type": "string" | |||||
} | } | ||||
} | } | ||||
}, | }, | ||||
@@ -1289,6 +1289,8 @@ definitions: | |||||
union_id: | union_id: | ||||
example: 微信UnionId | example: 微信UnionId | ||||
type: string | type: string | ||||
wechat_code: | |||||
type: string | |||||
required: | required: | ||||
- mobile | - mobile | ||||
type: object | type: object | ||||
@@ -1712,9 +1714,13 @@ definitions: | |||||
open_id: | open_id: | ||||
example: 微信openId | example: 微信openId | ||||
type: string | type: string | ||||
sex: | |||||
type: string | |||||
union_id: | union_id: | ||||
example: 微信UnionId | example: 微信UnionId | ||||
type: string | type: string | ||||
wechat_code: | |||||
type: string | |||||
type: object | type: object | ||||
md.WithdrawApplyReq: | md.WithdrawApplyReq: | ||||
properties: | properties: | ||||
@@ -15,7 +15,7 @@ require ( | |||||
github.com/go-playground/universal-translator v0.18.1 | github.com/go-playground/universal-translator v0.18.1 | ||||
github.com/go-playground/validator/v10 v10.20.0 | github.com/go-playground/validator/v10 v10.20.0 | ||||
github.com/go-redis/redis v6.15.9+incompatible | 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/jinzhu/copier v0.4.0 | ||||
github.com/makiuchi-d/gozxing v0.0.0-20210324052758-57132e828831 | github.com/makiuchi-d/gozxing v0.0.0-20210324052758-57132e828831 | ||||
github.com/qiniu/api.v7/v7 v7.8.2 | 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_es.git v1.0.1-0.20241118083738-0f22da9ba0be | ||||
code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git v0.0.5 | 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/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/crypto v0.0.1 | ||||
github.com/go-pay/gopay v1.5.101 | github.com/go-pay/gopay v1.5.101 | ||||
github.com/go-pay/xlog v0.0.2 | github.com/go-pay/xlog v0.0.2 | ||||