DriveLinked/pkg/dto/response.go

50 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package dto
import (
"drive-linked/pkg/serializer"
"github.com/kataras/golog"
"github.com/kataras/iris/v12"
"net/http"
)
type Response struct {
Ctx iris.Context
}
const errJsonUnmarshalMsg = "{\"code\":500,\"msg\":\"Json解析错误请联系管理员\",\"data\":null}"
func NewResponse(ctx iris.Context) *Response {
return &Response{Ctx: ctx}
}
// 成功 统一处理
func (r *Response) Success(data interface{}) {
resp := serializer.ResponseSerial{Code: http.StatusOK, Data: data}
_, err := r.Ctx.JSON(resp)
if err != nil {
golog.Error("Json解析错误: ", resp)
r.Ctx.Write([]byte(errJsonUnmarshalMsg))
return
}
}
// 失败 统一处理
func (r *Response) Error(code int, msg string) {
resp := serializer.ResponseSerial{Code: code, Msg: msg}
_, err := r.Ctx.JSON(resp)
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, "未登录")
}