workDesktop.go 8.7 KB

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