123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- package service
- import (
- MC "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/redis"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/zeromicro/go-zero/core/logx"
- "strings"
- "time"
- "userCenter/entity"
- "userCenter/rpc/pb"
- . "userCenter/rpc/usercenter"
- )
- //工作桌面--菜单当前选择模式--全部:all/可用:usable
- //工作桌面--常用功能更新
- //工作桌面--常用功能列表
- func RenewWorkDesktopMenuModeOrCommonly(in *WorkDesktopComprehensiveReq, size int, redisOutTime int, bigMemberOff bool) (r *WorkDesktopComprehensiveResp) {
- r = &WorkDesktopComprehensiveResp{}
- switch in.ActionMode {
- case "commonlyRenew": //常用功能更新
- //in.MenuIds 为空;删除
- if in.MenuIds == "" {
- count := entity.BaseMysql.CountBySql(`SELECT COUNT(id) FROM `+entity.WorkCommonly+` WHERE userid = ? AND appid = ? AND platform = ? AND field = ?`, in.UserId, in.AppId, in.Platform, in.ActionMode)
- if count > 0 {
- if count := entity.BaseMysql.UpdateOrDeleteBySql(`DELETE FROM `+entity.WorkCommonly+` WHERE userid = ? AND appid = ? AND platform = ? AND field = ?`, in.UserId, in.AppId, in.Platform, in.ActionMode); count <= 0 {
- r.ErrorCode = -1
- r.ErrorMsg = "常用功能-清空常用功能异常"
- }
- }
- } else {
- //in.MenuIds 不为空:更新
- if b, m := entity.CommonlyUpdate(in, size); !b {
- r.ErrorCode = -1
- r.ErrorMsg = m
- }
- }
- case "commonlyList":
- //查询常用功能列表
- in.ActionMode = "commonlyRenew"
- existingData := entity.BaseMysql.SelectBySql(`SELECT value FROM `+entity.WorkCommonly+` WHERE userid = ? AND appid = ? AND field = ? AND platform = ? ORDER BY id DESC `, in.UserId, in.AppId, in.ActionMode, in.Platform)
- if existingData != nil && len(*existingData) > 0 {
- eData := (*existingData)[0]
- if MC.ObjToString(eData["value"]) != "" {
- var (
- params []string
- pIds []interface{}
- )
- for _, pv := range strings.Split(MC.ObjToString(eData["value"]), ",") {
- params = append(params, "?")
- pIds = append(pIds, pv)
- }
- menuData := entity.BaseMysql.SelectBySql(fmt.Sprintf(`SELECT * from %s WHERE id IN (%s) AND status = 0`, entity.WorkMenu, strings.Join(params, ",")), pIds...)
- if menuData != nil && len(*menuData) > 0 {
- var menuList = []*pb.ThreeLevelMenu{}
- var m = &entity.WorkDesktopMenu{
- MenuTree: nil,
- UserId: in.UserId,
- TimeOut: redisOutTime,
- BigMemberOff: bigMemberOff,
- }
- for _, mv := range *menuData {
- //0:默认展示;1:需验证权限,有权限展示;2:需验证权限,无权限也可展示(可用服务无权限不展示)
- usable := m.VerifyPermissions(MC.ObjToString(mv["powerids"]))
- menu := &pb.ThreeLevelMenu{
- Name: MC.ObjToString(mv["name"]),
- Icon: MC.ObjToString(mv["icon"]),
- AppType: MC.ObjToString(mv["apptype"]),
- Usable: MC.If(MC.ObjToString(mv["powerids"]) != "", usable && MC.IntAll(mv["checkcode"]) == 1, true).(bool),
- OpenType: MC.ObjToString(mv["opentype"]),
- }
- switch in.Platform {
- case "WX":
- menu.Url = MC.ObjToString(mv["wxurl"])
- case "APP":
- menu.Url = MC.ObjToString(mv["appurl"])
- default:
- menu.Url = MC.ObjToString(mv["pcurl"])
- }
- if additionalInfo := MC.ObjToString(mv["additionalinfo"]); additionalInfo != "" {
- additional := pb.TipInfo{}
- if json.Unmarshal([]byte(additionalInfo), &additional) == nil {
- menu.TipInfo = &additional
- }
- }
- menuList = append(menuList, menu)
- }
- r.Data = menuList
- }
- }
- }
- case "menuMode": //菜单模式更新
- if in.MenuMode == "" || (in.MenuMode != "usable" && in.MenuMode != "all") {
- r.ErrorCode = -1
- r.ErrorMsg = "菜单模式-参数异常"
- } else {
- menuModes := entity.BaseMysql.SelectBySql(`SELECT id,value FROM `+entity.WorkCommonly+` WHERE appid=? AND userid=? AND field = ? AND platform = ? LIMIT 1`, in.AppId, in.UserId, in.ActionMode, in.Platform)
- if menuModes != nil && len(*menuModes) > 0 {
- menuMode := (*menuModes)[0]
- if MC.ObjToString(menuMode["value"]) != in.MenuMode {
- if entity.BaseMysql.UpdateOrDeleteBySql(`UPDATE `+entity.WorkCommonly+` SET value = ? WHERE id = ?`, in.MenuMode, MC.IntAll(menuMode["id"])) < 0 {
- r.ErrorCode = -1
- r.ErrorMsg = "菜单模式-更新异常"
- }
- }
- } else {
- if entity.BaseMysql.Insert(entity.WorkCommonly, map[string]interface{}{
- "appid": in.AppId,
- "userid": in.UserId,
- "field": in.ActionMode,
- "platform": in.Platform,
- "value": in.MenuMode,
- }) < 0 {
- r.ErrorCode = -1
- r.ErrorMsg = "菜单模式-插入异常"
- }
- }
- }
- }
- return
- }
- //
- func GetWorkDesktopMenuMode(in *WorkDesktopMenuInfoReq) (str string, err error) {
- if in.UserId == "" {
- return "", fmt.Errorf("参数异常")
- }
- str = "all"
- menuModes := entity.BaseMysql.SelectBySql(`SELECT value FROM `+entity.WorkCommonly+` WHERE appid=? AND userid=? AND field = 'menuMode' AND platform = ? LIMIT 1`, in.AppId, in.UserId, in.Platform)
- if menuModes != nil && len(*menuModes) > 0 {
- menuMode := (*menuModes)[0]
- if MC.ObjToString(menuMode["value"]) != "" {
- str = MC.ObjToString(menuMode["value"])
- }
- }
- return
- }
- //获取工作桌面菜单树
- func GetWordDesktopMenuTree(in *WorkDesktopMenuInfoReq, redisOutTime int, bigMemberOff bool) ([]*pb.MenuList, error) {
- t1 := time.Now()
- //redis缓存
- var menuList []*pb.MenuList
- RedisMenuKey := fmt.Sprintf(entity.RedisMenuKey, in.AppId, in.UserId)
- menuBytes, err := redis.GetBytes(entity.RedisCode, RedisMenuKey)
- if err == nil && len(*menuBytes) > 0 {
- if json.Unmarshal(*menuBytes, &menuList) == nil {
- return menuList, nil
- }
- }
- //tidb数据
- menuData := entity.BaseMysql.SelectBySql(`SELECT * FROM `+entity.WorkMenu+` WHERE status=0 AND appid=? ORDER BY id ASC`, in.AppId)
- if menuData == nil || len(*menuData) == 0 {
- return nil, errors.New("查询数据异常")
- }
- wdm := &entity.WorkDesktopMenu{
- MenuTree: []*entity.JYMenu{},
- UserId: in.UserId,
- TimeOut: redisOutTime,
- BigMemberOff: bigMemberOff,
- }
- for _, v := range *menuData {
- menu := &entity.JYMenu{
- Id: MC.IntAll(v["id"]),
- Name: MC.ObjToString(v["name"]),
- OrderId: MC.IntAll(v["orderid"]),
- ParentId: MC.IntAll(v["parentid"]),
- PowerIds: MC.ObjToString(v["powerids"]),
- CheckCode: MC.IntAll(v["checkcode"]),
- Icon: MC.ObjToString(v["icon"]),
- AppType: MC.ObjToString(v["apptype"]),
- OpenType: MC.ObjToString(v["opentype"]),
- }
- switch in.Platform {
- case "WX":
- menu.Url = MC.ObjToString(v["wxurl"])
- case "APP":
- menu.Url = MC.ObjToString(v["appurl"])
- default:
- menu.Url = MC.ObjToString(v["pcurl"])
- }
- if additionalInfo := MC.ObjToString(v["additionalinfo"]); additionalInfo != "" {
- additional := entity.Additional{}
- if json.Unmarshal([]byte(additionalInfo), &additional) == nil {
- menu.AdditionalInfo = additional
- }
- }
- if menu.Id > 0 {
- wdm.MenuTree = append(wdm.MenuTree, menu)
- }
- }
- logx.Info("数据长度:", len(wdm.MenuTree), "-- 查询菜单数据耗时:", time.Since(t1))
- //jyMenu := wdm.WorkMenuTree(0)
- wdm.MenuTree = wdm.WorkMenuTree(0)
- logx.Info("菜单树生成耗时:", time.Since(t1))
- menuList, err = wdm.WorkMenuFormat()
- logx.Info("菜单格式化耗时:", time.Since(t1))
- if err == nil && len(menuList) > 0 {
- if menuBytes, err := json.Marshal(menuList); err == nil {
- if redis.PutBytes(entity.RedisCode, RedisMenuKey, &menuBytes, redisOutTime) != nil {
- logx.Info("工作桌面菜单 redis缓存异常")
- }
- } else {
- logx.Info("菜单数据序列化异常")
- }
- }
- logx.Info("整体耗时:", time.Since(t1))
- return menuList, err
- }
|