增加社区列表接口,任务相关接口修改

This commit is contained in:
lianghuanjie
2024-12-19 21:14:14 +08:00
parent 391a9d3481
commit a3c4adfa25
32 changed files with 1163 additions and 197 deletions

View File

@@ -6,16 +6,14 @@ import (
)
type err struct {
code int
reason Reason
msg string
code Reason
msg string
}
func New(code int, reason Reason, message any) error {
func New(code Reason, message any) error {
return err{
code: code,
reason: reason,
msg: cast.ToString(message),
code: code,
msg: cast.ToString(message),
}
}
@@ -25,15 +23,10 @@ func (e err) Error() string {
}
// Code return code
func (e err) Code() int {
func (e err) Code() Reason {
return e.code
}
// Reason return reason
func (e err) Reason() Reason {
return e.reason
}
// Message return message
func (e err) Message() string {
return e.msg

View File

@@ -1,56 +1,5 @@
package errs
import (
"net/http"
)
func Success() error {
return New(http.StatusOK, ErrSucceed, "success")
}
func NotFound(reason Reason, v any) error {
return New(http.StatusNotFound, reason, v)
}
func BadRequest(reason Reason, v any) error {
return New(http.StatusBadRequest, reason, v)
}
func InternalServer(reason Reason, v any) error {
return New(http.StatusInternalServerError, reason, v)
}
// Unauthorized new Unauthorized error that is mapped to a 401 response.
func Unauthorized(reason Reason, v any) error {
return New(http.StatusUnauthorized, reason, v)
}
// Forbidden new Forbidden error that is mapped to a 403 response.
func Forbidden(reason Reason, v any) error {
return New(http.StatusForbidden, reason, v)
}
// Conflict new Conflict error that is mapped to a 409 response.
func Conflict(reason Reason, v any) error {
return New(http.StatusConflict, reason, v)
}
// ServiceUnavailable new ServiceUnavailable error that is mapped to an HTTP 503 response.
func ServiceUnavailable(reason Reason, v any) error {
return New(http.StatusServiceUnavailable, reason, v)
}
// GatewayTimeout new GatewayTimeout error that is mapped to an HTTP 504 response.
func GatewayTimeout(reason Reason, v any) error {
return New(http.StatusGatewayTimeout, reason, v)
}
// BadGateway new BadGateway error that is mapped to an HTTP 504 response.
func BadGateway(reason Reason, v any) error {
return New(http.StatusBadGateway, reason, v)
}
// ClientClosed new ClientClosed error that is mapped to an HTTP 499 response.
func ClientClosed(reason Reason, v any) error {
return New(499, reason, v)
return New(ErrSucceed, "ok")
}

View File

@@ -2,24 +2,45 @@ package errs
import (
"context"
"encoding/json"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest"
"github.com/zeromicro/go-zero/rest/httpx"
"net/http"
"os"
"strings"
)
var debug bool
func init() {
if strings.ToLower(os.Getenv("VANS_API_DEBUG")) == "on" {
debug = true
}
httpx.SetErrorHandlerCtx(ErrorHandleCtx)
httpx.SetErrorHandler(ErrorHandle)
httpx.SetOkHandler(OkHandle)
}
func SetDebug(d bool) {
debug = d
func OkHandle(ctx context.Context, v any) any {
return map[string]any{
"status": map[string]any{
"code": ErrSucceed,
"msg": "ok",
},
"data": v,
}
}
func WithUnauthorizedCallback() rest.RunOption {
return rest.WithUnauthorizedCallback(func(w http.ResponseWriter, r *http.Request, err error) {
body := map[string]any{
"status": map[string]any{
"code": ErrUnauthorized,
"msg": err.Error(),
},
}
data, _ := json.Marshal(body)
w.Header().Set(httpx.ContentType, "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, err = w.Write(data)
if err != nil {
logx.Errorw("error write response", logx.Field("type", "http"), logx.Field("error", err))
}
})
}
func ErrorHandle(err error) (int, any) {
@@ -27,10 +48,9 @@ func ErrorHandle(err error) (int, any) {
}
func ErrorHandleCtx(ctx context.Context, err error) (int, any) {
code := http.StatusBadRequest
reason := ErrInternalServer
code := ErrUnknownReason
var msg string
if ec, ok := err.(interface{ Code() int }); ok {
if ec, ok := err.(interface{ Code() Reason }); ok {
code = ec.Code()
}
if ec, ok := err.(interface{ Message() string }); ok {
@@ -38,27 +58,17 @@ func ErrorHandleCtx(ctx context.Context, err error) (int, any) {
} else {
msg = err.Error()
}
if ec, ok := err.(interface{ Reason() Reason }); ok {
reason = ec.Reason()
}
var errMsg string
if reason < ErrUnknownLogicError && reason != ErrSucceed {
errMsg = msg
if code < ErrUnknownReason && code >= ErrInternalServer {
logx.Errorw("request system error", logx.Field("err-msg", msg))
msg = "system error"
}
body := map[string]any{
"code": reason,
"message": msg,
"status": map[string]any{
"code": code,
"msg": msg,
},
}
if errMsg != "" && debug {
body["err"] = errMsg
}
if ec, ok := err.(interface{ Metadata() any }); ok {
if md := ec.Metadata(); md != nil {
body["metadata"] = md
}
}
return code, body
return http.StatusOK, body
}

View File

@@ -3,10 +3,11 @@ package errs
type Reason int
const (
// ======= 系统错误:0~999 =======
ErrUnknownReason Reason = 0 // 未知错误
ErrSucceed Reason = 200 // 成功
ErrOverload Reason = 403 // 请求超载
// ======= 系统错误:10000~19999 =======
ErrUnknownReason Reason = 10000 // 未知错误
ErrSucceed Reason = 10200 // 成功
ErrUnauthorized Reason = 10401 // 未授权
ErrOverload Reason = 10403 // 请求超载
// ======= 服务器内部错误1000~9999 =======
ErrInternalServer Reason = 1000 // 未知的服务器内部错误
@@ -15,22 +16,10 @@ const (
ErrEncodePassword Reason = 1003 // 密码加密错误
ErrGenerateUUid Reason = 1004 // 生成uuid错误
ErrGenerateToken Reason = 1005 // 生成token错误
ErrGetExchangeRate Reason = 1005 // 获取汇率错误
// ======= 业务层错误:10000~99999 =======
ErrUnknownLogicError Reason = 10000 // 未知的业务错误
ErrInvalidParam Reason = 10001 // 无效参数错误
ErrInvalidSignature Reason = 10002 // 无效签名错误
ErrInvalidToken Reason = 10003 // 无效的token
ErrInvoiceHasPaid Reason = 10004 // 订单已支付
ErrInvalidAppId Reason = 10005 // 应用id无效
ErrUserNotHasInviter Reason = 10006 // 用户没有邀请人
ErrTaskNotFound Reason = 10007 // 任务不存在
ErrTaskAlreadyReward Reason = 10008 // 任务已领取
ErrTaskNotFinished Reason = 10009 // 任务未完成
// ========= admin 业务相关错误码: 30000~39999 =========
ErrUnknownAdminError Reason = 30000 // 未知的admin错误
ErrInvalidPassword Reason = 30001 // 无效密码
ErrInvalidAccount Reason = 30002 // 无效账号
// ======= 业务层错误:20000~29999 =======
ErrUnknownLogicError Reason = 20000 // 未知的业务错误
ErrTaskNotFound Reason = 20001 // 任务不存在
ErrTaskAlreadyReward Reason = 20002 // 任务已领取
ErrTaskNotFinished Reason = 20003 // 任务未完成
)