sys_api.go 4.6 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) {
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 95
			} else { // didn't matched any order key in `orderMap`
				err = fmt.Errorf("非法的排序字段: %v", order)
				return err, apiList, total
96
			}
97

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

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

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

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

122
func (apiService *ApiService) GetApiById(id int) (err error, api system.SysApi) {
123 124 125 126
	err = global.GVA_DB.Where("id = ?", id).First(&api).Error
	return
}

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

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

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

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

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