workDesktop.go 11 KB

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