|
@@ -0,0 +1,341 @@
|
|
|
+package jy
|
|
|
+
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+ "fmt"
|
|
|
+ "log"
|
|
|
+ "math/rand"
|
|
|
+ . "mongodb"
|
|
|
+ qutil "qfw/util"
|
|
|
+ "qfw/util/mysql"
|
|
|
+ "qfw/util/redis"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+// 大会员状态redis缓存
|
|
|
+type BigVipBaseMsg struct {
|
|
|
+ Status int `json:"status"` //大会员状态
|
|
|
+ Used bool `json:"used"` //是否首次使用大会员
|
|
|
+ PowerMap map[int]bool `json:"p_map"` //权限列表
|
|
|
+ ProNum int `json:"p_num"` //可关注项目数量
|
|
|
+ EntNum int `json:"e_num"` //可关注企业数量(取企业情报监控和企业中标动态中最大的值)
|
|
|
+ DailyNum int `json:"d_num"` //数据包导出数量
|
|
|
+ Pid string `json:"pid"` //若为子账号此处为父节点userid
|
|
|
+ Uid string `json:"uid"` //用户id
|
|
|
+ HasTrial bool `json:"has_trial"` //是否试用过
|
|
|
+ Customers int `json:"customers"` //可关注客户数量
|
|
|
+ VipStatus int `json:"vip_status"` //超级订阅状态
|
|
|
+ Vip_BuySet BuySet `json:"vip_buyset"` //超级订阅套餐内容
|
|
|
+ EntnicheStatus int `json:"entniche_status"` //商机管理状态
|
|
|
+ IsUpgrade bool `json:"isUpgrade"` //是否是免费用户订阅升级用户 默认true
|
|
|
+ Registedate int64 `json:"registedate"` //用户注册时间
|
|
|
+ EntName string `json:"entname"` //企业名称
|
|
|
+ EntIsNew bool `json:"entIsNew"` //是否是新版商机管理用户
|
|
|
+ IsEntService bool `json:"isEntService"` //是否有商机管理服务
|
|
|
+ PrivateGD bool `json:"PrivateGD"` //广东移动DICT 用户
|
|
|
+ Email string `json:"email"`
|
|
|
+ EntInfo map[int]*EntInfoStruct `json:"entInfo"` //企业信息
|
|
|
+ MemberPowerType int64 `json:"memberPowerType"` //大会员权益类型 0无 1个人 2企业
|
|
|
+ VipPowerType int64 `json:"vipPowerType"` //超级订阅权益类型 0无 1个人 2企业
|
|
|
+ BaseUserId int `json:"base_user_id"` //用户base_user_id
|
|
|
+}
|
|
|
+
|
|
|
+type EntInfoStruct struct {
|
|
|
+ IsNew bool `json:"isNew"` //是否是新版商机管理
|
|
|
+ IsPower bool `json:"isPower"` //是否分配了商机管理权限
|
|
|
+ Status int `json:"status"` //是否购买
|
|
|
+ Name string `json:"name"` //企业名称
|
|
|
+ IsService bool `json:"isService"` //企业商机管理服务
|
|
|
+ RoleId int `json:"roleId"` //角色默认0:员工; 1:系统(企业)管理员;2:部门管理员
|
|
|
+}
|
|
|
+
|
|
|
+// 超级订阅购买内容
|
|
|
+type BuySet struct {
|
|
|
+ Upgrade int `json:"upgrade"` //是否是升级版;1是 其他不是
|
|
|
+ AreaCount int `json:"areacount"` //省份数量
|
|
|
+ BuyerclassCount int `json:"buyerclasscount"` //行业数
|
|
|
+}
|
|
|
+
|
|
|
+var FrontService, BackService map[string][]int
|
|
|
+
|
|
|
+const (
|
|
|
+ IsNewFreeTimeCell = 1637830020
|
|
|
+ BigmemberServiceTable = "bigmember_service"
|
|
|
+ BigmemberUserPowerTable = "bigmember_service_user"
|
|
|
+
|
|
|
+ PowerCacheDb = "other"
|
|
|
+ PowerCacheKey = "bigmember_power_3_%s"
|
|
|
+ OneDay = 60 * 60 * 24
|
|
|
+ UserUpdateAreaKey = "free_area_num_%s_%s"
|
|
|
+ BaseAreaNum = 1
|
|
|
+ VipFileUploadNumKey = "vip_file_num_%s_%s" //超级订阅附件本月一下载次数 %s:userid ; %s:当前月份-fmt.Sprint(time.Now().Month())
|
|
|
+ FilePackNumKey = "file_pack_num_%s_%s" //附件下载包本月 剩余次数 %s:userid %s 当前月份-fmt.Sprint(time.Now().Month())
|
|
|
+ BaseInfoCacheDb = "newother"
|
|
|
+ IsGetUserBaseInfoRedisKey = "baseinfo_%s"
|
|
|
+ RedisMenuKeyPC = "jy_workdesktopmenu_10000_PC_%s" //剑鱼appid:10000
|
|
|
+ RedisMenuKeyWX = "jy_workdesktopmenu_10000_WX_%s" //剑鱼appid:10000
|
|
|
+ RedisMenuKeyAPP = "jy_workdesktopmenu_10000_APP_%s" //剑鱼appid:10000
|
|
|
+ UserPowerRedisKey = "jy_userpowerredis_10000_%d_%s" //工作桌面 用户功能缓存(类似bigmember_power_3_%s)
|
|
|
+)
|
|
|
+
|
|
|
+// 初始化大会员权益
|
|
|
+func InitBigVipService(mysql *mysql.Mysql) {
|
|
|
+ serviceList := mysql.Find(BigmemberServiceTable, nil, "id,s_url_front,s_url_back", "id", -1, -1)
|
|
|
+ if serviceList == nil || len(*serviceList) == 0 {
|
|
|
+ panic(fmt.Sprintf("大会员初始权限失败,请检查mysql链接是否正常、查看%s表是否正常", BigmemberServiceTable))
|
|
|
+ }
|
|
|
+ FrontService, BackService = make(map[string][]int), make(map[string][]int)
|
|
|
+ for _, one := range *serviceList {
|
|
|
+ power := qutil.ObjToMap(one)
|
|
|
+ if power == nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ serviceId := qutil.IntAll((*power)["id"])
|
|
|
+ if urlFronts := qutil.ObjToString((*power)["s_url_front"]); urlFronts != "" {
|
|
|
+ for _, urlFront := range strings.Split(urlFronts, "&") {
|
|
|
+ FrontService[urlFront] = append(FrontService[urlFront], serviceId)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if urlBacks := qutil.ObjToString((*power)["s_url_back"]); urlBacks != "" {
|
|
|
+ for _, urlBack := range strings.Split(urlBacks, "&") {
|
|
|
+ BackService[urlBack] = append(BackService[urlBack], serviceId)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.Printf("init BigVipService status \nFrontService:%+v\nBackService:%+v\n", FrontService, BackService)
|
|
|
+}
|
|
|
+
|
|
|
+// 当大会员状态改变时清除此状态
|
|
|
+func ClearBigVipUserPower(userId string) bool {
|
|
|
+ cacheKey := fmt.Sprintf(PowerCacheKey, userId)
|
|
|
+ baseInfoCacheKey := fmt.Sprintf(IsGetUserBaseInfoRedisKey, userId)
|
|
|
+ redisMenuKeyPC := fmt.Sprintf(RedisMenuKeyPC, userId)
|
|
|
+ redisMenuKeyWX := fmt.Sprintf(RedisMenuKeyWX, userId)
|
|
|
+ redisMenuKeyAPP := fmt.Sprintf(RedisMenuKeyAPP, userId)
|
|
|
+ userPowerRedisKey := fmt.Sprintf(UserPowerRedisKey, time.Now().Day(), userId)
|
|
|
+ return redis.Del(BaseInfoCacheDb, cacheKey) && redis.Del(BaseInfoCacheDb, baseInfoCacheKey) && redis.Del(BaseInfoCacheDb, redisMenuKeyPC) && redis.Del(BaseInfoCacheDb, redisMenuKeyWX) && redis.Del(BaseInfoCacheDb, redisMenuKeyAPP) && redis.Del(BaseInfoCacheDb, userPowerRedisKey)
|
|
|
+}
|
|
|
+
|
|
|
+// 获取商机管理个人基本信息
|
|
|
+func GetEntnicheState(userId string, mysql *mysql.Mysql, mg MongodbSim) *BigVipBaseMsg {
|
|
|
+ userPower := BigVipBaseMsg{}
|
|
|
+ userPower.EntnicheStatus = 0
|
|
|
+ //手机号
|
|
|
+ data, ok := mg.FindById("user", userId, `{"s_phone":1,"s_m_phone":1,i_member_status":1,"i_member_give":1,"s_member_mainid":1,"i_member_sub_status":1,"i_member_trial":1,"i_vip_status":1,"o_vipjy":1,"o_jy":1,"l_registedate":1}`)
|
|
|
+ if ok && *data != nil && len(*data) > 0 {
|
|
|
+ //查询是否是商机管理付费用户
|
|
|
+ phone, _ := qutil.If((*data)["s_phone"] != nil, (*data)["s_phone"], (*data)["s_m_phone"]).(string)
|
|
|
+ if phone != "" {
|
|
|
+ if count := mysql.CountBySql(`SELECT count(1) FROM entniche_user u LEFT JOIN entniche_info i ON u.ent_id=i.id WHERE u.phone=? and u.power=1 and i.status=1`, phone); count > 0 {
|
|
|
+ userPower.EntnicheStatus = 1
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return &userPower
|
|
|
+}
|
|
|
+
|
|
|
+// 获取大会员个人基本信息
|
|
|
+func GetBigVipUserBaseMsg(userId string, mysql *mysql.Mysql, mg MongodbSim) *BigVipBaseMsg {
|
|
|
+ userPower := BigVipBaseMsg{}
|
|
|
+ userPower.PowerMap = make(map[int]bool)
|
|
|
+ if userId == "" {
|
|
|
+ return &userPower
|
|
|
+ }
|
|
|
+ userPower.IsUpgrade = false
|
|
|
+ userPower.Uid = userId
|
|
|
+ cacheKey := fmt.Sprintf(PowerCacheKey, userId)
|
|
|
+
|
|
|
+ if bytes, err := redis.GetBytes(BaseInfoCacheDb, cacheKey); err == nil && bytes != nil {
|
|
|
+ if err := json.Unmarshal(*bytes, &userPower); err == nil {
|
|
|
+ return &userPower
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //大会员状态
|
|
|
+ data, ok := mg.FindById("user", userId, `{"s_phone":1,"s_m_phone":1,i_member_status":1,"i_member_give":1,"s_member_mainid":1,"i_member_sub_status":1,"i_member_trial":1,"i_vip_status":1,"o_vipjy":1,"o_jy":1,"l_registedate":1,"s_myemail":1,"base_user_id":1}`)
|
|
|
+ if ok && *data != nil && len(*data) > 0 {
|
|
|
+ userPower.Email = qutil.ObjToString((*data)["s_myemail"])
|
|
|
+ userPower.BaseUserId = qutil.IntAll((*data)["base_user_id"])
|
|
|
+ userPower.Registedate = qutil.Int64All((*data)["l_registedate"])
|
|
|
+ userPower.Status = qutil.IntAllDef((*data)["i_member_status"], 0)
|
|
|
+ //子账号被启用
|
|
|
+ i_member_sub_status := qutil.IntAllDef((*data)["i_member_sub_status"], 0)
|
|
|
+ if (*data)["s_member_mainid"] != nil && qutil.ObjToString((*data)["s_member_mainid"]) != "" && i_member_sub_status > 0 {
|
|
|
+ userPower.Pid = qutil.ObjToString((*data)["s_member_mainid"])
|
|
|
+ }
|
|
|
+ if (userPower.Pid != "" && qutil.IntAllDef((*data)["i_member_sub_status"], 0) == 1) || mg.Count("member", map[string]interface{}{"userid": userId}) > 0 {
|
|
|
+ userPower.Used = true
|
|
|
+ }
|
|
|
+ if (*data)["i_member_trial"] != nil {
|
|
|
+ userPower.HasTrial = true
|
|
|
+ }
|
|
|
+ if vipStatus := qutil.IntAll((*data)["i_vip_status"]); vipStatus > 0 {
|
|
|
+ userPower.VipStatus = vipStatus
|
|
|
+ if o_vipjy := qutil.ObjToMap((*data)["o_vipjy"]); o_vipjy != nil {
|
|
|
+ if o_buyset := qutil.ObjToMap((*o_vipjy)["o_buyset"]); o_buyset != nil {
|
|
|
+ userPower.Vip_BuySet = BuySet{
|
|
|
+ Upgrade: qutil.IntAll((*o_buyset)["upgrade"]),
|
|
|
+ AreaCount: qutil.IntAll((*o_buyset)["areacount"]),
|
|
|
+ BuyerclassCount: qutil.IntAll((*o_buyset)["buyerclasscount"]),
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ userPower.VipPowerType = 1
|
|
|
+ }
|
|
|
+ if userPower.Status > 0 {
|
|
|
+ userPower.MemberPowerType = 1
|
|
|
+ }
|
|
|
+ //免费用户画像和附件下载权限
|
|
|
+ //if userPower.Status <= 0 && userPower.VipStatus <= 0 {
|
|
|
+ o_jy := qutil.ObjToMap((*data)["o_jy"])
|
|
|
+ //"i_newfree": 1, //新免费用户=>新订阅设置页面 20211122
|
|
|
+ //IsNewFreeTimeCell dev3.6.4版本之前发了个紧急版本处理老用户订阅问题,i_newfree字段必须用户选择地区才能生成,不能作为判断是否是新用户得唯一标识,在此版本添加了常量:IsNewFreeTimeCell作为判断标准;--ws
|
|
|
+ if qutil.IntAll((*o_jy)["i_newfree"]) > 0 || IsNewFreeTimeCell < userPower.Registedate {
|
|
|
+ userPower.IsUpgrade = true
|
|
|
+ }
|
|
|
+ //}
|
|
|
+ //查询是否是商机管理付费用户
|
|
|
+ //userPower.EntnicheStatus = 0
|
|
|
+ phone, _ := qutil.If((*data)["s_phone"] != nil, (*data)["s_phone"], (*data)["s_m_phone"]).(string)
|
|
|
+ if phone != "" {
|
|
|
+ res := mysql.SelectBySql(`SELECT i. STATUS AS status, i.isNew, i.power_source, r.role_id, u.power, i.name,i.id FROM (entniche_user u LEFT JOIN entniche_user_role r ON r.user_id = u.id ) LEFT JOIN entniche_info i ON u.ent_id = i.id WHERE u.phone = ? ORDER BY i. STATUS DESC, i.auth_status DESC`, phone)
|
|
|
+ if res != nil && len(*res) > 0 {
|
|
|
+ userPower.EntInfo = map[int]*EntInfoStruct{}
|
|
|
+ //商机管理
|
|
|
+ userPower.EntName = qutil.ObjToString((*res)[0]["name"])
|
|
|
+ //已购买企业未过期-商机管理用户
|
|
|
+ for _, v := range *res {
|
|
|
+ if qutil.IntAll(v["id"]) == 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ entId := qutil.IntAll(v["id"])
|
|
|
+ userPower.EntInfo[entId] = &EntInfoStruct{
|
|
|
+ IsNew: qutil.IntAll(v["isNew"]) > 0,
|
|
|
+ IsPower: qutil.IntAll(v["power"]) > 0,
|
|
|
+ Status: qutil.IntAll(v["status"]),
|
|
|
+ Name: qutil.ObjToString(v["name"]),
|
|
|
+ IsService: qutil.IntAll(v["power_source"]) > 0,
|
|
|
+ RoleId: qutil.IntAll(v["role_id"]),
|
|
|
+ }
|
|
|
+ // 判断是否是新商机管理
|
|
|
+ if qutil.IntAll(v["status"]) == 1 && qutil.IntAll(v["power"]) == 1 {
|
|
|
+ userPower.EntnicheStatus = 1
|
|
|
+ if qutil.IntAll(v["isNew"]) == 1 {
|
|
|
+ userPower.EntIsNew = true
|
|
|
+ // if userPower.EntIsNew && userPower.IsEntService {
|
|
|
+ // break
|
|
|
+ // }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 判断是否是商机管理服务 (在超级订阅或者大会员、医械通(暂无)的基础上才会有的)
|
|
|
+ // if qutil.IntAll(v["power_source"]) == 1 {
|
|
|
+ // userPower.IsEntService = true
|
|
|
+ // break
|
|
|
+ // }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 如果是商机管理服务用户,则不能是旧版商机管理及新版商机管理
|
|
|
+ // if userPower.IsEntService {
|
|
|
+ // userPower.EntnicheStatus = 0 // 商机管理状态
|
|
|
+ // userPower.EntIsNew = false // 新版商机管理
|
|
|
+ // }
|
|
|
+ //if count := mysql.CountBySql(`SELECT count(1) FROM entniche_user u LEFT JOIN entniche_info i ON u.ent_id=i.id WHERE u.phone=? and u.power=1 and i.status=1`, phone); count > 0 {
|
|
|
+ // userPower.EntnicheStatus = 1
|
|
|
+ //}
|
|
|
+ //广东移动DICT 用户
|
|
|
+ userPower.PrivateGD = mysql.CountBySql(`select count(1) from privatedata where phone = ?`, phone) > 0
|
|
|
+
|
|
|
+ //用于判断是否含有企业主体的超级订阅、大会员服务
|
|
|
+ epowerData := mysql.SelectBySql(`SELECT c.product_type FROM entniche_user a INNER JOIN entniche_power b
|
|
|
+ INNER JOIN entniche_wait_empower c
|
|
|
+ ON a.id =b.ent_user_id AND c.id = b.wait_empower_id WHERE a.phone =? AND c.end_time >?`, phone, time.Now().Format(qutil.Date_Full_Layout))
|
|
|
+ if epowerData != nil && len(*epowerData) > 0 {
|
|
|
+ for _, v := range *epowerData {
|
|
|
+ product_type := qutil.ObjToString(v["product_type"])
|
|
|
+ if strings.Contains(product_type, "VIP订阅") {
|
|
|
+ userPower.VipPowerType = 2
|
|
|
+ } else if strings.Contains(product_type, "大会员") {
|
|
|
+ userPower.MemberPowerType = 2
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //子账号查询父节点权限
|
|
|
+ queryId := qutil.If(userPower.Pid == "", userId, userPower.Pid).(string)
|
|
|
+ //用户购买的服务
|
|
|
+ serviceList := mysql.Find(BigmemberUserPowerTable, map[string]interface{}{"s_userid": queryId, "i_status": 0}, "DISTINCT(s_serviceid),i_frequency", "", -1, -1)
|
|
|
+ if serviceList != nil && len(*serviceList) != 0 {
|
|
|
+ pCount, eCount, dailyNum, customers := 0, 0, 0, 10
|
|
|
+ for _, item := range *serviceList {
|
|
|
+ serviceid := qutil.IntAll(item["s_serviceid"])
|
|
|
+ userPower.PowerMap[serviceid] = true
|
|
|
+ if serviceid == 14 { //项目数量
|
|
|
+ pCount = qutil.IntAll(item["i_frequency"])
|
|
|
+ } else if serviceid == 4 || serviceid == 12 || serviceid == 13 { //企业情报监控 企业中标动态
|
|
|
+ tEcount := qutil.IntAll(item["i_frequency"])
|
|
|
+ if tEcount > eCount {
|
|
|
+ eCount = tEcount
|
|
|
+ }
|
|
|
+ } else if serviceid == 17 || serviceid == 18 { //每日数据包
|
|
|
+ dailyNum = qutil.IntAll(item["i_frequency"])
|
|
|
+ } else if serviceid == 7 { //潜在客户 关注客户
|
|
|
+ customers = qutil.IntAll(item["i_frequency"])
|
|
|
+ }
|
|
|
+ }
|
|
|
+ userPower.EntNum = eCount
|
|
|
+ userPower.ProNum = pCount
|
|
|
+ userPower.DailyNum = dailyNum
|
|
|
+ userPower.Customers = customers
|
|
|
+ }
|
|
|
+
|
|
|
+ //存储缓存
|
|
|
+ go func() {
|
|
|
+ if bytes, err := json.Marshal(userPower); err == nil && bytes != nil {
|
|
|
+ oneDayMore := OneDay + rand.Intn(60*60)
|
|
|
+ _ = redis.PutBytes(BaseInfoCacheDb, cacheKey, &bytes, oneDayMore)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ return &userPower
|
|
|
+}
|
|
|
+
|
|
|
+// 权限判断
|
|
|
+func (this *BigVipBaseMsg) CheckBigVipFrontPower(reqFlag string) (pass bool) {
|
|
|
+ if reqFlag == "ent_portrait" || reqFlag == "unit_portrayal" { //画像页面无权限控制
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ if this.Vip_BuySet.Upgrade == 1 {
|
|
|
+ if reqFlag == "ent_portrait" || reqFlag == "svip" {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return this.checkPower(reqFlag, FrontService)
|
|
|
+}
|
|
|
+
|
|
|
+func (this *BigVipBaseMsg) CheckBigVipBackPower(reqFlag string) (pass bool) {
|
|
|
+ return this.checkPower(reqFlag, BackService)
|
|
|
+}
|
|
|
+
|
|
|
+func (this *BigVipBaseMsg) checkPower(reqFlag string, servicesPower map[string][]int) bool {
|
|
|
+ powers, ok := servicesPower[reqFlag]
|
|
|
+ if !ok {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ for _, p := range powers {
|
|
|
+ if this.PowerMap[p] {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return false
|
|
|
+}
|
|
|
+
|
|
|
+func (this *BigVipBaseMsg) GetUseId() string {
|
|
|
+ if this.Pid != "" {
|
|
|
+ return this.Pid
|
|
|
+ }
|
|
|
+ return this.Uid
|
|
|
+}
|