You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.0 KiB

  1. package sysModel
  2. import (
  3. "gin-vue-admin/controller/servers"
  4. "gin-vue-admin/init/qmsql"
  5. "gin-vue-admin/model/modelInterface"
  6. "github.com/jinzhu/gorm"
  7. "github.com/pkg/errors"
  8. )
  9. type SysApi struct {
  10. gorm.Model
  11. Path string `json:"path"`
  12. Description string `json:"description"`
  13. Group string `json:"group"`
  14. Method string `json:"method" gorm:"default:'POST'"`
  15. }
  16. //新增基础api
  17. func (a *SysApi) CreateApi() (err error) {
  18. findOne := qmsql.DEFAULTDB.Where("path = ?", a.Path).Find(&SysMenu{}).Error
  19. if findOne == nil {
  20. return errors.New("存在相同api")
  21. } else {
  22. err = qmsql.DEFAULTDB.Create(a).Error
  23. }
  24. return err
  25. }
  26. //删除基础api
  27. func (a *SysApi) DeleteApi() (err error) {
  28. err = qmsql.DEFAULTDB.Delete(a).Error
  29. new(CasbinModel).clearCasbin(1, a.Path)
  30. return err
  31. }
  32. //更新api
  33. func (a *SysApi) UpdataApi() (err error) {
  34. var oldA SysApi
  35. err = qmsql.DEFAULTDB.Where("id = ?", a.ID).First(&oldA).Error
  36. if err != nil {
  37. return err
  38. } else {
  39. err = new(CasbinModel).CasbinApiUpdata(oldA.Path, a.Path)
  40. if err != nil {
  41. return err
  42. } else {
  43. err = qmsql.DEFAULTDB.Save(a).Error
  44. }
  45. }
  46. return err
  47. }
  48. //获取选中角色所拥有的api
  49. func (a *SysApi) GetApiById(id float64) (err error, api SysApi) {
  50. err = qmsql.DEFAULTDB.Where("id = ?", id).First(&api).Error
  51. return
  52. }
  53. // 获取所有api信息
  54. func (a *SysApi) GetAllApis() (err error, apis []SysApi) {
  55. err = qmsql.DEFAULTDB.Find(&apis).Error
  56. return
  57. }
  58. // 分页获取数据 需要分页实现这个接口即可
  59. func (a *SysApi) GetInfoList(info modelInterface.PageInfo) (err error, list interface{}, total int) {
  60. // 封装分页方法 调用即可 传入 当前的结构体和分页信息
  61. err, db, total := servers.PagingServer(a, info)
  62. if err != nil {
  63. return
  64. } else {
  65. var apiList []SysApi
  66. err = qmsql.DEFAULTDB.Where("path LIKE ?", "%"+a.Path+"%").Find(&apiList).Count(&total).Error
  67. if err!=nil{
  68. return err, apiList, total
  69. }else{
  70. err = db.Order("group", true).Where("path LIKE ?", "%"+a.Path+"%").Find(&apiList).Error
  71. }
  72. return err, apiList, total
  73. }
  74. }