123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- package entity
- import (
- "encoding/json"
- "fmt"
- "strconv"
- "strings"
- "sync"
- "time"
- MC "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/redis"
- )
- const (
- IsNewFreeTimeCell = 1637830020
- BigMemberUserPowerTable = "bigmember_service_user"
- RedisCode = "newother"
- RedisMenuKey = "jy_workdesktopmenu_%s_%s_%s"
- UserPowerRedisKey = "jy_userpowerredis_%s_%d_%s"
- UserEntIdKey = "jy_userentid_%s_%d_%s"
- )
- type UserInfo struct {
- Capitals map[string]int
- Permissions map[string]int
- Lock *sync.Mutex
- }
- var (
- CapitalRetention = "capital_retention"
- //UserCapitals = map[string]map[string]int{}
- //UserPermissions = map[string]map[string]int{}
- //UserLock = map[string]*sync.Mutex{}
- OverallLock = &sync.Mutex{}
- UserInfoMap = map[string]*UserInfo{}
- UserRolePowers = map[string][]string{}
- )
- /*
- *待调整 调整为存redis
- *测试用例放的地方不对 待调整
- */
- //用户权限 初始化
- func AutoUserPowerInfo(userId, appId string, entId int64) map[string]int {
- /*
- * 商机管理--》新版商机管理vs老版商机管理
- * 大会员--》bigmember_service
- * 超级订阅--》新版超级订阅vs老版超级订阅
- * 免费用户--》新免费用户
- */
- var UserPowerMap = map[string]int{}
- //redis newother 查询是否存在用户功能信息
- userPowerRedisKey := fmt.Sprintf(UserPowerRedisKey, appId, time.Now().Day(), userId)
- bytes, err := redis.GetBytes(RedisCode, userPowerRedisKey)
- if err == nil && len(*bytes) > 0 {
- if err = json.Unmarshal(*bytes, &UserPowerMap); err == nil {
- return UserPowerMap
- }
- }
- //查询用户信息
- data, ok := Mgo.FindById("user", userId, `{"s_phone":1,"s_m_phone":1,i_member_status":1,“i_member_endtime”:1,"s_member_mainid":1,"i_member_sub_status":1,"i_vip_status":1,"l_vip_endtime":1,"o_vipjy":1,"o_jy":1,"l_registedate":1}`)
- if ok && *data != nil && len(*data) > 0 {
- var (
- isFree = true
- domainBool = false //第一版领域化权限判断 ;第二版:超级订阅||大会员 到期时间超过90天
- )
- phone, _ := MC.If((*data)["s_phone"] != nil, (*data)["s_phone"], (*data)["s_m_phone"]).(string)
- if phone != "" {
- //同一个手机号 多个商机管理角色 其中一个是企业管理员或者部门管理员 查的时候按角色权重排序
- entNicheInfos := Mysql.SelectBySql(`SELECT i.isNew,r.role_id,u.ent_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 = ? and u.power=1 and i.status=1 ORDER BY r.role_id,i.isNew DESC`, phone)
- //商机管理用户信息判断
- if entNicheInfos != nil && len(*entNicheInfos) > 0 {
- isFree = false
- for _, entNicheInfo := range *entNicheInfos {
- //判断是否是当前企业
- ent_id := MC.Int64All(entNicheInfo["ent_id"])
- if entId > 0 && ent_id != entId {
- continue
- }
- //entNicheInfo := (*entNicheInfos)[0]
- switch MC.IntAll(entNicheInfo["isNew"]) {
- case 1: //新版商机管理
- UserPowerMap["110"] = 1
- switch MC.IntAll(entNicheInfo["role_id"]) {
- case 1: //部门管理员
- UserPowerMap["111"] = 1
- case 2: //企业管理员
- UserPowerMap["112"] = 1
- }
- case 0: //老版商机管理
- UserPowerMap["100"] = 1
- switch MC.IntAll(entNicheInfo["role_id"]) {
- case 1: //部门管理员
- UserPowerMap["101"] = 1
- case 2: //企业管理员
- UserPowerMap["102"] = 1
- }
- }
- }
- }
- //大会员
- if memberStatus := MC.IntAll((*data)["i_member_status"]); memberStatus > 0 || ConfigJson.BigMemberOff {
- userId := userId
- if memberStatus > 0 {
- //到期时间超过90天
- domainBool = MC.Int64All((*data)["i_member_endtime"])-time.Now().Unix() >= ConfigJson.MedicalFieldTimespan
- isFree = false
- UserPowerMap["0"] = memberStatus
- //是否是子账号 而且 子账号被启用
- if (*data)["s_member_mainid"] != nil && MC.ObjToString((*data)["s_member_mainid"]) != "" && MC.IntAllDef((*data)["i_member_sub_status"], 0) > 0 {
- userId = MC.ObjToString((*data)["s_member_mainid"])
- }
- }
- //大会员用户购买的服务
- serviceList := Mysql.Find(BigMemberUserPowerTable, map[string]interface{}{"s_userid": userId, "i_status": 0}, "DISTINCT(s_serviceid),i_frequency", "", -1, -1)
- if serviceList != nil && len(*serviceList) != 0 {
- for _, sv := range *serviceList {
- UserPowerMap[MC.ObjToString(sv["s_serviceid"])] = MC.If(MC.IntAll(sv["i_frequency"]) > 0, MC.IntAll(sv["i_frequency"]), memberStatus).(int)
- }
- }
- }
- //VIP用户
- if vipStatus := MC.IntAll((*data)["i_vip_status"]); vipStatus > 0 {
- //到期时间超过90天
- domainBool = MC.Int64All((*data)["l_vip_endtime"])-time.Now().Unix() >= ConfigJson.MedicalFieldTimespan
- isFree = false
- UserPowerMap["200"] = vipStatus
- if vipSet := MC.ObjToMap((*data)["o_vipjy"]); vipSet != nil {
- if buySet := MC.ObjToMap((*vipSet)["o_buyset"]); buySet != nil {
- //vip 升级用户
- if MC.IntAll((*buySet)["upgrade"]) > 0 {
- UserPowerMap["201"] = vipStatus
- }
- }
- }
- }
- //免费用户
- if isFree {
- UserPowerMap["300"] = 1
- freeSet := MC.ObjToMap((*data)["o_jy"])
- if MC.IntAll((*freeSet)["i_newfree"]) > 0 || IsNewFreeTimeCell < MC.IntAll((*data)["l_registedate"]) {
- //新免费用户
- UserPowerMap["301"] = 1
- }
- }
- //广东移动DICT
- if Mysql.CountBySql(`select count(*) from privatedata where phone = ?`, phone) > 0 {
- UserPowerMap["400"] = 1
- }
- //领域化产品权限
- //第一版:必须是大会员或者超级订阅用户 且留资 留资表:capital_retention;source = 'medical_domain',未留资提示留资信息
- //第二版:调资源中台rpc获取用户是否有使用领域化产品的权限 无权限则去购买
- //需求调整:
- //1:是否是大会员或者超级订阅用户 否:提示购买到超级订阅购买页; 是>-2
- //2:判断用户是否留资 否:提示用去留资;是:>-3
- //3:资源中台获取用户权限码判断是否有权限 否:提示用户去联系客服
- if domainBool {
- //c := BaseMysql.CountBySql(`SELECT COUNT(id) FROM `+CapitalRetention+` WHERE source = 'medical_domain' AND user_id = ? AND appid = ?`, baseUserId, appId)
- //if c > 0 {
- UserPowerMap["500"] = 1
- //}
- }
- }
- if UserPowerMap != nil {
- bytes, err := json.Marshal(UserPowerMap)
- if err == nil && len(bytes) > 0 {
- redis.PutBytes(RedisCode, userPowerRedisKey, &bytes, ConfigJson.InternalTime)
- if entId > 0 {
- //商机管理用户entId 缓存
- userEntIdKey := fmt.Sprintf(UserEntIdKey, appId, time.Now().Day(), userId)
- redis.Put(RedisCode, userEntIdKey, entId, ConfigJson.InternalTime)
- }
- }
- }
- }
- return UserPowerMap
- }
- //clear One
- func ClearUserPowerFunc(userId, appId string) {
- userPowerRedisKey := fmt.Sprintf(UserPowerRedisKey, appId, time.Now().Day(), userId)
- redis.Del(RedisCode, userPowerRedisKey)
- for _, v := range []string{"PC", "APP", "WX"} {
- RedisMenuKey := fmt.Sprintf(RedisMenuKey, appId, v, userId)
- redis.Del(RedisCode, RedisMenuKey)
- }
- }
- //用户角色权限初始化
- func UserRolePowerInit(strs []string) {
- if len(strs) > 0 {
- jyUserRoleData := BaseMysql.SelectBySql(`SELECT id,name FROM jyfunction WHERE status = 1`)
- if jyUserRoleData != nil && len(*jyUserRoleData) > 0 {
- for _, jv := range *jyUserRoleData {
- for _, v := range strs {
- if strings.Contains(MC.ObjToString(jv["name"]), v) {
- UserRolePowers[v] = append(UserRolePowers[v], strconv.Itoa(MC.IntAll(jv["id"])))
- }
- }
- }
- }
- }
- }
- /*
- 1、不符合可以留资申请开通权限的用户
- 1、免费用户
- 2、超级订阅用户、大会员用户到期时间是否超过3个月
- (1、2:弹窗-医械通上线啦)
- 2、符合可以留资申请开通的用户
- 1、未留资
- (1:弹窗-完善基本信息)
- 2、已留资未开通
- 3、已留资且开通
- (2、3:弹窗-恭喜留资成功)
- */
- /*
- 1、判断是否满足超级订阅用户或大会员用户 且到期时间超过90天(配置);
- 1.1、否:权限=0;提示上线啦
- 1.2、是:是否留资
- 1.2.1、否:权限=0;提示留资
- 1.2.2、是:是否开通权限
- 1.2.2.1、否:权限=0;提示恭喜留资成功,客服联系
- 1.2.2.2、是:权限=1
- */
- // CheckCapitalResources 是否需要留资 且权限验证
- func CheckCapitalResources(menu *JYMenu, baseUserId, appId string, entId, entUserId int64, b bool) (title, content, confirmUrl, confirmText, appType, openType string, isShowCancel, usable bool) {
- OverallLock.Lock()
- userInfo := UserInfoMap[baseUserId]
- if userInfo == nil {
- userInfo = &UserInfo{}
- userInfo.Lock = &sync.Mutex{}
- userInfo.Capitals = map[string]int{}
- userInfo.Permissions = map[string]int{}
- UserInfoMap[baseUserId] = userInfo
- }
- OverallLock.Unlock()
- userInfo.Lock.Lock()
- defer userInfo.Lock.Unlock()
- var (
- capitalBool = false
- permissionBool = false
- )
- //用户是否需要留资
- if menu.CapitalCode != "" {
- capitalBool = false
- for _, cv := range strings.Split(menu.CapitalCode, ",") {
- if userInfo.Capitals[cv] == 0 {
- if c := BaseMysql.CountBySql(`SELECT COUNT(id) FROM `+CapitalRetention+` WHERE source = ? AND user_id = ? AND appid = ?`, cv, baseUserId, appId); c > 0 {
- userInfo.Capitals[cv] = 1
- } else {
- userInfo.Capitals[cv] = -1
- }
- }
- if userInfo.Capitals[cv] < 0 {
- //留资弹窗信息
- title = menu.CapitalInfo.Title
- content = menu.CapitalInfo.Content
- confirmUrl = menu.CapitalInfo.ConfirmUrl
- confirmText = menu.CapitalInfo.ConfirmText
- isShowCancel = menu.CapitalInfo.IsShowCancel
- appType = menu.CapitalInfo.AppType
- openType = menu.CapitalInfo.OpenType
- } else {
- //自定义弹窗
- title = menu.AdditionalInfo.Title
- content = menu.AdditionalInfo.Content
- confirmUrl = menu.AdditionalInfo.ConfirmUrl
- confirmText = menu.AdditionalInfo.ConfirmText
- isShowCancel = menu.AdditionalInfo.IsShowCancel
- appType = menu.AdditionalInfo.AppType
- openType = menu.AdditionalInfo.OpenType
- capitalBool = true
- break
- }
- }
- } else {
- capitalBool = true
- }
- //大会员超级订阅用户 才有权限去验证是否有医疗权限
- if b {
- //资源中台--- 无权限弹窗信息为默认信息(先留资)
- if menu.PermissionCode != "" {
- permissionBool = false
- if (menu.CapitalCode != "" && capitalBool) || menu.CapitalCode == "" {
- for _, pv := range strings.Split(menu.PermissionCode, ",") {
- if userInfo.Permissions[pv] == 0 {
- userInfo.Permissions[pv] = -1
- powerList := GetResources(appId, baseUserId, entId, entUserId)
- if len(powerList) > 0 {
- for _, plv := range powerList {
- userInfo.Permissions[plv] = 1
- }
- }
- }
- if userInfo.Permissions[pv] < 0 {
- //自定义弹窗
- title = menu.AdditionalInfo.Title
- content = menu.AdditionalInfo.Content
- confirmUrl = menu.AdditionalInfo.ConfirmUrl
- confirmText = menu.AdditionalInfo.ConfirmText
- isShowCancel = menu.AdditionalInfo.IsShowCancel
- appType = menu.AdditionalInfo.AppType
- openType = menu.AdditionalInfo.OpenType
- } else {
- title = ""
- permissionBool = true
- break
- }
- }
- }
- } else {
- permissionBool = true
- }
- }
- usable = capitalBool && permissionBool
- return
- }
|