workDesktop.go 9.7 KB

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