user_admin_share.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_mfyx/models"
  6. "hongze/hongze_mfyx/utils"
  7. "strconv"
  8. "time"
  9. )
  10. // 判断用户是否属于销售,并且获取对应加密的分享码
  11. func GetInviteShareCode(mobile string) (inviteShareCode string) {
  12. if mobile == "" {
  13. return
  14. }
  15. var err error
  16. defer func() {
  17. if err != nil {
  18. fmt.Println(err)
  19. go utils.SendAlarmMsg(fmt.Sprint("判断用户是否属于销售,并且获取对应加密的分享码失败 GetInviteShareCode, err:", err.Error()), 2)
  20. }
  21. }()
  22. adminDetail, e := models.GetSysAdminByMobile(mobile)
  23. if e != nil && e.Error() != utils.ErrNoRow() {
  24. err = errors.New("GetSysAdminByMobile, Err: " + e.Error())
  25. return
  26. }
  27. if adminDetail == nil {
  28. return
  29. }
  30. inviteShareCode = utils.MD5(strconv.Itoa(adminDetail.AdminId)) //MD5 对ID进行加密
  31. total, e := models.GetCygxUserAdminShareCodeByShareCodeCount(inviteShareCode)
  32. if e != nil {
  33. err = errors.New("GetCygxUserAdminShareCodeByShareCodeCount, Err: " + e.Error())
  34. return
  35. }
  36. //如果不存在就新增一个
  37. if total == 0 {
  38. item := new(models.CygxUserAdminShareCode)
  39. item.SellerName = adminDetail.RealName
  40. item.SellerId = adminDetail.AdminId
  41. item.Mobile = adminDetail.Mobile
  42. item.CreateTime = time.Now()
  43. item.ModifyTime = time.Now()
  44. item.ShareCode = inviteShareCode
  45. e = models.AddCygxUserAdminShareCode(item)
  46. if e != nil {
  47. err = errors.New("AddCygxUserAdminShareCode, Err: " + e.Error())
  48. return
  49. }
  50. }
  51. return
  52. }
  53. // 记录分享来源
  54. func AddCygxUserAdminShareHistory(user *models.WxUserItem, source, sourceTitle, inviteShareCode string, sourceId int) {
  55. if inviteShareCode == "" {
  56. return
  57. }
  58. if user.UserId == 0 {
  59. return
  60. }
  61. var err error
  62. //如果是已经上传了音视的活动不记录
  63. if source == utils.CYGX_OBJ_ACTIVITY {
  64. totalvi, e := models.GetCygxActivityVideoCount(sourceId)
  65. if e != nil {
  66. err = errors.New("GetCygxActivityVideoCount, Err: " + e.Error())
  67. return
  68. }
  69. if totalvi > 0 {
  70. return
  71. }
  72. totalvo, e := models.GetCygxActivityVoiceCount(sourceId)
  73. if e != nil {
  74. err = errors.New("GetCygxActivityVoiceCount, Err: " + e.Error())
  75. return
  76. }
  77. if totalvo > 0 {
  78. return
  79. }
  80. }
  81. //3秒之内多次请求,不重复记录
  82. key := "CYGX_" + source + "_" + strconv.Itoa(sourceId) + "_" + strconv.Itoa(user.UserId)
  83. if utils.Rc.IsExist(key) {
  84. return
  85. }
  86. utils.Rc.Put(key, 1, 3*time.Second)
  87. defer func() {
  88. if err != nil {
  89. fmt.Println(err)
  90. go utils.SendAlarmMsg(fmt.Sprint("记录分享来源失败 AddCygxUserAdminShareHistory, err:", err.Error(), "inviteShareCode:", inviteShareCode, "source", source), 2)
  91. }
  92. }()
  93. adminDetail, e := models.GetUserAdminShareCodeByShareCode(inviteShareCode)
  94. if e != nil {
  95. err = errors.New("GetUserAdminShareCodeByShareCode, Err: " + e.Error())
  96. return
  97. }
  98. if adminDetail == nil {
  99. return
  100. }
  101. item := new(models.CygxUserAdminShareHistory)
  102. switch source {
  103. case utils.CYGX_OBJ_ACTIVITY:
  104. item.Action = "查看活动"
  105. case utils.CYGX_OBJ_ARTICLE:
  106. item.Action = "查看报告"
  107. case utils.CYGX_OBJ_ACTIVITYVIDEO:
  108. item.Action = "查看活动回放"
  109. case utils.CYGX_OBJ_ACTIVITYVOICE:
  110. item.Action = "查看活动回放"
  111. case utils.CYGX_OBJ_YANXUANSPECIAL:
  112. item.Action = "查看专栏"
  113. case "login":
  114. item.Action = "注册"
  115. }
  116. item.UserId = user.UserId
  117. item.Mobile = user.Mobile
  118. item.Email = user.Email
  119. item.CompanyId = user.CompanyId
  120. item.CompanyName = user.CompanyName
  121. item.RealName = user.RealName
  122. item.SellerName, item.SellerId = GetSellerName(user) // 销售姓名
  123. item.Source = source
  124. item.SourceId = sourceId
  125. item.SourceTitle = sourceTitle
  126. item.ShareId = adminDetail.SellerId
  127. item.ShareName = adminDetail.SellerName
  128. item.RegisterPlatform = utils.REGISTER_PLATFORM
  129. item.CreateTime = time.Now()
  130. item.ModifyTime = time.Now()
  131. e = models.AddCygxUserAdminShareHistory(item)
  132. if e != nil {
  133. err = errors.New("AddCygxUserAdminShareHistory, Err: " + e.Error())
  134. return
  135. }
  136. return
  137. }
  138. // 根据分享码获取对应销售的ID以及姓名
  139. func GetAdminByInviteShareCode(inviteShareCode string) (shareName string, shareId int) {
  140. var err error
  141. defer func() {
  142. if err != nil {
  143. fmt.Println(err)
  144. go utils.SendAlarmMsg(fmt.Sprint("记录分享来源失败 GetAdminByInviteShareCode, err:", err.Error()), 2)
  145. }
  146. }()
  147. adminDetail, e := models.GetUserAdminShareCodeByShareCode(inviteShareCode)
  148. if e != nil && e.Error() != utils.ErrNoRow() {
  149. err = errors.New("GetUserAdminShareCodeByShareCode, Err: " + e.Error())
  150. return
  151. }
  152. if adminDetail == nil {
  153. return
  154. }
  155. shareId = adminDetail.SellerId
  156. shareName = adminDetail.SellerName
  157. return
  158. }