63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package utils
|
|
|
|
type IResponse interface {
|
|
IsResponse() bool
|
|
}
|
|
|
|
type Response[T any] struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data T `json:"data,omitempty"`
|
|
Total int `json:"total,omitempty"`
|
|
}
|
|
|
|
func (r *Response[T]) IsResponse() bool {
|
|
return true
|
|
}
|
|
|
|
func Fail(code int, message string) *Response[any] {
|
|
return &Response[any]{
|
|
Code: code,
|
|
Message: message,
|
|
Data: nil,
|
|
}
|
|
}
|
|
|
|
func FailParam(message string) *Response[any] {
|
|
return Fail(400, message)
|
|
}
|
|
|
|
func FailBusiness(message string) *Response[any] {
|
|
return Fail(500, message)
|
|
}
|
|
func NotFound() *Response[any] {
|
|
return Fail(404, "当前访问路径不存在")
|
|
}
|
|
func NotLogin() *Response[any] {
|
|
return Fail(401, "当前暂未登录")
|
|
}
|
|
func PermissionDenied() *Response[any] {
|
|
return Fail(403, "权限不足")
|
|
}
|
|
func SuccessWithMsg(message string) *Response[any] {
|
|
return Success[any](200, message, nil)
|
|
}
|
|
func SuccessWithData[T any](data T) *Response[T] {
|
|
return Success(200, "操作成功", data)
|
|
}
|
|
func SuccessWithTotal[T any](data T, total int) *Response[T] {
|
|
return &Response[T]{
|
|
Code: 200,
|
|
Message: "操作成功",
|
|
Data: data,
|
|
Total: total,
|
|
}
|
|
}
|
|
func Success[T any](code int, message string, data T) *Response[T] {
|
|
return &Response[T]{
|
|
Code: code,
|
|
Message: message,
|
|
Data: data,
|
|
}
|
|
}
|