workDesktop.go 16 KB

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