workDesktop.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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, size int, redisOutTime int, bigMemberOff bool) (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 userid = ? AND appid = ? AND platform = ? AND field = ?`, in.UserId, in.AppId, in.Platform, in.ActionMode)
  26. if count > 0 {
  27. if count := entity.BaseMysql.UpdateOrDeleteBySql(`DELETE FROM `+entity.WorkCommonly+` WHERE userid = ? AND appid = ? AND platform = ? AND field = ?`, in.UserId, 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, size); !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 userid = ? AND appid = ? AND field = ? AND platform = ? ORDER BY id DESC `, in.UserId, 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 []interface{}
  49. )
  50. for _, pv := range strings.Split(MC.ObjToString(eData["value"]), ",") {
  51. params = append(params, "?")
  52. pIds = append(pIds, pv)
  53. }
  54. menuData := entity.BaseMysql.SelectBySql(fmt.Sprintf(`SELECT * from %s WHERE id IN (%s) AND status = 0`, entity.WorkMenu, strings.Join(params, ",")), pIds...)
  55. if menuData != nil && len(*menuData) > 0 {
  56. var menuList = []*pb.ThreeLevelMenu{}
  57. var m = &entity.WorkDesktopMenu{
  58. MenuTree: nil,
  59. UserId: in.UserId,
  60. AppId: in.AppId,
  61. TimeOut: redisOutTime,
  62. BigMemberOff: bigMemberOff,
  63. }
  64. for _, mv := range *menuData {
  65. //0:默认展示;1:需验证权限,有权限展示;2:需验证权限,无权限也可展示(可用服务无权限不展示)
  66. usable := m.VerifyPermissions(MC.ObjToString(mv["powerids"]))
  67. menu := &pb.ThreeLevelMenu{
  68. Name: MC.ObjToString(mv["name"]),
  69. Icon: MC.ObjToString(mv["icon"]),
  70. AppType: MC.ObjToString(mv["apptype"]),
  71. Usable: MC.If(MC.ObjToString(mv["powerids"]) != "", usable && MC.IntAll(mv["checkcode"]) == 1, true).(bool),
  72. OpenType: MC.ObjToString(mv["opentype"]),
  73. }
  74. switch in.Platform {
  75. case "WX":
  76. menu.Url = MC.ObjToString(mv["wxurl"])
  77. case "APP":
  78. menu.Url = MC.ObjToString(mv["appurl"])
  79. default:
  80. menu.Url = MC.ObjToString(mv["pcurl"])
  81. }
  82. if additionalInfo := MC.ObjToString(mv["additionalinfo"]); additionalInfo != "" {
  83. additional := pb.TipInfo{}
  84. if json.Unmarshal([]byte(additionalInfo), &additional) == nil {
  85. menu.TipInfo = &additional
  86. }
  87. }
  88. menuList = append(menuList, menu)
  89. }
  90. r.Data = menuList
  91. }
  92. }
  93. }
  94. case "menuMode": //菜单模式更新
  95. if in.MenuMode == "" || (in.MenuMode != "usable" && in.MenuMode != "all") {
  96. r.ErrorCode = -1
  97. r.ErrorMsg = "菜单模式-参数异常"
  98. } else {
  99. menuModes := entity.BaseMysql.SelectBySql(`SELECT id,value FROM `+entity.WorkCommonly+` WHERE appid=? AND userid=? AND field = ? AND platform = ? LIMIT 1`, in.AppId, in.UserId, in.ActionMode, in.Platform)
  100. if menuModes != nil && len(*menuModes) > 0 {
  101. menuMode := (*menuModes)[0]
  102. if MC.ObjToString(menuMode["value"]) != in.MenuMode {
  103. if entity.BaseMysql.UpdateOrDeleteBySql(`UPDATE `+entity.WorkCommonly+` SET value = ? WHERE id = ?`, in.MenuMode, MC.IntAll(menuMode["id"])) < 0 {
  104. r.ErrorCode = -1
  105. r.ErrorMsg = "菜单模式-更新异常"
  106. }
  107. }
  108. } else {
  109. if entity.BaseMysql.Insert(entity.WorkCommonly, map[string]interface{}{
  110. "appid": in.AppId,
  111. "userid": in.UserId,
  112. "field": in.ActionMode,
  113. "platform": in.Platform,
  114. "value": in.MenuMode,
  115. }) < 0 {
  116. r.ErrorCode = -1
  117. r.ErrorMsg = "菜单模式-插入异常"
  118. }
  119. }
  120. }
  121. }
  122. return
  123. }
  124. //
  125. func GetWorkDesktopMenuMode(in *WorkDesktopMenuInfoReq) (str string, err error) {
  126. if in.UserId == "" {
  127. return "", fmt.Errorf("参数异常")
  128. }
  129. str = "all"
  130. menuModes := entity.BaseMysql.SelectBySql(`SELECT value FROM `+entity.WorkCommonly+` WHERE appid=? AND userid=? AND field = 'menuMode' AND platform = ? LIMIT 1`, in.AppId, in.UserId, in.Platform)
  131. if menuModes != nil && len(*menuModes) > 0 {
  132. menuMode := (*menuModes)[0]
  133. if MC.ObjToString(menuMode["value"]) != "" {
  134. str = MC.ObjToString(menuMode["value"])
  135. }
  136. }
  137. return
  138. }
  139. //获取工作桌面菜单树
  140. func GetWordDesktopMenuTree(in *WorkDesktopMenuInfoReq, redisOutTime, internalTime int, bigMemberOff bool) ([]*pb.MenuList, error) {
  141. t1 := time.Now()
  142. //redis缓存
  143. var menuList []*pb.MenuList
  144. RedisMenuKey := fmt.Sprintf(entity.RedisMenuKey, in.AppId, in.UserId)
  145. menuBytes, err := redis.GetBytes(entity.RedisCode, RedisMenuKey)
  146. if err == nil && len(*menuBytes) > 0 {
  147. if json.Unmarshal(*menuBytes, &menuList) == nil {
  148. return menuList, nil
  149. }
  150. }
  151. //tidb数据
  152. menuData := entity.BaseMysql.SelectBySql(`SELECT * FROM `+entity.WorkMenu+` WHERE status=0 AND appid=? ORDER BY id ASC`, in.AppId)
  153. if menuData == nil || len(*menuData) == 0 {
  154. return nil, errors.New("查询数据异常")
  155. }
  156. wdm := &entity.WorkDesktopMenu{
  157. MenuTree: []*entity.JYMenu{},
  158. UserId: in.UserId,
  159. AppId: in.AppId,
  160. TimeOut: internalTime,
  161. BigMemberOff: bigMemberOff,
  162. }
  163. for _, v := range *menuData {
  164. menu := &entity.JYMenu{
  165. Id: MC.IntAll(v["id"]),
  166. Name: MC.ObjToString(v["name"]),
  167. OrderId: MC.IntAll(v["orderid"]),
  168. ParentId: MC.IntAll(v["parentid"]),
  169. PowerIds: MC.ObjToString(v["powerids"]),
  170. CheckCode: MC.IntAll(v["checkcode"]),
  171. Icon: MC.ObjToString(v["icon"]),
  172. AppType: MC.ObjToString(v["apptype"]),
  173. OpenType: MC.ObjToString(v["opentype"]),
  174. }
  175. switch in.Platform {
  176. case "WX":
  177. menu.Url = MC.ObjToString(v["wxurl"])
  178. case "APP":
  179. menu.Url = MC.ObjToString(v["appurl"])
  180. default:
  181. menu.Url = MC.ObjToString(v["pcurl"])
  182. }
  183. if additionalInfo := MC.ObjToString(v["additionalinfo"]); additionalInfo != "" {
  184. additional := entity.Additional{}
  185. if json.Unmarshal([]byte(additionalInfo), &additional) == nil {
  186. menu.AdditionalInfo = additional
  187. }
  188. }
  189. if menu.Id > 0 {
  190. wdm.MenuTree = append(wdm.MenuTree, menu)
  191. }
  192. }
  193. logx.Info("数据长度:", len(wdm.MenuTree), "-- 查询菜单数据耗时:", time.Since(t1))
  194. //jyMenu := wdm.WorkMenuTree(0)
  195. wdm.MenuTree = wdm.WorkMenuTree(0)
  196. logx.Info("菜单树生成耗时:", time.Since(t1))
  197. menuList, err = wdm.WorkMenuFormat()
  198. logx.Info("菜单格式化耗时:", time.Since(t1))
  199. if err == nil && len(menuList) > 0 {
  200. if menuBytes, err := json.Marshal(menuList); err == nil {
  201. redisOutTime += rand.Intn(60 * 60)
  202. if redis.PutBytes(entity.RedisCode, RedisMenuKey, &menuBytes, redisOutTime) != nil {
  203. logx.Info("工作桌面菜单 redis缓存异常")
  204. }
  205. } else {
  206. logx.Info("菜单数据序列化异常")
  207. }
  208. }
  209. logx.Info("整体耗时:", time.Since(t1))
  210. return menuList, err
  211. }