workDesktop.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. if 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. // 判断用户身份
  56. var ids []string
  57. var oIds []string // 没有加密的id
  58. var (
  59. entId, _ = strconv.ParseInt(in.EntId, 10, 64)
  60. newUserId, _ = strconv.ParseInt(in.NewUserId, 10, 64)
  61. entUserId, _ = strconv.ParseInt(in.EntUserId, 10, 64)
  62. accountId, _ = strconv.ParseInt(in.AccountId, 10, 64)
  63. entAccountId, _ = strconv.ParseInt(in.EntAccountId, 10, 64)
  64. positionType, _ = strconv.ParseInt(in.PositionType, 10, 64)
  65. positionId, _ = strconv.ParseInt(in.PositionId, 10, 64)
  66. )
  67. userInfoRpc := entity.UserInfoRpc{
  68. AppId: in.AppId,
  69. UserId: in.UserId,
  70. BaseUserId: newUserId,
  71. EntId: entId,
  72. EntUserId: entUserId,
  73. AccountId: accountId,
  74. EntAccountId: entAccountId,
  75. PositionType: positionType,
  76. PositionId: positionId,
  77. MgoUserId: in.MgoUserId,
  78. }
  79. userPowers := userInfoRpc.GetUserPowers()
  80. var subBool bool
  81. for _, v := range strings.Split(entity.ConfigJson.CommonlyIds, ",") {
  82. // p562 我的订阅判断用户身份
  83. if userPowers != nil {
  84. if v == "163" { // 我的订阅(大会员)
  85. if userPowers.Member.Status <= 0 {
  86. continue
  87. } else {
  88. subBool = true
  89. }
  90. } else if v == "165" { // 我的订阅(超级订阅)
  91. if subBool || userPowers.Vip.Status <= 0 {
  92. continue
  93. } else {
  94. subBool = true
  95. }
  96. } else if v == "55" { // 我的订阅(商机管理)新版
  97. if subBool || userPowers.Entniche.Status <= 0 || userPowers.Entniche.IsNew <= 0 {
  98. continue
  99. } else {
  100. subBool = true
  101. }
  102. } else if v == "515" { // 我的订阅(商机管理)老版
  103. if subBool || userPowers.Entniche.Status <= 0 || userPowers.Entniche.IsNew > 0 {
  104. continue
  105. } else {
  106. subBool = true
  107. }
  108. } else if v == "167" { // 我的订阅
  109. if subBool || userPowers.Vip.Status > 0 || userPowers.Member.Status > 0 || userPowers.Entniche.Status > 0 {
  110. continue
  111. }
  112. }
  113. }
  114. oIds = append(oIds, v)
  115. ids = append(ids, encrypt.SE.EncodeString(v))
  116. }
  117. in.MenuIds = strings.Join(ids, ",")
  118. //初始化 常用功能
  119. if b, _ := entity.CommonlyUpdate(in); b {
  120. pIds = oIds
  121. } else {
  122. logx.Info("初始化常用功能异常")
  123. }
  124. }
  125. }
  126. } else { //未登录用户
  127. if entity.ConfigJson.NotLoginCommonlyIds != "" && len(strings.Split(entity.ConfigJson.NotLoginCommonlyIds, ",")) > 0 {
  128. // 判断用户身份
  129. var ids []string
  130. var oIds []string // 没有加密的id
  131. for _, v := range strings.Split(entity.ConfigJson.CommonlyIds, ",") {
  132. oIds = append(oIds, v)
  133. ids = append(ids, encrypt.SE.EncodeString(v))
  134. }
  135. in.MenuIds = strings.Join(ids, ",")
  136. //初始化 常用功能
  137. if b, _ := entity.CommonlyUpdate(in); b {
  138. pIds = oIds
  139. } else {
  140. logx.Info("初始化常用功能异常")
  141. }
  142. }
  143. }
  144. if len(pIds) > 0 {
  145. //P278 身份切换
  146. positionType := ``
  147. switch in.PositionType { //职位类型 0:个人 1:企业
  148. case "1":
  149. positionType = `AND available >= 2`
  150. //P364 企业角色
  151. if in.EntUserRole != "" {
  152. positionType += ` AND (FIND_IN_SET('0',entrole)`
  153. for _, entUserRole := range strings.Split(in.EntUserRole, ",") {
  154. positionType += fmt.Sprintf(` OR FIND_IN_SET('%d',entrole)`, MC.Int64All(entUserRole)+1)
  155. }
  156. positionType += `)`
  157. } else {
  158. positionType += fmt.Sprintf(` AND (FIND_IN_SET('0',entrole) OR FIND_IN_SET('%d',entrole)) `, in.EntNicheDis+1)
  159. }
  160. default:
  161. positionType = `AND available <= 2`
  162. }
  163. //常用功能存储是三级菜单id,需要四级菜单信息也查出来
  164. //会出现 用户设置常用功能,此功能下线,用户设置依然保存的有此功能 查询不能加 AND status = 0
  165. 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, ","))
  166. //外网访问
  167. if entity.ConfigJson.WorkTableInside != entity.ConfigJson.WorkTableOut && !in.IntranetBool {
  168. 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, ","))
  169. }
  170. menuData := entity.BaseMysql.SelectBySql(menuSql)
  171. if menuData != nil && len(*menuData) > 0 {
  172. var (
  173. entId, _ = strconv.ParseInt(in.EntId, 10, 64)
  174. newUserId, _ = strconv.ParseInt(in.NewUserId, 10, 64)
  175. entUserId, _ = strconv.ParseInt(in.EntUserId, 10, 64)
  176. accountId, _ = strconv.ParseInt(in.AccountId, 10, 64)
  177. entAccountId, _ = strconv.ParseInt(in.EntAccountId, 10, 64)
  178. positionType, _ = strconv.ParseInt(in.PositionType, 10, 64)
  179. positionId, _ = strconv.ParseInt(in.PositionId, 10, 64)
  180. m = &entity.WorkDesktopMenu{
  181. MenuTree: []*entity.JYMenu{},
  182. UserId: in.UserId,
  183. NewUserId: newUserId,
  184. AppId: in.AppId,
  185. EntId: entId,
  186. EntUserId: entUserId,
  187. Platform: in.Platform,
  188. AccountId: accountId,
  189. EntAccountId: entAccountId,
  190. PositionType: positionType,
  191. PositionId: positionId,
  192. IntranetBool: in.IntranetBool,
  193. MgoUserId: in.MgoUserId,
  194. }
  195. childMenus = map[int][]*entity.JYMenu{}
  196. )
  197. //是否存在父级
  198. parentIsExists := map[int]bool{}
  199. for _, mv := range *menuData {
  200. //id
  201. id := MC.IntAll(mv["id"])
  202. //parentId
  203. parentId := MC.IntAll(mv["parentid"])
  204. parentIsExists[id] = true
  205. menu := &entity.JYMenu{
  206. Id: MC.IntAll(mv["id"]),
  207. Name: MC.ObjToString(mv["name"]),
  208. Match: MC.ObjToString(mv["match"]),
  209. OrderId: MC.IntAll(mv["orderid"]),
  210. ParentId: parentId,
  211. PowerIds: MC.ObjToString(mv["powerids"]),
  212. CheckCode: MC.IntAll(mv["checkcode"]),
  213. Icon: MC.ObjToString(mv["icon"]),
  214. AppType: MC.ObjToString(mv["apptype"]),
  215. OpenType: MC.ObjToString(mv["opentype"]),
  216. Status: MC.IntAll(mv["status"]),
  217. PermissionCode: MC.ObjToString(mv["permissioncode"]),
  218. CapitalCode: MC.ObjToString(mv["capitalcode"]),
  219. Authority: MC.IntAll(mv["authority"]),
  220. Level: MC.IntAll(mv["level"]),
  221. Exclude: MC.ObjToString(mv["exclude"]),
  222. }
  223. var OpenType = map[string]string{}
  224. if err := json.Unmarshal([]byte(MC.ObjToString(mv["opentype"])), &OpenType); err == nil {
  225. menu.OpenType = OpenType[m.Platform]
  226. }
  227. switch in.Platform {
  228. case "WX":
  229. menu.Url = MC.ObjToString(mv["wxurl"])
  230. menu.PowerIds = MC.ObjToString(mv["wxpids"])
  231. case "APP":
  232. menu.Url = MC.ObjToString(mv["appurl"])
  233. menu.PowerIds = MC.ObjToString(mv["apppids"])
  234. default:
  235. menu.Url = MC.ObjToString(mv["pcurl"])
  236. }
  237. if additionalInfo := MC.ObjToString(mv["additionalinfo"]); additionalInfo != "" {
  238. additional := map[string]entity.Additional{}
  239. if json.Unmarshal([]byte(additionalInfo), &additional) == nil {
  240. menu.AdditionalInfo = map[string]entity.Additional{
  241. m.Platform: additional[m.Platform],
  242. }
  243. }
  244. }
  245. //外网不一致,
  246. if entity.ConfigJson.WorkTableInside != entity.ConfigJson.WorkTableOut {
  247. if parentIsExists[parentId] {
  248. childMenus[parentId] = append(childMenus[parentId], menu)
  249. } else {
  250. m.MenuTree = append(m.MenuTree, menu)
  251. }
  252. } else {
  253. if menu.Level == 4 {
  254. childMenus[parentId] = append(childMenus[parentId], menu)
  255. } else {
  256. m.MenuTree = append(m.MenuTree, menu)
  257. }
  258. }
  259. }
  260. //常用功能格式化
  261. menu, saveIds, delBool := m.CommonlyFormat(childMenus)
  262. //某一功能下架,但是用户收藏在常用功能中,需要更新此用户的常用功能数据
  263. if delBool && len(saveIds) > 0 {
  264. in.MenuIds = strings.Join(saveIds, ",")
  265. go func(in *WorkDesktopComprehensiveReq) {
  266. if b, _ := entity.CommonlyUpdate(in); !b {
  267. logx.Info("常用功能-更新数据失败")
  268. }
  269. }(in)
  270. }
  271. r.Data = menu
  272. }
  273. }
  274. r.CommonlySize = entity.ConfigJson.CommonlySize
  275. case "menuMode": //菜单模式更新
  276. if in.MenuMode == "" || (in.MenuMode != "usable" && in.MenuMode != "all") {
  277. r.ErrorCode = -1
  278. r.ErrorMsg = "菜单模式-参数异常"
  279. } else {
  280. 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)
  281. if menuModes != nil && len(*menuModes) > 0 {
  282. menuMode := (*menuModes)[0]
  283. if MC.ObjToString(menuMode["value"]) != in.MenuMode {
  284. if entity.BaseMysql.UpdateOrDeleteBySql(`UPDATE `+entity.WorkCommonly+` SET value = ? WHERE id = ?`, in.MenuMode, MC.IntAll(menuMode["id"])) < 0 {
  285. r.ErrorCode = -1
  286. r.ErrorMsg = "菜单模式-更新异常"
  287. }
  288. }
  289. } else {
  290. if entity.BaseMysql.Insert(entity.WorkCommonly, map[string]interface{}{
  291. "appid": in.AppId,
  292. "base_userid": in.NewUserId,
  293. "field": in.ActionMode,
  294. "platform": in.Platform,
  295. "value": in.MenuMode,
  296. }) < 0 {
  297. r.ErrorCode = -1
  298. r.ErrorMsg = "菜单模式-插入异常"
  299. }
  300. }
  301. }
  302. }
  303. return
  304. }
  305. // GetWorkDesktopMenuMode 获取工作菜单模式
  306. func GetWorkDesktopMenuMode(in *WorkDesktopMenuInfoReq) (str string, err error) {
  307. if in.UserId == "" {
  308. return "", fmt.Errorf("参数异常")
  309. }
  310. str = "all"
  311. 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)
  312. if menuModes != nil && len(*menuModes) > 0 {
  313. menuMode := (*menuModes)[0]
  314. if MC.ObjToString(menuMode["value"]) != "" {
  315. str = MC.ObjToString(menuMode["value"])
  316. }
  317. }
  318. return
  319. }
  320. // GetWordDesktopMenuTree 获取工作桌面菜单树
  321. func GetWordDesktopMenuTree(in *WorkDesktopMenuInfoReq) ([]*pb.MenuList, error) {
  322. //P278 身份切换 此处逻辑已不需要
  323. //if in.EntId != "" {
  324. // //判断商机管理用户是否切换企业
  325. // userEntIdKey := fmt.Sprintf(entity.UserEntIdKey, in.AppId, time.Now().Day(), in.UserId)
  326. // redisEntId := redis.GetInt(entity.RedisCode, userEntIdKey)
  327. // entId, err := strconv.Atoi(in.EntId)
  328. // if err == nil && int64(redisEntId) >= 0 && redisEntId != entId {
  329. // //商机管理用户切换企业---清除用户权限缓存&&清除用户菜单缓存
  330. // entity.ClearUserPowerFunc(in.UserId, in.AppId, in.EntId)
  331. // }
  332. //}
  333. t1 := time.Now()
  334. //菜单redis缓存
  335. var (
  336. menuList []*pb.MenuList
  337. )
  338. RedisMenuKey := fmt.Sprintf(entity.RedisMenuKey, in.AppId, in.Platform, entity.ConfigJson.MenuCacheKey, in.UserId)
  339. logx.Info("RedisMenuKey:", RedisMenuKey)
  340. menuBytes, err := redis.GetBytes(entity.RedisCode, RedisMenuKey)
  341. if in.WorkStatus == 0 && err == nil && len(*menuBytes) > 0 {
  342. if json.Unmarshal(*menuBytes, &menuList) == nil {
  343. return menuList, nil
  344. }
  345. }
  346. entId, _ := strconv.ParseInt(in.EntId, 10, 64)
  347. newUserId, _ := strconv.ParseInt(in.NewUserId, 10, 64)
  348. entUserId, _ := strconv.ParseInt(in.EntUserId, 10, 64)
  349. accountId, _ := strconv.ParseInt(in.AccountId, 10, 64)
  350. entAccountId, _ := strconv.ParseInt(in.EntAccountId, 10, 64)
  351. positionType, _ := strconv.ParseInt(in.PositionType, 10, 64)
  352. positionId, _ := strconv.ParseInt(in.PositionId, 10, 64)
  353. wdm := &entity.WorkDesktopMenu{
  354. MenuTree: []*entity.JYMenu{},
  355. UserId: in.UserId, //userId 会取消,职位id
  356. NewUserId: newUserId,
  357. AppId: in.AppId,
  358. EntId: entId,
  359. EntUserId: entUserId,
  360. Platform: in.Platform,
  361. IntranetBool: in.IntranetBool,
  362. WorkStatus: in.WorkStatus,
  363. AccountId: accountId,
  364. EntAccountId: entAccountId,
  365. PositionType: positionType, //职位类型 0:个人 1:企业
  366. PositionId: positionId,
  367. MgoUserId: in.MgoUserId,
  368. EntNicheDis: in.EntNicheDis + 1,
  369. EntUserRole: in.EntUserRole,
  370. }
  371. logx.Info("--------wdm--MgoUserId-------:", wdm.MgoUserId)
  372. //获取菜单树的数据
  373. if err := wdm.GetMenuTreeData(); err != nil {
  374. return nil, err
  375. }
  376. logx.Info("数据长度:", len(wdm.MenuTree), "-- 查询菜单数据耗时:", time.Since(t1))
  377. //jyMenu := wdm.WorkMenuTree(0)
  378. //菜单树生成
  379. wdm.MenuTree = wdm.WorkMenuTree(0)
  380. logx.Info("菜单树生成耗时:", time.Since(t1))
  381. menuList, err = wdm.WorkMenuFormat()
  382. logx.Info("菜单格式化耗时:", time.Since(t1))
  383. if err == nil && len(menuList) > 0 && in.WorkStatus == 0 { //我的页面in.WorkStatus == 2 菜单暂不存缓存
  384. if menuBytes, err := json.Marshal(menuList); err == nil {
  385. redisOutTime := entity.ConfigJson.RedisOutTime + rand.Intn(60)
  386. if redis.PutBytes(entity.RedisCode, RedisMenuKey, &menuBytes, redisOutTime) != nil {
  387. logx.Info("工作桌面菜单 redis缓存异常")
  388. }
  389. } else {
  390. logx.Info("菜单数据序列化异常")
  391. }
  392. }
  393. logx.Info("整体耗时:", time.Since(t1))
  394. return menuList, err
  395. }