feat():learning后台管理项目初始化
This commit is contained in:
99
internal/models/response.go
Normal file
99
internal/models/response.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package models
|
||||
|
||||
// Response 统一响应结构
|
||||
type Response struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// PageResponse 分页响应结构
|
||||
type PageResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
Total int64 `json:"total"`
|
||||
Page int `json:"page"`
|
||||
Size int `json:"size"`
|
||||
}
|
||||
|
||||
// LoginResponse 登录响应
|
||||
type LoginResponse struct {
|
||||
Token string `json:"token"`
|
||||
User User `json:"user"`
|
||||
ExpireAt int64 `json:"expireAt"`
|
||||
}
|
||||
|
||||
// UserListResponse 用户列表响应
|
||||
type UserListResponse struct {
|
||||
Users []User `json:"users"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
// PageTreeResponse 页面树响应
|
||||
type PageTreeResponse struct {
|
||||
Pages []Page `json:"pages"`
|
||||
}
|
||||
|
||||
// SystemConfigResponse 系统配置响应
|
||||
type SystemConfigResponse struct {
|
||||
Configs []SystemConfig `json:"configs"`
|
||||
}
|
||||
|
||||
// 响应码常量
|
||||
const (
|
||||
CodeSuccess = 200
|
||||
CodeError = 500
|
||||
CodeInvalid = 400
|
||||
CodeUnauthorized = 401
|
||||
CodeForbidden = 403
|
||||
CodeNotFound = 404
|
||||
)
|
||||
|
||||
// 响应消息常量
|
||||
const (
|
||||
MsgSuccess = "操作成功"
|
||||
MsgError = "操作失败"
|
||||
MsgInvalid = "参数错误"
|
||||
MsgUnauthorized = "未授权"
|
||||
MsgForbidden = "禁止访问"
|
||||
MsgNotFound = "资源不存在"
|
||||
)
|
||||
|
||||
// NewResponse 创建响应
|
||||
func NewResponse(code int, message string, data interface{}) *Response {
|
||||
return &Response{
|
||||
Code: code,
|
||||
Message: message,
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
// NewSuccessResponse 创建成功响应
|
||||
func NewSuccessResponse(data interface{}) *Response {
|
||||
return &Response{
|
||||
Code: CodeSuccess,
|
||||
Message: MsgSuccess,
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
// NewErrorResponse 创建错误响应
|
||||
func NewErrorResponse(message string) *Response {
|
||||
return &Response{
|
||||
Code: CodeError,
|
||||
Message: message,
|
||||
}
|
||||
}
|
||||
|
||||
// NewPageResponse 创建分页响应
|
||||
func NewPageResponse(data interface{}, total int64, page, size int) *PageResponse {
|
||||
return &PageResponse{
|
||||
Code: CodeSuccess,
|
||||
Message: MsgSuccess,
|
||||
Data: data,
|
||||
Total: total,
|
||||
Page: page,
|
||||
Size: size,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user