package entity import ( MC "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/redis" "encoding/json" "fmt" "strconv" "strings" "sync" "time" ) 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" ) var ( CapitalRetention = "capital_retention" UserCapitals = map[string]map[string]int{} UserPermissions = map[string]map[string]int{} UserLock = map[string]*sync.Mutex{} OverallLock = &sync.Mutex{} ) /* *待调整 调整为存redis *测试用例放的地方不对 待调整 */ //用户权限 初始化 func AutoUserPowerInfo(userId, baseUserId, appId string, internalTime int, bigMemberOff bool, 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,"s_member_mainid":1,"i_member_sub_status":1,"i_vip_status":1,"o_vipjy":1,"o_jy":1,"l_registedate":1}`) if ok && *data != nil && len(*data) > 0 { var ( isFree = true domainBool = false //第一版领域化权限判断 ) 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] UserPowerMap["100"] = 1 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: //老版商机管理 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 || bigMemberOff { userId := userId if memberStatus > 0 { domainBool = true 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 { domainBool = true 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, internalTime) if entId > 0 { //商机管理用户entId 缓存 userEntIdKey := fmt.Sprintf(UserEntIdKey, appId, time.Now().Day(), userId) redis.Put(RedisCode, userEntIdKey, entId, 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) } } var UserRolePowers = map[string][]string{} //用户角色权限初始化 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"]))) } } } } } } // func CheckCapitalResources(capitalCode, baseUserId, appId string, entId, entUserId int64, additional Additional, permissionCode string) (title, content, confirmUrl, confirmText string, usable bool) { OverallLock.Lock() if UserCapitals[baseUserId] == nil { UserCapitals[baseUserId] = map[string]int{} } if UserPermissions[baseUserId] == nil { UserPermissions[baseUserId] = map[string]int{} } UserLock[baseUserId] = &sync.Mutex{} OverallLock.Unlock() UserLock[baseUserId].Lock() var ( capitalBool = false permissionBool = false ) //用户是否需要留资 if capitalCode != "" { for _, cv := range strings.Split(capitalCode, ",") { userCapital := UserCapitals[baseUserId] if userCapital[cv] < 0 { title = additional.Title content = additional.Content confirmUrl = additional.ConfirmUrl confirmText = additional.ConfirmText } else { if c := BaseMysql.CountBySql(`SELECT COUNT(id) FROM `+CapitalRetention+` WHERE source = ? AND user_id = ? AND appid = ?`, cv, baseUserId, appId); c > 0 { userCapital[cv] = 1 capitalBool = true } else { userCapital[cv] = -1 } } } } else { capitalBool = true } //资源中台--- 无权限弹窗信息为空 if permissionCode != "" { for _, pv := range strings.Split(permissionCode, ",") { userPermission := UserPermissions[baseUserId] if userPermission[pv] == 0 { userPermission[pv] = -1 powerList := GetResources(appId, baseUserId, entId, entUserId) if len(powerList) > 0 { for _, plv := range powerList { userPermission[plv] = 1 } } } if userPermission[pv] < 0 { title = "" content = "" confirmUrl = "" confirmText = "" } else { permissionBool = true } } } else { permissionBool = true } usable = capitalBool && permissionBool UserLock[baseUserId].Unlock() return }