workDesktop.go 10 KB

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