user_record.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_cygxzs/models"
  6. "hongze/hongze_cygxzs/utils"
  7. "time"
  8. )
  9. func AddCygxUserRecord(wxUserInfo *WxUserInfo) {
  10. items := new(models.CygxUserRecord)
  11. items.OpenId = wxUserInfo.Openid
  12. items.UnionId = wxUserInfo.Unionid
  13. items.NickName = wxUserInfo.Nickname
  14. items.Sex = wxUserInfo.Sex
  15. items.Province = wxUserInfo.Province
  16. items.City = wxUserInfo.City
  17. items.Country = wxUserInfo.Country
  18. items.Headimgurl = wxUserInfo.Headimgurl
  19. items.CreateTime = time.Now()
  20. user, err := models.GetWxUserItemByUserUnionId(wxUserInfo.Unionid)
  21. if err != nil && err.Error() != utils.ErrNoRow() {
  22. go utils.SendAlarmMsg("发送邮件:用户关注查研观向小助手时记录相关信息失败,GetWxUserItemByUserUnionId Openid:"+wxUserInfo.Openid+err.Error(), 2)
  23. return
  24. }
  25. if user != nil {
  26. items.CygxUserId = user.UserId
  27. items.CygxBindAccount = user.Mobile
  28. }
  29. _, err = models.AddCygxUserRecord(items)
  30. if err != nil {
  31. go utils.SendAlarmMsg("发送邮件:用户关注查研观向小助手时记录相关信息失败,Openid:"+wxUserInfo.Openid+err.Error(), 2)
  32. }
  33. }
  34. // UpdateCygxUserRecordMobile 更新用户手机号与Openid绑定关系
  35. func UpdateCygxUserRecordMobile(mobile string, uid int) {
  36. if mobile == "" || uid == 0 {
  37. return
  38. }
  39. var err error
  40. defer func() {
  41. if err != nil {
  42. go utils.SendAlarmMsg(fmt.Sprint("更新用户手机号与Openid绑定关系失败 mobile:", mobile, err.Error()), 2)
  43. }
  44. }()
  45. userRecord, e := models.GetUserRecordByBindAccount(mobile)
  46. if e != nil {
  47. err = errors.New("GetUserRecordByBindAccount, Err: " + e.Error())
  48. return
  49. }
  50. if userRecord.UnionId != "" {
  51. e = models.UpdateCygxUserRecordMobile(uid, mobile, userRecord.UnionId)
  52. if e != nil {
  53. err = errors.New("UpdateCygxUserRecordMobile, Err: " + e.Error())
  54. return
  55. }
  56. }
  57. return
  58. }