workDesktop.go 12 KB

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