workDesktop.go 13 KB

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