user.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. package entity
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "sync"
  8. "time"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. MC "app.yhyue.com/moapp/jybase/common"
  11. "app.yhyue.com/moapp/jybase/redis"
  12. )
  13. const (
  14. RedisCode = "newother"
  15. RedisMenuKey = "jy_workdesktopmenu_%s_%s_%s"
  16. UserPowerRedisKey = "jy_userpowerredis_%s_%d_%s"
  17. UserRegisterTime = "jy_userregistertime_%s"
  18. UserPowerEndTime = "jy_user_power_end_time_%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. OverallLock = &sync.Mutex{}
  28. UserInfoMap = map[int64]*UserInfo{}
  29. UserRolePowers = map[string][]string{}
  30. )
  31. /*
  32. *待调整 调整为存redis
  33. *测试用例放的地方不对 待调整
  34. */
  35. // AutoUserPowerInfo 用户权限 初始化
  36. func (m *WorkDesktopMenu) AutoUserPowerInfo() map[string]int {
  37. /*
  38. * 商机管理--》新版商机管理vs老版商机管理
  39. * 大会员--》bigmember_service
  40. * 超级订阅--》新版超级订阅vs老版超级订阅
  41. * 免费用户--》新免费用户
  42. */
  43. /*
  44. * P278 用户身份切换
  45. * 用户权益 通过 权益中台rpc获取
  46. * 用户资源 通过 资源中台rpc获取
  47. */
  48. var UserPowerMap = map[string]int{}
  49. //redis newother 查询是否存在用户功能信息
  50. userPowerRedisKey := fmt.Sprintf(UserPowerRedisKey, m.AppId, time.Now().Day(), m.UserId)
  51. bytes, err := redis.GetBytes(RedisCode, userPowerRedisKey)
  52. if err == nil && len(*bytes) > 0 {
  53. if err = json.Unmarshal(*bytes, &UserPowerMap); err == nil {
  54. return UserPowerMap
  55. }
  56. }
  57. //数据库查询 、资源中台获取、权益中台获取 查询用户信息
  58. var (
  59. registerTime int64
  60. powerEndTime int64
  61. )
  62. //以上获取不到的信息 再自主查库
  63. userInfoRpc := UserInfoRpc{
  64. AppId: m.AppId,
  65. UserId: m.UserId,
  66. BaseUserId: m.NewUserId,
  67. EntId: m.EntId,
  68. EntUserId: m.EntUserId,
  69. AccountId: m.AccountId,
  70. EntAccountId: m.EntAccountId,
  71. PositionType: m.PositionType,
  72. PositionId: m.PositionId,
  73. MgoUserId: m.MgoUserId,
  74. }
  75. logx.Info(m.MgoUserId, m.PositionId, "------------------userInfoRpc-------------------------:", userInfoRpc)
  76. //权益中台获取权益
  77. userPowers := userInfoRpc.GetUserPowers()
  78. if userPowers != nil {
  79. logx.Info(userInfoRpc.MgoUserId, "------------userPowers:", userPowers)
  80. //注册时间
  81. registerTime = userPowers.Free.Registedate
  82. //大会员
  83. member := userPowers.Member
  84. //超级订阅
  85. vip := userPowers.Vip
  86. //企业信息
  87. entInfo := userPowers.Ent
  88. //商机管理
  89. entNiche := userPowers.Entniche
  90. //免费用户
  91. free := userPowers.Free
  92. if member.Status > 0 || ConfigJson.BigMemberOff {
  93. if member.Status > 0 {
  94. UserPowerMap["0"] = int(member.Status)
  95. UserPowerMap[strconv.Itoa(50+int(member.Status))] = 1
  96. //此处 如果 member.Status >7,和 下面58 会有冲突
  97. //人员行为统计菜单-1、购买大会员且创建了企业的管理员;2、新版商机管理管理员;3、商机管理服务管理员
  98. UserPowerMap["50"] = 1 //
  99. if entInfo.EntRoleId == 1 {
  100. UserPowerMap["58"] = 1 //企业管理员
  101. } else if entInfo.EntRoleId == 2 {
  102. UserPowerMap["59"] = 1 // 部门管理员
  103. }
  104. powerEndTime = member.EndTime
  105. }
  106. //大会员用户购买的服务 也有可能是非大会员用户 购买这些服务
  107. if member.MemberPowerList != nil && len(member.MemberPowerList) > 0 {
  108. for _, mp := range member.MemberPowerList {
  109. UserPowerMap[strconv.FormatInt(mp, 10)] = MC.If(member.Status > 0, int(member.Status), 1).(int)
  110. }
  111. }
  112. }
  113. if vip.Status > 0 {
  114. //
  115. if powerEndTime < vip.EndTime {
  116. powerEndTime = vip.EndTime
  117. }
  118. UserPowerMap["200"] = int(vip.Status)
  119. UserPowerMap["201"] = int(vip.Upgrade)
  120. //
  121. if vip.Upgrade > 0 && member.Status < 1 {
  122. UserPowerMap["202"] = int(vip.Status)
  123. }
  124. if entInfo.EntRoleId == 1 {
  125. UserPowerMap["203"] = 1 //企业管理员
  126. } else if entInfo.EntRoleId == 2 {
  127. UserPowerMap["204"] = 1 //部门管理员
  128. }
  129. }
  130. //企业信息
  131. if m.EntId > 0 {
  132. UserPowerMap["702"] = 1
  133. //机构中心 由企业 且 是企业版
  134. //m.PositionType 肯定大于 0; 702 和 901 是同样的权限。
  135. if m.PositionType > 0 {
  136. UserPowerMap["901"] = 1
  137. }
  138. //P386
  139. switch entInfo.EntRoleId {
  140. case 1: //企业管理员
  141. UserPowerMap["902"] = 1
  142. case 2: //部门管理员
  143. UserPowerMap["903"] = 1
  144. }
  145. }
  146. if entNiche.Status > 0 && entNiche.IsEntPower > 0 {
  147. //entInfo.EntRoleId 1:企业管理员 2:部门管理员
  148. //管理员 entInfo.EntRoleId>0 并不一定 entNiche.IsEntPower==1,entNiche.IsEntPower==0 也有权限(已废除)
  149. //管理员he员工被分配后 entNiche.IsEntPower==1否则没有权限
  150. //if entNiche.IsEntPower > 0 || entInfo.EntRoleId > 0{//(已废除)
  151. //商机管理服务 P259需求
  152. //有商机管理服务 不会再有商机管理订阅菜单
  153. //商机管理服务,上次陈老师说叫“企业管理服务”,不然和商机管理产品容易混淆-- 杨蘭 2022/12/14
  154. switch entNiche.PowerSource {
  155. case 0:
  156. switch entNiche.IsNew {
  157. case 1: //新版商机管理
  158. UserPowerMap["110"] = 1
  159. switch entInfo.EntRoleId {
  160. case 2: //部门管理员
  161. UserPowerMap["111"] = 1
  162. case 1: //企业管理员
  163. UserPowerMap["112"] = 1
  164. }
  165. case 0: //老版商机管理
  166. UserPowerMap["100"] = 1
  167. switch entInfo.EntRoleId {
  168. case 2: //部门管理员
  169. UserPowerMap["101"] = 1
  170. case 1: //企业管理员
  171. UserPowerMap["102"] = 1
  172. }
  173. //老版商机管理: model:1-统一订阅,2-个人订阅
  174. if entNiche.Model == 1 {
  175. UserPowerMap["103"] = 1
  176. } else if entNiche.Model == 2 {
  177. UserPowerMap["104"] = 1
  178. }
  179. }
  180. case 1:
  181. //客户管理服务(商机管理服务) [前提:大会员、超级订阅、医械通用户]---免费用户也可以用 (需求调整来自刘苗:产品已确认)
  182. UserPowerMap["600"] = 1
  183. switch entInfo.EntRoleId {
  184. case 2: //部门管理员
  185. UserPowerMap["602"] = 1
  186. case 1: //企业管理员
  187. UserPowerMap["601"] = 1
  188. }
  189. //}
  190. }
  191. }
  192. if free.IsFree {
  193. UserPowerMap["300"] = 1
  194. if free.IsUpgrade {
  195. //新免费用户
  196. UserPowerMap["301"] = MC.If(free.IsUpgrade, 1, 0).(int)
  197. }
  198. //P386
  199. switch entInfo.EntRoleId {
  200. case 1: //企业管理员
  201. UserPowerMap["302"] = 1
  202. case 2: //部门管理员
  203. UserPowerMap["303"] = 1
  204. }
  205. }
  206. //----------其他-----------
  207. //广东移动DICT
  208. if entInfo.PrivateGD {
  209. UserPowerMap["400"] = 1
  210. }
  211. //人员行为统计菜单-1、购买大会员且创建了企业的管理员;2、新版商机管理管理员;3、商机管理服务管理员
  212. //必须是企业管理员-&-购买了企业级应用服务
  213. //企业级服务 权限管理(只有企业管理员有权限)
  214. entService := entInfo.BuyMember > 0 || entInfo.BuyVip > 0 || entInfo.Services //P386: 只要购买了企业级产品 且是企业管理员 就有>> 权限管理
  215. if entService {
  216. if entInfo.EntRoleId == 1 {
  217. UserPowerMap["700"] = 1
  218. } else if entInfo.EntRoleId == 2 {
  219. //企业级服务 部门管理员
  220. UserPowerMap["703"] = 1
  221. }
  222. }
  223. //企业级服务 企业订阅--
  224. //1、存在未到期的购买主体为“企业”切购买产品为大会员或者超级订阅的部门管理员或企业管理员;c > 0
  225. //2、存在未到期的老版或者新版商机管理的企业管理员或部门管理员(非商机管理服务)。entnicheIsNew > -1 && powerSource == 0
  226. if entInfo.EntSubscribe > 0 {
  227. UserPowerMap["701"] = 1
  228. }
  229. //领域化产品权限
  230. //第一版:必须是大会员或者超级订阅用户 且留资 留资表:capital_retention;source = 'medical_domain',未留资提示留资信息
  231. //第二版:调资源中台rpc获取用户是否有使用领域化产品的权限 无权限则去购买
  232. //需求调整:
  233. //1:是否是大会员或者超级订阅用户 否:提示购买到超级订阅购买页; 是>-2
  234. //2:判断用户是否留资 否:提示用去留资;是:>-3
  235. //3:资源中台获取用户权限码判断是否有权限 否:提示用户去联系客服
  236. userRegisterTimeKey := fmt.Sprintf(UserRegisterTime, m.UserId)
  237. redis.Put(RedisCode, userRegisterTimeKey, strconv.Itoa(int(registerTime)), int(registerTime))
  238. if member.Status > 0 || vip.Status > 0 {
  239. userPowerEndTimeKey := fmt.Sprintf(UserPowerEndTime, m.UserId)
  240. redis.Put(RedisCode, userPowerEndTimeKey, strconv.Itoa(int(powerEndTime)), int(powerEndTime))
  241. UserPowerMap["500"] = 1
  242. }
  243. }
  244. //资源中台获取权限
  245. powerList := userInfoRpc.GetUserResources()
  246. if len(powerList) > 0 {
  247. for _, plv := range powerList {
  248. UserPowerMap[plv] = 1
  249. }
  250. }
  251. //其他权益数据库查询
  252. func(mgoUserId string) {
  253. //伙伴计划是否加入(移动端我的伙伴计划菜单)
  254. if count := Mysql.CountBySql(`select count(*) from dis_partner where uid=? and type=2`, mgoUserId); count > 0 {
  255. UserPowerMap["801"] = 1
  256. } else {
  257. UserPowerMap["800"] = 1
  258. }
  259. }(m.MgoUserId)
  260. //缓存
  261. if UserPowerMap != nil {
  262. bytes, err := json.Marshal(UserPowerMap)
  263. if err == nil && len(bytes) > 0 {
  264. redis.PutBytes(RedisCode, userPowerRedisKey, &bytes, ConfigJson.InternalTime)
  265. }
  266. }
  267. return UserPowerMap
  268. }
  269. // ClearUserPowerFunc clear One userId>positionId 职位id
  270. func ClearUserPowerFunc(positionId, appId string) bool {
  271. if positionId == "" {
  272. return false
  273. }
  274. for _, v := range strings.Split(positionId, ",") {
  275. for _, vv := range []string{"PC", "APP", "WX"} {
  276. //用户菜单缓存
  277. redis.Del(RedisCode, fmt.Sprintf(RedisMenuKey, appId, vv, v))
  278. }
  279. //用户权限缓存
  280. redis.Del(RedisCode, fmt.Sprintf(UserPowerRedisKey, appId, time.Now().Day(), v))
  281. }
  282. return true
  283. }
  284. // UserRolePowerInit 用户角色权限初始化
  285. func UserRolePowerInit(strs []string) {
  286. if len(strs) > 0 {
  287. jyUserRoleData := BaseMysql.SelectBySql(`SELECT id,name FROM jyfunction WHERE status = 1`)
  288. if jyUserRoleData != nil && len(*jyUserRoleData) > 0 {
  289. for _, jv := range *jyUserRoleData {
  290. for _, v := range strs {
  291. if strings.Contains(MC.ObjToString(jv["name"]), v) {
  292. UserRolePowers[v] = append(UserRolePowers[v], strconv.Itoa(MC.IntAll(jv["id"])))
  293. }
  294. }
  295. }
  296. }
  297. }
  298. }
  299. /*
  300. 菜单弹窗逻辑
  301. 如果需要留资
  302. 默认:
  303. 1、先判断是否已留资
  304. 2、再判断是否有权限
  305. 医械通:
  306. 1、不符合可以留资申请开通权限的用户
  307. 1、免费用户
  308. 2、超级订阅用户、大会员用户到期时间是否超过3个月
  309. (1、2:弹窗-医械通上线啦)
  310. 2、符合可以留资申请开通的用户
  311. 1、未留资
  312. (1:弹窗-完善基本信息)
  313. 2、已留资未开通
  314. 3、已留资且开通
  315. (2、3:弹窗-恭喜留资成功)
  316. */
  317. /*
  318. 1、判断是否满足超级订阅用户或大会员用户 且到期时间超过90天(配置);
  319. 1.1、否:权限=0;提示上线啦
  320. 1.2、是:是否留资
  321. 1.2.1、否:权限=0;提示留资
  322. 1.2.2、是:是否开通权限
  323. 1.2.2.1、否:权限=0;提示恭喜留资成功,客服联系
  324. 1.2.2.2、是:权限=1
  325. */
  326. /*存在超级订阅 或 大会员到期 而医械通未到期的情况*/
  327. // CheckCapitalResources 是否需要留资 且权限验证--弹窗处理
  328. // b 一级权限(超级订阅、大会员等)
  329. // p 二级权限(请求资源中台:医械通等)
  330. func CheckCapitalResources(menu *JYMenu, wd *WorkDesktopMenu, b, p bool, pUrl string) (title, content, confirmUrl, confirmText, appType, openType string, isShowCancel, usable bool) {
  331. var defaultPopupFunc = func(powerIds ...string) {
  332. dpKey := wd.Platform
  333. if len(powerIds) > 0 {
  334. dpKey = powerIds[0]
  335. }
  336. title = ConfigJson.DefaultPopup[dpKey].Title
  337. content = ConfigJson.DefaultPopup[dpKey].Content
  338. confirmUrlArr := strings.Split(ConfigJson.DefaultPopup[dpKey].ConfirmUrl, "__")
  339. if len(confirmUrlArr) > 1 {
  340. confirmUrl = MC.InterfaceToStr(MC.If(wd.Platform == "PC", confirmUrlArr[1], confirmUrlArr[0]))
  341. }
  342. confirmText = ConfigJson.DefaultPopup[dpKey].ConfirmText
  343. isShowCancel = ConfigJson.DefaultPopup[dpKey].IsShowCancel
  344. appType = ConfigJson.DefaultPopup[dpKey].AppType
  345. openType = ConfigJson.DefaultPopup[dpKey].OpenType
  346. }
  347. OverallLock.Lock()
  348. userInfo := UserInfoMap[wd.NewUserId]
  349. if userInfo == nil {
  350. userInfo = &UserInfo{}
  351. userInfo.Lock = &sync.Mutex{}
  352. userInfo.Capitals = map[string]int{}
  353. userInfo.Permissions = map[string]int{}
  354. UserInfoMap[wd.NewUserId] = userInfo
  355. }
  356. OverallLock.Unlock()
  357. userInfo.Lock.Lock()
  358. defer userInfo.Lock.Unlock()
  359. var (
  360. defaultPopup = false //默认弹窗
  361. capitalBool = func() bool {
  362. //用户是否需要留资
  363. if menu.CapitalCode != "" {
  364. capitalBool := false
  365. for _, cv := range strings.Split(menu.CapitalCode, ",") {
  366. if userInfo.Capitals[cv] <= 0 {
  367. if c := BaseMysql.CountBySql(`SELECT COUNT(id) FROM `+CapitalRetention+` WHERE source = ? AND position_id = ? AND appid = ?`, cv, wd.PositionId, wd.AppId); c > 0 {
  368. userInfo.Capitals[cv] = 1
  369. } else {
  370. userInfo.Capitals[cv] = -1
  371. }
  372. }
  373. if userInfo.Capitals[cv] > 0 {
  374. capitalBool = true
  375. break
  376. }
  377. }
  378. return capitalBool
  379. }
  380. return true
  381. }()
  382. )
  383. //
  384. usable = func() bool {
  385. switch menu.Authority {
  386. case 0:
  387. return b && p && capitalBool
  388. case 1:
  389. return b || p || capitalBool
  390. case 2:
  391. return b || p && capitalBool
  392. case 3:
  393. return b && p || capitalBool
  394. case 4:
  395. return b && capitalBool || p
  396. default:
  397. return false
  398. }
  399. }()
  400. //P630人脉管理
  401. //用户 有权限 不再判断 弹窗提示信息
  402. if usable {
  403. //菜单在当前平台没有权限
  404. if menu.Url == "" && pUrl == "" {
  405. usable = false
  406. defaultPopupFunc()
  407. }
  408. return
  409. }
  410. // 需要特殊处理的菜单 弹窗提示信息
  411. /*
  412. 菜单弹窗逻辑
  413. 如果需要留资
  414. 默认:
  415. 1、先判断是否已留资
  416. 2、再判断是否有权限
  417. 医械通:
  418. 1、不符合可以留资申请开通权限的用户
  419. 1、免费用户
  420. 2、超级订阅用户、大会员用户到期时间是否超过3个月
  421. (1、2:弹窗-医械通上线啦)
  422. 2、符合可以留资申请开通的用户
  423. 1、未留资
  424. (1:弹窗-完善基本信息)
  425. 2、已留资未开通
  426. 3、已留资且开通
  427. (2、3:弹窗-恭喜留资成功)
  428. */
  429. if PopupIdMap[menu.Id] || PopupIdMap[menu.ParentId] {
  430. // 超级订阅 or 大会员
  431. if b {
  432. userPowerEndTimeKey := fmt.Sprintf(UserPowerEndTime, wd.UserId)
  433. powerEndTime, _ := strconv.ParseInt(redis.GetStr(RedisCode, userPowerEndTimeKey), 10, 64)
  434. //超级订阅 大会员到期时间 是否 大于 90天
  435. if powerEndTime == 0 || powerEndTime-time.Now().Unix() < ConfigJson.MedicalFieldTimespan {
  436. //即使是超级订阅或大会员 也没有权限
  437. defaultPopup = true
  438. }
  439. }
  440. // 免费用户 或者 不满足条件的超级订阅or大会员用户
  441. if (!b || defaultPopup) && ConfigJson.DefaultPopup[menu.PowerIds].Title != "" {
  442. defaultPopupFunc(menu.PowerIds)
  443. } else if !capitalBool { //未留资的用户
  444. //留资弹窗信息
  445. title = menu.CapitalInfo.Title
  446. content = menu.CapitalInfo.Content
  447. confirmUrl = menu.CapitalInfo.ConfirmUrl
  448. confirmText = menu.CapitalInfo.ConfirmText
  449. isShowCancel = menu.CapitalInfo.IsShowCancel
  450. appType = menu.CapitalInfo.AppType
  451. openType = menu.CapitalInfo.OpenType
  452. } else {
  453. title = menu.AdditionalInfo[wd.Platform].Title
  454. content = menu.AdditionalInfo[wd.Platform].Content
  455. confirmUrl = menu.AdditionalInfo[wd.Platform].ConfirmUrl
  456. confirmText = menu.AdditionalInfo[wd.Platform].ConfirmText
  457. isShowCancel = menu.AdditionalInfo[wd.Platform].IsShowCancel
  458. appType = menu.AdditionalInfo[wd.Platform].AppType
  459. openType = menu.AdditionalInfo[wd.Platform].OpenType
  460. }
  461. } else { //默认
  462. // 留资
  463. if !capitalBool {
  464. //留资弹窗信息
  465. title = menu.CapitalInfo.Title
  466. content = menu.CapitalInfo.Content
  467. confirmUrl = menu.CapitalInfo.ConfirmUrl
  468. confirmText = menu.CapitalInfo.ConfirmText
  469. isShowCancel = menu.CapitalInfo.IsShowCancel
  470. appType = menu.CapitalInfo.AppType
  471. openType = menu.CapitalInfo.OpenType
  472. } else {
  473. title = menu.AdditionalInfo[wd.Platform].Title
  474. content = menu.AdditionalInfo[wd.Platform].Content
  475. confirmUrl = menu.AdditionalInfo[wd.Platform].ConfirmUrl
  476. confirmText = menu.AdditionalInfo[wd.Platform].ConfirmText
  477. isShowCancel = menu.AdditionalInfo[wd.Platform].IsShowCancel
  478. appType = menu.AdditionalInfo[wd.Platform].AppType
  479. openType = menu.AdditionalInfo[wd.Platform].OpenType
  480. }
  481. }
  482. return
  483. }