后端重构
This commit is contained in:
+65
-32
@@ -1,47 +1,80 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-kratos/kratos/v2/errors"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Response 统一响应格式
|
||||
// Response 统一响应结构(与 gva 保持一致)
|
||||
type Response struct {
|
||||
Code int `json:"code"`
|
||||
Data interface{} `json:"data"`
|
||||
Msg string `json:"msg"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// Encoder 响应编码器,包装响应为 {code, msg, data} 格式
|
||||
func Encoder(w http.ResponseWriter, r *http.Request, v interface{}) error {
|
||||
if v == nil {
|
||||
v = struct{}{}
|
||||
}
|
||||
reply := Response{
|
||||
Code: 0,
|
||||
Msg: "success",
|
||||
Data: v,
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
return json.NewEncoder(w).Encode(reply)
|
||||
const (
|
||||
ERROR = 7
|
||||
SUCCESS = 0
|
||||
)
|
||||
|
||||
// Result 返回结果
|
||||
func Result(code int, data interface{}, msg string, c *gin.Context) {
|
||||
c.JSON(http.StatusOK, Response{
|
||||
code,
|
||||
data,
|
||||
msg,
|
||||
})
|
||||
}
|
||||
|
||||
// ErrorEncoder 错误编码器,统一错误响应格式
|
||||
func ErrorEncoder(w http.ResponseWriter, r *http.Request, err error) {
|
||||
se := errors.FromError(err)
|
||||
code := int(se.Code)
|
||||
if code >= 400 && code < 500 {
|
||||
code = 7
|
||||
} else if code >= 500 {
|
||||
code = 7
|
||||
}
|
||||
reply := Response{
|
||||
Code: code,
|
||||
Msg: se.Message,
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(int(se.Code))
|
||||
json.NewEncoder(w).Encode(reply)
|
||||
// Ok 成功响应
|
||||
func Ok(c *gin.Context) {
|
||||
Result(SUCCESS, map[string]interface{}{}, "操作成功", c)
|
||||
}
|
||||
|
||||
// OkWithMessage 成功响应带消息
|
||||
func OkWithMessage(message string, c *gin.Context) {
|
||||
Result(SUCCESS, map[string]interface{}{}, message, c)
|
||||
}
|
||||
|
||||
// OkWithData 成功响应带数据
|
||||
func OkWithData(data interface{}, c *gin.Context) {
|
||||
Result(SUCCESS, data, "成功", c)
|
||||
}
|
||||
|
||||
// OkWithDetailed 成功响应带数据和消息
|
||||
func OkWithDetailed(data interface{}, message string, c *gin.Context) {
|
||||
Result(SUCCESS, data, message, c)
|
||||
}
|
||||
|
||||
// Fail 失败响应
|
||||
func Fail(c *gin.Context) {
|
||||
Result(ERROR, map[string]interface{}{}, "操作失败", c)
|
||||
}
|
||||
|
||||
// FailWithMessage 失败响应带消息
|
||||
func FailWithMessage(message string, c *gin.Context) {
|
||||
Result(ERROR, map[string]interface{}{}, message, c)
|
||||
}
|
||||
|
||||
// FailWithDetailed 失败响应带数据和消息
|
||||
func FailWithDetailed(data interface{}, message string, c *gin.Context) {
|
||||
Result(ERROR, data, message, c)
|
||||
}
|
||||
|
||||
// NoAuth 未授权响应
|
||||
func NoAuth(message string, c *gin.Context) {
|
||||
c.JSON(http.StatusUnauthorized, Response{
|
||||
7,
|
||||
nil,
|
||||
message,
|
||||
})
|
||||
}
|
||||
|
||||
// PageResult 分页响应结构
|
||||
type PageResult struct {
|
||||
List interface{} `json:"list"`
|
||||
Total int64 `json:"total"`
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"pageSize"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user