fusenapi/utils/basic/basic.go

66 lines
1.9 KiB
Go
Raw Normal View History

2023-06-05 09:56:55 +00:00
package basic
// StatusResponse 公司自定义状态码
type StatusResponse struct {
Code int
Message string
}
var (
2023-06-07 10:30:58 +00:00
CodeOK = &StatusResponse{200, "success"} // 成功
CodeApiErr = &StatusResponse{500, "api error"} // api 错误
CodeSaveErr = &StatusResponse{501, "fail to save"} // 储存错误
CodeServiceErr = &StatusResponse{510, "server logic error"} // server logic 错误
CodeUnAuth = &StatusResponse{401, "unauthorized"} // 未授权
2023-06-14 06:05:27 +00:00
CodeUpdateErr = &StatusResponse{5000, "update database error"} // update database logic 错误
CodeRequestParamsErr = &StatusResponse{5001, "invalid request param"} // api参数校验 错误
2023-06-05 09:56:55 +00:00
)
2023-06-12 09:28:49 +00:00
type Response struct {
Code int `json:"code"`
Message string `json:"msg"`
Data interface{} `json:"data"`
}
// Set 设置Response的Code和Message值
func (resp *Response) Set(Code int, Message string) *Response {
return &Response{
Code: Code,
Message: Message,
}
}
// Set 设置整个Response
func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response {
return &Response{
Code: Code,
Message: Message,
Data: Data,
}
}
// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数
func (resp *Response) SetStatus(sr *StatusResponse, data ...interface{}) *Response {
newResp := &Response{
2023-06-12 10:08:34 +00:00
Code: sr.Code,
Message: sr.Message,
2023-06-12 09:28:49 +00:00
}
if len(data) == 1 {
newResp.Data = data[0]
}
return newResp
}
// SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数
func (resp *Response) SetStatusWithMessage(sr *StatusResponse, msg string, data ...interface{}) *Response {
newResp := &Response{
Code: sr.Code,
Message: msg,
}
if len(data) == 1 {
newResp.Data = data[0]
}
return newResp
}