forceshare.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package public
  2. import (
  3. //"log"
  4. "time"
  5. util "app.yhyue.com/moapp/jybase/common"
  6. "app.yhyue.com/moapp/jybase/redis"
  7. "go.mongodb.org/mongo-driver/bson/primitive"
  8. )
  9. const (
  10. ShareType_detail = 1 //分享类型 1-详情页
  11. ShareType_push = 2 //2-推送列表
  12. ShareType_lab = 3 //3-实验室
  13. shareProperty_passive = 1 // 1-被动分享
  14. shareProperty_active = 2 // 2-主动分享
  15. prefix_shareTimes = "shareTimes_" //redis 分享次数上限前缀
  16. redisPoolCode = "other"
  17. )
  18. /**
  19. 判断是否需要强制分享
  20. return - true-需要强制分享 false-不需强制分享
  21. */
  22. func CheckNeedShared(stl1, stl2, stlt1, stlt2 int, Sysconfig map[string]interface{}) bool {
  23. reFlag := false
  24. var shareTimeLine int
  25. var shareTimeLineTime int
  26. shareIntervalDays := util.IntAll((Sysconfig["share"].(map[string]interface{}))["shareIntervalDays"])
  27. if stl1 != 0 && stlt1 != 0 && stl2 != 0 && stlt2 != 0 {
  28. if stl1 == shareProperty_passive && stl2 == shareProperty_passive {
  29. if stlt1-stlt2 > 0 {
  30. shareTimeLineTime = stlt1
  31. } else {
  32. shareTimeLineTime = stlt2
  33. }
  34. } else if stl1 == shareProperty_passive {
  35. shareTimeLineTime = stlt1
  36. } else if stl2 == shareProperty_passive {
  37. shareTimeLineTime = stlt2
  38. } else {
  39. reFlag = true
  40. }
  41. if shareTimeLineTime != 0 {
  42. interval := GetIntervalDayFromLastToNow(shareTimeLineTime)
  43. if interval >= shareIntervalDays {
  44. reFlag = true
  45. }
  46. }
  47. } else if stl1 != 0 && stlt1 != 0 {
  48. shareTimeLine = stl1
  49. if shareTimeLine == shareProperty_passive {
  50. shareTimeLineTime = stlt1
  51. interval := GetIntervalDayFromLastToNow(shareTimeLineTime)
  52. if interval >= shareIntervalDays {
  53. reFlag = true
  54. }
  55. } else {
  56. reFlag = true
  57. }
  58. } else if stl2 != 0 && stlt2 != 0 {
  59. shareTimeLine = stl2
  60. if shareTimeLine == shareProperty_passive {
  61. shareTimeLineTime = stlt2
  62. interval := GetIntervalDayFromLastToNow(shareTimeLineTime)
  63. if interval >= shareIntervalDays {
  64. reFlag = true
  65. }
  66. } else {
  67. reFlag = true
  68. }
  69. } else {
  70. reFlag = true
  71. }
  72. return reFlag
  73. }
  74. //当前时间距上个时间的间隔天数
  75. func GetIntervalDayFromLastToNow(lasttime int) int {
  76. return (int(time.Now().Unix()) - lasttime) / (60 * 60 * 24)
  77. }
  78. /**
  79. 判断用户是否需要强制分享
  80. userId - 用户userId
  81. shareType - 分享类型 1-详情页 2-推送列表 3-实验室
  82. return - true-需强制分享 false-不需强制分享
  83. */
  84. func CheckUserNeedForceShare(userId string, shareType int, Sysconfig map[string]interface{}) bool {
  85. defer util.Catch()
  86. weekday := time.Now().Local().Weekday().String()
  87. if weekday == "Sunday" || weekday == "Saturday" {
  88. return false
  89. }
  90. needForceShareFlag := false
  91. shareConfigMap := Sysconfig["share"].(map[string]interface{})
  92. //强制分享功能是否启用 true-启用 false-不启用
  93. if shareConfigMap["forceShareEnabled"].(bool) {
  94. userInfo, ok := MQFW.FindById("user", userId, nil)
  95. if ok && userInfo != nil {
  96. isNewUserFlag := false
  97. if shareType == ShareType_detail {
  98. //是否为新用户-详情页分享
  99. if (*userInfo)["l_registedate"] != nil {
  100. regDate := (*userInfo)["l_registedate"].(int64)
  101. onlineDate := shareConfigMap["onlineDate"].(string)
  102. regDaysForNewUser := util.IntAll(shareConfigMap["regDaysForNewUser"])
  103. onlineTime, _ := time.ParseInLocation("2006-01-02 15:04:05", onlineDate, time.Local)
  104. onlineTimeI := onlineTime.Unix()
  105. interval := GetIntervalDayFromLastToNow(int(regDate))
  106. //log.Println("----------", openid, "userRegisterDays:", interval, regDate, onlineTimeI, "----------")
  107. if regDate-onlineTimeI > 0 && interval >= regDaysForNewUser {
  108. isNewUserFlag = true
  109. } else {
  110. isNewUserFlag = false
  111. }
  112. }
  113. }
  114. //log.Println("----------", openid, "isNewUserFlag:", isNewUserFlag, "----------")
  115. // 1.详情页-只新用户,需强制分享(分享次数未达上限 && 未分享)
  116. // 2.推送消息、实验室-不区分新老用户,均需强制分享(分享次数未达上限 && 未分享)
  117. if (shareType == ShareType_detail && isNewUserFlag) || shareType == ShareType_push || shareType == ShareType_lab {
  118. //分享次数是否超过上限
  119. hour, _, _ := time.Now().Clock()
  120. startHour := util.IntAll(shareConfigMap["startHour"])
  121. endHour := util.IntAll(shareConfigMap["endHour"]) - 1
  122. shareTimesUpperLimitR := util.IntAll(shareConfigMap["shareTimesUpperLimitR"]) / (endHour - startHour + 1)
  123. shareTimesUpperLimitIrr := util.IntAll(shareConfigMap["shareTimesUpperLimitIrr"])
  124. reachedUpperLimitFlag := false
  125. if hour >= startHour && hour <= endHour {
  126. shareTimes := redis.GetInt(redisPoolCode, prefix_shareTimes+time.Now().Format("2006010215"))
  127. //log.Println("----------", openid, "redis-shareTimes_", time.Now().Format("2006010215"), shareTimes, "----------")
  128. if shareTimes >= shareTimesUpperLimitR {
  129. reachedUpperLimitFlag = true
  130. }
  131. } else {
  132. shareTimes := redis.GetInt(redisPoolCode, prefix_shareTimes+time.Now().Format("20060102"))
  133. //log.Println("----------", openid, "redis-shareTimes_", time.Now().Format("20060102"), shareTimes, "----------")
  134. if shareTimes >= shareTimesUpperLimitIrr {
  135. reachedUpperLimitFlag = true
  136. }
  137. }
  138. //log.Println("----------", openid, "reachedUpperLimitFlag:", reachedUpperLimitFlag, "----------")
  139. //分享次数未超上限,判断是否已分享
  140. if !reachedUpperLimitFlag {
  141. //是否已分享
  142. sharedFlag := false
  143. detailSTL := util.IntAll((*userInfo)["i_detailsharetimeline"]) //公告详情页分享标志
  144. pushSTL := util.IntAll((*userInfo)["i_pushsharetimeline"]) //推送列表分享标志
  145. labSTL := util.IntAll((*userInfo)["i_jylabsharetimeline"]) //实验室分享标志
  146. var shareTimeLine interface{}
  147. if shareType == ShareType_detail {
  148. shareTimeLine = detailSTL
  149. } else if shareType == ShareType_push {
  150. shareTimeLine = pushSTL
  151. } else if shareType == ShareType_lab {
  152. shareTimeLine = labSTL
  153. }
  154. if shareTimeLine != nil && (util.IntAll(shareTimeLine) == shareProperty_passive || util.IntAll(shareTimeLine) == shareProperty_active) {
  155. sharedFlag = true
  156. }
  157. //log.Println("----------", openid, "sharedFlag:", sharedFlag, "----------")
  158. //未分享时,判断是否需强制分享
  159. if !sharedFlag {
  160. detailSTLT := util.IntAll((*userInfo)["i_detailsharetimelinetime"])
  161. pushSTLT := util.IntAll((*userInfo)["i_pushsharetimelinetime"])
  162. labSTLT := util.IntAll((*userInfo)["i_jylabsharetimelinetime"])
  163. if shareType == ShareType_detail {
  164. needForceShareFlag = CheckNeedShared(pushSTL, labSTL, pushSTLT, labSTLT, Sysconfig)
  165. } else if shareType == ShareType_push {
  166. needForceShareFlag = CheckNeedShared(detailSTL, labSTL, detailSTLT, labSTLT, Sysconfig)
  167. } else if shareType == ShareType_lab {
  168. needForceShareFlag = CheckNeedShared(detailSTL, pushSTL, detailSTLT, pushSTLT, Sysconfig)
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. //log.Println("----------", openid, "needForceShareFlag:", needForceShareFlag, "----------")
  176. //log.Println("----------", "判断用户是否需要强制分享end", openid, "----------")
  177. return needForceShareFlag
  178. }
  179. /**
  180. 成功分享后 更改分享相关信息
  181. shareType - 分享类型 1-详情页 2-推送列表 3-实验室
  182. shareProperty - 分享性质 1-被动分享 2-主动分享
  183. */
  184. func UpdateShareStatus(userId, platform string, shareType, shareProperty, ispcforceshare int64, isRepair bool, Sysconfig map[string]interface{}) {
  185. defer util.Catch()
  186. if userId != "" && shareType != 0 && shareProperty != 0 {
  187. _id, _ := primitive.ObjectIDFromHex(userId)
  188. queryM := map[string]interface{}{
  189. "_id": _id,
  190. }
  191. setM := map[string]interface{}{}
  192. now := time.Now()
  193. tp := ""
  194. field := ""
  195. if shareType == ShareType_detail {
  196. tp = "detail"
  197. field = "i_detailsharetimeline"
  198. setM["i_detailsharetimeline"] = shareProperty
  199. setM["i_detailsharetimelinetime"] = now.Unix()
  200. if ispcforceshare == 1 {
  201. redis.Put(redisPoolCode, "pcbiddetail_shareTimeline_"+userId, 1, 60)
  202. }
  203. } else if shareType == ShareType_push {
  204. tp = "push"
  205. field = "i_pushsharetimeline"
  206. setM["i_pushsharetimeline"] = shareProperty
  207. setM["i_pushsharetimelinetime"] = now.Unix()
  208. } else if shareType == ShareType_lab {
  209. tp = "lab"
  210. field = "i_jylabsharetimeline"
  211. setM["i_jylabsharetimeline"] = shareProperty
  212. setM["i_jylabsharetimelinetime"] = now.Unix()
  213. }
  214. flag := true
  215. if isRepair {
  216. if MQFW.Count("user", map[string]interface{}{
  217. "_id": _id,
  218. field: map[string]interface{}{"$exists": 0},
  219. }) == 0 {
  220. flag = false
  221. }
  222. }
  223. if flag {
  224. if shareProperty == shareProperty_active {
  225. queryM[field] = map[string]interface{}{"$exists": 0}
  226. }
  227. MQFW.Update("user", queryM, map[string]interface{}{
  228. "$set": setM,
  229. }, false, false)
  230. logM := map[string]interface{}{
  231. "s_userid": userId,
  232. "i_status": shareProperty,
  233. "l_createtime": now.Unix(),
  234. "s_type": tp,
  235. "s_platform": platform,
  236. }
  237. if isRepair {
  238. logM["s_other"] = "repair"
  239. }
  240. MQFW.Save("share_log", logM)
  241. hour, _, _ := now.Clock()
  242. shareConfigMap := Sysconfig["share"].(map[string]interface{})
  243. startHour := util.IntAll(shareConfigMap["startHour"])
  244. endHour := util.IntAll(shareConfigMap["endHour"]) - 1
  245. if hour >= startHour && hour <= endHour {
  246. redis.Incr(redisPoolCode, prefix_shareTimes+now.Format("2006010215"))
  247. } else {
  248. redis.Incr(redisPoolCode, prefix_shareTimes+now.Format("20060102"))
  249. }
  250. }
  251. }
  252. }