workDesktop.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math/rand"
  6. "strconv"
  7. "strings"
  8. "time"
  9. MC "app.yhyue.com/moapp/jybase/common"
  10. "app.yhyue.com/moapp/jybase/encrypt"
  11. "app.yhyue.com/moapp/jybase/redis"
  12. "bp.jydev.jianyu360.cn/BaseService/userCenter/entity"
  13. "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb"
  14. . "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/usercenter"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. )
  17. // RenewWorkDesktopMenuModeOrCommonly 工作桌面--菜单当前选择模式--全部:all/可用:usable
  18. // 工作桌面--常用功能更新
  19. // 工作桌面--常用功能列表
  20. func RenewWorkDesktopMenuModeOrCommonly(in *WorkDesktopComprehensiveReq) (r *WorkDesktopComprehensiveResp) {
  21. if in.ActionMode != "commonlyList" && in.UserId == "" {
  22. return
  23. }
  24. r = &WorkDesktopComprehensiveResp{}
  25. switch in.ActionMode {
  26. case "commonlyRenew": //常用功能更新
  27. //in.MenuIds 不为空:更新
  28. if b, m := entity.CommonlyUpdate(in); !b {
  29. r.ErrorCode = -1
  30. r.ErrorMsg = m
  31. }
  32. case "commonlyList":
  33. //查询常用功能列表
  34. in.ActionMode = "commonlyRenew"
  35. var (
  36. pIds []string
  37. )
  38. logx.Info("--------------------:", fmt.Sprintf(`SELECT value FROM `+entity.WorkCommonly+` WHERE base_userid = '%s' AND appid = %s AND field = '%s' AND userid = '%s' ORDER BY id DESC `, in.NewUserId, in.AppId, in.ActionMode, in.UserId))
  39. if in.UserId != "" {
  40. //常用功能查看
  41. existingData := entity.BaseMysql.SelectBySql(`SELECT value FROM `+entity.WorkCommonly+` WHERE base_userid = ? AND appid = ? AND field = ? AND userid = ? ORDER BY id DESC `, in.NewUserId, in.AppId, in.ActionMode, in.UserId)
  42. if existingData == nil || len(*existingData) == 0 {
  43. //P278原查询逻辑
  44. existingData = entity.BaseMysql.SelectBySql(`SELECT value FROM `+entity.WorkCommonly+` WHERE base_userid = ? AND appid = ? AND field = ? ORDER BY id DESC `, in.NewUserId, in.AppId, in.ActionMode)
  45. }
  46. if existingData != nil && len(*existingData) > 0 {
  47. eData := (*existingData)[0]
  48. if MC.ObjToString(eData["value"]) != "" {
  49. for _, pv := range strings.Split(MC.ObjToString(eData["value"]), ",") {
  50. if pv == "" {
  51. continue
  52. }
  53. pIds = append(pIds, pv)
  54. }
  55. }
  56. } else {
  57. if entity.ConfigJson.CommonlyIds != "" && len(strings.Split(entity.ConfigJson.CommonlyIds, ",")) > 0 {
  58. // 判断用户身份
  59. var ids []string
  60. var oIds []string // 没有加密的id
  61. var (
  62. entId, _ = strconv.ParseInt(in.EntId, 10, 64)
  63. newUserId, _ = strconv.ParseInt(in.NewUserId, 10, 64)
  64. entUserId, _ = strconv.ParseInt(in.EntUserId, 10, 64)
  65. accountId, _ = strconv.ParseInt(in.AccountId, 10, 64)
  66. entAccountId, _ = strconv.ParseInt(in.EntAccountId, 10, 64)
  67. positionType, _ = strconv.ParseInt(in.PositionType, 10, 64)
  68. positionId, _ = strconv.ParseInt(in.PositionId, 10, 64)
  69. )
  70. userInfoRpc := entity.UserInfoRpc{
  71. AppId: in.AppId,
  72. UserId: in.UserId,
  73. BaseUserId: newUserId,
  74. EntId: entId,
  75. EntUserId: entUserId,
  76. AccountId: accountId,
  77. EntAccountId: entAccountId,
  78. PositionType: positionType,
  79. PositionId: positionId,
  80. MgoUserId: in.MgoUserId,
  81. }
  82. userPowers := userInfoRpc.GetUserPowers()
  83. var subBool bool
  84. for _, v := range strings.Split(entity.ConfigJson.CommonlyIds, ",") {
  85. // p562 我的订阅判断用户身份
  86. if userPowers != nil {
  87. if v == "163" { // 我的订阅(大会员)
  88. if userPowers.Member.Status <= 0 {
  89. continue
  90. } else {
  91. subBool = true
  92. }
  93. } else if v == "165" { // 我的订阅(超级订阅)
  94. if subBool || userPowers.Vip.Status <= 0 {
  95. continue
  96. } else {
  97. subBool = true
  98. }
  99. } else if v == "55" { // 我的订阅(商机管理)新版
  100. if subBool || userPowers.Entniche.Status <= 0 || userPowers.Entniche.IsNew <= 0 {
  101. continue
  102. } else {
  103. subBool = true
  104. }
  105. } else if v == "515" { // 我的订阅(商机管理)老版
  106. if subBool || userPowers.Entniche.Status <= 0 || userPowers.Entniche.IsNew > 0 {
  107. continue
  108. } else {
  109. subBool = true
  110. }
  111. } else if v == "167" { // 我的订阅
  112. if subBool || userPowers.Vip.Status > 0 || userPowers.Member.Status > 0 || userPowers.Entniche.Status > 0 {
  113. continue
  114. }
  115. }
  116. }
  117. oIds = append(oIds, v)
  118. ids = append(ids, encrypt.SE.EncodeString(v))
  119. }
  120. in.MenuIds = strings.Join(ids, ",")
  121. //初始化 常用功能
  122. if b, _ := entity.CommonlyUpdate(in); b {
  123. pIds = oIds
  124. } else {
  125. logx.Info("初始化常用功能异常")
  126. }
  127. }
  128. }
  129. } else { //未登录用户
  130. if entity.ConfigJson.NotLoginCommonlyIds != "" && len(strings.Split(entity.ConfigJson.NotLoginCommonlyIds, ",")) > 0 {
  131. // 判断用户身份
  132. var ids []string
  133. var oIds []string // 没有加密的id
  134. for _, v := range strings.Split(entity.ConfigJson.CommonlyIds, ",") {
  135. oIds = append(oIds, v)
  136. ids = append(ids, encrypt.SE.EncodeString(v))
  137. }
  138. in.MenuIds = strings.Join(ids, ",")
  139. //初始化 常用功能
  140. if b, _ := entity.CommonlyUpdate(in); b {
  141. pIds = oIds
  142. } else {
  143. logx.Info("初始化常用功能异常")
  144. }
  145. }
  146. }
  147. if len(pIds) > 0 {
  148. //P278 身份切换
  149. positionType := ``
  150. switch in.PositionType { //职位类型 0:个人 1:企业
  151. case "1":
  152. positionType = `AND available >= 2`
  153. //P364 企业角色
  154. if in.EntUserRole != "" {
  155. positionType += ` AND (FIND_IN_SET('0',entrole)`
  156. for _, entUserRole := range strings.Split(in.EntUserRole, ",") {
  157. positionType += fmt.Sprintf(` OR FIND_IN_SET('%d',entrole)`, MC.Int64All(entUserRole)+1)
  158. }
  159. positionType += `)`
  160. } else {
  161. positionType += fmt.Sprintf(` AND (FIND_IN_SET('0',entrole) OR FIND_IN_SET('%d',entrole)) `, in.EntNicheDis+1)
  162. }
  163. default:
  164. positionType = `AND available <= 2`
  165. }
  166. //常用功能存储是三级菜单id,需要四级菜单信息也查出来
  167. //会出现 用户设置常用功能,此功能下线,用户设置依然保存的有此功能 查询不能加 AND status = 0
  168. var menuSql = fmt.Sprintf(`SELECT * from %s WHERE (id IN (%s) OR (parentid IN (%s) AND status = 0)) %s ORDER BY FIELD(id , %s);`, entity.ConfigJson.WorkTableInside, strings.Join(pIds, ","), strings.Join(pIds, ","), positionType, strings.Join(pIds, ","))
  169. //外网访问
  170. if entity.ConfigJson.WorkTableInside != entity.ConfigJson.WorkTableOut && !in.IntranetBool {
  171. menuSql = fmt.Sprintf(`SELECT * from %s WHERE (id IN (%s) OR (parentid IN (%s) AND status = 0)) %s ORDER BY FIELD(id , %s);`, entity.ConfigJson.WorkTableOut, strings.Join(pIds, ","), strings.Join(pIds, ","), positionType, strings.Join(pIds, ","))
  172. }
  173. menuData := entity.BaseMysql.SelectBySql(menuSql)
  174. if menuData != nil && len(*menuData) > 0 {
  175. var (
  176. entId, _ = strconv.ParseInt(in.EntId, 10, 64)
  177. newUserId, _ = strconv.ParseInt(in.NewUserId, 10, 64)
  178. entUserId, _ = strconv.ParseInt(in.EntUserId, 10, 64)
  179. accountId, _ = strconv.ParseInt(in.AccountId, 10, 64)
  180. entAccountId, _ = strconv.ParseInt(in.EntAccountId, 10, 64)
  181. positionType, _ = strconv.ParseInt(in.PositionType, 10, 64)
  182. positionId, _ = strconv.ParseInt(in.PositionId, 10, 64)
  183. m = &entity.WorkDesktopMenu{
  184. MenuTree: []*entity.JYMenu{},
  185. UserId: in.UserId,
  186. NewUserId: newUserId,
  187. AppId: in.AppId,
  188. EntId: entId,
  189. EntUserId: entUserId,
  190. Platform: in.Platform,
  191. AccountId: accountId,
  192. EntAccountId: entAccountId,
  193. PositionType: positionType,
  194. PositionId: positionId,
  195. IntranetBool: in.IntranetBool,
  196. MgoUserId: in.MgoUserId,
  197. }
  198. childMenus = map[int][]*entity.JYMenu{}
  199. )
  200. //是否存在父级
  201. parentIsExists := map[int]bool{}
  202. for _, mv := range *menuData {
  203. //id
  204. id := MC.IntAll(mv["id"])
  205. //parentId
  206. parentId := MC.IntAll(mv["parentid"])
  207. parentIsExists[id] = true
  208. menu := &entity.JYMenu{
  209. Id: MC.IntAll(mv["id"]),
  210. Name: MC.ObjToString(mv["name"]),
  211. Match: MC.ObjToString(mv["match"]),
  212. OrderId: MC.IntAll(mv["orderid"]),
  213. ParentId: parentId,
  214. PowerIds: MC.ObjToString(mv["powerids"]),
  215. CheckCode: MC.IntAll(mv["checkcode"]),
  216. Icon: MC.ObjToString(mv["icon"]),
  217. AppType: MC.ObjToString(mv["apptype"]),
  218. OpenType: MC.ObjToString(mv["opentype"]),
  219. Status: MC.IntAll(mv["status"]),
  220. PermissionCode: MC.ObjToString(mv["permissioncode"]),
  221. CapitalCode: MC.ObjToString(mv["capitalcode"]),
  222. Authority: MC.IntAll(mv["authority"]),
  223. Level: MC.IntAll(mv["level"]),
  224. Exclude: MC.ObjToString(mv["exclude"]),
  225. }
  226. var OpenType = map[string]string{}
  227. if err := json.Unmarshal([]byte(MC.ObjToString(mv["opentype"])), &OpenType); err == nil {
  228. menu.OpenType = OpenType[m.Platform]
  229. }
  230. switch in.Platform {
  231. case "WX":
  232. menu.Url = MC.ObjToString(mv["wxurl"])
  233. menu.PowerIds = MC.ObjToString(mv["wxpids"])
  234. case "APP":
  235. menu.Url = MC.ObjToString(mv["appurl"])
  236. menu.PowerIds = MC.ObjToString(mv["apppids"])
  237. default:
  238. menu.Url = MC.ObjToString(mv["pcurl"])
  239. }
  240. if additionalInfo := MC.ObjToString(mv["additionalinfo"]); additionalInfo != "" {
  241. additional := map[string]entity.Additional{}
  242. if json.Unmarshal([]byte(additionalInfo), &additional) == nil {
  243. menu.AdditionalInfo = map[string]entity.Additional{
  244. m.Platform: additional[m.Platform],
  245. }
  246. }
  247. }
  248. //外网不一致,
  249. if entity.ConfigJson.WorkTableInside != entity.ConfigJson.WorkTableOut {
  250. if parentIsExists[parentId] {
  251. childMenus[parentId] = append(childMenus[parentId], menu)
  252. } else {
  253. m.MenuTree = append(m.MenuTree, menu)
  254. }
  255. } else {
  256. if menu.Level == 4 {
  257. childMenus[parentId] = append(childMenus[parentId], menu)
  258. } else {
  259. m.MenuTree = append(m.MenuTree, menu)
  260. }
  261. }
  262. }
  263. //常用功能格式化
  264. menu, saveIds, delBool := m.CommonlyFormat(childMenus)
  265. //某一功能下架,但是用户收藏在常用功能中,需要更新此用户的常用功能数据
  266. if delBool && len(saveIds) > 0 {
  267. in.MenuIds = strings.Join(saveIds, ",")
  268. go func(in *WorkDesktopComprehensiveReq) {
  269. if b, _ := entity.CommonlyUpdate(in); !b {
  270. logx.Info("常用功能-更新数据失败")
  271. }
  272. }(in)
  273. }
  274. r.Data = menu
  275. }
  276. }
  277. r.CommonlySize = entity.ConfigJson.CommonlySize
  278. case "menuMode": //菜单模式更新
  279. if in.MenuMode == "" || (in.MenuMode != "usable" && in.MenuMode != "all") {
  280. r.ErrorCode = -1
  281. r.ErrorMsg = "菜单模式-参数异常"
  282. } else {
  283. menuModes := entity.BaseMysql.SelectBySql(`SELECT id,value FROM `+entity.WorkCommonly+` WHERE appid=? AND base_userid =? AND field = ? LIMIT 1`, in.AppId, in.NewUserId, in.ActionMode)
  284. if menuModes != nil && len(*menuModes) > 0 {
  285. menuMode := (*menuModes)[0]
  286. if MC.ObjToString(menuMode["value"]) != in.MenuMode {
  287. if entity.BaseMysql.UpdateOrDeleteBySql(`UPDATE `+entity.WorkCommonly+` SET value = ? WHERE id = ?`, in.MenuMode, MC.IntAll(menuMode["id"])) < 0 {
  288. r.ErrorCode = -1
  289. r.ErrorMsg = "菜单模式-更新异常"
  290. }
  291. }
  292. } else {
  293. if entity.BaseMysql.Insert(entity.WorkCommonly, map[string]interface{}{
  294. "appid": in.AppId,
  295. "base_userid": in.NewUserId,
  296. "field": in.ActionMode,
  297. "platform": in.Platform,
  298. "value": in.MenuMode,
  299. }) < 0 {
  300. r.ErrorCode = -1
  301. r.ErrorMsg = "菜单模式-插入异常"
  302. }
  303. }
  304. }
  305. }
  306. return
  307. }
  308. // GetWorkDesktopMenuMode 获取工作菜单模式
  309. func GetWorkDesktopMenuMode(in *WorkDesktopMenuInfoReq) (str string, err error) {
  310. if in.UserId == "" {
  311. return "", fmt.Errorf("参数异常")
  312. }
  313. str = "all"
  314. menuModes := entity.BaseMysql.SelectBySql(`SELECT value FROM `+entity.WorkCommonly+` WHERE appid=? AND base_userid=? AND field = 'menuMode' AND platform = ? LIMIT 1`, in.AppId, in.NewUserId, in.Platform)
  315. if menuModes != nil && len(*menuModes) > 0 {
  316. menuMode := (*menuModes)[0]
  317. if MC.ObjToString(menuMode["value"]) != "" {
  318. str = MC.ObjToString(menuMode["value"])
  319. }
  320. }
  321. return
  322. }
  323. // GetWordDesktopMenuTree 获取工作桌面菜单树
  324. func GetWordDesktopMenuTree(in *WorkDesktopMenuInfoReq) ([]*pb.MenuList, error) {
  325. //P278 身份切换 此处逻辑已不需要
  326. //if in.EntId != "" {
  327. // //判断商机管理用户是否切换企业
  328. // userEntIdKey := fmt.Sprintf(entity.UserEntIdKey, in.AppId, time.Now().Day(), in.UserId)
  329. // redisEntId := redis.GetInt(entity.RedisCode, userEntIdKey)
  330. // entId, err := strconv.Atoi(in.EntId)
  331. // if err == nil && int64(redisEntId) >= 0 && redisEntId != entId {
  332. // //商机管理用户切换企业---清除用户权限缓存&&清除用户菜单缓存
  333. // entity.ClearUserPowerFunc(in.UserId, in.AppId, in.EntId)
  334. // }
  335. //}
  336. t1 := time.Now()
  337. //菜单redis缓存
  338. var (
  339. menuList []*pb.MenuList
  340. )
  341. RedisMenuKey := fmt.Sprintf(entity.RedisMenuKey, in.AppId, in.Platform, entity.ConfigJson.MenuCacheKey, in.UserId)
  342. logx.Info("RedisMenuKey:", RedisMenuKey)
  343. menuBytes, err := redis.GetBytes(entity.RedisCode, RedisMenuKey)
  344. if in.WorkStatus == 0 && err == nil && len(*menuBytes) > 0 {
  345. if json.Unmarshal(*menuBytes, &menuList) == nil {
  346. return menuList, nil
  347. }
  348. }
  349. entId, _ := strconv.ParseInt(in.EntId, 10, 64)
  350. newUserId, _ := strconv.ParseInt(in.NewUserId, 10, 64)
  351. entUserId, _ := strconv.ParseInt(in.EntUserId, 10, 64)
  352. accountId, _ := strconv.ParseInt(in.AccountId, 10, 64)
  353. entAccountId, _ := strconv.ParseInt(in.EntAccountId, 10, 64)
  354. positionType, _ := strconv.ParseInt(in.PositionType, 10, 64)
  355. positionId, _ := strconv.ParseInt(in.PositionId, 10, 64)
  356. wdm := &entity.WorkDesktopMenu{
  357. MenuTree: []*entity.JYMenu{},
  358. UserId: in.UserId, //userId 会取消,职位id
  359. NewUserId: newUserId,
  360. AppId: in.AppId,
  361. EntId: entId,
  362. EntUserId: entUserId,
  363. Platform: in.Platform,
  364. IntranetBool: in.IntranetBool,
  365. WorkStatus: in.WorkStatus,
  366. AccountId: accountId,
  367. EntAccountId: entAccountId,
  368. PositionType: positionType, //职位类型 0:个人 1:企业
  369. PositionId: positionId,
  370. MgoUserId: in.MgoUserId,
  371. EntNicheDis: in.EntNicheDis + 1,
  372. EntUserRole: in.EntUserRole,
  373. }
  374. logx.Info("--------wdm--MgoUserId-------:", wdm.MgoUserId)
  375. //获取菜单树的数据
  376. if err := wdm.GetMenuTreeData(); err != nil {
  377. return nil, err
  378. }
  379. logx.Info("数据长度:", len(wdm.MenuTree), "-- 查询菜单数据耗时:", time.Since(t1))
  380. //jyMenu := wdm.WorkMenuTree(0)
  381. //菜单树生成
  382. wdm.MenuTree = wdm.WorkMenuTree(0)
  383. logx.Info("菜单树生成耗时:", time.Since(t1))
  384. menuList, err = wdm.WorkMenuFormat()
  385. logx.Info("菜单格式化耗时:", time.Since(t1))
  386. if err == nil && len(menuList) > 0 && in.WorkStatus == 0 { //我的页面in.WorkStatus == 2 菜单暂不存缓存
  387. if menuBytes, err := json.Marshal(menuList); err == nil {
  388. redisOutTime := entity.ConfigJson.RedisOutTime + rand.Intn(60)
  389. if redis.PutBytes(entity.RedisCode, RedisMenuKey, &menuBytes, redisOutTime) != nil {
  390. logx.Info("工作桌面菜单 redis缓存异常")
  391. }
  392. } else {
  393. logx.Info("菜单数据序列化异常")
  394. }
  395. }
  396. logx.Info("整体耗时:", time.Since(t1))
  397. return menuList, err
  398. }