API.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package Account
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "github.com/astaxie/beego/cache"
  6. _ "github.com/astaxie/beego/cache/redis"
  7. "github.com/beego/beego/v2/adapter/orm"
  8. orm2 "github.com/beego/beego/v2/client/orm"
  9. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  10. "time"
  11. )
  12. type API struct {
  13. Id int `orm:"column(ID);size(11);auto;pk"`
  14. T_Menu_Id int `orm:"size(11);null"` // 所属菜单id 0 第一级菜单
  15. T_name string `orm:"size(256);null"` // 接口名称
  16. T_uri string `orm:"size(256);null"` // 接口路径
  17. T_method string `orm:"size(256);default(POST)"` // http请求类型 GET,POST,PUT,DELETE 等
  18. T_enable int `orm:"size(2);default(1)"` // 是否启用 0-禁用 1-启用
  19. }
  20. func (t *API) TableName() string {
  21. return "api" // 数据库名称 // ************** 替换 FormulaList **************
  22. }
  23. func NewAPI(orm orm.Ormer, redis cache.Cache) *APIDaoImpl {
  24. return &APIDaoImpl{orm: orm, redis: redis}
  25. }
  26. type APIDaoImpl struct {
  27. orm orm.Ormer
  28. redis cache.Cache
  29. }
  30. func (m *APIDaoImpl) Redis_API_Set(key string, r []API) (err error) {
  31. //json序列化
  32. b, err := json.Marshal(r)
  33. if err != nil {
  34. return
  35. }
  36. err = m.redis.Put(key, b, 24*time.Hour)
  37. return
  38. }
  39. func (m *APIDaoImpl) Redis_API_Get(key string) (r []API, err error) {
  40. if m.redis.IsExist(key) {
  41. //println("找到key:",key)
  42. v := m.redis.Get(key)
  43. err = json.Unmarshal(v.([]byte), &r)
  44. return
  45. }
  46. return r, errors.New("key not exist: " + key)
  47. }
  48. func (m *APIDaoImpl) Redis_API_DelK(key string) (err error) {
  49. return m.redis.Delete(key)
  50. }
  51. // 获取列表
  52. func (m *APIDaoImpl) Read_API_List_ByPower(T_power_id string, Menu_Bind string) (maps []API, err error) {
  53. if maps, err = m.Redis_API_Get(T_power_id); err == nil {
  54. return maps, nil
  55. }
  56. // 也可以直接使用 Model 结构体作为表名
  57. qs := m.orm.QueryTable(new(API))
  58. list := lib.SplitStringToIntIds(Menu_Bind, "M")
  59. AllIds := list
  60. if err = m.recursiveMenu(list, &AllIds); err != nil {
  61. return
  62. }
  63. _, err = qs.Filter("T_Menu_Id__in", list).All(&maps)
  64. if err != nil {
  65. return
  66. }
  67. m.Redis_API_Set(T_power_id, maps)
  68. return maps, nil
  69. }
  70. // 获取MenuIds下父id
  71. func (m *APIDaoImpl) recursiveMenu(MenuIds []int, AllIds *[]int) error {
  72. if len(MenuIds) == 0 {
  73. return nil
  74. }
  75. var subMenus []Menu
  76. qs := m.orm.QueryTable(new(Menu))
  77. cond := orm.NewCondition()
  78. cond = cond.And("Id__in", MenuIds)
  79. _, err := qs.SetCond((*orm2.Condition)(cond)).OrderBy("T_sort", "Id").All(&subMenus)
  80. if err != nil {
  81. return err
  82. }
  83. subIds := make([]int, 0)
  84. for _, menu := range subMenus {
  85. if menu.T_mid != 0 {
  86. subIds = append(subIds, menu.T_mid)
  87. *AllIds = append(*AllIds, menu.T_mid)
  88. }
  89. }
  90. return m.recursiveMenu(subIds, AllIds)
  91. }