enhance(response):统一响应代码。

This commit is contained in:
徐涛 2022-08-13 18:20:21 +08:00
parent 64d63e7f20
commit e56974af3c
2 changed files with 43 additions and 20 deletions

View File

@ -99,14 +99,14 @@ func (_UserController) InvalidUserPassword(c *gin.Context) {
return return
} }
if _, ok := err.(exceptions.UnsuccessfulOperationError); ok { if _, ok := err.(exceptions.UnsuccessfulOperationError); ok {
result.Error(500, "未能成功更新用户的密码。") result.NotAccept("未能成功更新用户的密码。")
return return
} }
if err != nil { if err != nil {
result.Error(500, err.Error()) result.Error(http.StatusInternalServerError, err.Error())
return return
} }
result.Json(http.StatusOK, http.StatusAccepted, "用户密码已经失效", gin.H{"verify": verifyCode}) result.Json(http.StatusAccepted, "用户密码已经失效", gin.H{"verify": verifyCode})
} }
type ResetPasswordFormData struct { type ResetPasswordFormData struct {
@ -166,11 +166,10 @@ func (_UserController) ListPagedUser(c *gin.Context) {
} }
users, total, err := repository.UserRepo.ListUserDetail(requestKeyword, requestUserType, requestUserStat, requestPage) users, total, err := repository.UserRepo.ListUserDetail(requestKeyword, requestUserType, requestUserStat, requestPage)
if err != nil { if err != nil {
result.Error(http.StatusNotFound, err.Error()) result.NotFound(err.Error())
return return
} }
result.Json( result.Json(
http.StatusOK,
http.StatusOK, http.StatusOK,
"已取得符合条件的用户集合。", "已取得符合条件的用户集合。",
response.NewPagedResponse(requestPage, total).ToMap(), response.NewPagedResponse(requestPage, total).ToMap(),
@ -190,7 +189,7 @@ func (_UserController) SwitchUserEnabling(c *gin.Context) {
err := service.UserService.SwitchUserState(switchForm.UserID, switchForm.Enabled) err := service.UserService.SwitchUserState(switchForm.UserID, switchForm.Enabled)
if err != nil { if err != nil {
if nfErr, ok := err.(*exceptions.NotFoundError); ok { if nfErr, ok := err.(*exceptions.NotFoundError); ok {
result.Error(http.StatusNotFound, nfErr.Message) result.NotFound(nfErr.Message)
return return
} else { } else {
result.Error(http.StatusInternalServerError, err.Error()) result.Error(http.StatusInternalServerError, err.Error())
@ -214,7 +213,7 @@ func (_UserController) CreateOPSAndManagementAccount(c *gin.Context) {
c.BindJSON(creationForm) c.BindJSON(creationForm)
exists, err := service.UserService.IsUsernameExists(creationForm.Username) exists, err := service.UserService.IsUsernameExists(creationForm.Username)
if exists { if exists {
result.Error(http.StatusConflict, "指定的用户名已经被使用了。") result.Conflict("指定的用户名已经被使用了。")
return return
} }
if err != nil { if err != nil {
@ -236,7 +235,7 @@ func (_UserController) CreateOPSAndManagementAccount(c *gin.Context) {
result.Error(http.StatusInternalServerError, err.Error()) result.Error(http.StatusInternalServerError, err.Error())
return return
} }
result.Json(http.StatusOK, http.StatusOK, "用户已经成功创建。", gin.H{"verify": verifyCode}) result.Json(http.StatusCreated, "用户已经成功创建。", gin.H{"verify": verifyCode})
} }
func (_UserController) GetUserDetail(c *gin.Context) { func (_UserController) GetUserDetail(c *gin.Context) {
@ -255,7 +254,7 @@ func (_UserController) GetUserDetail(c *gin.Context) {
result.Error(http.StatusInternalServerError, err.Error()) result.Error(http.StatusInternalServerError, err.Error())
return return
} }
result.Json(http.StatusOK, http.StatusOK, "用户详细信息已获取到。", gin.H{"user": userDetail}) result.Json(http.StatusOK, "用户详细信息已获取到。", gin.H{"user": userDetail})
} }
type EnterpriseCreationFormData struct { type EnterpriseCreationFormData struct {
@ -274,7 +273,7 @@ func (_UserController) CreateEnterpriseAccount(c *gin.Context) {
c.BindJSON(creationForm) c.BindJSON(creationForm)
exists, err := service.UserService.IsUsernameExists(creationForm.Username) exists, err := service.UserService.IsUsernameExists(creationForm.Username)
if exists { if exists {
result.Error(http.StatusConflict, "指定的用户名已经被使用了。") result.Conflict("指定的用户名已经被使用了。")
return return
} }
if err != nil { if err != nil {
@ -291,7 +290,7 @@ func (_UserController) CreateEnterpriseAccount(c *gin.Context) {
newUserDetail.Phone = creationForm.Phone newUserDetail.Phone = creationForm.Phone
newUserDetail.UnitServiceFee, err = decimal.NewFromString(*creationForm.UnitServiceFee) newUserDetail.UnitServiceFee, err = decimal.NewFromString(*creationForm.UnitServiceFee)
if err != nil { if err != nil {
result.Error(http.StatusBadRequest, "用户月服务费无法解析。") result.BadRequest("用户月服务费无法解析。")
return return
} }
newUserDetail.ServiceExpiration = time.Now() newUserDetail.ServiceExpiration = time.Now()
@ -301,5 +300,5 @@ func (_UserController) CreateEnterpriseAccount(c *gin.Context) {
result.Error(http.StatusInternalServerError, err.Error()) result.Error(http.StatusInternalServerError, err.Error())
return return
} }
result.Json(http.StatusOK, http.StatusOK, "用户已经成功创建。", gin.H{"verify": verifyCode}) result.Json(http.StatusCreated, "用户已经成功创建。", gin.H{"verify": verifyCode})
} }

View File

@ -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 := BaseResponse{}
res.Code = code res.Code = code
res.Message = msg res.Message = msg
r.Ctx.JSON(http.StatusInternalServerError, res) 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) { func (r *Result) Success(msg string) {
res := BaseResponse{} res := BaseResponse{}
@ -47,7 +55,7 @@ func (r *Result) Created(msg string) {
res := BaseResponse{} res := BaseResponse{}
res.Code = http.StatusCreated res.Code = http.StatusCreated
res.Message = msg 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 := BaseResponse{}
res.Code = http.StatusAccepted res.Code = http.StatusAccepted
res.Message = msg 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 := BaseResponse{}
res.Code = http.StatusNoContent res.Code = http.StatusNoContent
res.Message = msg 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 := BaseResponse{}
res.Code = http.StatusNotAcceptable res.Code = http.StatusNotAcceptable
res.Message = msg 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 := BaseResponse{}
res.Code = http.StatusNotFound res.Code = http.StatusNotFound
res.Message = msg 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格式响应 // 快速自由JSON格式响应
// ! 注意给定的map中同名的键会被覆盖。 // ! 注意给定的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) var finalPayload = make(map[string]interface{}, 0)
finalPayload["code"] = code finalPayload["code"] = code
finalPayload["message"] = &msg 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 { func NewPagedResponse(page int, total int64) *PagedResponse {