|
@@ -2,8 +2,10 @@ package services
|
|
|
|
|
|
import (
|
|
|
"errors"
|
|
|
+ "fmt"
|
|
|
"hongze/hongze_clpt/models"
|
|
|
"hongze/hongze_clpt/utils"
|
|
|
+ "strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
|
)
|
|
@@ -283,3 +285,129 @@ func BindWxUserMobileAndOpenid(mobile, openid, countryCode string) (wxUser *mode
|
|
|
formatWxUserAndUserRecord(wxUser, userRecord)
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+//微信登录
|
|
|
+func WxLogin(code, openId, unionId string, wxUserInfo *WxUserInfo) (token string, userId, firstLogin, permission int, err error) {
|
|
|
+ if unionId == "" {
|
|
|
+ unionId = wxUserInfo.Unionid
|
|
|
+ }
|
|
|
+ //firstLogin==1,强制绑定手机号或者邮箱
|
|
|
+ firstLogin = 1
|
|
|
+ //fmt.Println()
|
|
|
+ fmt.Println("GetWxUserItemByOpenId ", openId)
|
|
|
+
|
|
|
+QUERY_WX_USER:
|
|
|
+ wxUser, wxUserErr := GetWxUserItemByOpenId(openId)
|
|
|
+ fmt.Println("wxUserErr", wxUserErr)
|
|
|
+ if wxUserErr == ERR_NO_USER_RECORD { //没有用户openid记录
|
|
|
+ //先添加第三方信息(openid等信息)
|
|
|
+ _, recordErr := AddUserRecord(openId, unionId, wxUserInfo.Nickname, "", wxUserInfo.Province, wxUserInfo.City, wxUserInfo.Country, wxUserInfo.Headimgurl, wxUserInfo.SessionKey, utils.CE_LUE_ID, wxUserInfo.Sex, 0)
|
|
|
+ //如果插入失败,那么直接将错误信息返回
|
|
|
+ if recordErr != nil {
|
|
|
+ err = recordErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //插入成功后,需要重新查询该用户,并进入下面的逻辑
|
|
|
+ goto QUERY_WX_USER
|
|
|
+ } else if wxUserErr == ERR_USER_NOT_BIND {
|
|
|
+ //没有用户信息
|
|
|
+ //wxUser.FirstLogin = 1
|
|
|
+ } else if wxUserErr != nil {
|
|
|
+ err = wxUserErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fmt.Println("wxUserInfo", wxUserInfo)
|
|
|
+ fmt.Println("wxUserInfo.Nickname", wxUserInfo.Nickname)
|
|
|
+ fmt.Println("SessionKey", wxUserInfo.SessionKey)
|
|
|
+ if wxUserInfo != nil {
|
|
|
+ fmt.Println("ModifyUserRecordSessionKey")
|
|
|
+ err = models.ModifyUserRecordSessionKey(openId, wxUserInfo.SessionKey)
|
|
|
+ fmt.Println("ModifyUserRecordSessionKey Err", err)
|
|
|
+ }
|
|
|
+ //如果已经登录注册绑定的情况下
|
|
|
+ if wxUser != nil && wxUserErr == nil {
|
|
|
+ //获取用户权限
|
|
|
+ firstLogin = wxUser.FirstLogin
|
|
|
+ userId = wxUser.UserId
|
|
|
+ {
|
|
|
+ codeLog := new(models.WxUserCode)
|
|
|
+ codeLog.WxCode = code
|
|
|
+ codeLog.UserId = userId
|
|
|
+ codeLog.Code = 0
|
|
|
+ codeLog.FirstLogin = firstLogin
|
|
|
+ codeLog.Authorization = token
|
|
|
+ codeLog.UserPermission = permission
|
|
|
+ codeLog.CreateTime = time.Now()
|
|
|
+ go models.AddWxUserCode(codeLog)
|
|
|
+ }
|
|
|
+ if wxUser.Mobile == "" && wxUser.Email == "" {
|
|
|
+ firstLogin = 1
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取登录token
|
|
|
+ tokenItem, tokenErr := models.GetTokenByOpenId(openId)
|
|
|
+ if tokenErr != nil && tokenErr.Error() != utils.ErrNoRow() {
|
|
|
+ err = errors.New("登录失败,获取token失败:" + tokenErr.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fmt.Println("line 271 ", openId)
|
|
|
+ if tokenItem == nil || (tokenErr != nil && tokenErr.Error() == utils.ErrNoRow()) {
|
|
|
+ timeUnix := time.Now().Unix()
|
|
|
+ timeUnixStr := strconv.FormatInt(timeUnix, 10)
|
|
|
+ token = utils.MD5(openId) + utils.MD5(timeUnixStr)
|
|
|
+ //新增session
|
|
|
+ //{
|
|
|
+ // session := new(models.CygxSession)
|
|
|
+ // session.OpenId = openId
|
|
|
+ // session.UserId = userId
|
|
|
+ // session.CreatedTime = time.Now()
|
|
|
+ // session.LastUpdatedTime = time.Now()
|
|
|
+ // session.ExpireTime = time.Now().AddDate(0, 3, 0)
|
|
|
+ // session.AccessToken = token
|
|
|
+ // sessionErr := models.AddSession(session)
|
|
|
+ // if err != nil {
|
|
|
+ // err = errors.New("登录失败,新增用户session信息失败:" + sessionErr.Error())
|
|
|
+ // return
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+ } else {
|
|
|
+ token = tokenItem.AccessToken
|
|
|
+ }
|
|
|
+ fmt.Println("line 294 ", token)
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+//添加第三方用户(微信)记录
|
|
|
+func AddUserRecord(openId, unionId, nickName, realName, province, city, country, headimgurl, sessionKey string, platform, sex, subscribe int) (userRecord *models.UserRecord, err error) {
|
|
|
+ find, err := models.GetUserRecordByOpenId(openId)
|
|
|
+ if err != nil && err.Error() != utils.ErrNoRow() {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if find != nil {
|
|
|
+ userRecord = find
|
|
|
+ return
|
|
|
+ }
|
|
|
+ userRecord = &models.UserRecord{
|
|
|
+ OpenId: openId, //用户open_id
|
|
|
+ UnionId: unionId, //用户union_id
|
|
|
+ Subscribe: subscribe,
|
|
|
+ NickName: nickName, //用户昵称,最大长度:32
|
|
|
+ RealName: realName, //用户实际名称,最大长度:32
|
|
|
+ Sex: sex, //普通用户性别,1为男性,2为女性
|
|
|
+ Province: province, //普通用户个人资料填写的省份,最大长度:30
|
|
|
+ City: city, //普通用户个人资料填写的城市,最大长度:30
|
|
|
+ Country: country, //国家,如中国为CN,最大长度:30
|
|
|
+ Headimgurl: headimgurl, //用户第三方(微信)头像,最大长度:512
|
|
|
+ CreateTime: time.Now(), //创建时间,关系添加时间、用户授权时间
|
|
|
+ CreatePlatform: platform, //注册平台,1:日度点评公众号,2:管理后台,3:pc端网站,4:查研观向小程序;默认:1
|
|
|
+ SessionKey: sessionKey, //微信小程序会话密钥,最大长度:255
|
|
|
+ }
|
|
|
+ //recordId, err := models.AddUserRecord(userRecord)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //userRecord.UserRecordId = int(recordId)
|
|
|
+ return
|
|
|
+}
|