workDesktop.go 8.4 KB

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