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-15 08:08:43 +00:00
|
|
|
CodeEmailNotFoundErr = &StatusResponse{5050, "the email was not found"} // email 不存在
|
2023-06-15 09:51:06 +00:00
|
|
|
CodeUserIdNotFoundErr = &StatusResponse{5051, "the user was not found"} // userid 不存在
|
2023-06-15 08:08:43 +00:00
|
|
|
CodePasswordErr = &StatusResponse{5052, "invalid password"} // 密码错误
|
|
|
|
|
|
|
|
CodeGuestDupErr = &StatusResponse{5010, "the user is already a guest user and does not need to apply again"} // 用户已经是guest用户不需要重复申请 错误
|
|
|
|
CodeGuestGenErr = &StatusResponse{5011, "serialization failed for guest_id of the visitor"} // 游客guest_id序列化失败
|
|
|
|
|
2023-06-15 07:10:22 +00:00
|
|
|
CodeUpdateErr = &StatusResponse{5000, "update database error"} // update database logic 错误
|
|
|
|
CodeDupGuestErr = &StatusResponse{5001, "the user is already a guest user and does not need to apply again"} // 用户已经是guest用户不需要重复申请 错误
|
|
|
|
CodeRequestParamsErr = &StatusResponse{5002, "invalid request param"} // api参数校验 错误
|
2023-06-15 07:10:59 +00:00
|
|
|
CodeDbRecordNotFoundErr = &StatusResponse{5003, "db record not found"}
|
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
|
|
|
|
}
|