forked from free-lancers/electricity_bill_calc_service
enhance(response):统一响应代码。
This commit is contained in:
@@ -27,13 +27,21 @@ func NewResult(ctx *gin.Context) *Result {
|
||||
}
|
||||
|
||||
// 操作出错(未成功)响应
|
||||
func (r *Result) Error(code int, msg string) {
|
||||
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) Success(msg string) {
|
||||
res := BaseResponse{}
|
||||
@@ -47,7 +55,7 @@ func (r *Result) Created(msg string) {
|
||||
res := BaseResponse{}
|
||||
res.Code = http.StatusCreated
|
||||
res.Message = msg
|
||||
r.Ctx.JSON(http.StatusCreated, res)
|
||||
r.Ctx.JSON(http.StatusOK, res)
|
||||
}
|
||||
|
||||
// 数据成功更新
|
||||
@@ -55,7 +63,7 @@ func (r *Result) Updated(msg string) {
|
||||
res := BaseResponse{}
|
||||
res.Code = http.StatusAccepted
|
||||
res.Message = msg
|
||||
r.Ctx.JSON(http.StatusAccepted, res)
|
||||
r.Ctx.JSON(http.StatusOK, res)
|
||||
}
|
||||
|
||||
// 数据已成功删除
|
||||
@@ -63,7 +71,15 @@ func (r *Result) Deleted(msg string) {
|
||||
res := BaseResponse{}
|
||||
res.Code = http.StatusNoContent
|
||||
res.Message = msg
|
||||
r.Ctx.JSON(http.StatusNoContent, res)
|
||||
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)
|
||||
}
|
||||
|
||||
// 指定操作未被接受
|
||||
@@ -71,7 +87,7 @@ func (r *Result) NotAccept(msg string) {
|
||||
res := BaseResponse{}
|
||||
res.Code = http.StatusNotAcceptable
|
||||
res.Message = msg
|
||||
r.Ctx.JSON(http.StatusNotAcceptable, res)
|
||||
r.Ctx.JSON(http.StatusOK, res)
|
||||
}
|
||||
|
||||
// 数据未找到
|
||||
@@ -79,12 +95,20 @@ func (r *Result) NotFound(msg string) {
|
||||
res := BaseResponse{}
|
||||
res.Code = http.StatusNotFound
|
||||
res.Message = msg
|
||||
r.Ctx.JSON(http.StatusNotFound, res)
|
||||
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(status, code int, msg string, payloads ...map[string]interface{}) {
|
||||
func (r *Result) Json(code int, msg string, payloads ...map[string]interface{}) {
|
||||
var finalPayload = make(map[string]interface{}, 0)
|
||||
finalPayload["code"] = code
|
||||
finalPayload["message"] = &msg
|
||||
@@ -95,7 +119,7 @@ func (r *Result) Json(status, code int, msg string, payloads ...map[string]inter
|
||||
}
|
||||
}
|
||||
|
||||
r.Ctx.JSON(status, finalPayload)
|
||||
r.Ctx.JSON(http.StatusOK, finalPayload)
|
||||
}
|
||||
|
||||
func NewPagedResponse(page int, total int64) *PagedResponse {
|
||||
|
Reference in New Issue
Block a user