123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- package Account
- import (
- "ERP_user/conf"
- "encoding/json"
- "fmt"
- menulibs "git.baozhida.cn/ERP_libs/Menu"
- "git.baozhida.cn/ERP_libs/lib"
- "github.com/astaxie/beego/cache"
- _ "github.com/astaxie/beego/cache/redis"
- "github.com/astaxie/beego/logs"
- "github.com/beego/beego/v2/adapter/orm"
- "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
- }
|