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