You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

80 lines
1.5 KiB

package dto
// 定义响应结构
type Result struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"` // 修改为实体类型
}
/*成功,没有数据*/
func Success() *Result {
return &Result{
Code: 200,
Message: "success",
Data: nil,
}
}
/*成功,自定义相应信息*/
func SuccessWithMsg(msg string) *Result {
return &Result{
Code: 200,
Message: msg,
Data: nil,
}
}
/*成功,有数据*/
func SuccessWithData(data interface{}) *Result {
return &Result{
Code: 200,
Message: "success",
Data: data,
}
}
/*成功,自定义信息和数据*/
func SuccessWithMsgAndData(msg string, data interface{}) *Result {
return &Result{
Code: 200,
Message: msg,
Data: data,
}
}
/*错误,只有错误信息,错误码0*/
func Error(msg string) *Result {
return &Result{
Code: 400, // 错误码
Message: msg, // 错误信息
Data: nil,
}
}
/*错误,有错误信息,有错误码*/
func ErrorWithCode(code int, msg string) *Result {
return &Result{
Code: code, // 错误码
Message: msg, // 错误信息
Data: nil,
}
}
/*未授权,有错误信息,错误码为401*/
func Unauthorized(msg string) *Result {
return &Result{
Code: 401, // 错误码
Message: msg, // 错误信息
Data: nil,
}
}
/*未授权,有错误信息,允许自定义错误码*/
func UnauthorizedWithCode(code int, msg string) *Result {
return &Result{
Code: code, // 错误码
Message: msg, // 错误信息
Data: nil,
}
}