workDesktop.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. var (
  33. pIds []string
  34. )
  35. if existingData != nil && len(*existingData) > 0 {
  36. eData := (*existingData)[0]
  37. if MC.ObjToString(eData["value"]) != "" {
  38. for _, pv := range strings.Split(MC.ObjToString(eData["value"]), ",") {
  39. //params = append(params, "?")
  40. if pv == "" {
  41. continue
  42. }
  43. pIds = append(pIds, pv)
  44. }
  45. }
  46. } else {
  47. if entity.ConfigJson.CommonlyIds != "" && len(strings.Split(entity.ConfigJson.CommonlyIds, ",")) > 0 {
  48. var ids []string
  49. for _, v := range strings.Split(entity.ConfigJson.CommonlyIds, ",") {
  50. ids = append(ids, encrypt.SE.EncodeString(v))
  51. }
  52. in.MenuIds = strings.Join(ids, ",")
  53. //初始化 常用功能
  54. if b, _ := entity.CommonlyUpdate(in); b {
  55. pIds = strings.Split(entity.ConfigJson.CommonlyIds, ",")
  56. } else {
  57. logx.Info("初始化常用功能异常")
  58. }
  59. }
  60. }
  61. if len(pIds) > 0 {
  62. //常用功能存储是三级菜单id,需要四级菜单信息也查出来
  63. //会出现 用户设置常用功能,此功能下线,用户设置依然保存的有此功能 查询不能加 AND status = 0
  64. menuData := entity.BaseMysql.SelectBySql(fmt.Sprintf(`SELECT * from %s WHERE ( id IN (%s) OR parentid IN (%s))`, entity.WorkMenu, strings.Join(pIds, ","), strings.Join(pIds, ",")))
  65. if menuData != nil && len(*menuData) > 0 {
  66. var (
  67. m = &entity.WorkDesktopMenu{
  68. MenuTree: []*entity.JYMenu{},
  69. UserId: in.UserId,
  70. NewUserId: in.NewUserId,
  71. AppId: in.AppId,
  72. }
  73. childMenus = map[int][]*entity.JYMenu{}
  74. )
  75. //是否存在父级
  76. parentIsExists := map[int]bool{}
  77. for _, mv := range *menuData {
  78. //id
  79. id := MC.IntAll(mv["id"])
  80. //parentId
  81. parentId := MC.IntAll(mv["parentid"])
  82. parentIsExists[id] = true
  83. menu := &entity.JYMenu{
  84. Id: MC.IntAll(mv["id"]),
  85. Name: MC.ObjToString(mv["name"]),
  86. Match: MC.ObjToString(mv["match"]),
  87. OrderId: MC.IntAll(mv["orderid"]),
  88. ParentId: parentId,
  89. PowerIds: MC.ObjToString(mv["powerids"]),
  90. CheckCode: MC.IntAll(mv["checkcode"]),
  91. Icon: MC.ObjToString(mv["icon"]),
  92. AppType: MC.ObjToString(mv["apptype"]),
  93. OpenType: MC.ObjToString(mv["opentype"]),
  94. Status: MC.IntAll(mv["status"]),
  95. }
  96. switch in.Platform {
  97. case "WX":
  98. menu.Url = MC.ObjToString(mv["wxurl"])
  99. case "APP":
  100. menu.Url = MC.ObjToString(mv["appurl"])
  101. default:
  102. menu.Url = MC.ObjToString(mv["pcurl"])
  103. }
  104. if additionalInfo := MC.ObjToString(mv["additionalinfo"]); additionalInfo != "" {
  105. additional := entity.Additional{}
  106. if json.Unmarshal([]byte(additionalInfo), &additional) == nil {
  107. menu.AdditionalInfo = additional
  108. }
  109. }
  110. if parentIsExists[parentId] {
  111. childMenus[parentId] = append(childMenus[parentId], menu)
  112. } else {
  113. m.MenuTree = append(m.MenuTree, menu)
  114. }
  115. }
  116. //常用功能格式化
  117. menu, saveIds, delBool := m.CommonlyFormat(childMenus)
  118. if delBool && len(saveIds) > 0 {
  119. in.MenuIds = strings.Join(saveIds, ",")
  120. go func(in *WorkDesktopComprehensiveReq) {
  121. if b, _ := entity.CommonlyUpdate(in); !b {
  122. logx.Info("常用功能-更新数据失败")
  123. }
  124. }(in)
  125. }
  126. r.Data = menu
  127. }
  128. }
  129. case "menuMode": //菜单模式更新
  130. if in.MenuMode == "" || (in.MenuMode != "usable" && in.MenuMode != "all") {
  131. r.ErrorCode = -1
  132. r.ErrorMsg = "菜单模式-参数异常"
  133. } else {
  134. menuModes := entity.BaseMysql.SelectBySql(`SELECT id,value FROM `+entity.WorkCommonly+` WHERE appid=? AND base_userid =? AND field = ? AND platform = ? LIMIT 1`, in.AppId, in.NewUserId, in.ActionMode, in.Platform)
  135. if menuModes != nil && len(*menuModes) > 0 {
  136. menuMode := (*menuModes)[0]
  137. if MC.ObjToString(menuMode["value"]) != in.MenuMode {
  138. if entity.BaseMysql.UpdateOrDeleteBySql(`UPDATE `+entity.WorkCommonly+` SET value = ? WHERE id = ?`, in.MenuMode, MC.IntAll(menuMode["id"])) < 0 {
  139. r.ErrorCode = -1
  140. r.ErrorMsg = "菜单模式-更新异常"
  141. }
  142. }
  143. } else {
  144. if entity.BaseMysql.Insert(entity.WorkCommonly, map[string]interface{}{
  145. "appid": in.AppId,
  146. "base_userid": in.NewUserId,
  147. "field": in.ActionMode,
  148. "platform": in.Platform,
  149. "value": in.MenuMode,
  150. }) < 0 {
  151. r.ErrorCode = -1
  152. r.ErrorMsg = "菜单模式-插入异常"
  153. }
  154. }
  155. }
  156. }
  157. return
  158. }
  159. // GetWorkDesktopMenuMode 获取工作菜单模式
  160. func GetWorkDesktopMenuMode(in *WorkDesktopMenuInfoReq) (str string, err error) {
  161. if in.UserId == "" {
  162. return "", fmt.Errorf("参数异常")
  163. }
  164. str = "all"
  165. 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)
  166. if menuModes != nil && len(*menuModes) > 0 {
  167. menuMode := (*menuModes)[0]
  168. if MC.ObjToString(menuMode["value"]) != "" {
  169. str = MC.ObjToString(menuMode["value"])
  170. }
  171. }
  172. return
  173. }
  174. // GetWordDesktopMenuTree 获取工作桌面菜单树
  175. func GetWordDesktopMenuTree(in *WorkDesktopMenuInfoReq) ([]*pb.MenuList, error) {
  176. if in.EntId > 0 {
  177. //判断商机管理用户是否切换企业
  178. userEntIdKey := fmt.Sprintf(entity.UserEntIdKey, in.AppId, time.Now().Day(), in.UserId)
  179. redisEntid := redis.GetInt(entity.RedisCode, userEntIdKey)
  180. if int64(redisEntid) > 0 && int64(redisEntid) != in.EntId {
  181. //商机管理用户切换企业---清除用户权限缓存&&清除用户菜单缓存
  182. entity.ClearUserPowerFunc(in.UserId, in.AppId)
  183. }
  184. }
  185. t1 := time.Now()
  186. //redis缓存
  187. var menuList []*pb.MenuList
  188. RedisMenuKey := fmt.Sprintf(entity.RedisMenuKey, in.AppId, in.Platform, in.UserId)
  189. menuBytes, err := redis.GetBytes(entity.RedisCode, RedisMenuKey)
  190. if err == nil && len(*menuBytes) > 0 {
  191. if json.Unmarshal(*menuBytes, &menuList) == nil {
  192. return menuList, nil
  193. }
  194. }
  195. wdm := &entity.WorkDesktopMenu{
  196. MenuTree: []*entity.JYMenu{},
  197. UserId: in.UserId,
  198. NewUserId: in.NewUserId,
  199. AppId: in.AppId,
  200. EntId: in.EntId,
  201. EntUserId: in.EntUserId,
  202. Platform: in.Platform,
  203. IntranetBool: in.IntranetBool,
  204. }
  205. //获取菜单树的数据
  206. if err := wdm.GetMenuTreeData(); err != nil {
  207. return nil, err
  208. }
  209. logx.Info("数据长度:", len(wdm.MenuTree), "-- 查询菜单数据耗时:", time.Since(t1))
  210. //jyMenu := wdm.WorkMenuTree(0)
  211. //菜单树生成
  212. wdm.MenuTree = wdm.WorkMenuTree(0)
  213. logx.Info("菜单树生成耗时:", time.Since(t1))
  214. menuList, err = wdm.WorkMenuFormat()
  215. logx.Info("菜单格式化耗时:", time.Since(t1))
  216. if err == nil && len(menuList) > 0 {
  217. if menuBytes, err := json.Marshal(menuList); err == nil {
  218. redisOutTime := entity.ConfigJson.RedisOutTime + rand.Intn(60)
  219. if redis.PutBytes(entity.RedisCode, RedisMenuKey, &menuBytes, redisOutTime) != nil {
  220. logx.Info("工作桌面菜单 redis缓存异常")
  221. }
  222. } else {
  223. logx.Info("菜单数据序列化异常")
  224. }
  225. }
  226. logx.Info("整体耗时:", time.Since(t1))
  227. return menuList, err
  228. }