sys_api.go 4.8 KB
Newer Older
1
package system
2 3 4

import (
	"errors"
无星夜 已提交
5
	"fmt"
S
songzhibin97 已提交
6

7 8 9
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
10

Mr.奇淼('s avatar
Mr.奇淼( 已提交
11
	"gorm.io/gorm"
12 13
)

m0_50812349's avatar
m0_50812349 已提交
14 15 16 17 18
//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateApi
//@description: 新增基础api
//@param: api model.SysApi
//@return: err error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
19

S
songzhibin97 已提交
20
type ApiService struct{}
21 22 23 24

var ApiServiceApp = new(ApiService)

func (apiService *ApiService) CreateApi(api system.SysApi) (err error) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
25
	if !errors.Is(global.GVA_DB.Where("path = ? AND method = ?", api.Path, api.Method).First(&system.SysApi{}).Error, gorm.ErrRecordNotFound) {
26 27
		return errors.New("存在相同api")
	}
S
songzhibin97 已提交
28
	return global.GVA_DB.Create(&api).Error
29 30
}

m0_50812349's avatar
m0_50812349 已提交
31 32 33 34 35
//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteApi
//@description: 删除基础api
//@param: api model.SysApi
//@return: err error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
36

37
func (apiService *ApiService) DeleteApi(api system.SysApi) (err error) {
Sliver_Horn's avatar
Sliver_Horn 已提交
38 39 40 41 42 43 44 45 46 47 48
	var entity system.SysApi
	err = global.GVA_DB.Where("id = ?", api.ID).First(&entity).Error // 根据id查询api记录
	if errors.Is(err, gorm.ErrRecordNotFound) {                      // api记录不存在
		return err
	}
	err = global.GVA_DB.Delete(&entity).Error
	if err != nil {
		return err
	}
	CasbinServiceApp.ClearCasbin(1, entity.Path, entity.Method)
	return nil
49 50
}

m0_50812349's avatar
m0_50812349 已提交
51 52 53 54
//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetAPIInfoList
//@description: 分页获取数据,
//@param: api model.SysApi, info request.PageInfo, order string, desc bool
Mr.奇淼('s avatar
Mr.奇淼( 已提交
55
//@return: list interface{}, total int64, err error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
56

Mr.奇淼('s avatar
Mr.奇淼( 已提交
57
func (apiService *ApiService) GetAPIInfoList(api system.SysApi, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error) {
58 59
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
60 61
	db := global.GVA_DB.Model(&system.SysApi{})
	var apiList []system.SysApi
62

63 64
	if api.Path != "" {
		db = db.Where("path LIKE ?", "%"+api.Path+"%")
65
	}
66

67 68
	if api.Description != "" {
		db = db.Where("description LIKE ?", "%"+api.Description+"%")
69
	}
70

71 72
	if api.Method != "" {
		db = db.Where("method = ?", api.Method)
73
	}
74

75 76 77 78
	if api.ApiGroup != "" {
		db = db.Where("api_group = ?", api.ApiGroup)
	}

Mr.奇淼('s avatar
Mr.奇淼( 已提交
79
	err = db.Count(&total).Error
80

81
	if err != nil {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
82
		return apiList, total, err
83 84
	} else {
		db = db.Limit(limit).Offset(offset)
85
		if order != "" {
86
			var OrderStr string
87 88 89 90 91 92 93 94 95 96 97 98 99 100
			// 设置有效排序key 防止sql注入
			// 感谢 Tom4t0 提交漏洞信息
			orderMap := make(map[string]bool, 5)
			orderMap["id"] = true
			orderMap["path"] = true
			orderMap["api_group"] = true
			orderMap["description"] = true
			orderMap["method"] = true
			if orderMap[order] {
				if desc {
					OrderStr = order + " desc"
				} else {
					OrderStr = order
				}
无星夜 已提交
101 102
			} else { // didn't matched any order key in `orderMap`
				err = fmt.Errorf("非法的排序字段: %v", order)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
103
				return apiList, total, err
104
			}
105

Mr.奇淼('s avatar
Mr.奇淼( 已提交
106
			err = db.Order(OrderStr).Find(&apiList).Error
107
		} else {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
108
			err = db.Order("api_group").Find(&apiList).Error
109 110
		}
	}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
111
	return apiList, total, err
112 113
}

m0_50812349's avatar
m0_50812349 已提交
114 115 116
//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetAllApis
//@description: 获取所有的api
Mr.奇淼('s avatar
Mr.奇淼( 已提交
117
//@return:  apis []model.SysApi, err error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
118

Mr.奇淼('s avatar
Mr.奇淼( 已提交
119
func (apiService *ApiService) GetAllApis() (apis []system.SysApi, err error) {
120 121 122 123
	err = global.GVA_DB.Find(&apis).Error
	return
}

m0_50812349's avatar
m0_50812349 已提交
124 125 126 127
//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetApiById
//@description: 根据id获取api
//@param: id float64
Mr.奇淼('s avatar
Mr.奇淼( 已提交
128
//@return: api model.SysApi, err error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
129

Mr.奇淼('s avatar
Mr.奇淼( 已提交
130
func (apiService *ApiService) GetApiById(id int) (api system.SysApi, err error) {
131 132 133 134
	err = global.GVA_DB.Where("id = ?", id).First(&api).Error
	return
}

m0_50812349's avatar
m0_50812349 已提交
135 136 137 138 139
//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateApi
//@description: 根据id更新api
//@param: api model.SysApi
//@return: err error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
140

141
func (apiService *ApiService) UpdateApi(api system.SysApi) (err error) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
142
	var oldA system.SysApi
143 144
	err = global.GVA_DB.Where("id = ?", api.ID).First(&oldA).Error
	if oldA.Path != api.Path || oldA.Method != api.Method {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
145
		if !errors.Is(global.GVA_DB.Where("path = ? AND method = ?", api.Path, api.Method).First(&system.SysApi{}).Error, gorm.ErrRecordNotFound) {
146 147 148
			return errors.New("存在相同api路径")
		}
	}
149 150 151
	if err != nil {
		return err
	} else {
152
		err = CasbinServiceApp.UpdateCasbinApi(oldA.Path, api.Path, oldA.Method, api.Method)
153 154 155
		if err != nil {
			return err
		} else {
S
songzhibin97 已提交
156
			err = global.GVA_DB.Save(&api).Error
157 158 159 160
		}
	}
	return err
}
161 162 163 164 165 166 167

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteApis
//@description: 删除选中API
//@param: apis []model.SysApi
//@return: err error

168
func (apiService *ApiService) DeleteApisByIds(ids request.IdsReq) (err error) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
169
	err = global.GVA_DB.Delete(&[]system.SysApi{}, "id in ?", ids.Ids).Error
170
	return err
171
}
S
songzhibin97 已提交
172

173
func (apiService *ApiService) DeleteApiByIds(ids []string) (err error) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
174
	return global.GVA_DB.Delete(&system.SysApi{}, "id in ?", ids).Error
S
songzhibin97 已提交
175
}