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