1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package Account
- import (
- "encoding/json"
- "errors"
- "git.baozhida.cn/ERP_libs/lib"
- "github.com/astaxie/beego/cache"
- _ "github.com/astaxie/beego/cache/redis"
- "github.com/beego/beego/v2/adapter/orm"
- "time"
- )
- type API struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_Menu_Id int `orm:"size(11);null"` // 所属菜单id 0 第一级菜单
- T_name string `orm:"size(256);null"` // 接口名称
- T_uri string `orm:"size(256);null"` // 接口路径
- T_method string `orm:"size(256);default('POST')"` // http请求类型 GET,POST,PUT,DELETE 等
- T_enable int `orm:"size(2);default(1)"` // 是否启用 0-禁用 1-启用
- }
- func (t *API) TableName() string {
- return "api" // 数据库名称 // ************** 替换 FormulaList **************
- }
- func NewAPI(orm orm.Ormer, redis cache.Cache) *APIDaoImpl {
- return &APIDaoImpl{orm: orm, redis: redis}
- }
- type APIDaoImpl struct {
- orm orm.Ormer
- redis cache.Cache
- }
- func (m *APIDaoImpl) Redis_API_Set(key string, r []API) (err error) {
- //json序列化
- b, err := json.Marshal(r)
- if err != nil {
- return
- }
- err = m.redis.Put(key, b, 24*time.Hour)
- return
- }
- func (m *APIDaoImpl) Redis_API_Get(key string) (r []API, err error) {
- if m.redis.IsExist(key) {
- //println("找到key:",key)
- v := m.redis.Get(key)
- err = json.Unmarshal(v.([]byte), &r)
- return
- }
- return r, errors.New("key not exist: " + key)
- }
- func (m *APIDaoImpl) Redis_API_DelK(key string) (err error) {
- return m.redis.Delete(key)
- }
- // 获取列表
- func (m *APIDaoImpl) Read_API_List_ByPower(T_power_id string, Menu_Bind string) (maps []API, err error) {
- if maps, err = m.Redis_API_Get(T_power_id); err == nil {
- return maps, nil
- }
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(API))
- list := lib.SplitStringIds(Menu_Bind, "M")
- _, err = qs.Filter("T_Menu_Id__in", list).All(&maps)
- if err != nil {
- return
- }
- m.Redis_API_Set(T_power_id, maps)
- return maps, nil
- }
|