workDesktop.go 9.1 KB

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