workDesktop.go 7.7 KB

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