123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- package services
- import (
- "errors"
- "fmt"
- "hongze/hongze_api/models"
- "hongze/hongze_api/services/go_redis"
- "hongze/hongze_api/utils"
- "strconv"
- "time"
- )
- var ERR_NO_USER_RECORD = errors.New("用户关系没有入库")
- var ERR_USER_NOT_BIND = errors.New("用户没有绑定")
- //通过openid获取用户信息
- func GetWxUserItemByOpenId(openid string) (item *models.WxUserItem, err error) {
- //通过openid获取用户关联信息
- userRecord, userRecordErr := models.GetUserRecordByOpenId(openid)
- if userRecordErr != nil {
- if userRecordErr.Error() == utils.ErrNoRow() {
- err = ERR_NO_USER_RECORD
- return
- } else {
- err = userRecordErr
- return
- }
- }
- //该openid在系统中没有关联关系
- if userRecord == nil {
- err = ERR_NO_USER_RECORD
- return
- }
- //该openid没有绑定用户
- if userRecord.UserId <= 0 {
- err = ERR_USER_NOT_BIND
- item = new(models.WxUserItem)
- //格式化返回用户数据
- formatWxUserAndUserRecord(item, userRecord)
- return
- }
- //获取用户信息
- item, wxUserErr := models.GetWxUserItemByUserId(userRecord.UserId)
- if wxUserErr != nil {
- err = wxUserErr
- //如果是找不到数据,那么可能是该用户被删除了,但是user_record没有删除对应的关系
- if wxUserErr.Error() == utils.ErrNoRow() {
- //用户被删除了,但是user_record没有删除对应的关系,那么去解除绑定
- userUnbindErr := models.UnBindUserRecordByOpenid(openid)
- if userUnbindErr != nil {
- err = userUnbindErr
- return
- }
- //返回状态为 用户未绑定 逻辑代码
- err = ERR_USER_NOT_BIND
- item = new(models.WxUserItem)
- //格式化返回用户数据
- formatWxUserAndUserRecord(item, userRecord)
- return
- }
- return
- }
- //格式化返回用户数据
- formatWxUserAndUserRecord(item, userRecord)
- return
- }
- //根据用户id和平台id获取用户信息
- func GetWxUserItemByUserId(userId, platform int) (wxUserItem *models.WxUserItem, err error) {
- //获取用户信息
- wxUserItem, wxUserErr := models.GetWxUserItemByUserId(userId)
- if wxUserErr != nil {
- err = wxUserErr
- return
- }
- //格式化返回用户数据
- formatWxUser(wxUserItem, platform)
- return
- }
- //根据用户邮箱和平台id获取用户信息
- func GetWxUserItemByEmail(email string, platform int) (wxUserItem *models.WxUserItem, err error) {
- //获取用户信息
- wxUserItem, wxUserErr := models.GetWxUserItemByEmail(email)
- if wxUserErr != nil {
- err = wxUserErr
- return
- }
- //格式化返回用户数据
- formatWxUser(wxUserItem, platform)
- return
- }
- //根据用户手机号和平台id获取用户信息
- func GetWxUserItemByMobile(mobile string, platform int) (wxUserItem *models.WxUserItem, err error) {
- //获取用户信息
- wxUserItem, wxUserErr := models.GetWxUserItemByMobile(mobile)
- if wxUserErr != nil {
- err = wxUserErr
- return
- }
- //格式化返回用户数据
- formatWxUser(wxUserItem, platform)
- return
- }
- //根据用户unionid和平台id获取用户信息
- func GetWxUserItemByUnionId(unionId string, platform int) (wxUserItem *models.WxUserItem, err error) {
- //获取用户信息
- wxUserItem, wxUserErr := models.GetWxUserItemByUnionid(unionId)
- if wxUserErr != nil {
- err = wxUserErr
- return
- }
- //格式化返回用户数据
- formatWxUser(wxUserItem, platform)
- return
- }
- //通过用户 关系表记录 和 用户记录 格式化返回 用户数据
- func formatWxUserAndUserRecord(wxUser *models.WxUserItem, userRecord *models.UserRecord) {
- wxUser.OpenId = userRecord.OpenId
- wxUser.UnionId = userRecord.UnionId
- wxUser.NickName = userRecord.NickName
- //wxUser.RealName = userRecord.RealName
- //wxUser.BindAccount = userRecord.BindAccount
- wxUser.Headimgurl = userRecord.Headimgurl
- }
- //通过用户 用户记录 和 来源平台 格式化返回 用户数据
- func formatWxUser(wxUser *models.WxUserItem, platform int) {
- //根据用户id和平台id获取用户关系
- userRecord, userRecordErr := models.GetUserRecordByUserId(wxUser.UserId, platform)
- if userRecordErr != nil {
- if userRecordErr.Error() != utils.ErrNoRow() {
- return
- }
- if userRecordErr.Error() == utils.ErrNoRow() {
- return
- }
- }
- //该openid在系统中没有关联关系
- if userRecord == nil {
- return
- }
- wxUser.OpenId = userRecord.OpenId
- wxUser.UnionId = userRecord.UnionId
- wxUser.NickName = userRecord.NickName
- //wxUser.RealName = userRecord.RealName
- //wxUser.BindAccount = userRecord.BindAccount
- wxUser.Headimgurl = userRecord.Headimgurl
- return
- }
- // BindWxUser 用户绑定
- func BindWxUser(openid, mobile, email string, areaNum, registerPlatform int) (wxUser *models.WxUserItem, errMsg string, err error) {
- source := 1 //绑定来源,1:微信端,2:pc网页端,3:查研观向小程序,4:每日咨询
- if mobile == "" && email == "" {
- err = errors.New("手机号或邮箱必填一个")
- return
- }
- var bindAccount string
- //根据手机号获取用户信息
- if mobile != "" {
- tmpWxUser, wxUserErr := models.GetWxUserItemByMobile(mobile)
- if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() {
- err = wxUserErr
- return
- }
- wxUser = tmpWxUser
- bindAccount = mobile
- }
- //根据邮箱获取用户信息
- if wxUser == nil && email != "" {
- tmpWxUser, wxUserErr := models.GetWxUserItemByEmail(email)
- if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() {
- err = wxUserErr
- return
- }
- wxUser = tmpWxUser
- bindAccount = email
- }
- //查询openid的第三方(微信)信息
- userRecord, err := models.GetUserRecordByOpenId(openid)
- if err != nil {
- return
- }
- var userId int
- //如果查询出来的用户是nil,那么需要新增用户
- if wxUser == nil {
- key := "bind_wx_user:mobile:" + mobile + ":email:" + email
- isHas := go_redis.IsExist(key)
- if isHas {
- err = errors.New("多次提交,请关闭页面重新进入")
- return
- }
- go_redis.SetNX(key, "ok", time.Second*300)
- user := &models.WxUser{
- CompanyId: 1,
- CreatedTime: time.Now(),
- FirstLogin: 1,
- Enabled: 1,
- RegisterPlatform: 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),
- }
- tmpUserId, addUserErr := models.AddWxUser(user)
- //添加完成,清除缓存
- _ = go_redis.Delete(key)
- if addUserErr != nil {
- err = addUserErr
- return
- }
- user.UserId = int(tmpUserId)
- userId = int(tmpUserId)
- wxUser, err = models.GetWxUserItemByUserId(userId)
- } else {
- userId = wxUser.UserId
- }
- //如果存在该手机号/邮箱,那么需要校验
- if userRecord.UserId > 0 && userRecord.UserId != userId {
- err = errors.New(fmt.Sprint("用户已绑定其他账户,已绑定的用户编号:", userRecord.UserId, ",不允许重复绑定"))
- currUser, _ := models.GetWxUserItemByUserId(userRecord.UserId)
- if currUser != nil {
- currBindAccount := currUser.Mobile
- if currBindAccount == `` {
- currBindAccount = currUser.Email
- }
- errMsg = "微信已绑定其它账户:" + currBindAccount
- }
- return
- }
- if userRecord.UserId == 0 {
- err = models.BindUserRecordByOpenid(userId, openid, bindAccount)
- if err != nil {
- return
- }
- userRecord.UserId = userId
- }
- //如果当前该第三方用户信息的昵称为空串的话,那么需要去查询该用户的第一个绑定信息的数据作为来源做数据修复
- if userRecord.NickName == "" {
- oldUserRecord, err := models.GetUserThirdRecordByUserId(userId)
- if err == nil && oldUserRecord != nil {
- //如果该用户绑定的第一条数据的头像信息不为空串,那么就去做新数据的修复
- if oldUserRecord.NickName != "" {
- _ = models.ModifyUserRecordInfo(userRecord.OpenId, oldUserRecord.NickName, oldUserRecord.Headimgurl, oldUserRecord.City, oldUserRecord.Province, oldUserRecord.Country, oldUserRecord.Sex, userId)
- }
- }
- }
- //如果该用户 绑定注册状态 字段处于 未注册 的情况下,那么去修改该数据
- if wxUser.IsRegister == 0 {
- err = models.ModifyWxUserRegisterStatus(userId, 1, source, time.Now())
- if err != nil {
- return
- }
- }
- if userRecord.UnionId != `` {
- tmpErr := models.ModifyUserIdByUnionId(userRecord.UnionId, userRecord.UserId)
- if tmpErr != nil {
- utils.FileLog.Info("通过用户unionId修改所有未绑定用户的id失败,Err:" + err.Error())
- }
- }
- //格式化用户数据
- formatWxUserAndUserRecord(wxUser, userRecord)
- return
- }
- //微信登录
- func WxLogin(wxPlatform int, code string, wxAccessToken *WxAccessToken, wxUserInfo *WxUserInfo) (token string, userId, firstLogin, permission int, err error) {
- openId := wxAccessToken.Openid
- unionId := wxAccessToken.Unionid
- if unionId == "" {
- unionId = wxUserInfo.Unionid
- }
- //firstLogin==1,强制绑定手机号或者邮箱
- firstLogin = 1
- QUERY_WX_USER:
- wxUser, wxUserErr := GetWxUserItemByOpenId(openId)
- if wxUserErr == ERR_NO_USER_RECORD { //没有用户openid记录
- _, recordErr := AddUserRecord(openId, unionId, wxUserInfo.Nickname, "", wxUserInfo.Province, wxUserInfo.City, wxUserInfo.Country, wxUserInfo.Headimgurl, "", wxPlatform, 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
- }
- //如果已经登录注册绑定的情况下
- if wxUser != nil && wxUserErr == nil {
- //获取用户权限
- firstLogin = wxUser.FirstLogin
- userId = wxUser.UserId
- permission, permissionErr := CheckUserPermission(userId)
- if permissionErr != nil {
- //记录日志
- utils.FileLog.Info("userId:%s,err:%s", strconv.Itoa(userId), err)
- }
- if wxUserInfo != nil {
- go models.ModifyUserRecordInfo(openId, wxUserInfo.Nickname, wxUserInfo.Headimgurl, wxUserInfo.City, wxUserInfo.Province, wxUserInfo.Country, wxUserInfo.Sex, 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()
- models.AddWxUserCode(codeLog)
- }
- if wxUser.Mobile == "" && wxUser.Email == "" {
- firstLogin = 1
- } else {
- firstLogin = 0
- }
- }
- //获取登录token
- tokenItem, tokenErr := models.GetTokenByOpenId(openId)
- if tokenErr != nil && tokenErr.Error() != utils.ErrNoRow() {
- err = errors.New("登录失败,获取token失败:" + tokenErr.Error())
- return
- }
- 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.Session)
- 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
- //如果联系人编号不为空,且联系人编号与session里面的联系人编号不一致的时候,需要做session变更
- if userId > 0 && tokenItem.UserId != userId {
- _ = models.UpdateSession(tokenItem.SessionId, userId, time.Now().AddDate(0, 1, 0))
- }
- }
- //新增登录日志
- {
- loginLog := new(models.WxUserLog)
- loginLog.UserId = userId
- loginLog.OpenId = openId
- loginLog.UnionId = unionId
- loginLog.CreateTime = time.Now()
- loginLog.Handle = "wechat_login"
- loginLog.Remark = token
- go models.AddWxUserLog(loginLog)
- }
- 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
- }
|