api.go 4.7 KB
Newer Older
Mr.奇淼('s avatar
Mr.奇淼( 已提交
1 2 3 4 5 6 7
package api

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"main/controller/servers"
	"main/model/dbModel"
8
	"main/model/modelInterface"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
9 10 11 12 13 14 15 16
)

type CreateApiParams struct {
	Path        string `json:"path"`
	Description string `json:"description"`
}

type DeleteApiParams struct {
17
	ID uint `json:"id"`
Mr.奇淼('s avatar
Mr.奇淼( 已提交
18 19 20
}

// @Tags Api
Mr.奇淼('s avatar
Mr.奇淼( 已提交
21
// @Summary 创建基础api
Mr.奇淼('s avatar
Mr.奇淼( 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body api.CreateApiParams true "创建api"
// @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/createApi [post]
func CreateApi(c *gin.Context) {
	var api dbModel.Api
	_ = c.BindJSON(&api)
	err := api.CreateApi()
	if err != nil {
		servers.ReportFormat(c, false, fmt.Sprintf("创建失败:%v", err), gin.H{})
	} else {
		servers.ReportFormat(c, true, "创建成功", gin.H{})
	}
}

// @Tags Api
Mr.奇淼('s avatar
Mr.奇淼( 已提交
40
// @Summary 删除指定api
Mr.奇淼('s avatar
Mr.奇淼( 已提交
41 42 43
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
44
// @Param data body dbModel.Api true "删除api"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
45 46 47 48 49 50 51 52 53 54 55 56
// @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/deleteApi [post]
func DeleteApi(c *gin.Context) {
	var a dbModel.Api
	_ = c.BindJSON(&a)
	err := a.DeleteApi()
	if err != nil {
		servers.ReportFormat(c, false, fmt.Sprintf("删除失败:%v", err), gin.H{})
	} else {
		servers.ReportFormat(c, true, "删除成功", gin.H{})
	}
}
57 58

type AuthAndPathIn struct {
59 60
	AuthorityId string `json:"authorityId"`
	ApiIds      []uint `json:"apiIds"`
61
}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
62

63 64 65 66 67 68 69
// @Tags Api
// @Summary 创建api和角色关系
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body api.AuthAndPathIn true "创建api和角色关系"
// @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
70 71
// @Router /api/setAuthAndApi [post]
func SetAuthAndApi(c *gin.Context) {
72 73
	var authAndPathIn AuthAndPathIn
	_ = c.BindJSON(&authAndPathIn)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
74
	err := new(dbModel.ApiAuthority).SetAuthAndApi(authAndPathIn.AuthorityId, authAndPathIn.ApiIds)
75 76 77 78 79 80 81
	if err != nil {
		servers.ReportFormat(c, false, fmt.Sprintf("添加失败:%v", err), gin.H{})
	} else {
		servers.ReportFormat(c, true, "添加成功", gin.H{})
	}
}

82 83
//条件搜索后端看此api

Mr.奇淼('s avatar
Mr.奇淼( 已提交
84
// @Tags Api
Mr.奇淼('s avatar
Mr.奇淼( 已提交
85
// @Summary 分页获取API列表
86 87 88
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
Mr.奇淼('s avatar
Mr.奇淼( 已提交
89
// @Param data body modelInterface.PageInfo true "分页获取API列表"
90 91 92
// @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/getApiList [post]
func GetApiList(c *gin.Context) {
93 94 95 96 97 98 99 100
	// 此结构体仅本方法使用
	type searchParams struct {
		dbModel.Api
		modelInterface.PageInfo
	}
	var sp searchParams
	_ = c.ShouldBindJSON(&sp)
	err, list, total := sp.Api.GetInfoList(sp.PageInfo)
101 102 103 104
	if err != nil {
		servers.ReportFormat(c, false, fmt.Sprintf("获取数据失败,%v", err), gin.H{})
	} else {
		servers.ReportFormat(c, true, "获取数据成功", gin.H{
Mr.奇淼('s avatar
Mr.奇淼( 已提交
105
			"list":     list,
106
			"total":    total,
107 108
			"page":     sp.PageInfo.Page,
			"pageSize": sp.PageInfo.PageSize,
109 110 111
		})

	}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
112
}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

// @Tags Api
// @Summary 根据id获取api
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body modelInterface.PageInfo true "分页获取用户列表"
// @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/getApiById [post]
func GetApiById(c *gin.Context) {
	var idInfo GetById
	_ = c.BindJSON(&idInfo)
	err, api := new(dbModel.Api).GetApiById(idInfo.Id)
	if err != nil {
		servers.ReportFormat(c, false, fmt.Sprintf("获取数据失败,%v", err), gin.H{})
	} else {
		servers.ReportFormat(c, true, "获取数据成功", gin.H{
130
			"api": api,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143
		})

	}
}

// @Tags Api
// @Summary 创建基础api
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body api.CreateApiParams true "创建api"
// @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/updataApi [post]
144 145 146 147 148 149 150 151 152 153
func UpdataApi(c *gin.Context) {
	var api dbModel.Api
	_ = c.BindJSON(&api)
	err := api.UpdataApi()
	if err != nil {
		servers.ReportFormat(c, false, fmt.Sprintf("修改数据失败,%v", err), gin.H{})
	} else {
		servers.ReportFormat(c, true, "修改数据成功", gin.H{})
	}
}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
154 155 156 157 158 159 160 161

// @Tags Api
// @Summary 获取所有的Api 不分页
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/getAllApis [post]
162 163
func GetAllApis(c *gin.Context) {
	err, apis := new(dbModel.Api).GetAllApis()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
164 165 166 167
	if err != nil {
		servers.ReportFormat(c, false, fmt.Sprintf("获取数据失败,%v", err), gin.H{})
	} else {
		servers.ReportFormat(c, true, "获取数据成功", gin.H{
168
			"apis": apis,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
169 170
		})
	}
171
}