user.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package entity
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. "github.com/zeromicro/go-zero/core/logx"
  5. "sync"
  6. "time"
  7. )
  8. const (
  9. IsNewFreeTimeCell = 1637830020
  10. BigMemberUserPowerTable = "bigmember_service_user"
  11. RedisCode = "newother"
  12. RedisMenuKey = "jy_workdesktopmenu_%s_%s"
  13. )
  14. var (
  15. PowerLock = sync.Mutex{}
  16. UserPowerLock = map[string]*sync.Mutex{}
  17. UserPowerMapMap = map[string]map[string]int{}
  18. UserPowerPools = make(chan string, 5000)
  19. UserPowerOutTime = 3 * time.Minute
  20. )
  21. //用户权限 初始化
  22. func AutoUserPowerInfo(userId string, internalTime int, bigMemberOff bool) map[string]int {
  23. /*
  24. * 商机管理--》新版商机管理vs老版商机管理
  25. * 大会员--》bigmember_service
  26. * 超级订阅--》新版超级订阅vs老版超级订阅
  27. * 免费用户--》新免费用户
  28. */
  29. if UserPowerMapMap[userId] == nil {
  30. PowerLock.Lock()
  31. UserPowerLock[userId] = &sync.Mutex{}
  32. UserPowerMapMap[userId] = map[string]int{}
  33. PowerLock.Unlock()
  34. UserPowerLock[userId].Lock()
  35. defer UserPowerLock[userId].Unlock()
  36. //查询用户信息
  37. 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}`)
  38. if ok && *data != nil && len(*data) > 0 {
  39. phone, _ := MC.If((*data)["s_phone"] != nil, (*data)["s_phone"], (*data)["s_m_phone"]).(string)
  40. if phone != "" {
  41. //商机管理用户
  42. //同一个手机号 多个商机管理角色 其中一个是企业管理员或者部门管理员 查的时候按角色权重排序
  43. entNicheInfos := Mysql.SelectBySql(`SELECT i.isNew,r.role_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 DESC`, phone)
  44. //商机管理用户信息判断
  45. if entNicheInfos != nil && len(*entNicheInfos) > 0 {
  46. entNicheInfo := (*entNicheInfos)[0]
  47. if MC.ObjToString(entNicheInfo["isNew"]) != "" {
  48. switch MC.ObjToString(entNicheInfo["isNew"]) {
  49. case "1": //新版商机管理
  50. UserPowerMapMap[userId]["110"] = 1
  51. switch MC.ObjToString(entNicheInfo["role_id"]) {
  52. case "1": //部门管理员
  53. UserPowerMapMap[userId]["111"] = 1
  54. case "2": //企业管理员
  55. UserPowerMapMap[userId]["112"] = 1
  56. }
  57. case "0": //老版商机管理
  58. UserPowerMapMap[userId]["100"] = 1
  59. switch MC.ObjToString(entNicheInfo["role_id"]) {
  60. case "1": //部门管理员
  61. UserPowerMapMap[userId]["101"] = 1
  62. case "2": //企业管理员
  63. UserPowerMapMap[userId]["102"] = 1
  64. }
  65. }
  66. }
  67. }
  68. //大会员
  69. if memberStatus := MC.IntAll((*data)["i_member_status"]); memberStatus > 0 || bigMemberOff {
  70. userId := userId
  71. if memberStatus > 0 {
  72. UserPowerMapMap[userId]["1"] = memberStatus
  73. //是否是子账号 而且 子账号被启用
  74. if (*data)["s_member_mainid"] != nil && MC.ObjToString((*data)["s_member_mainid"]) != "" && MC.IntAllDef((*data)["i_member_sub_status"], 0) > 0 {
  75. userId = MC.ObjToString((*data)["s_member_mainid"])
  76. }
  77. }
  78. //大会员用户购买的服务
  79. serviceList := Mysql.Find(BigMemberUserPowerTable, map[string]interface{}{"s_userid": userId, "i_status": 0}, "DISTINCT(s_serviceid),i_frequency", "", -1, -1)
  80. if serviceList != nil && len(*serviceList) != 0 {
  81. for _, sv := range *serviceList {
  82. UserPowerMapMap[userId][MC.ObjToString(sv["s_serviceid"])] = MC.IntAll(sv["i_frequency"])
  83. }
  84. }
  85. }
  86. //VIP用户
  87. if vipStatus := MC.IntAll((*data)["i_vip_status"]); vipStatus > 0 {
  88. UserPowerMapMap[userId]["200"] = vipStatus
  89. if vipSet := MC.ObjToMap((*data)["o_vipjy"]); vipSet != nil {
  90. if buySet := MC.ObjToMap((*vipSet)["o_buyset"]); buySet != nil {
  91. //vip 升级用户
  92. if MC.IntAll((*buySet)["upgrade"]) > 0 {
  93. UserPowerMapMap[userId]["201"] = vipStatus
  94. }
  95. }
  96. }
  97. }
  98. //免费用户
  99. freeSet := MC.ObjToMap((*data)["o_jy"])
  100. if MC.IntAll((*freeSet)["i_newfree"]) > 0 || IsNewFreeTimeCell < MC.IntAll((*data)["l_registedate"]) {
  101. //新免费用户
  102. UserPowerMapMap[userId]["300"] = 1
  103. }
  104. //广东移动DICT
  105. if Mysql.CountBySql(`select count(*) from privatedata where phone = ?`, phone) > 0 {
  106. UserPowerMapMap[userId]["400"] = 1
  107. }
  108. }
  109. }
  110. go func(userId string) {
  111. //默认存2分钟
  112. time.Sleep(time.Duration(internalTime))
  113. UserPowerPools <- userId
  114. }(userId)
  115. }
  116. return UserPowerMapMap[userId]
  117. }
  118. //定时清用户权限缓存
  119. func ClearUserPower() {
  120. for {
  121. select {
  122. case userId := <-UserPowerPools:
  123. if userId != "" {
  124. PowerLock.Lock()
  125. userPower := UserPowerMapMap[userId]
  126. PowerLock.Unlock()
  127. if userPower != nil {
  128. func() {
  129. UserPowerLock[userId].Lock()
  130. defer UserPowerLock[userId].Lock()
  131. PowerLock.Lock()
  132. delete(UserPowerMapMap, userId)
  133. PowerLock.Unlock()
  134. }()
  135. }
  136. }
  137. case <-time.After(UserPowerOutTime):
  138. logx.Info("-超时-", UserPowerOutTime)
  139. //超时时间累加
  140. UserPowerOutTime += UserPowerOutTime
  141. }
  142. }
  143. }