123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- package services
- import (
- "errors"
- "fmt"
- "hongze/hongze_mfyx_gzh/models"
- "hongze/hongze_mfyx_gzh/utils"
- )
- var ERR_NO_USER_RECORD = errors.New("用户关系没有入库")
- var ERR_USER_NOT_BIND = errors.New("用户没有绑定")
- //通过用户 关系表记录 和 用户记录 格式化返回 用户数据
- func formatWxUserAndUserRecord(wxUser *models.WxUserItem, userRecord *models.CygxMfyxGzhUserRecord) {
- wxUser.OpenId = userRecord.OpenId
- wxUser.UnionId = userRecord.UnionId
- wxUser.NickName = userRecord.NickName
- //wxUser.RealName = userRecord.RealName
- //wxUser.BindAccount = userRecord.BindAccount
- wxUser.Headimgurl = userRecord.Headimgurl
- //wxUser.SessionKey = userRecord.SessionKey
- if wxUser.Headimgurl == "" {
- wxUser.Headimgurl = utils.HeadimgurlDefault
- }
- }
- func GetWxUserItemByOpenId(unionId string) (item *models.WxUserItem, err error) {
- //通过openid获取用户关联信息
- item = new(models.WxUserItem)
- item.UnionId = unionId // 先写入 unionId
- userRecord, userRecordErr := models.GetUserRecordByUnionId(unionId)
- if userRecordErr != nil && userRecordErr.Error() != utils.ErrNoRow() {
- err = userRecordErr
- return
- }
- //如果 userRecord 表中的手机号不为空,那么就通过手机号来获取详情
- if userRecord != nil {
- if userRecord.BindAccount != "" {
- user, userErr := models.GetWxUserItemByUserMobile(userRecord.BindAccount)
- if userErr != nil && userErr.Error() != utils.ErrNoRow() {
- err = userErr
- return
- }
- if user != nil {
- item = user
- }
- }
- }
- return
- }
- // 通过openid获取用户信息
- func GetWxUserItemByOpenId2(openid string) (item *models.WxUserItem, err error) {
- //通过openid获取用户关联信息
- userRecord, userRecordErr := models.GetCygxUserRecordByOpenid(openid)
- fmt.Println(userRecordErr)
- //fmt.Println("userRecordErr", userRecordErr)
- 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.CygxUserId <= 0 {
- //err = ERR_USER_NOT_BIND
- item = new(models.WxUserItem)
- //格式化返回用户数据
- formatWxUserAndUserRecord(item, userRecord)
- return
- }
- fmt.Println("userRecord.UserId ", userRecord.CygxUserId)
- //获取用户信息
- item, wxUserErr := models.GetWxUserItemByUserId(userRecord.CygxUserId)
- //fmt.Println("wxUserErr", wxUserErr)
- 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
- }
- if item.RealName == "" {
- item.RealName = userRecord.RealName
- }
- //格式化返回用户数据
- formatWxUserAndUserRecord(item, userRecord)
- return
- }
|