fusenapi/utils/basic/basic.go

130 lines
4.3 KiB
Go
Raw Normal View History

2023-06-05 09:56:55 +00:00
package basic
// StatusResponse 公司自定义状态码
type StatusResponse struct {
2023-06-20 08:46:56 +00:00
Code int // 状态码
Message string // 状态码相关解析
2023-06-05 09:56:55 +00:00
}
var (
2023-06-07 10:30:58 +00:00
CodeOK = &StatusResponse{200, "success"} // 成功
2023-06-20 08:46:56 +00:00
CodeApiErr = &StatusResponse{500, "api error"} // API错误
CodeSaveErr = &StatusResponse{501, "failed to save"} // 保存失败
2023-06-20 08:46:56 +00:00
CodeServiceErr = &StatusResponse{510, "server logic error"} // 服务逻辑错误
2023-06-07 10:30:58 +00:00
CodeUnAuth = &StatusResponse{401, "unauthorized"} // 未授权
CodeEmailNotFoundErr = &StatusResponse{5050, "email not found"} // 未找到email
CodeUserIdNotFoundErr = &StatusResponse{5051, "user not found"} // 未找到用户
CodePasswordErr = &StatusResponse{5052, "invalid password"} // 无效密码
2023-06-15 08:08:43 +00:00
CodeSafeValueRangeErr = &StatusResponse{5040, "value not in range"} // 值不在范围内
2023-06-19 10:27:31 +00:00
CodeOrderNotFoundErr = &StatusResponse{5030, "order not found"} //未找到订单
CodeOrderNotCancelledErr = &StatusResponse{5031, "current order cannot be cancelled"} // 当前订单无法取消
2023-06-15 08:08:43 +00:00
CodePayNotFoundErr = &StatusResponse{5020, "pay info not found"} // 支付信息无法查询
CodePayCancelOk = &StatusResponse{5021, "cancellation successful"} // 支付取消成功
CodePayCancelNotOk = &StatusResponse{5022, "cancellation failed"} // 支付取消失败
2023-06-20 11:44:31 +00:00
CodeGuestDupErr = &StatusResponse{5010, "user is already guest and does not need to reapply"} // 用户已经是访客用户,无需重复申请
CodeGuestGenErr = &StatusResponse{5011, "serialization failed for guest ID"} // 访客ID序列化失败
2023-06-20 08:46:56 +00:00
CodeDbUpdateErr = &StatusResponse{5000, "database update error"} // 数据库更新错误
CodeRequestParamsErr = &StatusResponse{5002, "invalid request parameters"} // 请求参数无效
CodeDbRecordNotFoundErr = &StatusResponse{5003, "database record not found"} //数据库中未找到记录
CodeDbCreateErr = &StatusResponse{5004, "failed to create record in database"} // 数据库中创建记录失败
CodeDbSqlErr = &StatusResponse{5005, "database error"} // 数据库错误
CodeJsonErr = &StatusResponse{5006, "JSON error"} // JSON解析错误
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
}
2023-06-20 11:52:38 +00:00
switch len(data) {
case 0:
// 0 直接返回
case 1:
2023-06-19 12:10:32 +00:00
if err, ok := data[0].(error); ok {
newResp.Message = err.Error()
} else {
newResp.Data = data[0]
}
2023-06-20 11:52:38 +00:00
case 2:
2023-06-20 11:36:28 +00:00
var dataIndex = 0
if err, ok := data[dataIndex].(error); ok {
newResp.Message = err.Error()
dataIndex++
} else {
panic("data只接收1或2个 data 或 error.Error() 和 data ")
}
newResp.Data = data[1]
2023-06-20 11:52:38 +00:00
default:
2023-06-20 11:36:28 +00:00
panic("data只接收1或2个 data, error.Error() 和 data ")
2023-06-12 09:28:49 +00:00
}
2023-06-20 04:15:14 +00:00
2023-06-12 09:28:49 +00:00
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,
}
2023-06-21 06:26:29 +00:00
switch len(data) {
case 0:
// 0 直接返回
case 1:
2023-06-12 09:28:49 +00:00
newResp.Data = data[0]
2023-06-21 06:26:29 +00:00
default:
2023-06-20 11:36:28 +00:00
panic("data只接收一个 data")
}
2023-06-21 06:26:29 +00:00
2023-06-20 11:36:28 +00:00
return newResp
}
func (resp *Response) SetStatusAddMessage(sr *StatusResponse, msg string, data ...interface{}) *Response {
newResp := &Response{
Code: sr.Code,
Message: sr.Message + ":" + msg,
}
2023-06-21 08:39:55 +00:00
2023-06-21 06:26:29 +00:00
switch len(data) {
case 0:
// 0 直接返回
case 1:
2023-06-20 11:36:28 +00:00
newResp.Data = data[0]
2023-06-21 06:26:29 +00:00
default:
panic("data只接收一个 data")
2023-06-12 09:28:49 +00:00
}
return newResp
}