workDesktop.go 9.6 KB

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