Refine the result things.

This commit is contained in:
zicla
2018-11-23 16:37:46 +08:00
parent f916e24bf4
commit b557e9717a
8 changed files with 103 additions and 139 deletions

View File

@ -417,7 +417,7 @@ func (this *AlienController) Download(writer http.ResponseWriter, request *http.
tokenUser := this.userDao.CheckByUuid(downloadToken.UserUuid) tokenUser := this.userDao.CheckByUuid(downloadToken.UserUuid)
if matter.UserUuid != tokenUser.Uuid { 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) user := this.checkUser(writer, request)
if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid {
panic(RESULT_CODE_UNAUTHORIZED) panic(CODE_WRAPPER_UNAUTHORIZED)
} }
} }

View File

@ -64,10 +64,10 @@ func (this *BaseController) Wrap(f func(writer http.ResponseWriter, request *htt
if user.Status == USER_STATUS_DISABLED { if user.Status == USER_STATUS_DISABLED {
//判断用户是否被禁用。 //判断用户是否被禁用。
webResult = ConstWebResult(RESULT_CODE_LOGIN_INVALID) webResult = ConstWebResult(CODE_WRAPPER_USER_DISABLED)
} else { } else {
if qualifiedRole == USER_ROLE_ADMINISTRATOR && user.Role != USER_ROLE_ADMINISTRATOR { if qualifiedRole == USER_ROLE_ADMINISTRATOR && user.Role != USER_ROLE_ADMINISTRATOR {
webResult = ConstWebResult(RESULT_CODE_UNAUTHORIZED) webResult = ConstWebResult(CODE_WRAPPER_UNAUTHORIZED)
} else { } else {
webResult = f(writer, request) webResult = f(writer, request)
} }
@ -84,13 +84,11 @@ func (this *BaseController) Wrap(f func(writer http.ResponseWriter, request *htt
//用json的方式输出返回值。 //用json的方式输出返回值。
var json = jsoniter.ConfigCompatibleWithStandardLibrary var json = jsoniter.ConfigCompatibleWithStandardLibrary
b, _ := json.Marshal(webResult) b, err := json.Marshal(webResult)
if webResult.Code == RESULT_CODE_OK { this.PanicError(err)
writer.WriteHeader(http.StatusOK)
} else { writer.WriteHeader(FetchHttpStatus(webResult.Code))
writer.WriteHeader(http.StatusBadRequest)
}
fmt.Fprintf(writer, string(b)) fmt.Fprintf(writer, string(b))
} else { } else {
@ -105,13 +103,13 @@ func (this *BaseController) Wrap(f func(writer http.ResponseWriter, request *htt
func (this *BaseController) Success(data interface{}) *WebResult { func (this *BaseController) Success(data interface{}) *WebResult {
var webResult *WebResult = nil var webResult *WebResult = nil
if value, ok := data.(string); ok { 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 { } else if value, ok := data.(*WebResult); ok {
webResult = value webResult = value
} else if _, ok := data.(types.Nil); ok { } else if _, ok := data.(types.Nil); ok {
webResult = ConstWebResult(RESULT_CODE_OK) webResult = ConstWebResult(CODE_WRAPPER_OK)
} else { } else {
webResult = &WebResult{Code: RESULT_CODE_OK, Data: data} webResult = &WebResult{Code: CODE_WRAPPER_OK.Code, Data: data}
} }
return webResult return webResult
} }
@ -120,15 +118,15 @@ func (this *BaseController) Success(data interface{}) *WebResult {
func (this *BaseController) Error(err interface{}) *WebResult { func (this *BaseController) Error(err interface{}) *WebResult {
var webResult *WebResult = nil var webResult *WebResult = nil
if value, ok := err.(string); ok { if value, ok := err.(string); ok {
webResult = &WebResult{Code: RESULT_CODE_UTIL_EXCEPTION, Msg: value} webResult = &WebResult{Code: CODE_WRAPPER_UNKNOWN.Code, Msg: value}
} else if value, ok := err.(int); ok { } else if _, ok := err.(int); ok {
webResult = ConstWebResult(value) webResult = ConstWebResult(CODE_WRAPPER_UNKNOWN)
} else if value, ok := err.(*WebResult); ok { } else if value, ok := err.(*WebResult); ok {
webResult = value webResult = value
} else if value, ok := err.(error); ok { } 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 { } else {
webResult = &WebResult{Code: RESULT_CODE_UTIL_EXCEPTION, Msg: "服务器未知错误"} webResult = &WebResult{Code: CODE_WRAPPER_UNKNOWN.Code, Msg: "服务器未知错误"}
} }
return webResult return webResult
} }
@ -138,20 +136,20 @@ func (this *BaseController) checkLogin(writer http.ResponseWriter, request *http
//验证用户是否已经登录。 //验证用户是否已经登录。
sessionCookie, err := request.Cookie(COOKIE_AUTH_KEY) sessionCookie, err := request.Cookie(COOKIE_AUTH_KEY)
if err != nil { if err != nil {
panic(ConstWebResult(RESULT_CODE_LOGIN)) panic(ConstWebResult(CODE_WRAPPER_LOGIN))
} }
session := this.sessionDao.FindByUuid(sessionCookie.Value) session := this.sessionDao.FindByUuid(sessionCookie.Value)
if session == nil { if session == nil {
panic(ConstWebResult(RESULT_CODE_LOGIN)) panic(ConstWebResult(CODE_WRAPPER_LOGIN))
} else { } else {
if session.ExpireTime.Before(time.Now()) { if session.ExpireTime.Before(time.Now()) {
panic(ConstWebResult(RESULT_CODE_LOGIN_EXPIRED)) panic(ConstWebResult(CODE_WRAPPER_LOGIN_EXPIRE))
} else { } else {
user := this.userDao.FindByUuid(session.UserUuid) user := this.userDao.FindByUuid(session.UserUuid)
if user == nil { if user == nil {
panic(ConstWebResult(RESULT_CODE_LOGIN_INVALID)) panic(ConstWebResult(CODE_WRAPPER_LOGIN))
} else { } else {
return session, user return session, user
} }

View File

@ -122,7 +122,7 @@ func (this *ImageCacheController) Delete(writer http.ResponseWriter, request *ht
//判断文件的所属人是否正确 //判断文件的所属人是否正确
user := this.checkUser(writer, request) user := this.checkUser(writer, request)
if user.Role != USER_ROLE_ADMINISTRATOR && imageCache.UserUuid != user.Uuid { 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) this.imageCacheDao.Delete(imageCache)
@ -147,7 +147,7 @@ func (this *ImageCacheController) DeleteBatch(writer http.ResponseWriter, reques
//判断文件的所属人是否正确 //判断文件的所属人是否正确
user := this.checkUser(writer, request) user := this.checkUser(writer, request)
if user.Role != USER_ROLE_ADMINISTRATOR && imageCache.UserUuid != user.Uuid { 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) this.imageCacheDao.Delete(imageCache)

View File

@ -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) { func (this *ImageCacheService) ResizeParams(request *http.Request) (needProcess bool, resizeMode string, resizeWidth int, resizeHeight int) {
var err error var err error
//模式准备逐步废弃掉 //1.0 模式准备逐步废弃掉
if request.FormValue("imageProcess") == "resize" { if request.FormValue("imageProcess") == "resize" {
//老模式使用 imageResizeM,imageResizeW,imageResizeH //老模式使用 imageResizeM,imageResizeW,imageResizeH
imageResizeM := request.FormValue("imageResizeM") imageResizeM := request.FormValue("imageResizeM")

View File

@ -325,7 +325,7 @@ func (this *MatterController) Delete(writer http.ResponseWriter, request *http.R
//判断文件的所属人是否正确 //判断文件的所属人是否正确
user := this.checkUser(writer, request) user := this.checkUser(writer, request)
if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { 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) this.matterDao.Delete(matter)
@ -350,7 +350,7 @@ func (this *MatterController) DeleteBatch(writer http.ResponseWriter, request *h
//判断文件的所属人是否正确 //判断文件的所属人是否正确
user := this.checkUser(writer, request) user := this.checkUser(writer, request)
if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { 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) this.matterDao.Delete(matter)
@ -383,7 +383,7 @@ func (this *MatterController) Rename(writer http.ResponseWriter, request *http.R
user := this.checkUser(writer, request) user := this.checkUser(writer, request)
if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { 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 { if name == matter.Name {
@ -421,7 +421,7 @@ func (this *MatterController) ChangePrivacy(writer http.ResponseWriter, request
//权限验证 //权限验证
user := this.checkUser(writer, request) user := this.checkUser(writer, request)
if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { 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 matter.Privacy = privacy
@ -464,7 +464,7 @@ func (this *MatterController) Move(writer http.ResponseWriter, request *http.Req
destMatter = this.matterService.Detail(destUuid) destMatter = this.matterService.Detail(destUuid)
if user.Role != USER_ROLE_ADMINISTRATOR && destMatter.UserUuid != user.Uuid { 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) srcMatter := this.matterDao.CheckByUuid(uuid)
if user.Role != USER_ROLE_ADMINISTRATOR && srcMatter.UserUuid != user.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 { if srcMatter.Puuid == destUuid {

View File

@ -41,10 +41,10 @@ func (this *Router) GlobalPanicHandler(writer http.ResponseWriter, request *http
var webResult *WebResult = nil var webResult *WebResult = nil
if value, ok := err.(string); ok { if value, ok := err.(string); ok {
writer.WriteHeader(http.StatusBadRequest) writer.WriteHeader(http.StatusBadRequest)
webResult = &WebResult{Code: RESULT_CODE_UTIL_EXCEPTION, Msg: value} webResult = &WebResult{Code: CODE_WRAPPER_UNKNOWN.Code, Msg: value}
} else if value, ok := err.(int); ok { } else if _, ok := err.(int); ok {
writer.WriteHeader(http.StatusBadRequest) writer.WriteHeader(http.StatusBadRequest)
webResult = ConstWebResult(value) webResult = ConstWebResult(CODE_WRAPPER_UNKNOWN)
} else if value, ok := err.(*WebResult); ok { } else if value, ok := err.(*WebResult); ok {
writer.WriteHeader(http.StatusBadRequest) writer.WriteHeader(http.StatusBadRequest)
webResult = value webResult = value
@ -53,16 +53,16 @@ func (this *Router) GlobalPanicHandler(writer http.ResponseWriter, request *http
webResult = &value webResult = &value
} else if value, ok := err.(*WebError); ok { } else if value, ok := err.(*WebError); ok {
writer.WriteHeader(value.Code) 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 { } else if value, ok := err.(WebError); ok {
writer.WriteHeader((&value).Code) 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 { } else if value, ok := err.(error); ok {
writer.WriteHeader(http.StatusBadRequest) 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 { } else {
writer.WriteHeader(http.StatusInternalServerError) writer.WriteHeader(http.StatusInternalServerError)
webResult = &WebResult{Code: RESULT_CODE_UTIL_EXCEPTION, Msg: "服务器未知错误"} webResult = &WebResult{Code: CODE_WRAPPER_UNKNOWN.Code, Msg: "服务器未知错误"}
} }
//输出的是json格式 返回的内容申明是jsonutf-8 //输出的是json格式 返回的内容申明是jsonutf-8

View File

@ -185,7 +185,7 @@ func (this *UserController) Edit(writer http.ResponseWriter, request *http.Reque
user.SizeLimit = sizeLimit user.SizeLimit = sizeLimit
} else { } else {
if currentUser.Uuid != uuid { 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) currentUser := this.checkUser(writer, request)
if currentUser.Role != USER_ROLE_ADMINISTRATOR { if currentUser.Role != USER_ROLE_ADMINISTRATOR {
return this.Error(RESULT_CODE_UNAUTHORIZED) return this.Error(CODE_WRAPPER_UNAUTHORIZED)
} }
user := this.userDao.CheckByUuid(userUuid) user := this.userDao.CheckByUuid(userUuid)

View File

@ -1,7 +1,9 @@
package rest package rest
import "net/http"
type WebResult struct { type WebResult struct {
Code int `json:"code"` Code string `json:"code"`
Msg string `json:"msg"` Msg string `json:"msg"`
Data interface{} `json:"data"` Data interface{} `json:"data"`
} }
@ -10,106 +12,70 @@ func (this *WebResult) Error() string {
return this.Msg return this.Msg
} }
const ( type CodeWrapper struct {
//正常 Code string
RESULT_CODE_OK = 200 HttpStatus int
Description string
}
//未登录 var (
RESULT_CODE_LOGIN = -400 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: "验证码错误"}
RESULT_CODE_UNAUTHORIZED = -401 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: "用户名或密码错误"}
RESULT_CODE_BAD_REQUEST = -402 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: "账户被禁用,禁止访问"}
RESULT_CODE_NOT_FOUND = -404 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: "服务器未知错误"}
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
) )
func ConstWebResult(code int) *WebResult { //根据 CodeWrapper来获取对应的HttpStatus
func FetchHttpStatus(code string) int {
wr := &WebResult{} if code == CODE_WRAPPER_OK.Code {
switch code { return CODE_WRAPPER_OK.HttpStatus
//正常 } else if code == CODE_WRAPPER_BAD_REQUEST.Code {
case RESULT_CODE_OK: return CODE_WRAPPER_BAD_REQUEST.HttpStatus
wr.Msg = "成功" } else if code == CODE_WRAPPER_CAPTCHA_ERROR.Code {
//未登录 return CODE_WRAPPER_CAPTCHA_ERROR.HttpStatus
case RESULT_CODE_LOGIN: } else if code == CODE_WRAPPER_NEED_CAPTCHA.Code {
wr.Msg = "没有登录,禁止访问" return CODE_WRAPPER_NEED_CAPTCHA.HttpStatus
//没有权限 } else if code == CODE_WRAPPER_USERNAME_PASSWORD_ERROR.Code {
case RESULT_CODE_UNAUTHORIZED: return CODE_WRAPPER_USERNAME_PASSWORD_ERROR.HttpStatus
wr.Msg = "没有权限" } else if code == CODE_WRAPPER_PARAMS_ERROR.Code {
//请求错误 return CODE_WRAPPER_PARAMS_ERROR.HttpStatus
case RESULT_CODE_BAD_REQUEST: } else if code == CODE_WRAPPER_LOGIN.Code {
wr.Msg = "请求错误" return CODE_WRAPPER_LOGIN.HttpStatus
//没有找到 } else if code == CODE_WRAPPER_LOGIN_EXPIRE.Code {
case RESULT_CODE_NOT_FOUND: return CODE_WRAPPER_LOGIN_EXPIRE.HttpStatus
wr.Msg = "没有找到" } else if code == CODE_WRAPPER_USER_DISABLED.Code {
//登录过期 return CODE_WRAPPER_USER_DISABLED.HttpStatus
case RESULT_CODE_LOGIN_EXPIRED: } else if code == CODE_WRAPPER_UNAUTHORIZED.Code {
wr.Msg = "登录过期" return CODE_WRAPPER_UNAUTHORIZED.HttpStatus
} else if code == CODE_WRAPPER_NOT_FOUND.Code {
//该登录用户不是有效用户 return CODE_WRAPPER_NOT_FOUND.HttpStatus
case RESULT_CODE_LOGIN_INVALID: } else {
wr.Msg = "该登录用户不是有效用户或者用户已被禁用" return CODE_WRAPPER_UNKNOWN.HttpStatus
//提交的表单验证不通过
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 = "服务器未知错误"
} }
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
} }