workDesktop.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. logx.Info("in:", in)
  180. if in.EntId > 0 {
  181. //判断商机管理用户是否切换企业
  182. userEntIdKey := fmt.Sprintf(entity.UserEntIdKey, in.AppId, time.Now().Day(), in.UserId)
  183. redisEntid := redis.GetInt(entity.RedisCode, userEntIdKey)
  184. logx.Info(redisEntid, "----EntId:", in.EntId)
  185. if int64(redisEntid) > 0 && int64(redisEntid) != in.EntId {
  186. //商机管理用户切换企业---清除用户权限缓存&&清除用户菜单缓存
  187. entity.ClearUserPowerFunc(in.UserId, in.AppId)
  188. logx.Info("+++++++++++++++++++++++++++++++++++++")
  189. }
  190. }
  191. t1 := time.Now()
  192. //redis缓存
  193. var menuList []*pb.MenuList
  194. RedisMenuKey := fmt.Sprintf(entity.RedisMenuKey, in.AppId, in.Platform, in.UserId)
  195. menuBytes, err := redis.GetBytes(entity.RedisCode, RedisMenuKey)
  196. if err == nil && len(*menuBytes) > 0 {
  197. if json.Unmarshal(*menuBytes, &menuList) == nil {
  198. return menuList, nil
  199. }
  200. }
  201. //tidb数据
  202. query := `SELECT * FROM ` + entity.WorkMenu + ` WHERE appid=? AND status=0 ORDER BY id ASC`
  203. //是否开启内网访问数据权限
  204. if in.IntranetBool {
  205. query = `SELECT * FROM ` + entity.WorkMenu + ` WHERE appid=? AND status>=0 ORDER BY id ASC`
  206. }
  207. menuData := entity.BaseMysql.SelectBySql(query, in.AppId)
  208. if menuData == nil || len(*menuData) == 0 {
  209. return nil, errors.New("查询数据异常")
  210. }
  211. wdm := &entity.WorkDesktopMenu{
  212. MenuTree: []*entity.JYMenu{},
  213. UserId: in.UserId,
  214. NewUserId: in.NewUserId,
  215. AppId: in.AppId,
  216. TimeOut: int(in.InternalTime) + rand.Intn(60), //用户权限 和 企业id
  217. BigMemberOff: in.BigMemberOff,
  218. EntId: in.EntId,
  219. EntUserId: in.EntUserId,
  220. }
  221. logx.Info("-----TimeOut:", wdm.TimeOut, "-----entuserId:", wdm.EntUserId)
  222. for _, v := range *menuData {
  223. menu := &entity.JYMenu{
  224. Id: MC.IntAll(v["id"]),
  225. Name: MC.ObjToString(v["name"]),
  226. Match: MC.ObjToString(v["match"]),
  227. OrderId: MC.IntAll(v["orderid"]),
  228. ParentId: MC.IntAll(v["parentid"]),
  229. PowerIds: MC.ObjToString(v["powerids"]),
  230. CheckCode: MC.IntAll(v["checkcode"]),
  231. Icon: MC.ObjToString(v["icon"]),
  232. AppType: MC.ObjToString(v["apptype"]),
  233. OpenType: MC.ObjToString(v["opentype"]),
  234. Status: MC.IntAll(v["status"]),
  235. PermissionCode: MC.ObjToString(v["permissioncode"]),
  236. CapitalCode: MC.ObjToString(v["capitalcode"]),
  237. }
  238. switch in.Platform {
  239. case "WX":
  240. menu.Url = MC.ObjToString(v["wxurl"])
  241. case "APP":
  242. menu.Url = MC.ObjToString(v["appurl"])
  243. default:
  244. menu.Url = MC.ObjToString(v["pcurl"])
  245. }
  246. if additionalInfo := MC.ObjToString(v["additionalinfo"]); additionalInfo != "" {
  247. additional := entity.Additional{}
  248. if json.Unmarshal([]byte(additionalInfo), &additional) == nil {
  249. menu.AdditionalInfo = additional
  250. }
  251. }
  252. if capitalinfo := MC.ObjToString(v["capitalinfo"]); capitalinfo != "" {
  253. additional := entity.Additional{}
  254. if json.Unmarshal([]byte(capitalinfo), &additional) == nil {
  255. menu.CapitalInfo = additional
  256. }
  257. }
  258. if menu.Id > 0 {
  259. wdm.MenuTree = append(wdm.MenuTree, menu)
  260. }
  261. }
  262. logx.Info("数据长度:", len(wdm.MenuTree), "-- 查询菜单数据耗时:", time.Since(t1))
  263. //jyMenu := wdm.WorkMenuTree(0)
  264. wdm.MenuTree = wdm.WorkMenuTree(0)
  265. logx.Info("菜单树生成耗时:", time.Since(t1))
  266. menuList, err = wdm.WorkMenuFormat()
  267. logx.Info("菜单格式化耗时:", time.Since(t1))
  268. if err == nil && len(menuList) > 0 {
  269. if menuBytes, err := json.Marshal(menuList); err == nil {
  270. redisOutTime := int(in.RedisOutTime) + rand.Intn(60)
  271. logx.Info("-----redisOutTime:", redisOutTime)
  272. if redis.PutBytes(entity.RedisCode, RedisMenuKey, &menuBytes, redisOutTime) != nil {
  273. logx.Info("工作桌面菜单 redis缓存异常")
  274. }
  275. } else {
  276. logx.Info("菜单数据序列化异常")
  277. }
  278. }
  279. logx.Info("整体耗时:", time.Since(t1))
  280. return menuList, err
  281. }