workDesktop.go 7.6 KB

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