123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- package public
- import (
- //"log"
- "time"
- util "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/redis"
- "go.mongodb.org/mongo-driver/bson/primitive"
- )
- const (
- ShareType_detail = 1 //分享类型 1-详情页
- ShareType_push = 2 //2-推送列表
- ShareType_lab = 3 //3-实验室
- shareProperty_passive = 1 // 1-被动分享
- shareProperty_active = 2 // 2-主动分享
- prefix_shareTimes = "shareTimes_" //redis 分享次数上限前缀
- redisPoolCode = "other"
- )
- /**
- 判断是否需要强制分享
- return - true-需要强制分享 false-不需强制分享
- */
- func CheckNeedShared(stl1, stl2, stlt1, stlt2 int, Sysconfig map[string]interface{}) bool {
- reFlag := false
- var shareTimeLine int
- var shareTimeLineTime int
- shareIntervalDays := util.IntAll((Sysconfig["share"].(map[string]interface{}))["shareIntervalDays"])
- if stl1 != 0 && stlt1 != 0 && stl2 != 0 && stlt2 != 0 {
- if stl1 == shareProperty_passive && stl2 == shareProperty_passive {
- if stlt1-stlt2 > 0 {
- shareTimeLineTime = stlt1
- } else {
- shareTimeLineTime = stlt2
- }
- } else if stl1 == shareProperty_passive {
- shareTimeLineTime = stlt1
- } else if stl2 == shareProperty_passive {
- shareTimeLineTime = stlt2
- } else {
- reFlag = true
- }
- if shareTimeLineTime != 0 {
- interval := GetIntervalDayFromLastToNow(shareTimeLineTime)
- if interval >= shareIntervalDays {
- reFlag = true
- }
- }
- } else if stl1 != 0 && stlt1 != 0 {
- shareTimeLine = stl1
- if shareTimeLine == shareProperty_passive {
- shareTimeLineTime = stlt1
- interval := GetIntervalDayFromLastToNow(shareTimeLineTime)
- if interval >= shareIntervalDays {
- reFlag = true
- }
- } else {
- reFlag = true
- }
- } else if stl2 != 0 && stlt2 != 0 {
- shareTimeLine = stl2
- if shareTimeLine == shareProperty_passive {
- shareTimeLineTime = stlt2
- interval := GetIntervalDayFromLastToNow(shareTimeLineTime)
- if interval >= shareIntervalDays {
- reFlag = true
- }
- } else {
- reFlag = true
- }
- } else {
- reFlag = true
- }
- return reFlag
- }
- //当前时间距上个时间的间隔天数
- func GetIntervalDayFromLastToNow(lasttime int) int {
- return (int(time.Now().Unix()) - lasttime) / (60 * 60 * 24)
- }
- /**
- 判断用户是否需要强制分享
- userId - 用户userId
- shareType - 分享类型 1-详情页 2-推送列表 3-实验室
- return - true-需强制分享 false-不需强制分享
- */
- func CheckUserNeedForceShare(userId string, shareType int, Sysconfig map[string]interface{}) bool {
- defer util.Catch()
- weekday := time.Now().Local().Weekday().String()
- if weekday == "Sunday" || weekday == "Saturday" {
- return false
- }
- needForceShareFlag := false
- shareConfigMap := Sysconfig["share"].(map[string]interface{})
- //强制分享功能是否启用 true-启用 false-不启用
- if shareConfigMap["forceShareEnabled"].(bool) {
- userInfo, ok := MQFW.FindById("user", userId, nil)
- if ok && userInfo != nil {
- isNewUserFlag := false
- if shareType == ShareType_detail {
- //是否为新用户-详情页分享
- if (*userInfo)["l_registedate"] != nil {
- regDate := (*userInfo)["l_registedate"].(int64)
- onlineDate := shareConfigMap["onlineDate"].(string)
- regDaysForNewUser := util.IntAll(shareConfigMap["regDaysForNewUser"])
- onlineTime, _ := time.ParseInLocation("2006-01-02 15:04:05", onlineDate, time.Local)
- onlineTimeI := onlineTime.Unix()
- interval := GetIntervalDayFromLastToNow(int(regDate))
- //log.Println("----------", openid, "userRegisterDays:", interval, regDate, onlineTimeI, "----------")
- if regDate-onlineTimeI > 0 && interval >= regDaysForNewUser {
- isNewUserFlag = true
- } else {
- isNewUserFlag = false
- }
- }
- }
- //log.Println("----------", openid, "isNewUserFlag:", isNewUserFlag, "----------")
- // 1.详情页-只新用户,需强制分享(分享次数未达上限 && 未分享)
- // 2.推送消息、实验室-不区分新老用户,均需强制分享(分享次数未达上限 && 未分享)
- if (shareType == ShareType_detail && isNewUserFlag) || shareType == ShareType_push || shareType == ShareType_lab {
- //分享次数是否超过上限
- hour, _, _ := time.Now().Clock()
- startHour := util.IntAll(shareConfigMap["startHour"])
- endHour := util.IntAll(shareConfigMap["endHour"]) - 1
- shareTimesUpperLimitR := util.IntAll(shareConfigMap["shareTimesUpperLimitR"]) / (endHour - startHour + 1)
- shareTimesUpperLimitIrr := util.IntAll(shareConfigMap["shareTimesUpperLimitIrr"])
- reachedUpperLimitFlag := false
- if hour >= startHour && hour <= endHour {
- shareTimes := redis.GetInt(redisPoolCode, prefix_shareTimes+time.Now().Format("2006010215"))
- //log.Println("----------", openid, "redis-shareTimes_", time.Now().Format("2006010215"), shareTimes, "----------")
- if shareTimes >= shareTimesUpperLimitR {
- reachedUpperLimitFlag = true
- }
- } else {
- shareTimes := redis.GetInt(redisPoolCode, prefix_shareTimes+time.Now().Format("20060102"))
- //log.Println("----------", openid, "redis-shareTimes_", time.Now().Format("20060102"), shareTimes, "----------")
- if shareTimes >= shareTimesUpperLimitIrr {
- reachedUpperLimitFlag = true
- }
- }
- //log.Println("----------", openid, "reachedUpperLimitFlag:", reachedUpperLimitFlag, "----------")
- //分享次数未超上限,判断是否已分享
- if !reachedUpperLimitFlag {
- //是否已分享
- sharedFlag := false
- detailSTL := util.IntAll((*userInfo)["i_detailsharetimeline"]) //公告详情页分享标志
- pushSTL := util.IntAll((*userInfo)["i_pushsharetimeline"]) //推送列表分享标志
- labSTL := util.IntAll((*userInfo)["i_jylabsharetimeline"]) //实验室分享标志
- var shareTimeLine interface{}
- if shareType == ShareType_detail {
- shareTimeLine = detailSTL
- } else if shareType == ShareType_push {
- shareTimeLine = pushSTL
- } else if shareType == ShareType_lab {
- shareTimeLine = labSTL
- }
- if shareTimeLine != nil && (util.IntAll(shareTimeLine) == shareProperty_passive || util.IntAll(shareTimeLine) == shareProperty_active) {
- sharedFlag = true
- }
- //log.Println("----------", openid, "sharedFlag:", sharedFlag, "----------")
- //未分享时,判断是否需强制分享
- if !sharedFlag {
- detailSTLT := util.IntAll((*userInfo)["i_detailsharetimelinetime"])
- pushSTLT := util.IntAll((*userInfo)["i_pushsharetimelinetime"])
- labSTLT := util.IntAll((*userInfo)["i_jylabsharetimelinetime"])
- if shareType == ShareType_detail {
- needForceShareFlag = CheckNeedShared(pushSTL, labSTL, pushSTLT, labSTLT, Sysconfig)
- } else if shareType == ShareType_push {
- needForceShareFlag = CheckNeedShared(detailSTL, labSTL, detailSTLT, labSTLT, Sysconfig)
- } else if shareType == ShareType_lab {
- needForceShareFlag = CheckNeedShared(detailSTL, pushSTL, detailSTLT, pushSTLT, Sysconfig)
- }
- }
- }
- }
- }
- }
- //log.Println("----------", openid, "needForceShareFlag:", needForceShareFlag, "----------")
- //log.Println("----------", "判断用户是否需要强制分享end", openid, "----------")
- return needForceShareFlag
- }
- /**
- 成功分享后 更改分享相关信息
- shareType - 分享类型 1-详情页 2-推送列表 3-实验室
- shareProperty - 分享性质 1-被动分享 2-主动分享
- */
- func UpdateShareStatus(userId, platform string, shareType, shareProperty, ispcforceshare int64, isRepair bool, Sysconfig map[string]interface{}) {
- defer util.Catch()
- if userId != "" && shareType != 0 && shareProperty != 0 {
- _id, _ := primitive.ObjectIDFromHex(userId)
- queryM := map[string]interface{}{
- "_id": _id,
- }
- setM := map[string]interface{}{}
- now := time.Now()
- tp := ""
- field := ""
- if shareType == ShareType_detail {
- tp = "detail"
- field = "i_detailsharetimeline"
- setM["i_detailsharetimeline"] = shareProperty
- setM["i_detailsharetimelinetime"] = now.Unix()
- if ispcforceshare == 1 {
- redis.Put(redisPoolCode, "pcbiddetail_shareTimeline_"+userId, 1, 60)
- }
- } else if shareType == ShareType_push {
- tp = "push"
- field = "i_pushsharetimeline"
- setM["i_pushsharetimeline"] = shareProperty
- setM["i_pushsharetimelinetime"] = now.Unix()
- } else if shareType == ShareType_lab {
- tp = "lab"
- field = "i_jylabsharetimeline"
- setM["i_jylabsharetimeline"] = shareProperty
- setM["i_jylabsharetimelinetime"] = now.Unix()
- }
- flag := true
- if isRepair {
- if MQFW.Count("user", map[string]interface{}{
- "_id": _id,
- field: map[string]interface{}{"$exists": 0},
- }) == 0 {
- flag = false
- }
- }
- if flag {
- if shareProperty == shareProperty_active {
- queryM[field] = map[string]interface{}{"$exists": 0}
- }
- MQFW.Update("user", queryM, map[string]interface{}{
- "$set": setM,
- }, false, false)
- logM := map[string]interface{}{
- "s_userid": userId,
- "i_status": shareProperty,
- "l_createtime": now.Unix(),
- "s_type": tp,
- "s_platform": platform,
- }
- if isRepair {
- logM["s_other"] = "repair"
- }
- MQFW.Save("share_log", logM)
- hour, _, _ := now.Clock()
- shareConfigMap := Sysconfig["share"].(map[string]interface{})
- startHour := util.IntAll(shareConfigMap["startHour"])
- endHour := util.IntAll(shareConfigMap["endHour"]) - 1
- if hour >= startHour && hour <= endHour {
- redis.Incr(redisPoolCode, prefix_shareTimes+now.Format("2006010215"))
- } else {
- redis.Incr(redisPoolCode, prefix_shareTimes+now.Format("20060102"))
- }
- }
- }
- }
|