workDesktop.go 9.9 KB

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