user.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package services
  2. import (
  3. "errors"
  4. "eta/eta_mini_api/models"
  5. "eta/eta_mini_api/services/go_redis"
  6. "eta/eta_mini_api/utils"
  7. "time"
  8. )
  9. // BindUser 用户绑定
  10. func BindUser(unionId, openId, phone, email, areaCode string, departmentId int, realName string) (userItem *models.User, errMsg string, err error) {
  11. if phone == "" && email == "" {
  12. err = errors.New("手机号或邮箱必填一个")
  13. return
  14. }
  15. // 根据openid获取用户信息
  16. if openId == "" {
  17. err = errors.New("openid不能为空")
  18. return
  19. }
  20. userItem, err = models.GetUserByOpenId(openId)
  21. if err != nil && err.Error() != utils.ErrNoRow() {
  22. return
  23. }
  24. var userId int
  25. curTime := time.Now()
  26. if userItem != nil {
  27. userId = userItem.UserId
  28. if userItem.Phone != "" && userItem.Phone != phone && phone != "" {
  29. errMsg = "该手机号已绑定其他微信号"
  30. return
  31. }
  32. if userItem.Email != "" && userItem.Email != email && email != "" {
  33. errMsg = "该邮箱已绑定其他微信号"
  34. return
  35. }
  36. if userItem.Phone == "" && phone != "" {
  37. user := &models.User{
  38. UserId: userItem.UserId,
  39. Phone: phone,
  40. AreaCode: areaCode,
  41. RegisterTime: curTime,
  42. ModifyTime: curTime,
  43. }
  44. updateUserErr := user.Update([]string{"phone", "area_code", "register_time", "modify_time"})
  45. if updateUserErr != nil {
  46. err = updateUserErr
  47. return
  48. }
  49. }
  50. if userItem.Email == "" && email != "" {
  51. user := &models.User{
  52. UserId: userItem.UserId,
  53. Email: email,
  54. RegisterTime: curTime,
  55. ModifyTime: curTime,
  56. }
  57. updateUserErr := user.Update([]string{"email", "modify_time", "register_time"})
  58. if updateUserErr != nil {
  59. err = updateUserErr
  60. return
  61. }
  62. }
  63. } else {
  64. //根据手机号获取用户信息
  65. var phoneUser, emailUser *models.User
  66. if phone != "" {
  67. tmpUser, userErr := models.GetUserByPhone(phone)
  68. if userErr != nil && userErr.Error() != utils.ErrNoRow() {
  69. err = userErr
  70. return
  71. }
  72. phoneUser = tmpUser
  73. }
  74. //根据邮箱获取用户信息
  75. if email != "" {
  76. tmpUser, userErr := models.GetUserByEmail(email)
  77. if userErr != nil && userErr.Error() != utils.ErrNoRow() {
  78. err = userErr
  79. return
  80. }
  81. emailUser = tmpUser
  82. }
  83. // 理论上来说不会同时出现手机号和邮箱同时存在的情况
  84. if phoneUser != nil && emailUser != nil && phoneUser.UserId != emailUser.UserId {
  85. errMsg = "该手机号和邮箱已绑定其他微信号"
  86. err = errors.New("该手机号和邮箱已绑定其他微信号")
  87. return
  88. }
  89. if phoneUser != nil {
  90. userItem = phoneUser
  91. }
  92. if emailUser != nil {
  93. userItem = emailUser
  94. }
  95. //如果查询出来的用户是nil,那么需要新增用户
  96. if userItem == nil {
  97. key := utils.CACHE_ACCESS_WX_BIND + phone + ":" + email
  98. isHas := go_redis.IsExist(key)
  99. if isHas {
  100. err = errors.New("多次提交,请关闭页面重新进入")
  101. return
  102. }
  103. go_redis.SetNX(key, "ok", time.Second*300)
  104. user := &models.User{
  105. Phone: phone,
  106. AreaCode: areaCode,
  107. Email: email,
  108. Status: utils.UserStatusPotential,
  109. OpenId: openId,
  110. UnionId: unionId,
  111. }
  112. tmpUserId, addUserErr := user.Insert()
  113. //添加完成,清除缓存
  114. _ = go_redis.Delete(key)
  115. if addUserErr != nil {
  116. err = addUserErr
  117. return
  118. }
  119. user.UserId = int(tmpUserId)
  120. userId = int(tmpUserId)
  121. userItem, err = models.GetUserById(userId)
  122. if err != nil {
  123. errMsg = "获取用户信息失败"
  124. return
  125. }
  126. } else {
  127. userItem.OpenId = openId
  128. userItem.UnionId = unionId
  129. err = userItem.Update([]string{"open_id", "union_id"})
  130. if err != nil {
  131. errMsg = "用户绑定失败"
  132. return
  133. }
  134. userId = userItem.UserId
  135. }
  136. }
  137. //如果该用户 绑定注册状态 字段处于 未注册 的情况下,那么去修改该数据
  138. if !userItem.IsRegistered {
  139. err = models.ModifyUserRegisterStatus(userId, true, curTime, curTime)
  140. if err != nil {
  141. return
  142. }
  143. }
  144. userRecord, er := models.GetUserRecordByUnionId(userItem.UnionId)
  145. if er != nil && er.Error() != utils.ErrNoRow() {
  146. err = errors.New("获取用户关注记录失败")
  147. return
  148. }
  149. if !userItem.IsSubscribed && userRecord != nil {
  150. userRecord.UserId = userId
  151. er = userRecord.Update([]string{"user_id"})
  152. if er != nil {
  153. err = errors.New("获取用户关注记录失败")
  154. return
  155. }
  156. userItem.IsSubscribed = true
  157. er = userItem.Update([]string{"is_subscribed"})
  158. if er != nil {
  159. err = errors.New("获取用户关注记录失败")
  160. return
  161. }
  162. }
  163. msg, err := SaveUserInfo(userId, departmentId, realName)
  164. if msg != "" {
  165. errMsg = msg
  166. }
  167. return
  168. }
  169. // SaveUserInfo 保存用户信息
  170. func SaveUserInfo(userId, departmentId int, realName string) (msg string, err error) {
  171. user, err := models.GetUserById(userId)
  172. if err != nil {
  173. return
  174. }
  175. if user.RealName == "" && realName == "" {
  176. msg = "请输入姓名"
  177. return
  178. }
  179. updateCols := make([]string, 0)
  180. if realName != "" && user.RealName != realName {
  181. user.RealName = realName
  182. updateCols = append(updateCols, "real_name")
  183. }
  184. if departmentId > 0 && user.SellerDepartmentId != departmentId {
  185. department, er := models.GetSysDepartmentById(departmentId)
  186. if er != nil {
  187. err = er
  188. if err.Error() == utils.ErrNoRow() {
  189. msg = "部门不存在,请刷新重试"
  190. return
  191. }
  192. msg = "获取部门信息失败"
  193. return
  194. }
  195. parentDepartment, er := models.GetSysDepartmentById(department.ParentId)
  196. if er != nil {
  197. err = er
  198. if err.Error() == utils.ErrNoRow() {
  199. msg = "父级部门不存在,请刷新重试"
  200. return
  201. }
  202. msg = "获取部门信息失败"
  203. return
  204. }
  205. if parentDepartment.SysDepartmentName != "营业部" {
  206. msg = "部门信息异常"
  207. return
  208. }
  209. user.SellerDepartmentId = departmentId
  210. user.SellerDepartmentName = department.SysDepartmentName
  211. updateCols = append(updateCols, "seller_department_id")
  212. updateCols = append(updateCols, "seller_department_name")
  213. }
  214. if (user.SellerDepartmentName == "" || user.SellerDepartmentId == 0) && departmentId <= 0 {
  215. msg = "请选择营业部门"
  216. return
  217. }
  218. err = user.Update(updateCols)
  219. if err != nil {
  220. msg = "用户信息保存失败"
  221. return
  222. }
  223. return
  224. }