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

import (
	"errors"
S
songzhibin97 已提交
5

6 7 8
	"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"
9

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

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

19 20 21 22 23 24
type ApiService struct {
}

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) {
38
	err = global.GVA_DB.Delete(&api).Error
39
	CasbinServiceApp.ClearCasbin(1, api.Path, api.Method)
40 41 42
	return err
}

m0_50812349's avatar
m0_50812349 已提交
43 44 45 46 47
//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetAPIInfoList
//@description: 分页获取数据,
//@param: api model.SysApi, info request.PageInfo, order string, desc bool
//@return: err error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
48

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

55 56
	if api.Path != "" {
		db = db.Where("path LIKE ?", "%"+api.Path+"%")
57
	}
58

59 60
	if api.Description != "" {
		db = db.Where("description LIKE ?", "%"+api.Description+"%")
61
	}
62

63 64
	if api.Method != "" {
		db = db.Where("method = ?", api.Method)
65
	}
66

67 68 69 70
	if api.ApiGroup != "" {
		db = db.Where("api_group = ?", api.ApiGroup)
	}

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

73 74 75 76
	if err != nil {
		return err, apiList, total
	} else {
		db = db.Limit(limit).Offset(offset)
77
		if order != "" {
78
			var OrderStr string
79 80 81 82 83 84 85 86 87 88 89 90 91 92
			// 设置有效排序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
				}
93
			}
94

Mr.奇淼('s avatar
Mr.奇淼( 已提交
95
			err = db.Order(OrderStr).Find(&apiList).Error
96
		} else {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
97
			err = db.Order("api_group").Find(&apiList).Error
98 99
		}
	}
100
	return err, apiList, total
101 102
}

m0_50812349's avatar
m0_50812349 已提交
103 104 105 106
//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetAllApis
//@description: 获取所有的api
//@return: err error, apis []model.SysApi
Mr.奇淼('s avatar
Mr.奇淼( 已提交
107

108
func (apiService *ApiService) GetAllApis() (err error, apis []system.SysApi) {
109 110 111 112
	err = global.GVA_DB.Find(&apis).Error
	return
}

m0_50812349's avatar
m0_50812349 已提交
113 114 115 116 117
//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetApiById
//@description: 根据id获取api
//@param: id float64
//@return: err error, api model.SysApi
Mr.奇淼('s avatar
Mr.奇淼( 已提交
118

119
func (apiService *ApiService) GetApiById(id float64) (err error, api system.SysApi) {
120 121 122 123
	err = global.GVA_DB.Where("id = ?", id).First(&api).Error
	return
}

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

130
func (apiService *ApiService) UpdateApi(api system.SysApi) (err error) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
131
	var oldA system.SysApi
132 133
	err = global.GVA_DB.Where("id = ?", api.ID).First(&oldA).Error
	if oldA.Path != api.Path || oldA.Method != api.Method {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
134
		if !errors.Is(global.GVA_DB.Where("path = ? AND method = ?", api.Path, api.Method).First(&system.SysApi{}).Error, gorm.ErrRecordNotFound) {
135 136 137
			return errors.New("存在相同api路径")
		}
	}
138 139 140
	if err != nil {
		return err
	} else {
141
		err = CasbinServiceApp.UpdateCasbinApi(oldA.Path, api.Path, oldA.Method, api.Method)
142 143 144
		if err != nil {
			return err
		} else {
S
songzhibin97 已提交
145
			err = global.GVA_DB.Save(&api).Error
146 147 148 149
		}
	}
	return err
}
150 151 152 153 154 155 156

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

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

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