workDesktop.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. MC "app.yhyue.com/moapp/jybase/common"
  9. "app.yhyue.com/moapp/jybase/encrypt"
  10. "app.yhyue.com/moapp/jybase/redis"
  11. "bp.jydev.jianyu360.cn/BaseService/userCenter/entity"
  12. "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb"
  13. . "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/usercenter"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. // RenewWorkDesktopMenuModeOrCommonly 工作桌面--菜单当前选择模式--全部:all/可用:usable
  17. // 工作桌面--常用功能更新
  18. // 工作桌面--常用功能列表
  19. func RenewWorkDesktopMenuModeOrCommonly(in *WorkDesktopComprehensiveReq) (r *WorkDesktopComprehensiveResp) {
  20. r = &WorkDesktopComprehensiveResp{}
  21. switch in.ActionMode {
  22. case "commonlyRenew": //常用功能更新
  23. //in.MenuIds 不为空:更新
  24. if b, m := entity.CommonlyUpdate(in); !b {
  25. r.ErrorCode = -1
  26. r.ErrorMsg = m
  27. }
  28. case "commonlyList":
  29. //查询常用功能列表
  30. in.ActionMode = "commonlyRenew"
  31. var (
  32. pIds []string
  33. )
  34. logx.Info("--------------------:", fmt.Sprintf(`SELECT value FROM `+entity.WorkCommonly+` WHERE base_userid = '%s' AND appid = %s AND field = '%s' AND userid = '%s' ORDER BY id DESC `, in.NewUserId, in.AppId, in.ActionMode, in.UserId))
  35. //常用功能查看
  36. existingData := entity.BaseMysql.SelectBySql(`SELECT value FROM `+entity.WorkCommonly+` WHERE base_userid = ? AND appid = ? AND field = ? AND userid = ? ORDER BY id DESC `, in.NewUserId, in.AppId, in.ActionMode, in.UserId)
  37. if existingData == nil || len(*existingData) == 0 {
  38. //P278原查询逻辑
  39. existingData = entity.BaseMysql.SelectBySql(`SELECT value FROM `+entity.WorkCommonly+` WHERE base_userid = ? AND appid = ? AND field = ? ORDER BY id DESC `, in.NewUserId, in.AppId, in.ActionMode)
  40. }
  41. if existingData != nil && len(*existingData) > 0 {
  42. eData := (*existingData)[0]
  43. if MC.ObjToString(eData["value"]) != "" {
  44. for _, pv := range strings.Split(MC.ObjToString(eData["value"]), ",") {
  45. if pv == "" {
  46. continue
  47. }
  48. pIds = append(pIds, pv)
  49. }
  50. }
  51. } else {
  52. if entity.ConfigJson.CommonlyIds != "" && len(strings.Split(entity.ConfigJson.CommonlyIds, ",")) > 0 {
  53. var ids []string
  54. for _, v := range strings.Split(entity.ConfigJson.CommonlyIds, ",") {
  55. ids = append(ids, encrypt.SE.EncodeString(v))
  56. }
  57. in.MenuIds = strings.Join(ids, ",")
  58. //初始化 常用功能
  59. if b, _ := entity.CommonlyUpdate(in); b {
  60. pIds = strings.Split(entity.ConfigJson.CommonlyIds, ",")
  61. } else {
  62. logx.Info("初始化常用功能异常")
  63. }
  64. }
  65. }
  66. if len(pIds) > 0 {
  67. //P278 身份切换
  68. positionType := ``
  69. switch in.PositionType { //职位类型 0:个人 1:企业
  70. case "1":
  71. positionType = `AND available >= 2`
  72. default:
  73. positionType = `AND available <= 2`
  74. }
  75. //常用功能存储是三级菜单id,需要四级菜单信息也查出来
  76. //会出现 用户设置常用功能,此功能下线,用户设置依然保存的有此功能 查询不能加 AND status = 0
  77. var menuSql = fmt.Sprintf(`SELECT * from %s WHERE (id IN (%s) OR (parentid IN (%s) AND status = 0)) %s ORDER BY FIELD(id , %s);`, entity.ConfigJson.WorkTableInside, strings.Join(pIds, ","), strings.Join(pIds, ","), positionType, strings.Join(pIds, ","))
  78. //外网访问
  79. if entity.ConfigJson.WorkTableInside != entity.ConfigJson.WorkTableOut && !in.IntranetBool {
  80. menuSql = fmt.Sprintf(`SELECT * from %s WHERE (id IN (%s) OR (parentid IN (%s) AND status = 0)) %s ORDER BY FIELD(id , %s);`, entity.ConfigJson.WorkTableOut, strings.Join(pIds, ","), strings.Join(pIds, ","), positionType, strings.Join(pIds, ","))
  81. }
  82. menuData := entity.BaseMysql.SelectBySql(menuSql)
  83. if menuData != nil && len(*menuData) > 0 {
  84. var (
  85. entId, _ = strconv.ParseInt(in.EntId, 10, 64)
  86. newUserId, _ = strconv.ParseInt(in.NewUserId, 10, 64)
  87. entUserId, _ = strconv.ParseInt(in.EntUserId, 10, 64)
  88. accountId, _ = strconv.ParseInt(in.AccountId, 10, 64)
  89. entAccountId, _ = strconv.ParseInt(in.EntAccountId, 10, 64)
  90. positionType, _ = strconv.ParseInt(in.PositionType, 10, 64)
  91. positionId, _ = strconv.ParseInt(in.PositionId, 10, 64)
  92. m = &entity.WorkDesktopMenu{
  93. MenuTree: []*entity.JYMenu{},
  94. UserId: in.UserId,
  95. NewUserId: newUserId,
  96. AppId: in.AppId,
  97. EntId: entId,
  98. EntUserId: entUserId,
  99. Platform: in.Platform,
  100. AccountId: accountId,
  101. EntAccountId: entAccountId,
  102. PositionType: positionType,
  103. PositionId: positionId,
  104. IntranetBool: in.IntranetBool,
  105. MgoUserId: in.MgoUserId,
  106. }
  107. childMenus = map[int][]*entity.JYMenu{}
  108. )
  109. //是否存在父级
  110. parentIsExists := map[int]bool{}
  111. for _, mv := range *menuData {
  112. //id
  113. id := MC.IntAll(mv["id"])
  114. //parentId
  115. parentId := MC.IntAll(mv["parentid"])
  116. parentIsExists[id] = true
  117. menu := &entity.JYMenu{
  118. Id: MC.IntAll(mv["id"]),
  119. Name: MC.ObjToString(mv["name"]),
  120. Match: MC.ObjToString(mv["match"]),
  121. OrderId: MC.IntAll(mv["orderid"]),
  122. ParentId: parentId,
  123. PowerIds: MC.ObjToString(mv["powerids"]),
  124. CheckCode: MC.IntAll(mv["checkcode"]),
  125. Icon: MC.ObjToString(mv["icon"]),
  126. AppType: MC.ObjToString(mv["apptype"]),
  127. OpenType: MC.ObjToString(mv["opentype"]),
  128. Status: MC.IntAll(mv["status"]),
  129. PermissionCode: MC.ObjToString(mv["permissioncode"]),
  130. CapitalCode: MC.ObjToString(mv["capitalcode"]),
  131. Authority: MC.IntAll(mv["authority"]),
  132. Level: MC.IntAll(mv["level"]),
  133. }
  134. var OpenType = map[string]string{}
  135. if err := json.Unmarshal([]byte(MC.ObjToString(mv["opentype"])), &OpenType); err == nil {
  136. menu.OpenType = OpenType[m.Platform]
  137. }
  138. switch in.Platform {
  139. case "WX":
  140. menu.Url = MC.ObjToString(mv["wxurl"])
  141. menu.PowerIds = MC.ObjToString(mv["wxpids"])
  142. case "APP":
  143. menu.Url = MC.ObjToString(mv["appurl"])
  144. menu.PowerIds = MC.ObjToString(mv["apppids"])
  145. default:
  146. menu.Url = MC.ObjToString(mv["pcurl"])
  147. }
  148. if additionalInfo := MC.ObjToString(mv["additionalinfo"]); additionalInfo != "" {
  149. additional := map[string]entity.Additional{}
  150. if json.Unmarshal([]byte(additionalInfo), &additional) == nil {
  151. menu.AdditionalInfo = map[string]entity.Additional{
  152. m.Platform: additional[m.Platform],
  153. }
  154. }
  155. }
  156. //外网不一致,
  157. if entity.ConfigJson.WorkTableInside != entity.ConfigJson.WorkTableOut {
  158. if parentIsExists[parentId] {
  159. childMenus[parentId] = append(childMenus[parentId], menu)
  160. } else {
  161. m.MenuTree = append(m.MenuTree, menu)
  162. }
  163. } else {
  164. if menu.Level == 4 {
  165. childMenus[parentId] = append(childMenus[parentId], menu)
  166. } else {
  167. m.MenuTree = append(m.MenuTree, menu)
  168. }
  169. }
  170. }
  171. //常用功能格式化
  172. menu, saveIds, delBool := m.CommonlyFormat(childMenus)
  173. //某一功能下架,但是用户收藏在常用功能中,需要更新此用户的常用功能数据
  174. if delBool && len(saveIds) > 0 {
  175. in.MenuIds = strings.Join(saveIds, ",")
  176. go func(in *WorkDesktopComprehensiveReq) {
  177. if b, _ := entity.CommonlyUpdate(in); !b {
  178. logx.Info("常用功能-更新数据失败")
  179. }
  180. }(in)
  181. }
  182. r.Data = menu
  183. }
  184. }
  185. case "menuMode": //菜单模式更新
  186. if in.MenuMode == "" || (in.MenuMode != "usable" && in.MenuMode != "all") {
  187. r.ErrorCode = -1
  188. r.ErrorMsg = "菜单模式-参数异常"
  189. } else {
  190. menuModes := entity.BaseMysql.SelectBySql(`SELECT id,value FROM `+entity.WorkCommonly+` WHERE appid=? AND base_userid =? AND field = ? LIMIT 1`, in.AppId, in.NewUserId, in.ActionMode)
  191. if menuModes != nil && len(*menuModes) > 0 {
  192. menuMode := (*menuModes)[0]
  193. if MC.ObjToString(menuMode["value"]) != in.MenuMode {
  194. if entity.BaseMysql.UpdateOrDeleteBySql(`UPDATE `+entity.WorkCommonly+` SET value = ? WHERE id = ?`, in.MenuMode, MC.IntAll(menuMode["id"])) < 0 {
  195. r.ErrorCode = -1
  196. r.ErrorMsg = "菜单模式-更新异常"
  197. }
  198. }
  199. } else {
  200. if entity.BaseMysql.Insert(entity.WorkCommonly, map[string]interface{}{
  201. "appid": in.AppId,
  202. "base_userid": in.NewUserId,
  203. "field": in.ActionMode,
  204. "platform": in.Platform,
  205. "value": in.MenuMode,
  206. }) < 0 {
  207. r.ErrorCode = -1
  208. r.ErrorMsg = "菜单模式-插入异常"
  209. }
  210. }
  211. }
  212. }
  213. return
  214. }
  215. // GetWorkDesktopMenuMode 获取工作菜单模式
  216. func GetWorkDesktopMenuMode(in *WorkDesktopMenuInfoReq) (str string, err error) {
  217. if in.UserId == "" {
  218. return "", fmt.Errorf("参数异常")
  219. }
  220. str = "all"
  221. 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)
  222. if menuModes != nil && len(*menuModes) > 0 {
  223. menuMode := (*menuModes)[0]
  224. if MC.ObjToString(menuMode["value"]) != "" {
  225. str = MC.ObjToString(menuMode["value"])
  226. }
  227. }
  228. return
  229. }
  230. // GetWordDesktopMenuTree 获取工作桌面菜单树
  231. func GetWordDesktopMenuTree(in *WorkDesktopMenuInfoReq) ([]*pb.MenuList, error) {
  232. //P278 身份切换 此处逻辑已不需要
  233. //if in.EntId != "" {
  234. // //判断商机管理用户是否切换企业
  235. // userEntIdKey := fmt.Sprintf(entity.UserEntIdKey, in.AppId, time.Now().Day(), in.UserId)
  236. // redisEntId := redis.GetInt(entity.RedisCode, userEntIdKey)
  237. // entId, err := strconv.Atoi(in.EntId)
  238. // if err == nil && int64(redisEntId) >= 0 && redisEntId != entId {
  239. // //商机管理用户切换企业---清除用户权限缓存&&清除用户菜单缓存
  240. // entity.ClearUserPowerFunc(in.UserId, in.AppId, in.EntId)
  241. // }
  242. //}
  243. t1 := time.Now()
  244. //菜单redis缓存
  245. var (
  246. menuList []*pb.MenuList
  247. )
  248. RedisMenuKey := fmt.Sprintf(entity.RedisMenuKey, in.AppId, in.Platform, entity.ConfigJson.MenuCacheKey, in.UserId)
  249. logx.Info("RedisMenuKey:", RedisMenuKey)
  250. menuBytes, err := redis.GetBytes(entity.RedisCode, RedisMenuKey)
  251. if in.WorkStatus == 0 && err == nil && len(*menuBytes) > 0 {
  252. if json.Unmarshal(*menuBytes, &menuList) == nil {
  253. return menuList, nil
  254. }
  255. }
  256. entId, _ := strconv.ParseInt(in.EntId, 10, 64)
  257. newUserId, _ := strconv.ParseInt(in.NewUserId, 10, 64)
  258. entUserId, _ := strconv.ParseInt(in.EntUserId, 10, 64)
  259. accountId, _ := strconv.ParseInt(in.AccountId, 10, 64)
  260. entAccountId, _ := strconv.ParseInt(in.EntAccountId, 10, 64)
  261. positionType, _ := strconv.ParseInt(in.PositionType, 10, 64)
  262. positionId, _ := strconv.ParseInt(in.PositionId, 10, 64)
  263. wdm := &entity.WorkDesktopMenu{
  264. MenuTree: []*entity.JYMenu{},
  265. UserId: in.UserId, //userId 会取消,职位id
  266. NewUserId: newUserId,
  267. AppId: in.AppId,
  268. EntId: entId,
  269. EntUserId: entUserId,
  270. Platform: in.Platform,
  271. IntranetBool: in.IntranetBool,
  272. WorkStatus: in.WorkStatus,
  273. AccountId: accountId,
  274. EntAccountId: entAccountId,
  275. PositionType: positionType, //职位类型 0:个人 1:企业
  276. PositionId: positionId,
  277. MgoUserId: in.MgoUserId,
  278. }
  279. logx.Info("--------wdm--MgoUserId-------:", wdm.MgoUserId)
  280. //获取菜单树的数据
  281. if err := wdm.GetMenuTreeData(); err != nil {
  282. return nil, err
  283. }
  284. logx.Info("数据长度:", len(wdm.MenuTree), "-- 查询菜单数据耗时:", time.Since(t1))
  285. //jyMenu := wdm.WorkMenuTree(0)
  286. //菜单树生成
  287. wdm.MenuTree = wdm.WorkMenuTree(0)
  288. logx.Info("菜单树生成耗时:", time.Since(t1))
  289. menuList, err = wdm.WorkMenuFormat()
  290. logx.Info("菜单格式化耗时:", time.Since(t1))
  291. if err == nil && len(menuList) > 0 && in.WorkStatus == 0 { //我的页面in.WorkStatus == 2 菜单暂不存缓存
  292. /*if menuBytes, err := json.Marshal(menuList); err == nil {
  293. redisOutTime := entity.ConfigJson.RedisOutTime + rand.Intn(60)
  294. if redis.PutBytes(entity.RedisCode, RedisMenuKey, &menuBytes, redisOutTime) != nil {
  295. logx.Info("工作桌面菜单 redis缓存异常")
  296. }
  297. } else {
  298. logx.Info("菜单数据序列化异常")
  299. }*/
  300. }
  301. logx.Info("整体耗时:", time.Since(t1))
  302. return menuList, err
  303. }