From b557e9717aeccad1d7005ce454861f1063f5954d Mon Sep 17 00:00:00 2001 From: zicla Date: Fri, 23 Nov 2018 16:37:46 +0800 Subject: [PATCH] Refine the result things. --- rest/alien_controller.go | 4 +- rest/base_controller.go | 38 ++++---- rest/image_cache_controller.go | 4 +- rest/image_cache_service.go | 2 +- rest/matter_controller.go | 12 +-- rest/router.go | 14 +-- rest/user_controller.go | 4 +- rest/web_result.go | 164 +++++++++++++-------------------- 8 files changed, 103 insertions(+), 139 deletions(-) diff --git a/rest/alien_controller.go b/rest/alien_controller.go index 5a9ebfb..088b1e2 100644 --- a/rest/alien_controller.go +++ b/rest/alien_controller.go @@ -417,7 +417,7 @@ func (this *AlienController) Download(writer http.ResponseWriter, request *http. tokenUser := this.userDao.CheckByUuid(downloadToken.UserUuid) if matter.UserUuid != tokenUser.Uuid { - panic(RESULT_CODE_UNAUTHORIZED) + panic(CODE_WRAPPER_UNAUTHORIZED) } //下载之后立即过期掉。 @@ -429,7 +429,7 @@ func (this *AlienController) Download(writer http.ResponseWriter, request *http. //判断文件的所属人是否正确 user := this.checkUser(writer, request) if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { - panic(RESULT_CODE_UNAUTHORIZED) + panic(CODE_WRAPPER_UNAUTHORIZED) } } diff --git a/rest/base_controller.go b/rest/base_controller.go index cb81743..169c0ea 100644 --- a/rest/base_controller.go +++ b/rest/base_controller.go @@ -64,10 +64,10 @@ func (this *BaseController) Wrap(f func(writer http.ResponseWriter, request *htt if user.Status == USER_STATUS_DISABLED { //判断用户是否被禁用。 - webResult = ConstWebResult(RESULT_CODE_LOGIN_INVALID) + webResult = ConstWebResult(CODE_WRAPPER_USER_DISABLED) } else { if qualifiedRole == USER_ROLE_ADMINISTRATOR && user.Role != USER_ROLE_ADMINISTRATOR { - webResult = ConstWebResult(RESULT_CODE_UNAUTHORIZED) + webResult = ConstWebResult(CODE_WRAPPER_UNAUTHORIZED) } else { webResult = f(writer, request) } @@ -84,13 +84,11 @@ func (this *BaseController) Wrap(f func(writer http.ResponseWriter, request *htt //用json的方式输出返回值。 var json = jsoniter.ConfigCompatibleWithStandardLibrary - b, _ := json.Marshal(webResult) + b, err := json.Marshal(webResult) - if webResult.Code == RESULT_CODE_OK { - writer.WriteHeader(http.StatusOK) - } else { - writer.WriteHeader(http.StatusBadRequest) - } + this.PanicError(err) + + writer.WriteHeader(FetchHttpStatus(webResult.Code)) fmt.Fprintf(writer, string(b)) } else { @@ -105,13 +103,13 @@ func (this *BaseController) Wrap(f func(writer http.ResponseWriter, request *htt func (this *BaseController) Success(data interface{}) *WebResult { var webResult *WebResult = nil if value, ok := data.(string); ok { - webResult = &WebResult{Code: RESULT_CODE_OK, Msg: value} + webResult = &WebResult{Code: CODE_WRAPPER_OK.Code, Msg: value} } else if value, ok := data.(*WebResult); ok { webResult = value } else if _, ok := data.(types.Nil); ok { - webResult = ConstWebResult(RESULT_CODE_OK) + webResult = ConstWebResult(CODE_WRAPPER_OK) } else { - webResult = &WebResult{Code: RESULT_CODE_OK, Data: data} + webResult = &WebResult{Code: CODE_WRAPPER_OK.Code, Data: data} } return webResult } @@ -120,15 +118,15 @@ func (this *BaseController) Success(data interface{}) *WebResult { func (this *BaseController) Error(err interface{}) *WebResult { var webResult *WebResult = nil if value, ok := err.(string); ok { - webResult = &WebResult{Code: RESULT_CODE_UTIL_EXCEPTION, Msg: value} - } else if value, ok := err.(int); ok { - webResult = ConstWebResult(value) + webResult = &WebResult{Code: CODE_WRAPPER_UNKNOWN.Code, Msg: value} + } else if _, ok := err.(int); ok { + webResult = ConstWebResult(CODE_WRAPPER_UNKNOWN) } else if value, ok := err.(*WebResult); ok { webResult = value } else if value, ok := err.(error); ok { - webResult = &WebResult{Code: RESULT_CODE_UTIL_EXCEPTION, Msg: value.Error()} + webResult = &WebResult{Code: CODE_WRAPPER_UNKNOWN.Code, Msg: value.Error()} } else { - webResult = &WebResult{Code: RESULT_CODE_UTIL_EXCEPTION, Msg: "服务器未知错误"} + webResult = &WebResult{Code: CODE_WRAPPER_UNKNOWN.Code, Msg: "服务器未知错误"} } return webResult } @@ -138,20 +136,20 @@ func (this *BaseController) checkLogin(writer http.ResponseWriter, request *http //验证用户是否已经登录。 sessionCookie, err := request.Cookie(COOKIE_AUTH_KEY) if err != nil { - panic(ConstWebResult(RESULT_CODE_LOGIN)) + panic(ConstWebResult(CODE_WRAPPER_LOGIN)) } session := this.sessionDao.FindByUuid(sessionCookie.Value) if session == nil { - panic(ConstWebResult(RESULT_CODE_LOGIN)) + panic(ConstWebResult(CODE_WRAPPER_LOGIN)) } else { if session.ExpireTime.Before(time.Now()) { - panic(ConstWebResult(RESULT_CODE_LOGIN_EXPIRED)) + panic(ConstWebResult(CODE_WRAPPER_LOGIN_EXPIRE)) } else { user := this.userDao.FindByUuid(session.UserUuid) if user == nil { - panic(ConstWebResult(RESULT_CODE_LOGIN_INVALID)) + panic(ConstWebResult(CODE_WRAPPER_LOGIN)) } else { return session, user } diff --git a/rest/image_cache_controller.go b/rest/image_cache_controller.go index c609802..ba4bd8c 100644 --- a/rest/image_cache_controller.go +++ b/rest/image_cache_controller.go @@ -122,7 +122,7 @@ func (this *ImageCacheController) Delete(writer http.ResponseWriter, request *ht //判断文件的所属人是否正确 user := this.checkUser(writer, request) if user.Role != USER_ROLE_ADMINISTRATOR && imageCache.UserUuid != user.Uuid { - return this.Error(RESULT_CODE_UNAUTHORIZED) + return this.Error(CODE_WRAPPER_UNAUTHORIZED) } this.imageCacheDao.Delete(imageCache) @@ -147,7 +147,7 @@ func (this *ImageCacheController) DeleteBatch(writer http.ResponseWriter, reques //判断文件的所属人是否正确 user := this.checkUser(writer, request) if user.Role != USER_ROLE_ADMINISTRATOR && imageCache.UserUuid != user.Uuid { - return this.Error(RESULT_CODE_UNAUTHORIZED) + return this.Error(CODE_WRAPPER_UNAUTHORIZED) } this.imageCacheDao.Delete(imageCache) diff --git a/rest/image_cache_service.go b/rest/image_cache_service.go index e04bb6f..a7092a1 100644 --- a/rest/image_cache_service.go +++ b/rest/image_cache_service.go @@ -45,7 +45,7 @@ func (this *ImageCacheService) Detail(uuid string) *ImageCache { func (this *ImageCacheService) ResizeParams(request *http.Request) (needProcess bool, resizeMode string, resizeWidth int, resizeHeight int) { var err error - //老模式准备逐步废弃掉 + //1.0 模式准备逐步废弃掉 if request.FormValue("imageProcess") == "resize" { //老模式使用 imageResizeM,imageResizeW,imageResizeH imageResizeM := request.FormValue("imageResizeM") diff --git a/rest/matter_controller.go b/rest/matter_controller.go index bfd4793..a90682d 100644 --- a/rest/matter_controller.go +++ b/rest/matter_controller.go @@ -325,7 +325,7 @@ func (this *MatterController) Delete(writer http.ResponseWriter, request *http.R //判断文件的所属人是否正确 user := this.checkUser(writer, request) if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { - return this.Error(RESULT_CODE_UNAUTHORIZED) + return this.Error(CODE_WRAPPER_UNAUTHORIZED) } this.matterDao.Delete(matter) @@ -350,7 +350,7 @@ func (this *MatterController) DeleteBatch(writer http.ResponseWriter, request *h //判断文件的所属人是否正确 user := this.checkUser(writer, request) if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { - return this.Error(RESULT_CODE_UNAUTHORIZED) + return this.Error(CODE_WRAPPER_UNAUTHORIZED) } this.matterDao.Delete(matter) @@ -383,7 +383,7 @@ func (this *MatterController) Rename(writer http.ResponseWriter, request *http.R user := this.checkUser(writer, request) if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { - return this.Error(RESULT_CODE_UNAUTHORIZED) + return this.Error(CODE_WRAPPER_UNAUTHORIZED) } if name == matter.Name { @@ -421,7 +421,7 @@ func (this *MatterController) ChangePrivacy(writer http.ResponseWriter, request //权限验证 user := this.checkUser(writer, request) if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { - return this.Error(RESULT_CODE_UNAUTHORIZED) + return this.Error(CODE_WRAPPER_UNAUTHORIZED) } matter.Privacy = privacy @@ -464,7 +464,7 @@ func (this *MatterController) Move(writer http.ResponseWriter, request *http.Req destMatter = this.matterService.Detail(destUuid) if user.Role != USER_ROLE_ADMINISTRATOR && destMatter.UserUuid != user.Uuid { - return this.Error(RESULT_CODE_UNAUTHORIZED) + return this.Error(CODE_WRAPPER_UNAUTHORIZED) } } } @@ -476,7 +476,7 @@ func (this *MatterController) Move(writer http.ResponseWriter, request *http.Req srcMatter := this.matterDao.CheckByUuid(uuid) if user.Role != USER_ROLE_ADMINISTRATOR && srcMatter.UserUuid != user.Uuid { - return this.Error(RESULT_CODE_UNAUTHORIZED) + return this.Error(CODE_WRAPPER_UNAUTHORIZED) } if srcMatter.Puuid == destUuid { diff --git a/rest/router.go b/rest/router.go index 73e2a9b..fe9265f 100644 --- a/rest/router.go +++ b/rest/router.go @@ -41,10 +41,10 @@ func (this *Router) GlobalPanicHandler(writer http.ResponseWriter, request *http var webResult *WebResult = nil if value, ok := err.(string); ok { writer.WriteHeader(http.StatusBadRequest) - webResult = &WebResult{Code: RESULT_CODE_UTIL_EXCEPTION, Msg: value} - } else if value, ok := err.(int); ok { + webResult = &WebResult{Code: CODE_WRAPPER_UNKNOWN.Code, Msg: value} + } else if _, ok := err.(int); ok { writer.WriteHeader(http.StatusBadRequest) - webResult = ConstWebResult(value) + webResult = ConstWebResult(CODE_WRAPPER_UNKNOWN) } else if value, ok := err.(*WebResult); ok { writer.WriteHeader(http.StatusBadRequest) webResult = value @@ -53,16 +53,16 @@ func (this *Router) GlobalPanicHandler(writer http.ResponseWriter, request *http webResult = &value } else if value, ok := err.(*WebError); ok { writer.WriteHeader(value.Code) - webResult = &WebResult{Code: RESULT_CODE_UTIL_EXCEPTION, Msg: value.Msg} + webResult = &WebResult{Code: CODE_WRAPPER_UNKNOWN.Code, Msg: value.Msg} } else if value, ok := err.(WebError); ok { writer.WriteHeader((&value).Code) - webResult = &WebResult{Code: RESULT_CODE_UTIL_EXCEPTION, Msg: (&value).Msg} + webResult = &WebResult{Code: CODE_WRAPPER_UNKNOWN.Code, Msg: (&value).Msg} } else if value, ok := err.(error); ok { writer.WriteHeader(http.StatusBadRequest) - webResult = &WebResult{Code: RESULT_CODE_UTIL_EXCEPTION, Msg: value.Error()} + webResult = &WebResult{Code: CODE_WRAPPER_UNKNOWN.Code, Msg: value.Error()} } else { writer.WriteHeader(http.StatusInternalServerError) - webResult = &WebResult{Code: RESULT_CODE_UTIL_EXCEPTION, Msg: "服务器未知错误"} + webResult = &WebResult{Code: CODE_WRAPPER_UNKNOWN.Code, Msg: "服务器未知错误"} } //输出的是json格式 返回的内容申明是json,utf-8 diff --git a/rest/user_controller.go b/rest/user_controller.go index 0eff9d3..ff2f50d 100644 --- a/rest/user_controller.go +++ b/rest/user_controller.go @@ -185,7 +185,7 @@ func (this *UserController) Edit(writer http.ResponseWriter, request *http.Reque user.SizeLimit = sizeLimit } else { if currentUser.Uuid != uuid { - return this.Error(RESULT_CODE_UNAUTHORIZED) + return this.Error(CODE_WRAPPER_UNAUTHORIZED) } } @@ -362,7 +362,7 @@ func (this *UserController) ResetPassword(writer http.ResponseWriter, request *h currentUser := this.checkUser(writer, request) if currentUser.Role != USER_ROLE_ADMINISTRATOR { - return this.Error(RESULT_CODE_UNAUTHORIZED) + return this.Error(CODE_WRAPPER_UNAUTHORIZED) } user := this.userDao.CheckByUuid(userUuid) diff --git a/rest/web_result.go b/rest/web_result.go index daeb99e..396929f 100644 --- a/rest/web_result.go +++ b/rest/web_result.go @@ -1,7 +1,9 @@ package rest +import "net/http" + type WebResult struct { - Code int `json:"code"` + Code string `json:"code"` Msg string `json:"msg"` Data interface{} `json:"data"` } @@ -10,106 +12,70 @@ func (this *WebResult) Error() string { return this.Msg } -const ( - //正常 - RESULT_CODE_OK = 200 +type CodeWrapper struct { + Code string + HttpStatus int + Description string +} - //未登录 - RESULT_CODE_LOGIN = -400 - - //没有权限 - RESULT_CODE_UNAUTHORIZED = -401 - - //请求错误 - RESULT_CODE_BAD_REQUEST = -402 - - //没有找到 - RESULT_CODE_NOT_FOUND = -404 - - //登录过期 - RESULT_CODE_LOGIN_EXPIRED = -405 - - //该登录用户不是有效用户 - RESULT_CODE_LOGIN_INVALID = -406 - - //提交的表单验证不通过 - RESULT_CODE_FORM_INVALID = -410 - - //请求太频繁 - RESULT_CODE_FREQUENCY = -420 - - //服务器出错。 - RESULT_CODE_SERVER_ERROR = -500 - - //远程服务不可用 - RESULT_CODE_NOT_AVAILABLE = -501 - - //并发异常 - RESULT_CODE_CONCURRENCY = -511 - - //远程微服务没有找到 - RESULT_CODE_SERVICE_NOT_FOUND = -600 - - //远程微服务连接超时 - RESULT_CODE_SERVICE_TIME_OUT = -610 - - //通用的异常 - RESULT_CODE_UTIL_EXCEPTION = -700 +var ( + CODE_WRAPPER_OK = &CodeWrapper{Code: "OK", HttpStatus: http.StatusOK, Description: "成功"} + CODE_WRAPPER_BAD_REQUEST = &CodeWrapper{Code: "BAD_REQUEST", HttpStatus: http.StatusBadRequest, Description: "请求不合法"} + CODE_WRAPPER_CAPTCHA_ERROR = &CodeWrapper{Code: "CAPTCHA_ERROR", HttpStatus: http.StatusBadRequest, Description: "验证码错误"} + CODE_WRAPPER_NEED_CAPTCHA = &CodeWrapper{Code: "NEED_CAPTCHA", HttpStatus: http.StatusBadRequest, Description: "验证码必填"} + CODE_WRAPPER_USERNAME_PASSWORD_ERROR = &CodeWrapper{Code: "USERNAME_PASSWORD_ERROR", HttpStatus: http.StatusBadRequest, Description: "用户名或密码错误"} + CODE_WRAPPER_PARAMS_ERROR = &CodeWrapper{Code: "PARAMS_ERROR", HttpStatus: http.StatusBadRequest, Description: "用户名或密码错误"} + CODE_WRAPPER_LOGIN = &CodeWrapper{Code: "LOGIN", HttpStatus: http.StatusUnauthorized, Description: "未登录,禁止访问"} + CODE_WRAPPER_LOGIN_EXPIRE = &CodeWrapper{Code: "LOGIN_EXPIRE", HttpStatus: http.StatusUnauthorized, Description: "登录过期,请重新登录"} + CODE_WRAPPER_USER_DISABLED = &CodeWrapper{Code: "USER_DISABLED", HttpStatus: http.StatusForbidden, Description: "账户被禁用,禁止访问"} + CODE_WRAPPER_UNAUTHORIZED = &CodeWrapper{Code: "LOGIN", HttpStatus: http.StatusUnauthorized, Description: "没有权限,禁止访问"} + CODE_WRAPPER_NOT_FOUND = &CodeWrapper{Code: "NOT_FOUND", HttpStatus: http.StatusNotFound, Description: "内容不存在"} + CODE_WRAPPER_UNKNOWN = &CodeWrapper{Code: "UNKNOWN", HttpStatus: http.StatusInternalServerError, Description: "服务器未知错误"} ) -func ConstWebResult(code int) *WebResult { - - wr := &WebResult{} - switch code { - //正常 - case RESULT_CODE_OK: - wr.Msg = "成功" - //未登录 - case RESULT_CODE_LOGIN: - wr.Msg = "没有登录,禁止访问" - //没有权限 - case RESULT_CODE_UNAUTHORIZED: - wr.Msg = "没有权限" - //请求错误 - case RESULT_CODE_BAD_REQUEST: - wr.Msg = "请求错误" - //没有找到 - case RESULT_CODE_NOT_FOUND: - wr.Msg = "没有找到" - //登录过期 - case RESULT_CODE_LOGIN_EXPIRED: - wr.Msg = "登录过期" - - //该登录用户不是有效用户 - case RESULT_CODE_LOGIN_INVALID: - wr.Msg = "该登录用户不是有效用户或者用户已被禁用" - - //提交的表单验证不通过 - case RESULT_CODE_FORM_INVALID: - wr.Msg = "提交的表单验证不通过" - //请求太频繁 - case RESULT_CODE_FREQUENCY: - wr.Msg = "请求太频繁" - //服务器出错。 - case RESULT_CODE_SERVER_ERROR: - wr.Msg = "服务器出错" - //远程服务不可用 - case RESULT_CODE_NOT_AVAILABLE: - wr.Msg = "远程服务不可用" - //并发异常 - case RESULT_CODE_CONCURRENCY: - wr.Msg = "并发异常" - //远程微服务没有找到 - case RESULT_CODE_SERVICE_NOT_FOUND: - wr.Msg = "远程微服务没有找到" - //远程微服务连接超时 - case RESULT_CODE_SERVICE_TIME_OUT: - wr.Msg = "远程微服务连接超时" - default: - code = RESULT_CODE_UTIL_EXCEPTION - wr.Msg = "服务器未知错误" +//根据 CodeWrapper来获取对应的HttpStatus +func FetchHttpStatus(code string) int { + if code == CODE_WRAPPER_OK.Code { + return CODE_WRAPPER_OK.HttpStatus + } else if code == CODE_WRAPPER_BAD_REQUEST.Code { + return CODE_WRAPPER_BAD_REQUEST.HttpStatus + } else if code == CODE_WRAPPER_CAPTCHA_ERROR.Code { + return CODE_WRAPPER_CAPTCHA_ERROR.HttpStatus + } else if code == CODE_WRAPPER_NEED_CAPTCHA.Code { + return CODE_WRAPPER_NEED_CAPTCHA.HttpStatus + } else if code == CODE_WRAPPER_USERNAME_PASSWORD_ERROR.Code { + return CODE_WRAPPER_USERNAME_PASSWORD_ERROR.HttpStatus + } else if code == CODE_WRAPPER_PARAMS_ERROR.Code { + return CODE_WRAPPER_PARAMS_ERROR.HttpStatus + } else if code == CODE_WRAPPER_LOGIN.Code { + return CODE_WRAPPER_LOGIN.HttpStatus + } else if code == CODE_WRAPPER_LOGIN_EXPIRE.Code { + return CODE_WRAPPER_LOGIN_EXPIRE.HttpStatus + } else if code == CODE_WRAPPER_USER_DISABLED.Code { + return CODE_WRAPPER_USER_DISABLED.HttpStatus + } else if code == CODE_WRAPPER_UNAUTHORIZED.Code { + return CODE_WRAPPER_UNAUTHORIZED.HttpStatus + } else if code == CODE_WRAPPER_NOT_FOUND.Code { + return CODE_WRAPPER_NOT_FOUND.HttpStatus + } else { + return CODE_WRAPPER_UNKNOWN.HttpStatus } - wr.Code = code - return wr - +} + +func ConstWebResult(codeWrapper *CodeWrapper) *WebResult { + + wr := &WebResult{ + Code: codeWrapper.Code, + Msg: codeWrapper.Description, + } + return wr +} + +func CustomWebResult(codeWrapper *CodeWrapper, description string) *WebResult { + + wr := &WebResult{ + Code: codeWrapper.Code, + Msg: description, + } + return wr }