user.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_mfyx_gzh/models"
  6. "hongze/hongze_mfyx_gzh/utils"
  7. )
  8. var ERR_NO_USER_RECORD = errors.New("用户关系没有入库")
  9. var ERR_USER_NOT_BIND = errors.New("用户没有绑定")
  10. //通过用户 关系表记录 和 用户记录 格式化返回 用户数据
  11. func formatWxUserAndUserRecord(wxUser *models.WxUserItem, userRecord *models.CygxMfyxGzhUserRecord) {
  12. wxUser.OpenId = userRecord.OpenId
  13. wxUser.UnionId = userRecord.UnionId
  14. wxUser.NickName = userRecord.NickName
  15. //wxUser.RealName = userRecord.RealName
  16. //wxUser.BindAccount = userRecord.BindAccount
  17. wxUser.Headimgurl = userRecord.Headimgurl
  18. //wxUser.SessionKey = userRecord.SessionKey
  19. if wxUser.Headimgurl == "" {
  20. wxUser.Headimgurl = utils.HeadimgurlDefault
  21. }
  22. }
  23. func GetWxUserItemByOpenId(unionId string) (item *models.WxUserItem, err error) {
  24. //通过openid获取用户关联信息
  25. item = new(models.WxUserItem)
  26. item.UnionId = unionId // 先写入 unionId
  27. userRecord, userRecordErr := models.GetUserRecordByUnionId(unionId)
  28. if userRecordErr != nil && userRecordErr.Error() != utils.ErrNoRow() {
  29. err = userRecordErr
  30. return
  31. }
  32. //如果 userRecord 表中的手机号不为空,那么就通过手机号来获取详情
  33. if userRecord != nil {
  34. if userRecord.BindAccount != "" {
  35. user, userErr := models.GetWxUserItemByUserMobile(userRecord.BindAccount)
  36. if userErr != nil && userErr.Error() != utils.ErrNoRow() {
  37. err = userErr
  38. return
  39. }
  40. if user != nil {
  41. item = user
  42. }
  43. }
  44. }
  45. return
  46. }
  47. // 通过openid获取用户信息
  48. func GetWxUserItemByOpenId2(openid string) (item *models.WxUserItem, err error) {
  49. //通过openid获取用户关联信息
  50. userRecord, userRecordErr := models.GetCygxUserRecordByOpenid(openid)
  51. fmt.Println(userRecordErr)
  52. //fmt.Println("userRecordErr", userRecordErr)
  53. if userRecordErr != nil {
  54. if userRecordErr.Error() == utils.ErrNoRow() {
  55. err = ERR_NO_USER_RECORD
  56. return
  57. } else {
  58. err = userRecordErr
  59. return
  60. }
  61. }
  62. //该openid在系统中没有关联关系
  63. if userRecord == nil {
  64. err = ERR_NO_USER_RECORD
  65. return
  66. }
  67. //该openid没有绑定用户
  68. if userRecord.CygxUserId <= 0 {
  69. //err = ERR_USER_NOT_BIND
  70. item = new(models.WxUserItem)
  71. //格式化返回用户数据
  72. formatWxUserAndUserRecord(item, userRecord)
  73. return
  74. }
  75. fmt.Println("userRecord.UserId ", userRecord.CygxUserId)
  76. //获取用户信息
  77. item, wxUserErr := models.GetWxUserItemByUserId(userRecord.CygxUserId)
  78. //fmt.Println("wxUserErr", wxUserErr)
  79. if wxUserErr != nil {
  80. err = wxUserErr
  81. //如果是找不到数据,那么可能是该用户被删除了,但是user_record没有删除对应的关系
  82. if wxUserErr.Error() == utils.ErrNoRow() {
  83. //用户被删除了,但是user_record没有删除对应的关系,那么去解除绑定
  84. userUnbindErr := models.UnBindUserRecordByOpenid(openid)
  85. if userUnbindErr != nil {
  86. err = userUnbindErr
  87. return
  88. }
  89. //返回状态为 用户未绑定 逻辑代码
  90. //err = ERR_USER_NOT_BIND
  91. item = new(models.WxUserItem)
  92. //格式化返回用户数据
  93. formatWxUserAndUserRecord(item, userRecord)
  94. return
  95. }
  96. return
  97. }
  98. if item.RealName == "" {
  99. item.RealName = userRecord.RealName
  100. }
  101. //格式化返回用户数据
  102. formatWxUserAndUserRecord(item, userRecord)
  103. return
  104. }