user.go 11 KB

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