user.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. UserRegisterTime = "jy_userregistertime_%s"
  20. )
  21. type UserInfo struct {
  22. Capitals map[string]int
  23. Permissions map[string]int
  24. Lock *sync.Mutex
  25. }
  26. var (
  27. CapitalRetention = "capital_retention"
  28. //UserCapitals = map[string]map[string]int{}
  29. //UserPermissions = map[string]map[string]int{}
  30. //UserLock = map[string]*sync.Mutex{}
  31. OverallLock = &sync.Mutex{}
  32. UserInfoMap = map[string]*UserInfo{}
  33. UserRolePowers = map[string][]string{}
  34. )
  35. /*
  36. *待调整 调整为存redis
  37. *测试用例放的地方不对 待调整
  38. */
  39. //用户权限 初始化
  40. func AutoUserPowerInfo(userId, appId string, entId int64) map[string]int {
  41. /*
  42. * 商机管理--》新版商机管理vs老版商机管理
  43. * 大会员--》bigmember_service
  44. * 超级订阅--》新版超级订阅vs老版超级订阅
  45. * 免费用户--》新免费用户
  46. */
  47. var UserPowerMap = map[string]int{}
  48. //redis newother 查询是否存在用户功能信息
  49. userPowerRedisKey := fmt.Sprintf(UserPowerRedisKey, appId, time.Now().Day(), 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. data, ok := Mgo.FindById("user", userId, `{"s_phone":1,"s_m_phone":1,i_member_status":1,“i_member_endtime”:1,"s_member_mainid":1,"i_member_sub_status":1,"i_vip_status":1,"l_vip_endtime":1,"o_vipjy":1,"o_jy":1,"l_registedate":1}`)
  58. if ok && *data != nil && len(*data) > 0 {
  59. var (
  60. isFree = true
  61. domainBool = false //第一版领域化权限判断 ;第二版:超级订阅||大会员 到期时间超过90天
  62. registerTime int64 = 0
  63. )
  64. phone, _ := MC.If((*data)["s_phone"] != nil, (*data)["s_phone"], (*data)["s_m_phone"]).(string)
  65. if phone != "" {
  66. //同一个手机号 多个商机管理角色 其中一个是企业管理员或者部门管理员 查的时候按角色权重排序
  67. 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)
  68. //商机管理用户信息判断
  69. if entNicheInfos != nil && len(*entNicheInfos) > 0 {
  70. isFree = false
  71. for _, entNicheInfo := range *entNicheInfos {
  72. //判断是否是当前企业
  73. ent_id := MC.Int64All(entNicheInfo["ent_id"])
  74. if entId > 0 && ent_id != entId {
  75. continue
  76. }
  77. //entNicheInfo := (*entNicheInfos)[0]
  78. switch MC.IntAll(entNicheInfo["isNew"]) {
  79. case 1: //新版商机管理
  80. UserPowerMap["110"] = 1
  81. switch MC.IntAll(entNicheInfo["role_id"]) {
  82. case 1: //部门管理员
  83. UserPowerMap["111"] = 1
  84. case 2: //企业管理员
  85. UserPowerMap["112"] = 1
  86. }
  87. case 0: //老版商机管理
  88. UserPowerMap["100"] = 1
  89. switch MC.IntAll(entNicheInfo["role_id"]) {
  90. case 1: //部门管理员
  91. UserPowerMap["101"] = 1
  92. case 2: //企业管理员
  93. UserPowerMap["102"] = 1
  94. }
  95. }
  96. }
  97. }
  98. //大会员
  99. if memberStatus := MC.IntAll((*data)["i_member_status"]); memberStatus > 0 || ConfigJson.BigMemberOff {
  100. mainUserId := userId
  101. if memberStatus > 0 {
  102. registerTime = MC.Int64All((*data)["i_member_endtime"])
  103. domainBool = true
  104. isFree = false
  105. UserPowerMap["0"] = memberStatus
  106. //是否是子账号 而且 子账号被启用
  107. if (*data)["s_member_mainid"] != nil && MC.ObjToString((*data)["s_member_mainid"]) != "" && MC.IntAllDef((*data)["i_member_sub_status"], 0) > 0 {
  108. mainUserId = MC.ObjToString((*data)["s_member_mainid"])
  109. }
  110. }
  111. //大会员用户购买的服务
  112. serviceList := Mysql.Find(BigMemberUserPowerTable, map[string]interface{}{"s_userid": mainUserId, "i_status": 0}, "DISTINCT(s_serviceid),i_frequency", "", -1, -1)
  113. if serviceList != nil && len(*serviceList) != 0 {
  114. for _, sv := range *serviceList {
  115. UserPowerMap[MC.ObjToString(sv["s_serviceid"])] = MC.If(MC.IntAll(sv["i_frequency"]) > 0, MC.IntAll(sv["i_frequency"]), memberStatus).(int)
  116. }
  117. }
  118. }
  119. //VIP用户
  120. if vipStatus := MC.IntAll((*data)["i_vip_status"]); vipStatus > 0 {
  121. if registerTime < MC.Int64All((*data)["l_vip_endtime"]) {
  122. registerTime = MC.Int64All((*data)["l_vip_endtime"])
  123. }
  124. domainBool = true
  125. isFree = false
  126. UserPowerMap["200"] = vipStatus
  127. if vipSet := MC.ObjToMap((*data)["o_vipjy"]); vipSet != nil {
  128. if buySet := MC.ObjToMap((*vipSet)["o_buyset"]); buySet != nil {
  129. //vip 升级用户
  130. if MC.IntAll((*buySet)["upgrade"]) > 0 {
  131. UserPowerMap["201"] = vipStatus
  132. }
  133. }
  134. }
  135. }
  136. //免费用户
  137. if isFree {
  138. UserPowerMap["300"] = 1
  139. freeSet := MC.ObjToMap((*data)["o_jy"])
  140. if MC.IntAll((*freeSet)["i_newfree"]) > 0 || IsNewFreeTimeCell < MC.IntAll((*data)["l_registedate"]) {
  141. //新免费用户
  142. UserPowerMap["301"] = 1
  143. }
  144. }
  145. //广东移动DICT
  146. if Mysql.CountBySql(`select count(*) from privatedata where phone = ?`, phone) > 0 {
  147. UserPowerMap["400"] = 1
  148. }
  149. //领域化产品权限
  150. //第一版:必须是大会员或者超级订阅用户 且留资 留资表:capital_retention;source = 'medical_domain',未留资提示留资信息
  151. //第二版:调资源中台rpc获取用户是否有使用领域化产品的权限 无权限则去购买
  152. //需求调整:
  153. //1:是否是大会员或者超级订阅用户 否:提示购买到超级订阅购买页; 是>-2
  154. //2:判断用户是否留资 否:提示用去留资;是:>-3
  155. //3:资源中台获取用户权限码判断是否有权限 否:提示用户去联系客服
  156. if domainBool {
  157. userRegisterTimeKey := fmt.Sprintf(UserRegisterTime, userId)
  158. redis.Put(RedisCode, userRegisterTimeKey, strconv.Itoa(int(registerTime)), int(registerTime))
  159. UserPowerMap["500"] = 1
  160. }
  161. }
  162. if UserPowerMap != nil {
  163. bytes, err := json.Marshal(UserPowerMap)
  164. if err == nil && len(bytes) > 0 {
  165. redis.PutBytes(RedisCode, userPowerRedisKey, &bytes, ConfigJson.InternalTime)
  166. if entId > 0 {
  167. //商机管理用户entId 缓存
  168. userEntIdKey := fmt.Sprintf(UserEntIdKey, appId, time.Now().Day(), userId)
  169. redis.Put(RedisCode, userEntIdKey, entId, ConfigJson.InternalTime)
  170. }
  171. }
  172. }
  173. }
  174. return UserPowerMap
  175. }
  176. //clear One
  177. func ClearUserPowerFunc(userId, appId string) {
  178. userPowerRedisKey := fmt.Sprintf(UserPowerRedisKey, appId, time.Now().Day(), userId)
  179. redis.Del(RedisCode, userPowerRedisKey)
  180. for _, v := range []string{"PC", "APP", "WX"} {
  181. RedisMenuKey := fmt.Sprintf(RedisMenuKey, appId, v, userId)
  182. redis.Del(RedisCode, RedisMenuKey)
  183. }
  184. }
  185. //用户角色权限初始化
  186. func UserRolePowerInit(strs []string) {
  187. if len(strs) > 0 {
  188. jyUserRoleData := BaseMysql.SelectBySql(`SELECT id,name FROM jyfunction WHERE status = 1`)
  189. if jyUserRoleData != nil && len(*jyUserRoleData) > 0 {
  190. for _, jv := range *jyUserRoleData {
  191. for _, v := range strs {
  192. if strings.Contains(MC.ObjToString(jv["name"]), v) {
  193. UserRolePowers[v] = append(UserRolePowers[v], strconv.Itoa(MC.IntAll(jv["id"])))
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. /*
  201. 1、不符合可以留资申请开通权限的用户
  202. 1、免费用户
  203. 2、超级订阅用户、大会员用户到期时间是否超过3个月
  204. (1、2:弹窗-医械通上线啦)
  205. 2、符合可以留资申请开通的用户
  206. 1、未留资
  207. (1:弹窗-完善基本信息)
  208. 2、已留资未开通
  209. 3、已留资且开通
  210. (2、3:弹窗-恭喜留资成功)
  211. */
  212. /*
  213. 1、判断是否满足超级订阅用户或大会员用户 且到期时间超过90天(配置);
  214. 1.1、否:权限=0;提示上线啦
  215. 1.2、是:是否留资
  216. 1.2.1、否:权限=0;提示留资
  217. 1.2.2、是:是否开通权限
  218. 1.2.2.1、否:权限=0;提示恭喜留资成功,客服联系
  219. 1.2.2.2、是:权限=1
  220. */
  221. /*存在超级订阅 或 大会员到期 而医械通未到期的情况*/
  222. // CheckCapitalResources 是否需要留资 且权限验证
  223. func CheckCapitalResources(menu *JYMenu, wd *WorkDesktopMenu, b bool) (title, content, confirmUrl, confirmText, appType, openType string, isShowCancel, usable bool) {
  224. OverallLock.Lock()
  225. userInfo := UserInfoMap[wd.NewUserId]
  226. if userInfo == nil {
  227. userInfo = &UserInfo{}
  228. userInfo.Lock = &sync.Mutex{}
  229. userInfo.Capitals = map[string]int{}
  230. userInfo.Permissions = map[string]int{}
  231. UserInfoMap[wd.NewUserId] = userInfo
  232. }
  233. OverallLock.Unlock()
  234. userInfo.Lock.Lock()
  235. defer userInfo.Lock.Unlock()
  236. var (
  237. capitalBool = true //是否留资
  238. permissionBool = true //是否有功能权限
  239. customPopup = true //自定义弹窗
  240. defaultPopup = false //默认弹窗
  241. )
  242. //无权限(医械通-:1:免费用户;2:超级订阅、大会员到期时间不在规定范围内)
  243. if b {
  244. //大会员超级订阅用户 才有权限去验证是否有医疗权限
  245. //用户是否需要留资
  246. if menu.CapitalCode != "" {
  247. capitalBool = false
  248. for _, cv := range strings.Split(menu.CapitalCode, ",") {
  249. if userInfo.Capitals[cv] == 0 {
  250. if c := BaseMysql.CountBySql(`SELECT COUNT(id) FROM `+CapitalRetention+` WHERE source = ? AND user_id = ? AND appid = ?`, cv, wd.NewUserId, wd.AppId); c > 0 {
  251. userInfo.Capitals[cv] = 1
  252. } else {
  253. userInfo.Capitals[cv] = -1
  254. }
  255. }
  256. if userInfo.Capitals[cv] < 0 {
  257. customPopup = false
  258. //留资弹窗信息
  259. title = menu.CapitalInfo.Title
  260. content = menu.CapitalInfo.Content
  261. confirmUrl = menu.CapitalInfo.ConfirmUrl
  262. confirmText = menu.CapitalInfo.ConfirmText
  263. isShowCancel = menu.CapitalInfo.IsShowCancel
  264. appType = menu.CapitalInfo.AppType
  265. openType = menu.CapitalInfo.OpenType
  266. } else {
  267. capitalBool = true
  268. break
  269. }
  270. }
  271. }
  272. //资源中台--- 无权限弹窗信息为默认信息(先留资)
  273. if menu.PermissionCode != "" {
  274. permissionBool = false
  275. if (menu.CapitalCode != "" && capitalBool) || menu.CapitalCode == "" {
  276. for _, pv := range strings.Split(menu.PermissionCode, ",") {
  277. if userInfo.Permissions[pv] == 0 {
  278. userInfo.Permissions[pv] = -1
  279. powerList := GetResources(wd.AppId, wd.NewUserId, wd.EntId, wd.EntUserId)
  280. if len(powerList) > 0 {
  281. for _, plv := range powerList {
  282. userInfo.Permissions[plv] = 1
  283. }
  284. }
  285. }
  286. if userInfo.Permissions[pv] >= 0 {
  287. title = ""
  288. permissionBool = true
  289. customPopup = false
  290. break
  291. }
  292. }
  293. }
  294. }
  295. //有用户权限 查看功能权限
  296. //医械通---用户没有功能权限 也没有留资 查看
  297. if !permissionBool {
  298. switch menu.PowerIds {
  299. case "500":
  300. userRegisterTime, _ := strconv.ParseInt(redis.GetStr(RedisCode, fmt.Sprintf(UserRegisterTime, wd.UserId)), 10, 64)
  301. //超级订阅 大会员到期时间 是否 大于 90天
  302. if userRegisterTime-time.Now().Unix() < ConfigJson.MedicalFieldTimespan {
  303. //即使是超级订阅或大会员 也没有权限
  304. defaultPopup = true
  305. }
  306. }
  307. }
  308. //
  309. usable = capitalBool && permissionBool
  310. }
  311. //无权限--免费用户||不符合条件的付费用户
  312. if !b || defaultPopup {
  313. //配置弹窗信息
  314. if ConfigJson.DefaultPopup[menu.PowerIds].Title != "" {
  315. customPopup = false
  316. title = ConfigJson.DefaultPopup[menu.PowerIds].Title
  317. content = ConfigJson.DefaultPopup[menu.PowerIds].Content
  318. confirmUrl = ConfigJson.DefaultPopup[menu.PowerIds].ConfirmUrl
  319. confirmText = ConfigJson.DefaultPopup[menu.PowerIds].ConfirmText
  320. isShowCancel = ConfigJson.DefaultPopup[menu.PowerIds].IsShowCancel
  321. appType = ConfigJson.DefaultPopup[menu.PowerIds].AppType
  322. openType = ConfigJson.DefaultPopup[menu.PowerIds].OpenType
  323. }
  324. }
  325. //无权限(有权限未留资|留资了未开通权限) --》 没有配置弹窗信息 --》自定义弹窗
  326. if customPopup {
  327. title = menu.AdditionalInfo.Title
  328. content = menu.AdditionalInfo.Content
  329. confirmUrl = menu.AdditionalInfo.ConfirmUrl
  330. confirmText = menu.AdditionalInfo.ConfirmText
  331. isShowCancel = menu.AdditionalInfo.IsShowCancel
  332. appType = menu.AdditionalInfo.AppType
  333. openType = menu.AdditionalInfo.OpenType
  334. }
  335. return
  336. }