39 lines
1.1 KiB
Go
39 lines
1.1 KiB
Go
package handler
|
||
|
||
import "net/http"
|
||
|
||
// Response 统一的 API 响应格式,成功与失败均使用 code、msg、data;接口失败时错误信息通过 msg 返回。
|
||
type Response struct {
|
||
Code int `json:"code"` // 状态码
|
||
Msg string `json:"msg"` // 提示信息(成功可为空或 success,失败时为错误信息)
|
||
Data interface{} `json:"data"` // 数据对象,失败时为 null
|
||
}
|
||
|
||
// SuccessResponse 成功响应
|
||
func SuccessResponse(data interface{}) Response {
|
||
return Response{
|
||
Code: http.StatusOK,
|
||
Msg: "success",
|
||
Data: data,
|
||
}
|
||
}
|
||
|
||
// ErrorResponse 错误响应,错误信息通过 msg 返回
|
||
func ErrorResponse(code int, msg string) Response {
|
||
return Response{
|
||
Code: code,
|
||
Msg: msg,
|
||
Data: nil,
|
||
}
|
||
}
|
||
|
||
// BadRequestResponse 400 错误响应,错误信息通过 msg 返回
|
||
func BadRequestResponse(msg string) Response {
|
||
return ErrorResponse(http.StatusBadRequest, msg)
|
||
}
|
||
|
||
// InternalServerErrorResponse 500 错误响应,错误信息通过 msg 返回
|
||
func InternalServerErrorResponse(msg string) Response {
|
||
return ErrorResponse(http.StatusInternalServerError, msg)
|
||
}
|