workDesktop.go 9.1 KB

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