user.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. RedisCode = "newother"
  14. RedisMenuKey = "jy_workdesktopmenu_%s_%s_%s"
  15. UserPowerRedisKey = "jy_userpowerredis_%s_%d_%d_%s"
  16. UserEntIdKey = "jy_userentid_%s_%d_%s"
  17. UserRegisterTime = "jy_userregistertime_%s"
  18. )
  19. type UserInfo struct {
  20. Capitals map[string]int
  21. Permissions map[string]int
  22. Lock *sync.Mutex
  23. }
  24. var (
  25. CapitalRetention = "capital_retention"
  26. OverallLock = &sync.Mutex{}
  27. UserInfoMap = map[int64]*UserInfo{}
  28. UserRolePowers = map[string][]string{}
  29. )
  30. /*
  31. *待调整 调整为存redis
  32. *测试用例放的地方不对 待调整
  33. */
  34. //用户权限 初始化
  35. func (m *WorkDesktopMenu) AutoUserPowerInfo() map[string]int {
  36. /*
  37. * 商机管理--》新版商机管理vs老版商机管理
  38. * 大会员--》bigmember_service
  39. * 超级订阅--》新版超级订阅vs老版超级订阅
  40. * 免费用户--》新免费用户
  41. */
  42. /*
  43. * P278 用户身份切换
  44. * 用户权益 通过 权益中台rpc获取
  45. * 用户资源 通过 资源中台rpc获取
  46. */
  47. var UserPowerMap = map[string]int{}
  48. //redis newother 查询是否存在用户功能信息
  49. userPowerRedisKey := fmt.Sprintf(UserPowerRedisKey, m.AppId, time.Now().Day(), m.EntId, m.UserId)
  50. bytes, err := redis.GetBytes(RedisCode, userPowerRedisKey)
  51. if err == nil && len(*bytes) > 0 {
  52. if err = json.Unmarshal(*bytes, &UserPowerMap); err == nil {
  53. return UserPowerMap
  54. }
  55. }
  56. //数据库查询 、资源中台获取、权益中台获取 查询用户信息
  57. var (
  58. registerTime int64
  59. )
  60. //以上获取不到的信息 再自主查库
  61. userInfoRpc := UserInfoRpc{
  62. AppId: m.AppId,
  63. UserId: m.UserId,
  64. BaseUserId: m.NewUserId,
  65. EntId: m.EntId,
  66. EntUserId: m.EntUserId,
  67. AccountId: m.AccountId,
  68. PositionType: m.PositionType,
  69. PositionId: m.PositionId,
  70. }
  71. //权益中台获取权益
  72. userPowers := userInfoRpc.GetUserPowers()
  73. if userPowers != nil {
  74. //注册时间
  75. registerTime = userPowers.Free.Registedate
  76. //大会员
  77. member := userPowers.Member
  78. if member.Status > 0 || ConfigJson.BigMemberOff {
  79. if member.Status > 0 {
  80. UserPowerMap["0"] = int(member.Status)
  81. UserPowerMap[strconv.Itoa(50+int(member.Status))] = 1
  82. }
  83. //大会员用户购买的服务 也有可能是非大会员用户 购买这些服务
  84. if member.MemberPowerList != nil && len(member.MemberPowerList) > 0 {
  85. for _, mp := range member.MemberPowerList {
  86. UserPowerMap[strconv.FormatInt(mp, 10)] = MC.If(member.Status > 0, int(member.Status), 1).(int)
  87. }
  88. }
  89. }
  90. //超级订阅
  91. vip := userPowers.Vip
  92. if vip.Status > 0 {
  93. if registerTime < vip.EndTime {
  94. registerTime = vip.EndTime
  95. }
  96. UserPowerMap["200"] = int(vip.Status)
  97. UserPowerMap["201"] = int(vip.Upgrade)
  98. }
  99. //企业信息
  100. if m.EntId > 0 {
  101. UserPowerMap["702"] = 1
  102. }
  103. entInfo := userPowers.Ent
  104. //商机管理
  105. entNiche := userPowers.Entniche
  106. if entNiche.Status > 0 {
  107. //entInfo.EntRoleId 1:企业管理员 2:部门管理员
  108. //管理员 entInfo.EntRoleId>0 并不一定 entNiche.IsEntPower==1,entNiche.IsEntPower==0 也有权限
  109. //员工被分配后 entNiche.IsEntPower==1否则没有权限
  110. if entNiche.IsEntPower > 0 || entInfo.EntRoleId > 0 {
  111. //商机管理服务 P259需求
  112. //有商机管理服务 不会再有商机管理订阅菜单
  113. //商机管理服务,上次陈老师说叫“企业管理服务”,不然和商机管理产品容易混淆-- 杨蘭 2022/12/14
  114. switch entNiche.PowerSource {
  115. case 0:
  116. switch entNiche.IsNew {
  117. case 1: //新版商机管理
  118. UserPowerMap["110"] = 1
  119. switch entInfo.EntRoleId {
  120. case 2: //部门管理员
  121. UserPowerMap["111"] = 1
  122. case 1: //企业管理员
  123. UserPowerMap["112"] = 1
  124. }
  125. case 0: //老版商机管理
  126. UserPowerMap["100"] = 1
  127. switch entInfo.EntRoleId {
  128. case 2: //部门管理员
  129. UserPowerMap["101"] = 1
  130. case 1: //企业管理员
  131. UserPowerMap["102"] = 1
  132. }
  133. //老版商机管理: model:1-统一订阅,2-个人订阅
  134. //if entNiche.Model == 1 {
  135. // UserPowerMap["103"] = 1
  136. //} else if entNiche.Model == 2 {
  137. // UserPowerMap["104"] = 1
  138. //}
  139. }
  140. case 1:
  141. //客户管理服务(商机管理服务) [前提:大会员、超级订阅、医械通用户]---免费用户也可以用 (需求调整来自刘苗:产品已确认)
  142. UserPowerMap["600"] = 1
  143. switch entInfo.EntRoleId {
  144. case 2: //部门管理员
  145. UserPowerMap["602"] = 1
  146. case 1: //企业管理员
  147. UserPowerMap["601"] = 1
  148. }
  149. }
  150. }
  151. }
  152. //免费用户
  153. free := userPowers.Free
  154. if free.IsFree {
  155. UserPowerMap["300"] = 1
  156. if free.IsUpgrade {
  157. //新免费用户
  158. UserPowerMap["301"] = MC.If(free.IsUpgrade, 1, 0).(int)
  159. }
  160. }
  161. //----------其他-----------
  162. //广东移动DICT
  163. if entInfo.PrivateGD {
  164. UserPowerMap["400"] = 1
  165. }
  166. //人员行为统计菜单-1、购买大会员且创建了企业的管理员;2、新版商机管理管理员;3、商机管理服务管理员
  167. if entInfo.EntRoleId > 0 && member.Status > 0 {
  168. UserPowerMap["50"] = 1
  169. }
  170. //必须是企业管理员-&-购买了企业级应用服务
  171. //企业级服务 权限管理(只有企业管理员有权限)
  172. entService := entInfo.BuyMember > 0 || entInfo.BuyVip > 0
  173. if entInfo.EntRoleId == 1 && entService {
  174. UserPowerMap["700"] = 1
  175. }
  176. //企业级服务 企业订阅--
  177. //1、存在未到期的购买主体为“企业”切购买产品为大会员或者超级订阅的部门管理员或企业管理员;c > 0
  178. //2、存在未到期的老版或者新版商机管理的企业管理员或部门管理员(非商机管理服务)。entnicheIsNew > -1 && powerSource == 0
  179. if (entService || entInfo.PowerSource == 0) && entInfo.EntRoleId > 0 {
  180. UserPowerMap["701"] = 1
  181. }
  182. //领域化产品权限
  183. //第一版:必须是大会员或者超级订阅用户 且留资 留资表:capital_retention;source = 'medical_domain',未留资提示留资信息
  184. //第二版:调资源中台rpc获取用户是否有使用领域化产品的权限 无权限则去购买
  185. //需求调整:
  186. //1:是否是大会员或者超级订阅用户 否:提示购买到超级订阅购买页; 是>-2
  187. //2:判断用户是否留资 否:提示用去留资;是:>-3
  188. //3:资源中台获取用户权限码判断是否有权限 否:提示用户去联系客服
  189. if member.Status > 0 || vip.Status > 0 {
  190. userRegisterTimeKey := fmt.Sprintf(UserRegisterTime, m.UserId)
  191. redis.Put(RedisCode, userRegisterTimeKey, strconv.Itoa(int(registerTime)), int(registerTime))
  192. UserPowerMap["500"] = 1
  193. }
  194. }
  195. //资源中台获取权限
  196. powerList := userInfoRpc.GetUserResources()
  197. if len(powerList) > 0 {
  198. for _, plv := range powerList {
  199. UserPowerMap[plv] = 1
  200. }
  201. }
  202. //缓存
  203. if UserPowerMap != nil {
  204. bytes, err := json.Marshal(UserPowerMap)
  205. if err == nil && len(bytes) > 0 {
  206. redis.PutBytes(RedisCode, userPowerRedisKey, &bytes, ConfigJson.InternalTime)
  207. if m.EntId > 0 {
  208. //商机管理用户entId 缓存 --- 用户判断 下次是否是当前企业
  209. userEntIdKey := fmt.Sprintf(UserEntIdKey, m.AppId, time.Now().Day(), m.UserId)
  210. redis.Put(RedisCode, userEntIdKey, m.EntId, ConfigJson.InternalTime)
  211. }
  212. }
  213. }
  214. return UserPowerMap
  215. }
  216. // ClearUserPowerFunc clear One
  217. func ClearUserPowerFunc(userId, appId, entId string) {
  218. entIdInt, _ := strconv.Atoi(entId)
  219. userPowerRedisKey := fmt.Sprintf(UserPowerRedisKey, appId, time.Now().Day(), entIdInt, userId)
  220. redis.Del(RedisCode, userPowerRedisKey)
  221. for _, v := range []string{"PC", "APP", "WX"} {
  222. RedisMenuKey := fmt.Sprintf(RedisMenuKey, appId, v, userId)
  223. redis.Del(RedisCode, RedisMenuKey)
  224. }
  225. }
  226. // UserRolePowerInit 用户角色权限初始化
  227. func UserRolePowerInit(strs []string) {
  228. if len(strs) > 0 {
  229. jyUserRoleData := BaseMysql.SelectBySql(`SELECT id,name FROM jyfunction WHERE status = 1`)
  230. if jyUserRoleData != nil && len(*jyUserRoleData) > 0 {
  231. for _, jv := range *jyUserRoleData {
  232. for _, v := range strs {
  233. if strings.Contains(MC.ObjToString(jv["name"]), v) {
  234. UserRolePowers[v] = append(UserRolePowers[v], strconv.Itoa(MC.IntAll(jv["id"])))
  235. }
  236. }
  237. }
  238. }
  239. }
  240. }
  241. /*
  242. 1、不符合可以留资申请开通权限的用户
  243. 1、免费用户
  244. 2、超级订阅用户、大会员用户到期时间是否超过3个月
  245. (1、2:弹窗-医械通上线啦)
  246. 2、符合可以留资申请开通的用户
  247. 1、未留资
  248. (1:弹窗-完善基本信息)
  249. 2、已留资未开通
  250. 3、已留资且开通
  251. (2、3:弹窗-恭喜留资成功)
  252. */
  253. /*
  254. 1、判断是否满足超级订阅用户或大会员用户 且到期时间超过90天(配置);
  255. 1.1、否:权限=0;提示上线啦
  256. 1.2、是:是否留资
  257. 1.2.1、否:权限=0;提示留资
  258. 1.2.2、是:是否开通权限
  259. 1.2.2.1、否:权限=0;提示恭喜留资成功,客服联系
  260. 1.2.2.2、是:权限=1
  261. */
  262. /*存在超级订阅 或 大会员到期 而医械通未到期的情况*/
  263. // CheckCapitalResources 是否需要留资 且权限验证--弹窗处理
  264. //b 一级权限(超级订阅、大会员等)
  265. //p 二级权限(请求资源中台:医械通等)
  266. func CheckCapitalResources(menu *JYMenu, wd *WorkDesktopMenu, b, p bool) (title, content, confirmUrl, confirmText, appType, openType string, isShowCancel, usable bool) {
  267. OverallLock.Lock()
  268. userInfo := UserInfoMap[wd.NewUserId]
  269. if userInfo == nil {
  270. userInfo = &UserInfo{}
  271. userInfo.Lock = &sync.Mutex{}
  272. userInfo.Capitals = map[string]int{}
  273. userInfo.Permissions = map[string]int{}
  274. UserInfoMap[wd.NewUserId] = userInfo
  275. }
  276. OverallLock.Unlock()
  277. userInfo.Lock.Lock()
  278. defer userInfo.Lock.Unlock()
  279. var (
  280. capitalBool = true //是否留资
  281. permissionBool = true //是否有功能权限
  282. //customPopup = true //自定义弹窗
  283. defaultPopup = false //默认弹窗
  284. )
  285. //大会员超级订阅用户 才有权限去验证是否有医疗权限
  286. //用户是否需要留资
  287. if menu.CapitalCode != "" {
  288. capitalBool = false
  289. for _, cv := range strings.Split(menu.CapitalCode, ",") {
  290. if userInfo.Capitals[cv] == 0 {
  291. if c := BaseMysql.CountBySql(`SELECT COUNT(id) FROM `+CapitalRetention+` WHERE source = ? AND user_id = ? AND appid = ?`, cv, wd.NewUserId, wd.AppId); c > 0 {
  292. userInfo.Capitals[cv] = 1
  293. } else {
  294. userInfo.Capitals[cv] = -1
  295. }
  296. }
  297. if userInfo.Capitals[cv] < 0 {
  298. //customPopup = false
  299. //留资弹窗信息
  300. title = menu.CapitalInfo.Title
  301. content = menu.CapitalInfo.Content
  302. confirmUrl = menu.CapitalInfo.ConfirmUrl
  303. confirmText = menu.CapitalInfo.ConfirmText
  304. isShowCancel = menu.CapitalInfo.IsShowCancel
  305. appType = menu.CapitalInfo.AppType
  306. openType = menu.CapitalInfo.OpenType
  307. } else {
  308. capitalBool = true
  309. //无权限(有权限未留资|留资了未开通权限) --》 没有配置弹窗信息 --》自定义弹窗
  310. title = menu.AdditionalInfo.Title
  311. content = menu.AdditionalInfo.Content
  312. confirmUrl = menu.AdditionalInfo.ConfirmUrl
  313. confirmText = menu.AdditionalInfo.ConfirmText
  314. isShowCancel = menu.AdditionalInfo.IsShowCancel
  315. appType = menu.AdditionalInfo.AppType
  316. openType = menu.AdditionalInfo.OpenType
  317. break
  318. }
  319. }
  320. }
  321. //无权限(医械通-:1:免费用户;2:超级订阅、大会员到期时间不在规定范围内)
  322. if b {
  323. //资源中台--- 无权限弹窗信息为默认信息(先留资)
  324. if menu.PermissionCode != "" {
  325. permissionBool = false
  326. if (menu.CapitalCode != "" && capitalBool) || menu.CapitalCode == "" {
  327. if p {
  328. title = ""
  329. permissionBool = true
  330. //customPopup = false
  331. }
  332. }
  333. }
  334. //有剑鱼用户权限 查看资源中台功能权限
  335. //医械通---用户没有资源中台功能权限 也没有留资 查看
  336. if !permissionBool {
  337. switch menu.PowerIds {
  338. case "500":
  339. userRegisterTime, _ := strconv.ParseInt(redis.GetStr(RedisCode, fmt.Sprintf(UserRegisterTime, wd.UserId)), 10, 64)
  340. //超级订阅 大会员到期时间 是否 大于 90天
  341. if userRegisterTime-time.Now().Unix() < ConfigJson.MedicalFieldTimespan {
  342. //即使是超级订阅或大会员 也没有权限
  343. defaultPopup = true
  344. }
  345. }
  346. }
  347. }
  348. //
  349. usable = func() bool {
  350. switch menu.Authority {
  351. case 0:
  352. return b && permissionBool && capitalBool
  353. case 1:
  354. return b || permissionBool || capitalBool
  355. case 2:
  356. return b || permissionBool && capitalBool
  357. case 3:
  358. return b && permissionBool || capitalBool
  359. case 4:
  360. return b && capitalBool || permissionBool
  361. default:
  362. return false
  363. }
  364. }()
  365. //无权限--免费用户||不符合条件的付费用户
  366. if !b || defaultPopup {
  367. //配置弹窗信息
  368. if ConfigJson.DefaultPopup[menu.PowerIds].Title != "" {
  369. //customPopup = false
  370. title = ConfigJson.DefaultPopup[menu.PowerIds].Title
  371. content = ConfigJson.DefaultPopup[menu.PowerIds].Content
  372. confirmUrl = ConfigJson.DefaultPopup[menu.PowerIds].ConfirmUrl
  373. confirmText = ConfigJson.DefaultPopup[menu.PowerIds].ConfirmText
  374. isShowCancel = ConfigJson.DefaultPopup[menu.PowerIds].IsShowCancel
  375. appType = ConfigJson.DefaultPopup[menu.PowerIds].AppType
  376. openType = ConfigJson.DefaultPopup[menu.PowerIds].OpenType
  377. }
  378. }
  379. return
  380. }