user.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package entity
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "sync"
  8. "time"
  9. MC "app.yhyue.com/moapp/jybase/common"
  10. "app.yhyue.com/moapp/jybase/redis"
  11. )
  12. const (
  13. IsNewFreeTimeCell = 1637830020
  14. BigMemberUserPowerTable = "bigmember_service_user"
  15. RedisCode = "newother"
  16. RedisMenuKey = "jy_workdesktopmenu_%s_%s_%s"
  17. UserPowerRedisKey = "jy_userpowerredis_%s_%d_%s"
  18. UserEntIdKey = "jy_userentid_%s_%d_%s"
  19. )
  20. type UserInfo struct {
  21. Capitals map[string]int
  22. Permissions map[string]int
  23. Lock *sync.Mutex
  24. }
  25. var (
  26. CapitalRetention = "capital_retention"
  27. //UserCapitals = map[string]map[string]int{}
  28. //UserPermissions = map[string]map[string]int{}
  29. //UserLock = map[string]*sync.Mutex{}
  30. OverallLock = &sync.Mutex{}
  31. UserInfoMap = map[string]*UserInfo{}
  32. )
  33. /*
  34. *待调整 调整为存redis
  35. *测试用例放的地方不对 待调整
  36. */
  37. //用户权限 初始化
  38. func AutoUserPowerInfo(userId, appId string, internalTime int, bigMemberOff bool, entId int64) map[string]int {
  39. /*
  40. * 商机管理--》新版商机管理vs老版商机管理
  41. * 大会员--》bigmember_service
  42. * 超级订阅--》新版超级订阅vs老版超级订阅
  43. * 免费用户--》新免费用户
  44. */
  45. var UserPowerMap = map[string]int{}
  46. //redis newother 查询是否存在用户功能信息
  47. userPowerRedisKey := fmt.Sprintf(UserPowerRedisKey, appId, time.Now().Day(), userId)
  48. bytes, err := redis.GetBytes(RedisCode, userPowerRedisKey)
  49. if err == nil && len(*bytes) > 0 {
  50. if err = json.Unmarshal(*bytes, &UserPowerMap); err == nil {
  51. return UserPowerMap
  52. }
  53. }
  54. //查询用户信息
  55. 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}`)
  56. if ok && *data != nil && len(*data) > 0 {
  57. var (
  58. isFree = true
  59. domainBool = false //第一版领域化权限判断
  60. )
  61. phone, _ := MC.If((*data)["s_phone"] != nil, (*data)["s_phone"], (*data)["s_m_phone"]).(string)
  62. if phone != "" {
  63. //同一个手机号 多个商机管理角色 其中一个是企业管理员或者部门管理员 查的时候按角色权重排序
  64. 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)
  65. //商机管理用户信息判断
  66. if entNicheInfos != nil && len(*entNicheInfos) > 0 {
  67. isFree = false
  68. for _, entNicheInfo := range *entNicheInfos {
  69. //判断是否是当前企业
  70. ent_id := MC.Int64All(entNicheInfo["ent_id"])
  71. if entId > 0 && ent_id != entId {
  72. continue
  73. }
  74. //entNicheInfo := (*entNicheInfos)[0]
  75. UserPowerMap["100"] = 1
  76. switch MC.IntAll(entNicheInfo["isNew"]) {
  77. case 1: //新版商机管理
  78. UserPowerMap["110"] = 1
  79. switch MC.IntAll(entNicheInfo["role_id"]) {
  80. case 1: //部门管理员
  81. UserPowerMap["111"] = 1
  82. case 2: //企业管理员
  83. UserPowerMap["112"] = 1
  84. }
  85. case 0: //老版商机管理
  86. switch MC.IntAll(entNicheInfo["role_id"]) {
  87. case 1: //部门管理员
  88. UserPowerMap["101"] = 1
  89. case 2: //企业管理员
  90. UserPowerMap["102"] = 1
  91. }
  92. }
  93. }
  94. }
  95. //大会员
  96. if memberStatus := MC.IntAll((*data)["i_member_status"]); memberStatus > 0 || bigMemberOff {
  97. userId := userId
  98. if memberStatus > 0 {
  99. domainBool = true
  100. isFree = false
  101. UserPowerMap["0"] = memberStatus
  102. //是否是子账号 而且 子账号被启用
  103. if (*data)["s_member_mainid"] != nil && MC.ObjToString((*data)["s_member_mainid"]) != "" && MC.IntAllDef((*data)["i_member_sub_status"], 0) > 0 {
  104. userId = MC.ObjToString((*data)["s_member_mainid"])
  105. }
  106. }
  107. //大会员用户购买的服务
  108. serviceList := Mysql.Find(BigMemberUserPowerTable, map[string]interface{}{"s_userid": userId, "i_status": 0}, "DISTINCT(s_serviceid),i_frequency", "", -1, -1)
  109. if serviceList != nil && len(*serviceList) != 0 {
  110. for _, sv := range *serviceList {
  111. UserPowerMap[MC.ObjToString(sv["s_serviceid"])] = MC.If(MC.IntAll(sv["i_frequency"]) > 0, MC.IntAll(sv["i_frequency"]), memberStatus).(int)
  112. }
  113. }
  114. }
  115. //VIP用户
  116. if vipStatus := MC.IntAll((*data)["i_vip_status"]); vipStatus > 0 {
  117. domainBool = true
  118. isFree = false
  119. UserPowerMap["200"] = vipStatus
  120. if vipSet := MC.ObjToMap((*data)["o_vipjy"]); vipSet != nil {
  121. if buySet := MC.ObjToMap((*vipSet)["o_buyset"]); buySet != nil {
  122. //vip 升级用户
  123. if MC.IntAll((*buySet)["upgrade"]) > 0 {
  124. UserPowerMap["201"] = vipStatus
  125. }
  126. }
  127. }
  128. }
  129. //免费用户
  130. if isFree {
  131. UserPowerMap["300"] = 1
  132. freeSet := MC.ObjToMap((*data)["o_jy"])
  133. if MC.IntAll((*freeSet)["i_newfree"]) > 0 || IsNewFreeTimeCell < MC.IntAll((*data)["l_registedate"]) {
  134. //新免费用户
  135. UserPowerMap["301"] = 1
  136. }
  137. }
  138. //广东移动DICT
  139. if Mysql.CountBySql(`select count(*) from privatedata where phone = ?`, phone) > 0 {
  140. UserPowerMap["400"] = 1
  141. }
  142. //领域化产品权限
  143. //第一版:必须是大会员或者超级订阅用户 且留资 留资表:capital_retention;source = 'medical_domain',未留资提示留资信息
  144. //第二版:调资源中台rpc获取用户是否有使用领域化产品的权限 无权限则去购买
  145. //需求调整:
  146. //1:是否是大会员或者超级订阅用户 否:提示购买到超级订阅购买页; 是>-2
  147. //2:判断用户是否留资 否:提示用去留资;是:>-3
  148. //3:资源中台获取用户权限码判断是否有权限 否:提示用户去联系客服
  149. if domainBool {
  150. //c := BaseMysql.CountBySql(`SELECT COUNT(id) FROM `+CapitalRetention+` WHERE source = 'medical_domain' AND user_id = ? AND appid = ?`, baseUserId, appId)
  151. //if c > 0 {
  152. UserPowerMap["500"] = 1
  153. //}
  154. }
  155. }
  156. if UserPowerMap != nil {
  157. bytes, err := json.Marshal(UserPowerMap)
  158. if err == nil && len(bytes) > 0 {
  159. redis.PutBytes(RedisCode, userPowerRedisKey, &bytes, internalTime)
  160. if entId > 0 {
  161. //商机管理用户entId 缓存
  162. userEntIdKey := fmt.Sprintf(UserEntIdKey, appId, time.Now().Day(), userId)
  163. redis.Put(RedisCode, userEntIdKey, entId, internalTime)
  164. }
  165. }
  166. }
  167. }
  168. return UserPowerMap
  169. }
  170. //clear One
  171. func ClearUserPowerFunc(userId, appId string) {
  172. userPowerRedisKey := fmt.Sprintf(UserPowerRedisKey, appId, time.Now().Day(), userId)
  173. redis.Del(RedisCode, userPowerRedisKey)
  174. for _, v := range []string{"PC", "APP", "WX"} {
  175. RedisMenuKey := fmt.Sprintf(RedisMenuKey, appId, v, userId)
  176. redis.Del(RedisCode, RedisMenuKey)
  177. }
  178. }
  179. var UserRolePowers = map[string][]string{}
  180. //用户角色权限初始化
  181. func UserRolePowerInit(strs []string) {
  182. if len(strs) > 0 {
  183. jyUserRoleData := BaseMysql.SelectBySql(`SELECT id,name FROM jyfunction WHERE status = 1`)
  184. if jyUserRoleData != nil && len(*jyUserRoleData) > 0 {
  185. for _, jv := range *jyUserRoleData {
  186. for _, v := range strs {
  187. if strings.Contains(MC.ObjToString(jv["name"]), v) {
  188. UserRolePowers[v] = append(UserRolePowers[v], strconv.Itoa(MC.IntAll(jv["id"])))
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }
  195. //
  196. func CheckCapitalResources(menu *JYMenu, baseUserId, appId string, entId, entUserId int64, b bool) (title, content, confirmUrl, confirmText, appType, openType string, isShowCancel, usable bool) {
  197. OverallLock.Lock()
  198. userInfo := UserInfoMap[baseUserId]
  199. if userInfo == nil {
  200. userInfo = &UserInfo{}
  201. userInfo.Lock = &sync.Mutex{}
  202. userInfo.Capitals = map[string]int{}
  203. userInfo.Permissions = map[string]int{}
  204. UserInfoMap[baseUserId] = userInfo
  205. }
  206. OverallLock.Unlock()
  207. userInfo.Lock.Lock()
  208. defer userInfo.Lock.Unlock()
  209. var (
  210. capitalBool = false
  211. permissionBool = false
  212. )
  213. //用户是否需要留资
  214. if menu.CapitalCode != "" {
  215. capitalBool = false
  216. for _, cv := range strings.Split(menu.CapitalCode, ",") {
  217. if userInfo.Capitals[cv] == 0 {
  218. if c := BaseMysql.CountBySql(`SELECT COUNT(id) FROM `+CapitalRetention+` WHERE source = ? AND user_id = ? AND appid = ?`, cv, baseUserId, appId); c > 0 {
  219. userInfo.Capitals[cv] = 1
  220. } else {
  221. userInfo.Capitals[cv] = -1
  222. }
  223. }
  224. if userInfo.Capitals[cv] < 0 {
  225. //留资弹窗信息
  226. title = menu.CapitalInfo.Title
  227. content = menu.CapitalInfo.Content
  228. confirmUrl = menu.CapitalInfo.ConfirmUrl
  229. confirmText = menu.CapitalInfo.ConfirmText
  230. isShowCancel = menu.CapitalInfo.IsShowCancel
  231. appType = menu.CapitalInfo.AppType
  232. openType = menu.CapitalInfo.OpenType
  233. } else {
  234. //自定义弹窗
  235. title = menu.AdditionalInfo.Title
  236. content = menu.AdditionalInfo.Content
  237. confirmUrl = menu.AdditionalInfo.ConfirmUrl
  238. confirmText = menu.AdditionalInfo.ConfirmText
  239. isShowCancel = menu.AdditionalInfo.IsShowCancel
  240. appType = menu.AdditionalInfo.AppType
  241. openType = menu.AdditionalInfo.OpenType
  242. capitalBool = true
  243. break
  244. }
  245. }
  246. } else {
  247. capitalBool = true
  248. }
  249. //大会员超级订阅用户 才有权限去验证是否有医疗权限
  250. if b {
  251. //资源中台--- 无权限弹窗信息为默认信息(先留资)
  252. if menu.PermissionCode != "" {
  253. permissionBool = false
  254. if (menu.CapitalCode != "" && capitalBool) || menu.CapitalCode == "" {
  255. for _, pv := range strings.Split(menu.PermissionCode, ",") {
  256. if userInfo.Permissions[pv] == 0 {
  257. userInfo.Permissions[pv] = -1
  258. powerList := GetResources(appId, baseUserId, entId, entUserId)
  259. if len(powerList) > 0 {
  260. for _, plv := range powerList {
  261. userInfo.Permissions[plv] = 1
  262. }
  263. }
  264. }
  265. if userInfo.Permissions[pv] < 0 {
  266. //自定义弹窗
  267. title = menu.AdditionalInfo.Title
  268. content = menu.AdditionalInfo.Content
  269. confirmUrl = menu.AdditionalInfo.ConfirmUrl
  270. confirmText = menu.AdditionalInfo.ConfirmText
  271. isShowCancel = menu.AdditionalInfo.IsShowCancel
  272. appType = menu.AdditionalInfo.AppType
  273. openType = menu.AdditionalInfo.OpenType
  274. } else {
  275. title = ""
  276. permissionBool = true
  277. break
  278. }
  279. }
  280. }
  281. } else {
  282. permissionBool = true
  283. }
  284. }
  285. usable = capitalBool && permissionBool
  286. return
  287. }