123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- 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"
- orm2 "github.com/beego/beego/v2/client/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
- }
- // 也可以直接使用 Model 结构体作为表名
- qs := m.orm.QueryTable(new(API))
- list := lib.SplitStringToIntIds(Menu_Bind, "M")
- AllIds := list
- if err = m.recursiveMenu(list, &AllIds); err != nil {
- return
- }
- _, 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
- }
- // 获取MenuIds下父id
- func (m *APIDaoImpl) recursiveMenu(MenuIds []int, AllIds *[]int) error {
- if len(MenuIds) == 0 {
- return nil
- }
- var subMenus []Menu
- qs := m.orm.QueryTable(new(Menu))
- cond := orm.NewCondition()
- cond = cond.And("Id__in", MenuIds)
- _, err := qs.SetCond((*orm2.Condition)(cond)).OrderBy("T_sort", "Id").All(&subMenus)
- if err != nil {
- return err
- }
- subIds := make([]int, 0)
- for _, menu := range subMenus {
- if menu.T_mid != 0 {
- subIds = append(subIds, menu.T_mid)
- *AllIds = append(*AllIds, menu.T_mid)
- }
- }
- return m.recursiveMenu(subIds, AllIds)
- }
|