框架慢慢修改

This commit is contained in:
zhangchao
2024-07-04 19:15:44 +08:00
parent d79e6bdaba
commit 82eadb8921
13 changed files with 152 additions and 96 deletions

81
controllers/response.go Normal file
View File

@@ -0,0 +1,81 @@
/*
@Time : 2020/6/28 22:37
@Author : xuyiqing
@File : response.py
*/
package controllers
import (
"github.com/gin-gonic/gin"
)
const (
Success = 200
BadRequest = 400
Unauthenticated = 401
NoPermisson = 403
NotFund = 404
ServerError = 500
)
type Response struct {
Ctx *gin.Context
}
// 定义基础返回结构体
type JsonResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data,omitempty"`
Pager interface{} `json:"pager,omitempty"`
}
func (resp *Response) Response(data interface{}, pager interface{}) {
resp.Ctx.JSON(Success, JsonResponse{
Code: Success,
Msg: "返回成功",
Data: data,
Pager: pager,
})
}
// 400错误请求
func (resp *Response) BadRequest(msg string) {
resp.Ctx.AbortWithStatusJSON(Success, JsonResponse{
Code: BadRequest,
Msg: msg,
})
}
// 401未登录验证
func (resp *Response) Unauthenticated(msg string) {
resp.Ctx.AbortWithStatusJSON(Success, JsonResponse{
Code: Unauthenticated,
Msg: msg,
})
}
// 403没有权限
func (resp *Response) NoPermisson(msg string) {
resp.Ctx.AbortWithStatusJSON(Success, JsonResponse{
Code: NoPermisson,
Msg: msg,
})
}
// 404资源不存在
func (resp *Response) NotFund(msg string) {
resp.Ctx.AbortWithStatusJSON(Success, JsonResponse{
Code: NotFund,
Msg: msg,
})
}
// 500服务器出错
func (resp *Response) ServerError(msg string) {
resp.Ctx.AbortWithStatusJSON(200, JsonResponse{
Code: ServerError,
Msg: msg,
})
}