user.go 8.9 KB

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