workDesktop.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.WorkMenu, 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. for _, mv := range *menuData {
  82. //parentId
  83. parentId := MC.IntAll(mv["parentid"])
  84. menu := &entity.JYMenu{
  85. Id: MC.IntAll(mv["id"]),
  86. Name: MC.ObjToString(mv["name"]),
  87. Match: MC.ObjToString(mv["match"]),
  88. OrderId: MC.IntAll(mv["orderid"]),
  89. ParentId: parentId,
  90. PowerIds: MC.ObjToString(mv["powerids"]),
  91. CheckCode: MC.IntAll(mv["checkcode"]),
  92. Icon: MC.ObjToString(mv["icon"]),
  93. AppType: MC.ObjToString(mv["apptype"]),
  94. OpenType: MC.ObjToString(mv["opentype"]),
  95. Status: MC.IntAll(mv["status"]),
  96. PermissionCode: MC.ObjToString(mv["permissioncode"]),
  97. CapitalCode: MC.ObjToString(mv["capitalcode"]),
  98. Authority: MC.IntAll(mv["authority"]),
  99. Level: MC.IntAll(mv["level"]),
  100. }
  101. var OpenType = map[string]string{}
  102. if err := json.Unmarshal([]byte(MC.ObjToString(mv["opentype"])), &OpenType); err == nil {
  103. menu.OpenType = OpenType[m.Platform]
  104. }
  105. switch in.Platform {
  106. case "WX":
  107. menu.Url = MC.ObjToString(mv["wxurl"])
  108. menu.PowerIds = MC.ObjToString(mv["wxpids"])
  109. case "APP":
  110. menu.Url = MC.ObjToString(mv["appurl"])
  111. menu.PowerIds = MC.ObjToString(mv["apppids"])
  112. default:
  113. menu.Url = MC.ObjToString(mv["pcurl"])
  114. }
  115. if additionalInfo := MC.ObjToString(mv["additionalinfo"]); additionalInfo != "" {
  116. additional := entity.Additional{}
  117. if json.Unmarshal([]byte(additionalInfo), &additional) == nil {
  118. menu.AdditionalInfo = additional
  119. }
  120. }
  121. if menu.Level == 4 {
  122. childMenus[parentId] = append(childMenus[parentId], menu)
  123. } else {
  124. m.MenuTree = append(m.MenuTree, menu)
  125. }
  126. }
  127. //常用功能格式化
  128. menu, saveIds, delBool := m.CommonlyFormat(childMenus)
  129. //某一功能下架,但是用户收藏在常用功能中,需要更新此用户的常用功能数据
  130. if delBool && len(saveIds) > 0 {
  131. in.MenuIds = strings.Join(saveIds, ",")
  132. go func(in *WorkDesktopComprehensiveReq) {
  133. if b, _ := entity.CommonlyUpdate(in); !b {
  134. logx.Info("常用功能-更新数据失败")
  135. }
  136. }(in)
  137. }
  138. r.Data = menu
  139. }
  140. }
  141. case "menuMode": //菜单模式更新
  142. if in.MenuMode == "" || (in.MenuMode != "usable" && in.MenuMode != "all") {
  143. r.ErrorCode = -1
  144. r.ErrorMsg = "菜单模式-参数异常"
  145. } else {
  146. 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)
  147. if menuModes != nil && len(*menuModes) > 0 {
  148. menuMode := (*menuModes)[0]
  149. if MC.ObjToString(menuMode["value"]) != in.MenuMode {
  150. if entity.BaseMysql.UpdateOrDeleteBySql(`UPDATE `+entity.WorkCommonly+` SET value = ? WHERE id = ?`, in.MenuMode, MC.IntAll(menuMode["id"])) < 0 {
  151. r.ErrorCode = -1
  152. r.ErrorMsg = "菜单模式-更新异常"
  153. }
  154. }
  155. } else {
  156. if entity.BaseMysql.Insert(entity.WorkCommonly, map[string]interface{}{
  157. "appid": in.AppId,
  158. "base_userid": in.NewUserId,
  159. "field": in.ActionMode,
  160. "platform": in.Platform,
  161. "value": in.MenuMode,
  162. }) < 0 {
  163. r.ErrorCode = -1
  164. r.ErrorMsg = "菜单模式-插入异常"
  165. }
  166. }
  167. }
  168. }
  169. return
  170. }
  171. // GetWorkDesktopMenuMode 获取工作菜单模式
  172. func GetWorkDesktopMenuMode(in *WorkDesktopMenuInfoReq) (str string, err error) {
  173. if in.UserId == "" {
  174. return "", fmt.Errorf("参数异常")
  175. }
  176. str = "all"
  177. 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)
  178. if menuModes != nil && len(*menuModes) > 0 {
  179. menuMode := (*menuModes)[0]
  180. if MC.ObjToString(menuMode["value"]) != "" {
  181. str = MC.ObjToString(menuMode["value"])
  182. }
  183. }
  184. return
  185. }
  186. // GetWordDesktopMenuTree 获取工作桌面菜单树
  187. func GetWordDesktopMenuTree(in *WorkDesktopMenuInfoReq) ([]*pb.MenuList, error) {
  188. if in.EntId > 0 {
  189. //判断商机管理用户是否切换企业
  190. userEntIdKey := fmt.Sprintf(entity.UserEntIdKey, in.AppId, time.Now().Day(), in.UserId)
  191. redisEntid := redis.GetInt(entity.RedisCode, userEntIdKey)
  192. if int64(redisEntid) > 0 && int64(redisEntid) != in.EntId {
  193. //商机管理用户切换企业---清除用户权限缓存&&清除用户菜单缓存
  194. entity.ClearUserPowerFunc(in.UserId, in.AppId)
  195. }
  196. }
  197. t1 := time.Now()
  198. //redis缓存
  199. var menuList []*pb.MenuList
  200. RedisMenuKey := fmt.Sprintf(entity.RedisMenuKey, in.AppId, in.Platform, in.UserId)
  201. logx.Info("RedisMenuKey:", RedisMenuKey)
  202. menuBytes, err := redis.GetBytes(entity.RedisCode, RedisMenuKey)
  203. if err == nil && len(*menuBytes) > 0 {
  204. if json.Unmarshal(*menuBytes, &menuList) == nil {
  205. return menuList, nil
  206. }
  207. }
  208. wdm := &entity.WorkDesktopMenu{
  209. MenuTree: []*entity.JYMenu{},
  210. UserId: in.UserId,
  211. NewUserId: in.NewUserId,
  212. AppId: in.AppId,
  213. EntId: in.EntId,
  214. EntUserId: in.EntUserId,
  215. Platform: in.Platform,
  216. IntranetBool: in.IntranetBool,
  217. }
  218. //获取菜单树的数据
  219. if err := wdm.GetMenuTreeData(); err != nil {
  220. return nil, err
  221. }
  222. logx.Info("数据长度:", len(wdm.MenuTree), "-- 查询菜单数据耗时:", time.Since(t1))
  223. //jyMenu := wdm.WorkMenuTree(0)
  224. //菜单树生成
  225. wdm.MenuTree = wdm.WorkMenuTree(0)
  226. logx.Info("菜单树生成耗时:", time.Since(t1))
  227. menuList, err = wdm.WorkMenuFormat()
  228. logx.Info("菜单格式化耗时:", time.Since(t1))
  229. if err == nil && len(menuList) > 0 {
  230. if menuBytes, err := json.Marshal(menuList); err == nil {
  231. redisOutTime := entity.ConfigJson.RedisOutTime + rand.Intn(60)
  232. if redis.PutBytes(entity.RedisCode, RedisMenuKey, &menuBytes, redisOutTime) != nil {
  233. logx.Info("工作桌面菜单 redis缓存异常")
  234. }
  235. } else {
  236. logx.Info("菜单数据序列化异常")
  237. }
  238. }
  239. logx.Info("整体耗时:", time.Since(t1))
  240. return menuList, err
  241. }