123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package system
- import (
- "errors"
- "gorm.io/gorm"
- "server/dao/system"
- "server/global"
- "server/model/common/request"
- )
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: CreateApi
- //@description: 新增基础api
- //@param: api model.SysApi
- //@return: err error
- type ApiService struct{}
- var ApiServiceApp = new(ApiService)
- func (apiService *ApiService) CreateApi(api system.SysApi) (err error) {
- _, err = system.QueryApiByPathMethod(api.Path, api.Method)
- if !errors.Is(err, gorm.ErrRecordNotFound) {
- return errors.New("存在相同api")
- }
- return api.CreateApi()
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: DeleteApi
- //@description: 删除基础api
- //@param: api model.SysApi
- //@return: err error
- func (apiService *ApiService) DeleteApi(api system.SysApi) (err error) {
- entity, err := system.QueryApiById(api.ID)
- if errors.Is(err, gorm.ErrRecordNotFound) { // api记录不存在
- return err
- }
- err = api.DeleteApi()
- if err != nil {
- return err
- }
- CasbinServiceApp.ClearCasbin(1, entity.Path, entity.Method)
- if err != nil {
- return err
- }
- return nil
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: GetAPIInfoList
- //@description: 分页获取数据,
- //@param: api model.SysApi, info request.PageInfo, order string, desc bool
- //@return: list interface{}, total int64, err error
- func (apiService *ApiService) GetAPIInfoList(api system.SysApi, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error) {
- limit := info.PageSize
- offset := info.PageSize * (info.Page - 1)
- return api.GetAPIInfoList(limit, offset, order, desc)
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: GetAllApis
- //@description: 获取所有的api
- //@return: apis []model.SysApi, err error
- func (apiService *ApiService) GetAllApis() (apis []system.SysApi, err error) {
- return system.GetAllApis()
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: GetApiById
- //@description: 根据id获取api
- //@param: id float64
- //@return: api model.SysApi, err error
- func (apiService *ApiService) GetApiById(id int) (api system.SysApi, err error) {
- return system.QueryApiById(uint(id))
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: UpdateApi
- //@description: 根据id更新api
- //@param: api model.SysApi
- //@return: err error
- func (apiService *ApiService) UpdateApi(api system.SysApi) (err error) {
- oldA, err := system.QueryApiById(api.ID)
- if oldA.Path != api.Path || oldA.Method != api.Method {
- var duplicateApi system.SysApi
- duplicateApi, err := system.QueryApiByPathMethod(api.Path, api.Method)
- if err != nil {
- if !errors.Is(err, gorm.ErrRecordNotFound) {
- return err
- }
- }
- if duplicateApi.ID != api.ID {
- return errors.New("存在相同api路径")
- }
- }
- if err != nil {
- return err
- }
- err = CasbinServiceApp.UpdateCasbinApi(oldA.Path, api.Path, oldA.Method, api.Method)
- if err != nil {
- return err
- }
- return api.SaveApi()
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: DeleteApis
- //@description: 删除选中API
- //@param: apis []model.SysApi
- //@return: err error
- func (apiService *ApiService) DeleteApisByIds(ids request.IdsReq) (err error) {
- return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
- var apis []system.SysApi
- err = tx.Find(&apis, "id in ?", ids.Ids).Error
- if err != nil {
- return err
- }
- err = tx.Delete(&[]system.SysApi{}, "id in ?", ids.Ids).Error
- if err != nil {
- return err
- }
- for _, sysApi := range apis {
- CasbinServiceApp.ClearCasbin(1, sysApi.Path, sysApi.Method)
- }
- return err
- })
- }
|