92 lines
2.2 KiB
Go
92 lines
2.2 KiB
Go
package response
|
|
|
|
const (
|
|
SuccessCode = "8000"
|
|
SystemErrorCode = "8010"
|
|
ErrorCode = "8020"
|
|
ErrorUserNotExist = "8030" // 用户不存在
|
|
ErrorRedEnvelopeOver = "8080" // 红包已抢完
|
|
ErrorRedEnvelopeOverdue = "8081" // 红包已过期
|
|
ErrorUserNotActive = "8040" // 未激活
|
|
DiamondNotEnough = "8050" // 账户余额不足
|
|
HaveDispatchingOrder = "8031" // 有派单中的订单
|
|
AccessTokenInvalid = "8100" //ACCESSTOKEN无效
|
|
ChangedDeviceCode = "8038" // 切换了设备登录
|
|
NeedUserToAuth = "8037" // 需要身份认证
|
|
CantKickKing = "8039" // 不能踢国王
|
|
)
|
|
|
|
type BaseResponse struct {
|
|
Code string
|
|
Result any
|
|
Msg string
|
|
// Ext any `json:"Ext,omitempty"`
|
|
}
|
|
|
|
type EmptyResponse struct {
|
|
}
|
|
|
|
type PageResponse struct {
|
|
List any
|
|
Total int64
|
|
}
|
|
|
|
func (r *BaseResponse) Success() bool {
|
|
return r.Code == SuccessCode
|
|
}
|
|
|
|
// 构造API错误结果
|
|
func GetErrorResponse(code, errMsg string) BaseResponse {
|
|
response := BaseResponse{Code: code, Msg: errMsg}
|
|
return response
|
|
}
|
|
|
|
// 构造API错误结果
|
|
func GetFailed(errMsg string) BaseResponse {
|
|
response := BaseResponse{Code: ErrorCode, Msg: errMsg}
|
|
return response
|
|
}
|
|
|
|
// 构造API错误结果
|
|
func GetPageResponse(data any, total int64) BaseResponse {
|
|
return BaseResponse{
|
|
Code: SuccessCode,
|
|
Result: PageResponse{
|
|
List: data,
|
|
Total: total,
|
|
},
|
|
Msg: "",
|
|
}
|
|
}
|
|
|
|
// 构造API错误结果
|
|
func Failed(errMsg string) BaseResponse {
|
|
response := BaseResponse{Code: ErrorCode, Msg: errMsg}
|
|
return response
|
|
}
|
|
|
|
// 构造API正确结果
|
|
func GetSuccessResponse(result any) BaseResponse {
|
|
response := BaseResponse{Code: SuccessCode, Result: result}
|
|
return response
|
|
}
|
|
|
|
// 构造API正确结果
|
|
func GetSuccessMsgResponse(msg string, result any) BaseResponse {
|
|
response := BaseResponse{Code: SuccessCode, Msg: msg, Result: result}
|
|
return response
|
|
}
|
|
|
|
// 构造API正确结果
|
|
func GetEmptyResponse() BaseResponse {
|
|
object := EmptyResponse{}
|
|
response := BaseResponse{Code: SuccessCode, Result: object}
|
|
return response
|
|
}
|
|
|
|
// 获取Result=null 对象不存在
|
|
func GetNullResultResponse() BaseResponse {
|
|
response := BaseResponse{Code: SuccessCode, Result: nil}
|
|
return response
|
|
}
|