refactor(response):完全重构快速响应函数,使之可以方便的扩展。

This commit is contained in:
徐涛 2022-08-23 21:37:24 +08:00
parent 1d1cb226ac
commit b6b69896cb

View File

@ -26,100 +26,10 @@ func NewResult(ctx *gin.Context) *Result {
return &Result{Ctx: ctx}
}
// 操作出错(未成功)响应
func (r *Result) Failure(code int, msg string) {
res := BaseResponse{}
res.Code = code
res.Message = msg
r.Ctx.JSON(http.StatusInternalServerError, res)
}
// 操作出错(未成功)响应
func (r *Result) Error(code int, msg string) {
res := BaseResponse{}
res.Code = code
res.Message = msg
r.Ctx.JSON(http.StatusOK, res)
}
// 用户未获得授权)响应
func (r *Result) Unauthorized(msg string) {
res := BaseResponse{}
res.Code = http.StatusUnauthorized
res.Message = msg
r.Ctx.JSON(http.StatusOK, res)
}
// 简易操作成功信息
func (r *Result) Success(msg string) {
res := BaseResponse{}
res.Code = http.StatusOK
res.Message = msg
r.Ctx.JSON(http.StatusOK, res)
}
// 数据成功创建
func (r *Result) Created(msg string) {
res := BaseResponse{}
res.Code = http.StatusCreated
res.Message = msg
r.Ctx.JSON(http.StatusOK, res)
}
// 数据成功更新
func (r *Result) Updated(msg string) {
res := BaseResponse{}
res.Code = http.StatusAccepted
res.Message = msg
r.Ctx.JSON(http.StatusOK, res)
}
// 数据已成功删除
func (r *Result) Deleted(msg string) {
res := BaseResponse{}
res.Code = http.StatusNoContent
res.Message = msg
r.Ctx.JSON(http.StatusOK, res)
}
// 指定操作未被接受
func (r *Result) BadRequest(msg string) {
res := BaseResponse{}
res.Code = http.StatusBadRequest
res.Message = msg
r.Ctx.JSON(http.StatusOK, res)
}
// 指定操作未被接受
func (r *Result) NotAccept(msg string) {
res := BaseResponse{}
res.Code = http.StatusNotAcceptable
res.Message = msg
r.Ctx.JSON(http.StatusOK, res)
}
// 数据未找到
func (r *Result) NotFound(msg string) {
res := BaseResponse{}
res.Code = http.StatusNotFound
res.Message = msg
r.Ctx.JSON(http.StatusOK, res)
}
// 数据存在冲突
func (r *Result) Conflict(msg string) {
res := BaseResponse{}
res.Code = http.StatusConflict
res.Message = msg
r.Ctx.JSON(http.StatusOK, res)
}
// 快速自由JSON格式响应
// ! 注意给定的map中同名的键会被覆盖。
func (r *Result) Json(code int, msg string, payloads ...map[string]interface{}) {
func (r *Result) response(status, code int, msg string, payloads ...map[string]interface{}) {
var finalPayload = make(map[string]interface{}, 0)
finalPayload["code"] = code
finalPayload["message"] = &msg
finalPayload["message"] = msg
for _, m := range payloads {
for k, v := range m {
@ -127,7 +37,68 @@ func (r *Result) Json(code int, msg string, payloads ...map[string]interface{})
}
}
r.Ctx.JSON(http.StatusOK, finalPayload)
r.Ctx.JSON(status, finalPayload)
}
// 操作出错(未成功)响应
func (r *Result) Failure(code int, msg string) {
r.response(http.StatusInternalServerError, code, msg)
}
// 操作出错(未成功)响应
func (r *Result) Error(code int, msg string) {
r.response(http.StatusOK, code, msg)
}
// 用户未获得授权)响应
func (r *Result) Unauthorized(msg string) {
r.response(http.StatusOK, http.StatusUnauthorized, msg)
}
// 简易操作成功信息
func (r *Result) Success(msg string) {
r.response(http.StatusOK, http.StatusOK, msg)
}
// 数据成功创建
func (r *Result) Created(msg string, payloads ...map[string]interface{}) {
r.response(http.StatusOK, http.StatusCreated, msg, payloads...)
}
// 数据成功更新
func (r *Result) Updated(msg string) {
r.response(http.StatusOK, http.StatusAccepted, msg)
}
// 数据已成功删除
func (r *Result) Deleted(msg string) {
r.response(http.StatusOK, http.StatusNoContent, msg)
}
// 指定操作未被接受
func (r *Result) BadRequest(msg string) {
r.response(http.StatusOK, http.StatusBadRequest, msg)
}
// 指定操作未被接受
func (r *Result) NotAccept(msg string) {
r.response(http.StatusOK, http.StatusNotAcceptable, msg)
}
// 数据未找到
func (r *Result) NotFound(msg string) {
r.response(http.StatusOK, http.StatusNotFound, msg)
}
// 数据存在冲突
func (r *Result) Conflict(msg string) {
r.response(http.StatusOK, http.StatusConflict, msg)
}
// 快速自由JSON格式响应
// ! 注意给定的map中同名的键会被覆盖。
func (r *Result) Json(code int, msg string, payloads ...map[string]interface{}) {
r.response(http.StatusOK, code, msg, payloads...)
}
func NewPagedResponse(page int, total int64) *PagedResponse {