workDesktop.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package service
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/encrypt"
  5. "app.yhyue.com/moapp/jybase/redis"
  6. "bp.jydev.jianyu360.cn/BaseService/userCenter/entity"
  7. "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb"
  8. . "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/usercenter"
  9. "encoding/json"
  10. "fmt"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. "math/rand"
  13. "strings"
  14. "time"
  15. )
  16. // RenewWorkDesktopMenuModeOrCommonly 工作桌面--菜单当前选择模式--全部:all/可用:usable
  17. //工作桌面--常用功能更新
  18. //工作桌面--常用功能列表
  19. func RenewWorkDesktopMenuModeOrCommonly(in *WorkDesktopComprehensiveReq) (r *WorkDesktopComprehensiveResp) {
  20. r = &WorkDesktopComprehensiveResp{}
  21. switch in.ActionMode {
  22. case "commonlyRenew": //常用功能更新
  23. //in.MenuIds 不为空:更新
  24. if b, m := entity.CommonlyUpdate(in); !b {
  25. r.ErrorCode = -1
  26. r.ErrorMsg = m
  27. }
  28. case "commonlyList":
  29. //查询常用功能列表
  30. in.ActionMode = "commonlyRenew"
  31. //existingData := entity.BaseMysql.SelectBySql(`SELECT value FROM `+entity.WorkCommonly+` WHERE base_userid = ? AND appid = ? AND field = ? AND platform = ? ORDER BY id DESC `, in.NewUserId, in.AppId, in.ActionMode, in.Platform)
  32. //常用功能不再分平台,来自需求
  33. 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)
  34. var (
  35. pIds []string
  36. )
  37. if existingData != nil && len(*existingData) > 0 {
  38. eData := (*existingData)[0]
  39. if MC.ObjToString(eData["value"]) != "" {
  40. for _, pv := range strings.Split(MC.ObjToString(eData["value"]), ",") {
  41. //params = append(params, "?")
  42. if pv == "" {
  43. continue
  44. }
  45. pIds = append(pIds, pv)
  46. }
  47. }
  48. } else {
  49. if entity.ConfigJson.CommonlyIds != "" && len(strings.Split(entity.ConfigJson.CommonlyIds, ",")) > 0 {
  50. var ids []string
  51. for _, v := range strings.Split(entity.ConfigJson.CommonlyIds, ",") {
  52. ids = append(ids, encrypt.SE.EncodeString(v))
  53. }
  54. in.MenuIds = strings.Join(ids, ",")
  55. //初始化 常用功能
  56. if b, _ := entity.CommonlyUpdate(in); b {
  57. pIds = strings.Split(entity.ConfigJson.CommonlyIds, ",")
  58. } else {
  59. logx.Info("初始化常用功能异常")
  60. }
  61. }
  62. }
  63. if len(pIds) > 0 {
  64. //常用功能存储是三级菜单id,需要四级菜单信息也查出来
  65. //会出现 用户设置常用功能,此功能下线,用户设置依然保存的有此功能 查询不能加 AND status = 0
  66. menuData := entity.BaseMysql.SelectBySql(fmt.Sprintf(`SELECT * from %s WHERE id IN (%s) OR (parentid IN (%s) AND status = 0) ORDER BY FIELD(id , %s);`, entity.ConfigJson.WorkTableOut, strings.Join(pIds, ","), strings.Join(pIds, ","), strings.Join(pIds, ",")))
  67. if menuData != nil && len(*menuData) > 0 {
  68. var (
  69. m = &entity.WorkDesktopMenu{
  70. MenuTree: []*entity.JYMenu{},
  71. UserId: in.UserId,
  72. NewUserId: in.NewUserId,
  73. AppId: in.AppId,
  74. Platform: in.Platform,
  75. EntId: in.EntId,
  76. EntUserId: in.EntUserId,
  77. //IntranetBool: in.IntranetBool,
  78. }
  79. childMenus = map[int][]*entity.JYMenu{}
  80. )
  81. //是否存在父级
  82. parentIsExists := map[int]bool{}
  83. for _, mv := range *menuData {
  84. //id
  85. id := MC.IntAll(mv["id"])
  86. //parentId
  87. parentId := MC.IntAll(mv["parentid"])
  88. parentIsExists[id] = true
  89. menu := &entity.JYMenu{
  90. Id: MC.IntAll(mv["id"]),
  91. Name: MC.ObjToString(mv["name"]),
  92. Match: MC.ObjToString(mv["match"]),
  93. OrderId: MC.IntAll(mv["orderid"]),
  94. ParentId: parentId,
  95. PowerIds: MC.ObjToString(mv["powerids"]),
  96. CheckCode: MC.IntAll(mv["checkcode"]),
  97. Icon: MC.ObjToString(mv["icon"]),
  98. AppType: MC.ObjToString(mv["apptype"]),
  99. OpenType: MC.ObjToString(mv["opentype"]),
  100. Status: MC.IntAll(mv["status"]),
  101. PermissionCode: MC.ObjToString(mv["permissioncode"]),
  102. CapitalCode: MC.ObjToString(mv["capitalcode"]),
  103. Authority: MC.IntAll(mv["authority"]),
  104. Level: MC.IntAll(mv["level"]),
  105. }
  106. var OpenType = map[string]string{}
  107. if err := json.Unmarshal([]byte(MC.ObjToString(mv["opentype"])), &OpenType); err == nil {
  108. menu.OpenType = OpenType[m.Platform]
  109. }
  110. switch in.Platform {
  111. case "WX":
  112. menu.Url = MC.ObjToString(mv["wxurl"])
  113. menu.PowerIds = MC.ObjToString(mv["wxpids"])
  114. case "APP":
  115. menu.Url = MC.ObjToString(mv["appurl"])
  116. menu.PowerIds = MC.ObjToString(mv["apppids"])
  117. default:
  118. menu.Url = MC.ObjToString(mv["pcurl"])
  119. }
  120. if additionalInfo := MC.ObjToString(mv["additionalinfo"]); additionalInfo != "" {
  121. additional := entity.Additional{}
  122. if json.Unmarshal([]byte(additionalInfo), &additional) == nil {
  123. menu.AdditionalInfo = additional
  124. }
  125. }
  126. //外网不一致,
  127. if "work_menu" == entity.ConfigJson.WorkTableOut {
  128. if parentIsExists[parentId] {
  129. childMenus[parentId] = append(childMenus[parentId], menu)
  130. } else {
  131. m.MenuTree = append(m.MenuTree, menu)
  132. }
  133. } else {
  134. if menu.Level == 4 {
  135. childMenus[parentId] = append(childMenus[parentId], menu)
  136. } else {
  137. m.MenuTree = append(m.MenuTree, menu)
  138. }
  139. }
  140. }
  141. //常用功能格式化
  142. menu, saveIds, delBool := m.CommonlyFormat(childMenus)
  143. //某一功能下架,但是用户收藏在常用功能中,需要更新此用户的常用功能数据
  144. if delBool && len(saveIds) > 0 {
  145. in.MenuIds = strings.Join(saveIds, ",")
  146. go func(in *WorkDesktopComprehensiveReq) {
  147. if b, _ := entity.CommonlyUpdate(in); !b {
  148. logx.Info("常用功能-更新数据失败")
  149. }
  150. }(in)
  151. }
  152. r.Data = menu
  153. }
  154. }
  155. case "menuMode": //菜单模式更新
  156. if in.MenuMode == "" || (in.MenuMode != "usable" && in.MenuMode != "all") {
  157. r.ErrorCode = -1
  158. r.ErrorMsg = "菜单模式-参数异常"
  159. } else {
  160. 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)
  161. if menuModes != nil && len(*menuModes) > 0 {
  162. menuMode := (*menuModes)[0]
  163. if MC.ObjToString(menuMode["value"]) != in.MenuMode {
  164. if entity.BaseMysql.UpdateOrDeleteBySql(`UPDATE `+entity.WorkCommonly+` SET value = ? WHERE id = ?`, in.MenuMode, MC.IntAll(menuMode["id"])) < 0 {
  165. r.ErrorCode = -1
  166. r.ErrorMsg = "菜单模式-更新异常"
  167. }
  168. }
  169. } else {
  170. if entity.BaseMysql.Insert(entity.WorkCommonly, map[string]interface{}{
  171. "appid": in.AppId,
  172. "base_userid": in.NewUserId,
  173. "field": in.ActionMode,
  174. "platform": in.Platform,
  175. "value": in.MenuMode,
  176. }) < 0 {
  177. r.ErrorCode = -1
  178. r.ErrorMsg = "菜单模式-插入异常"
  179. }
  180. }
  181. }
  182. }
  183. return
  184. }
  185. // GetWorkDesktopMenuMode 获取工作菜单模式
  186. func GetWorkDesktopMenuMode(in *WorkDesktopMenuInfoReq) (str string, err error) {
  187. if in.UserId == "" {
  188. return "", fmt.Errorf("参数异常")
  189. }
  190. str = "all"
  191. 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)
  192. if menuModes != nil && len(*menuModes) > 0 {
  193. menuMode := (*menuModes)[0]
  194. if MC.ObjToString(menuMode["value"]) != "" {
  195. str = MC.ObjToString(menuMode["value"])
  196. }
  197. }
  198. return
  199. }
  200. // GetWordDesktopMenuTree 获取工作桌面菜单树
  201. func GetWordDesktopMenuTree(in *WorkDesktopMenuInfoReq) ([]*pb.MenuList, error) {
  202. if in.EntId > 0 {
  203. //判断商机管理用户是否切换企业
  204. userEntIdKey := fmt.Sprintf(entity.UserEntIdKey, in.AppId, time.Now().Day(), in.UserId)
  205. redisEntid := redis.GetInt(entity.RedisCode, userEntIdKey)
  206. if int64(redisEntid) > 0 && int64(redisEntid) != in.EntId {
  207. //商机管理用户切换企业---清除用户权限缓存&&清除用户菜单缓存
  208. entity.ClearUserPowerFunc(in.UserId, in.AppId)
  209. }
  210. }
  211. t1 := time.Now()
  212. //redis缓存
  213. var menuList []*pb.MenuList
  214. RedisMenuKey := fmt.Sprintf(entity.RedisMenuKey, in.AppId, in.Platform, in.UserId)
  215. logx.Info("RedisMenuKey:", RedisMenuKey)
  216. menuBytes, err := redis.GetBytes(entity.RedisCode, RedisMenuKey)
  217. if err == nil && len(*menuBytes) > 0 {
  218. if json.Unmarshal(*menuBytes, &menuList) == nil {
  219. return menuList, nil
  220. }
  221. }
  222. wdm := &entity.WorkDesktopMenu{
  223. MenuTree: []*entity.JYMenu{},
  224. UserId: in.UserId,
  225. NewUserId: in.NewUserId,
  226. AppId: in.AppId,
  227. EntId: in.EntId,
  228. EntUserId: in.EntUserId,
  229. Platform: in.Platform,
  230. IntranetBool: in.IntranetBool,
  231. }
  232. //获取菜单树的数据
  233. if err := wdm.GetMenuTreeData(); err != nil {
  234. return nil, err
  235. }
  236. logx.Info("数据长度:", len(wdm.MenuTree), "-- 查询菜单数据耗时:", time.Since(t1))
  237. //jyMenu := wdm.WorkMenuTree(0)
  238. //菜单树生成
  239. wdm.MenuTree = wdm.WorkMenuTree(0)
  240. logx.Info("菜单树生成耗时:", time.Since(t1))
  241. menuList, err = wdm.WorkMenuFormat()
  242. logx.Info("菜单格式化耗时:", time.Since(t1))
  243. if err == nil && len(menuList) > 0 {
  244. if menuBytes, err := json.Marshal(menuList); err == nil {
  245. redisOutTime := entity.ConfigJson.RedisOutTime + rand.Intn(60)
  246. if redis.PutBytes(entity.RedisCode, RedisMenuKey, &menuBytes, redisOutTime) != nil {
  247. logx.Info("工作桌面菜单 redis缓存异常")
  248. }
  249. } else {
  250. logx.Info("菜单数据序列化异常")
  251. }
  252. }
  253. logx.Info("整体耗时:", time.Since(t1))
  254. return menuList, err
  255. }