package dto import ( "drive-linked/pkg/serializer" "github.com/kataras/iris/v12" "net/http" ) type Response struct { Ctx iris.Context } func NewResponse(ctx iris.Context) *Response { return &Response{Ctx: ctx} } const errJsonUnmarshalMsg = "{\"code\":500,\"msg\":\"Json解析错误,请联系管理员\",\"data\":null}" // 成功 统一处理 func (r *Response) Success(data interface{}) { res := serializer.Response{ Code: 200, Msg: "", Data: data, } _, err := r.Ctx.JSON(res) if err != nil { r.Ctx.Write([]byte(errJsonUnmarshalMsg)) return } } // 失败 统一处理 func (r *Response) Error(code int, msg string) { res := serializer.Response{ Code: code, Msg: msg, Data: nil, } _, err := r.Ctx.JSON(res) if err != nil { r.Ctx.Write([]byte(errJsonUnmarshalMsg)) return } } // 参数类型错误 func (r *Response) ErrBadRequest() { r.Error(http.StatusBadRequest, "请求参数错误") } // 未登录/未授权错误 func (r *Response) ErrUnauthorized() { r.Error(http.StatusUnauthorized, "未登录") }