API.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package Account
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "git.baozhida.cn/ERP_libs/lib"
  6. "github.com/astaxie/beego/cache"
  7. _ "github.com/astaxie/beego/cache/redis"
  8. "github.com/beego/beego/v2/adapter/orm"
  9. "time"
  10. )
  11. type API struct {
  12. Id int `orm:"column(ID);size(11);auto;pk"`
  13. T_Menu_Id int `orm:"size(11);null"` // 所属菜单id 0 第一级菜单
  14. T_name string `orm:"size(256);null"` // 接口名称
  15. T_uri string `orm:"size(256);null"` // 接口路径
  16. T_method string `orm:"size(256);default('POST')"` // http请求类型 GET,POST,PUT,DELETE 等
  17. T_enable int `orm:"size(2);default(1)"` // 是否启用 0-禁用 1-启用
  18. }
  19. func (t *API) TableName() string {
  20. return "api" // 数据库名称 // ************** 替换 FormulaList **************
  21. }
  22. func NewAPI(orm orm.Ormer, redis cache.Cache) *APIDaoImpl {
  23. return &APIDaoImpl{orm: orm, redis: redis}
  24. }
  25. type APIDaoImpl struct {
  26. orm orm.Ormer
  27. redis cache.Cache
  28. }
  29. func (m *APIDaoImpl) Redis_API_Set(key string, r []API) (err error) {
  30. //json序列化
  31. b, err := json.Marshal(r)
  32. if err != nil {
  33. return
  34. }
  35. err = m.redis.Put(key, b, 24*time.Hour)
  36. return
  37. }
  38. func (m *APIDaoImpl) Redis_API_Get(key string) (r []API, err error) {
  39. if m.redis.IsExist(key) {
  40. //println("找到key:",key)
  41. v := m.redis.Get(key)
  42. err = json.Unmarshal(v.([]byte), &r)
  43. return
  44. }
  45. return r, errors.New("key not exist: " + key)
  46. }
  47. func (m *APIDaoImpl) Redis_API_DelK(key string) (err error) {
  48. return m.redis.Delete(key)
  49. }
  50. // 获取列表
  51. func (m *APIDaoImpl) Read_API_List_ByPower(T_power_id string, Menu_Bind string) (maps []API, err error) {
  52. if maps, err = m.Redis_API_Get(T_power_id); err == nil {
  53. return maps, nil
  54. }
  55. o := orm.NewOrm()
  56. // 也可以直接使用 Model 结构体作为表名
  57. qs := o.QueryTable(new(API))
  58. list := lib.SplitStringIds(Menu_Bind, "M")
  59. _, err = qs.Filter("T_Menu_Id__in", list).All(&maps)
  60. if err != nil {
  61. return
  62. }
  63. m.Redis_API_Set(T_power_id, maps)
  64. return maps, nil
  65. }