fusenapi/utils/basic/basic.go
2023-06-21 16:39:55 +08:00

130 lines
4.2 KiB
Go

package basic
// StatusResponse 公司自定义状态码
type StatusResponse struct {
Code int // 状态码
Message string // 状态码相关解析
}
var (
CodeOK = &StatusResponse{200, "success"} // 成功
CodeApiErr = &StatusResponse{500, "api error"} // API错误
CodeSaveErr = &StatusResponse{501, "fail to save"} // 保存失败
CodeServiceErr = &StatusResponse{510, "server logic error"} // 服务逻辑错误
CodeUnAuth = &StatusResponse{401, "unauthorized"} // 未授权
CodeEmailNotFoundErr = &StatusResponse{5050, "the email was not found"} // 未找到email
CodeUserIdNotFoundErr = &StatusResponse{5051, "the user was not found"} // 未找到用户
CodePasswordErr = &StatusResponse{5052, "invalid password"} // 无效密码
CodeSafeValueRangeErr = &StatusResponse{5040, "the value was not in values"} // 值不在范围内
CodeOrderNotFoundErr = &StatusResponse{5030, "the order not found"} //未找到订单
CodeOrderNotCancelledErr = &StatusResponse{5031, "The current order cannot be cancelled"} // 当前订单无法取消
CodePayNotFoundErr = &StatusResponse{5020, "The pay not found"} // 支付信息无法查询
CodePayCancelOk = &StatusResponse{5021, "Cancel successfully"} // 支付取消
CodePayCancelNotOk = &StatusResponse{5022, "Cancel failure"} // 支付取消
CodeGuestDupErr = &StatusResponse{5010, "the user is already a guest user and does not need to apply again"} // 用户已经是访客用户,不需要重复申请
CodeGuestGenErr = &StatusResponse{5011, "serialization failed for guest_id of the visitor"} // 访客ID序列化失败
CodeDbUpdateErr = &StatusResponse{5000, "update database error"} // 数据库更新错误
// 5001
CodeRequestParamsErr = &StatusResponse{5002, "invalid request param"} // 请求参数无效
CodeDbRecordNotFoundErr = &StatusResponse{5003, "db record not found"} //未在数据库中找到记录
CodeDbCreateErr = &StatusResponse{5004, "create one in database error"} // 在数据库中创建错误
CodeDbSqlErr = &StatusResponse{5005, "database is error"} // 数据库错误
)
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{
Code: sr.Code,
Message: sr.Message,
}
switch len(data) {
case 0:
// 0 直接返回
case 1:
if err, ok := data[0].(error); ok {
newResp.Message = err.Error()
} else {
newResp.Data = data[0]
}
case 2:
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]
default:
panic("data只接收1或2个 data, error.Error() 和 data ")
}
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,
}
switch len(data) {
case 0:
// 0 直接返回
case 1:
newResp.Data = data[0]
default:
panic("data只接收一个 data")
}
return newResp
}
func (resp *Response) SetStatusAddMessage(sr *StatusResponse, msg string, data ...interface{}) *Response {
newResp := &Response{
Code: sr.Code,
Message: sr.Message + ":" + msg,
}
switch len(data) {
case 0:
// 0 直接返回
case 1:
newResp.Data = data[0]
default:
panic("data只接收一个 data")
}
return newResp
}