|
@@ -1,13 +1,11 @@
|
|
|
-package services
|
|
|
+package user
|
|
|
|
|
|
import (
|
|
|
- "context"
|
|
|
"encoding/json"
|
|
|
"errors"
|
|
|
- "fmt"
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
"github.com/silenceper/wechat/v2/miniprogram/auth"
|
|
|
- "hongze/hongze_yb/global"
|
|
|
- "hongze/hongze_yb/models/tables/session"
|
|
|
+ "hongze/hongze_yb/models/tables/rddp/session"
|
|
|
"hongze/hongze_yb/models/tables/user_record"
|
|
|
"hongze/hongze_yb/models/tables/wx_user"
|
|
|
"hongze/hongze_yb/models/tables/wx_user_log"
|
|
@@ -132,7 +130,7 @@ func GetWxUserItemByUnionId(unionId string, platform int) (userInfo UserInfo, er
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//通过用户 关系表记录 和 用户记录 格式化返回 用户数据
|
|
|
+// formatWxUserAndUserRecord 通过用户 关系表记录 和 用户记录 格式化返回 用户数据
|
|
|
func formatWxUserAndUserRecord(wxUser *wx_user.WxUser, userRecord *user_record.UserRecord) (userInfo UserInfo) {
|
|
|
wxUser.OpenID = userRecord.OpenID
|
|
|
wxUser.UnionID = userRecord.UnionID
|
|
@@ -147,7 +145,7 @@ func formatWxUserAndUserRecord(wxUser *wx_user.WxUser, userRecord *user_record.U
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//通过用户 用户记录 和 来源平台 格式化返回 用户数据
|
|
|
+// formatWxUser 通过用户 用户记录 和 来源平台 格式化返回 用户数据
|
|
|
func formatWxUser(wxUser *wx_user.WxUser, platform int) (userInfo UserInfo) {
|
|
|
//根据用户id和平台id获取用户关系
|
|
|
userRecord, userRecordErr := user_record.GetByUserId(int(wxUser.UserID), platform)
|
|
@@ -168,128 +166,10 @@ func formatWxUser(wxUser *wx_user.WxUser, platform int) (userInfo UserInfo) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//用户绑定
|
|
|
-func BindWxUser(openid, mobile, email string, areaNum, registerPlatform int) (wxUser *wx_user.WxUser, err error) {
|
|
|
- var source int8
|
|
|
- source = 6 //绑定来源,1:微信端,2:pc网页端,3:查研观向小程序,4:每日咨询
|
|
|
- if mobile == "" && email == "" {
|
|
|
- err = errors.New("手机号或邮箱必填一个")
|
|
|
- return
|
|
|
- }
|
|
|
- var bindAccount string
|
|
|
- //根据手机号获取用户信息
|
|
|
- if mobile != "" {
|
|
|
- tmpWxUser, wxUserErr := wx_user.GetByMobile(mobile)
|
|
|
- if wxUserErr != nil && wxUserErr != utils.ErrNoRow {
|
|
|
- err = wxUserErr
|
|
|
- return
|
|
|
- }
|
|
|
- wxUser = tmpWxUser
|
|
|
- bindAccount = mobile
|
|
|
- }
|
|
|
- //根据邮箱获取用户信息
|
|
|
- if wxUser == nil && email != "" {
|
|
|
- tmpWxUser, wxUserErr := wx_user.GetByEmail(email)
|
|
|
- if wxUserErr != nil && wxUserErr != utils.ErrNoRow {
|
|
|
- err = wxUserErr
|
|
|
- return
|
|
|
- }
|
|
|
- wxUser = tmpWxUser
|
|
|
- bindAccount = email
|
|
|
- }
|
|
|
-
|
|
|
- //查询openid的第三方(微信)信息
|
|
|
- userRecord, err := user_record.GetByOpenID(openid)
|
|
|
- if err != nil {
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- var userId int
|
|
|
- //如果查询出来的用户是nil,那么需要新增用户
|
|
|
- if wxUser == nil {
|
|
|
- key := "bind_wx_user:mobile:" + mobile + ":email:" + email
|
|
|
- isHas, _ := global.Redis.Exists(context.TODO(), key).Result()
|
|
|
- if isHas > 0 {
|
|
|
- err = errors.New("多次提交,请关闭页面重新进入")
|
|
|
- return
|
|
|
- }
|
|
|
- global.Redis.SetNX(context.TODO(), key, "ok", time.Second*300)
|
|
|
- addwxUser := &wx_user.WxUser{
|
|
|
- CompanyID: 1,
|
|
|
- CreatedTime: time.Now(),
|
|
|
- FirstLogin: 1,
|
|
|
- Enabled: 1,
|
|
|
- RegisterPlatform: int8(registerPlatform), //账号注册来源,注册平台,1:微信端,2:PC网页端
|
|
|
- RegisterTime: time.Now(),
|
|
|
- Mobile: mobile,
|
|
|
- Email: email,
|
|
|
- IsRegister: 1,
|
|
|
- Source: source,
|
|
|
- CountryCode: strconv.Itoa(areaNum),
|
|
|
- OutboundMobile: mobile,
|
|
|
- OutboundCountryCode: strconv.Itoa(areaNum),
|
|
|
- }
|
|
|
-
|
|
|
- addUserErr := addwxUser.Create()
|
|
|
- //添加完成,清除缓存
|
|
|
- _ = global.Redis.Del(context.TODO(), key)
|
|
|
- if addUserErr != nil {
|
|
|
- err = addUserErr
|
|
|
- return
|
|
|
- }
|
|
|
- userId = int(addwxUser.UserID)
|
|
|
- tmpWxUser, _ := wx_user.GetByUserId(userId)
|
|
|
- wxUser = tmpWxUser
|
|
|
- } else {
|
|
|
- userId = int(wxUser.UserID)
|
|
|
- }
|
|
|
- //如果存在该手机号/邮箱,那么需要校验
|
|
|
- if userRecord.UserID > 0 && userRecord.UserID != userId {
|
|
|
- err = errors.New(fmt.Sprint("用户已绑定其他账户,已绑定的用户编号:", userRecord.UserID, ",不允许重复绑定"))
|
|
|
- return
|
|
|
- }
|
|
|
- if userRecord.UserID == 0 {
|
|
|
- userRecord.BindAccount = bindAccount
|
|
|
- userRecord.UserID = userId
|
|
|
- var updateCols = []string{"UserID", "BindAccount"}
|
|
|
- tmpErr := userRecord.Update(updateCols)
|
|
|
- if tmpErr != nil {
|
|
|
- err = tmpErr
|
|
|
- return
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //如果当前该第三方用户信息的昵称为空串的话,那么需要去查询该用户的第一个绑定信息的数据作为来源做数据修复
|
|
|
- if userRecord.NickName == "" {
|
|
|
- oldUserRecord, err := user_record.GetUserThirdRecordByUserId(userId)
|
|
|
- if err == nil && oldUserRecord != nil {
|
|
|
- //如果该用户绑定的第一条数据的头像信息不为空串,那么就去做新数据的修复
|
|
|
- if oldUserRecord.NickName != "" {
|
|
|
- _ = userRecord.ModifyUserRecordInfo(oldUserRecord.NickName, oldUserRecord.Headimgurl, oldUserRecord.City, oldUserRecord.Province, oldUserRecord.Country, oldUserRecord.Sex)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //如果该用户 绑定注册状态 字段处于 未注册 的情况下,那么去修改该数据
|
|
|
- if wxUser.IsRegister == 0 {
|
|
|
- err = wxUser.ModifyWxUserRegisterStatus(1, source, time.Now())
|
|
|
- if err != nil {
|
|
|
- return
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //格式化用户数据
|
|
|
- formatWxUserAndUserRecord(wxUser, userRecord)
|
|
|
- return
|
|
|
-}
|
|
|
-
|
|
|
-//微信登录
|
|
|
-func WxLogin(wxPlatform int, code string, wxSession auth.ResCode2Session, userInfo UserInfo) (token string, userId, firstLogin, permission int, err error) {
|
|
|
+// WxLogin 微信登录
|
|
|
+func WxLogin(wxPlatform int, wxSession auth.ResCode2Session) (token string, userId, firstLogin int, err error) {
|
|
|
openId := wxSession.OpenID
|
|
|
unionId := wxSession.UnionID
|
|
|
- if unionId == "" {
|
|
|
- unionId = userInfo.UnionID
|
|
|
- }
|
|
|
|
|
|
//firstLogin==1,强制绑定手机号或者邮箱
|
|
|
firstLogin = 1
|
|
@@ -337,7 +217,7 @@ QUERY_WX_USER:
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- if tokenItem == nil || (tokenErr != nil && tokenErr == utils.ErrNoRow) {
|
|
|
+ if tokenErr != nil && tokenErr == utils.ErrNoRow {
|
|
|
timeUnix := time.Now().Unix()
|
|
|
timeUnixStr := strconv.FormatInt(timeUnix, 10)
|
|
|
token = utils.MD5(openId) + utils.MD5(timeUnixStr)
|
|
@@ -369,7 +249,7 @@ QUERY_WX_USER:
|
|
|
token = tokenItem.AccessToken
|
|
|
//如果联系人编号不为空,且联系人编号与session里面的联系人编号不一致的时候,需要做session变更
|
|
|
if userId > 0 && tokenItem.UserID != int64(userId) {
|
|
|
- _ = tokenItem.UpdateSession(userId, time.Now().AddDate(0, 1, 0))
|
|
|
+ _ = tokenItem.UpdateSession(int64(userId), time.Now().AddDate(0, 1, 0))
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -391,13 +271,14 @@ QUERY_WX_USER:
|
|
|
// AddUserRecord 添加第三方用户(微信)记录
|
|
|
func AddUserRecord(openId, unionId, nickName, realName, province, city, country, headimgurl, sessionKey string, platform, sex, subscribe int) (userRecordInfo *user_record.UserRecord, err error) {
|
|
|
find, err := user_record.GetByOpenID(openId)
|
|
|
- if err != nil && err != utils.ErrNoRow {
|
|
|
+ if err == nil {
|
|
|
+ userRecordInfo = find
|
|
|
return
|
|
|
}
|
|
|
- if find != nil {
|
|
|
- userRecordInfo = find
|
|
|
+ if err != utils.ErrNoRow {
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
userRecordInfo = &user_record.UserRecord{
|
|
|
OpenID: openId, //用户open_id
|
|
|
UnionID: unionId, //用户union_id
|
|
@@ -414,8 +295,13 @@ func AddUserRecord(openId, unionId, nickName, realName, province, city, country,
|
|
|
SessionKey: sessionKey, //微信小程序会话密钥,最大长度:255
|
|
|
}
|
|
|
err = userRecordInfo.Create()
|
|
|
- if err != nil {
|
|
|
- return
|
|
|
- }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// GetInfoByClaims 从Claims中获取用户信息
|
|
|
+func GetInfoByClaims(c *gin.Context) (userInfo *UserInfo) {
|
|
|
+ //获取jwt数据失败
|
|
|
+ claims, _ := c.Get("userInfo")
|
|
|
+ userInfo = claims.(*UserInfo)
|
|
|
return
|
|
|
}
|