workDesktop.go 11 KB

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