package Account import ( "ERP_user/conf" "encoding/json" "fmt" "github.com/astaxie/beego/cache" _ "github.com/astaxie/beego/cache/redis" "github.com/astaxie/beego/logs" "github.com/beego/beego/v2/adapter/orm" menulibs "gogs.baozhida.cn/zoie/ERP_libs/Menu" "gogs.baozhida.cn/zoie/ERP_libs/lib" "time" ) var redisCache_API cache.Cache var redisCache_Menu cache.Cache func init() { //注册模型 orm.RegisterModel(new(menulibs.Menu)) orm.RegisterModel(new(menulibs.API)) var err error menuConfig := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`, "redis_User_Menu", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password) fmt.Println(menuConfig) redisCache_Menu, err = cache.NewCache("redis", menuConfig) if err != nil || redisCache_Menu == nil { errMsg := "failed to init redis menu" logs.Error(errMsg, err) panic(errMsg) } apiConfig := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`, "redis_ERP_USER_API", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password) fmt.Println(apiConfig) redisCache_API, err = cache.NewCache("redis", apiConfig) if err != nil || redisCache_API == nil { errMsg := "failed to init redis api" logs.Error(errMsg, err) panic(errMsg) } } func Redis_Munu_Set(T_power_id string, r []menulibs.Menu) (err error) { //json序列化 str, err := json.Marshal(r) if err != nil { logs.Error(lib.FuncName(), err) return } err = redisCache_Menu.Put(T_power_id, str, 24*time.Hour) if err != nil { logs.Error("set key:", T_power_id+",value:", str, err) } return } func Redis_Menu_Get(T_power_id string) (r []menulibs.Menu, is bool) { if redisCache_Menu.IsExist(T_power_id) { //println("找到key:",key) v := redisCache_Menu.Get(T_power_id) err := json.Unmarshal(v.([]byte), &r) if err != nil { logs.Error(lib.FuncName(), err) return r, false } return r, true } return r, false } func Redis_Menu_DelK(T_power_id string) (err error) { err = redisCache_Menu.Delete(T_power_id) if err != nil { logs.Error(lib.FuncName(), err) return } return } func Read_Menu_List() ([]menulibs.Menu, error) { o := orm.NewOrm() MenuDao := menulibs.NewMenu(o) m, err := MenuDao.Read_Menu_List() if err != nil { logs.Error(lib.FuncName(), err) return nil, err } return m, nil } // Read_User_Bind_Menu_List 获取用户绑定的菜单列表 func Read_User_Bind_Menu_List(T_Menu string) ([]menulibs.Menu, error) { o := orm.NewOrm() MenuDao := menulibs.NewMenu(o) m, err := MenuDao.Read_Menu_List_ByPower_T_Menu(T_Menu) if err != nil { logs.Error(lib.FuncName(), err) return nil, err } return m, nil } // 获取列表 func Read_API_List_ByPower(T_power_id string, Menu_Bind string) (maps []menulibs.API) { if len(Menu_Bind) == 0 { return } o := orm.NewOrm() APIDao := menulibs.NewAPI(o, redisCache_API) maps, err := APIDao.Read_API_List_ByPower(T_power_id, Menu_Bind) if err != nil { logs.Error(lib.FuncName(), err) return } return maps }