Try to add share code in download api.

This commit is contained in:
zicla 2019-05-01 02:00:09 +08:00
parent cfd32089ff
commit ebd20c6db5
12 changed files with 130 additions and 102 deletions

View File

@ -94,7 +94,7 @@ func (this *AlienService) PreviewOrDownload(
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_WRAPPER_UNAUTHORIZED) panic(result.UNAUTHORIZED)
} }
//下载之后立即过期掉。如果是分块下载的,必须以最终获取到完整的数据为准。 //下载之后立即过期掉。如果是分块下载的,必须以最终获取到完整的数据为准。
@ -106,7 +106,7 @@ func (this *AlienService) PreviewOrDownload(
//判断文件的所属人是否正确 //判断文件的所属人是否正确
operator := this.findUser(writer, request) operator := this.findUser(writer, request)
if operator == nil || (operator.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != operator.Uuid) { if operator == nil || (operator.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != operator.Uuid) {
panic(result.CODE_WRAPPER_UNAUTHORIZED) panic(result.UNAUTHORIZED)
} }
} }

View File

@ -65,7 +65,7 @@ func (this *BaseBean) findUser(writer http.ResponseWriter, request *http.Request
//获取当前登录的用户,找不到就返回登录错误 //获取当前登录的用户,找不到就返回登录错误
func (this *BaseBean) checkUser(writer http.ResponseWriter, request *http.Request) *User { func (this *BaseBean) checkUser(writer http.ResponseWriter, request *http.Request) *User {
if this.findUser(writer, request) == nil { if this.findUser(writer, request) == nil {
panic(result.ConstWebResult(result.CODE_WRAPPER_LOGIN)) panic(result.ConstWebResult(result.LOGIN))
} else { } else {
return this.findUser(writer, request) return this.findUser(writer, request)
} }

View File

@ -58,10 +58,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 = result.ConstWebResult(result.CODE_WRAPPER_USER_DISABLED) webResult = result.ConstWebResult(result.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 = result.ConstWebResult(result.CODE_WRAPPER_UNAUTHORIZED) webResult = result.ConstWebResult(result.UNAUTHORIZED)
} else { } else {
webResult = f(writer, request) webResult = f(writer, request)
} }
@ -98,16 +98,16 @@ func (this *BaseController) Success(data interface{}) *result.WebResult {
var webResult *result.WebResult = nil var webResult *result.WebResult = nil
if value, ok := data.(string); ok { if value, ok := data.(string); ok {
//返回一句普通的消息 //返回一句普通的消息
webResult = &result.WebResult{Code: result.CODE_WRAPPER_OK.Code, Msg: value} webResult = &result.WebResult{Code: result.OK.Code, Msg: value}
} else if value, ok := data.(*result.WebResult); ok { } else if value, ok := data.(*result.WebResult); ok {
//返回一个webResult对象 //返回一个webResult对象
webResult = value webResult = value
} else if _, ok := data.(types.Nil); ok { } else if _, ok := data.(types.Nil); ok {
//返回一个空指针 //返回一个空指针
webResult = result.ConstWebResult(result.CODE_WRAPPER_OK) webResult = result.ConstWebResult(result.OK)
} else { } else {
//返回的类型不明确。 //返回的类型不明确。
webResult = &result.WebResult{Code: result.CODE_WRAPPER_OK.Code, Data: data} webResult = &result.WebResult{Code: result.OK.Code, Data: data}
} }
return webResult return webResult
} }

View File

@ -80,7 +80,7 @@ func (this *DavController) CheckCurrentUser(writer http.ResponseWriter, request
//要求前端使用Basic的形式授权 //要求前端使用Basic的形式授权
writer.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) writer.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
panic(result.ConstWebResult(result.CODE_WRAPPER_LOGIN)) panic(result.ConstWebResult(result.LOGIN))
} }

View File

@ -16,6 +16,7 @@ type MatterController struct {
downloadTokenDao *DownloadTokenDao downloadTokenDao *DownloadTokenDao
imageCacheDao *ImageCacheDao imageCacheDao *ImageCacheDao
shareDao *ShareDao shareDao *ShareDao
shareService *ShareService
bridgeDao *BridgeDao bridgeDao *BridgeDao
imageCacheService *ImageCacheService imageCacheService *ImageCacheService
} }
@ -50,6 +51,11 @@ func (this *MatterController) Init() {
this.shareDao = b this.shareDao = b
} }
b = core.CONTEXT.GetBean(this.shareService)
if b, ok := b.(*ShareService); ok {
this.shareService = b
}
b = core.CONTEXT.GetBean(this.bridgeDao) b = core.CONTEXT.GetBean(this.bridgeDao)
if b, ok := b.(*BridgeDao); ok { if b, ok := b.(*BridgeDao); ok {
this.bridgeDao = b this.bridgeDao = b
@ -136,25 +142,14 @@ func (this *MatterController) Page(writer http.ResponseWriter, request *http.Req
if puuid == "" { if puuid == "" {
panic(result.BadRequest("puuid必填")) panic(result.BadRequest("puuid必填"))
} }
dirMatter := this.matterDao.CheckByUuid(puuid) dirMatter := this.matterDao.CheckByUuid(puuid)
if !dirMatter.Dir { if !dirMatter.Dir {
panic(result.BadRequest("puuid 对应的不是文件夹")) panic(result.BadRequest("puuid 对应的不是文件夹"))
} }
share := this.shareDao.CheckByUuid(shareUuid)
//如果是自己的分享,可以不要提取码
user := this.findUser(writer, request) user := this.findUser(writer, request)
if user == nil { share := this.shareService.CheckShare(shareUuid, shareCode, user)
if share.Code != shareCode {
panic(result.Unauthorized("提取码错误!"))
}
} else {
if user.Uuid != share.UserUuid {
if share.Code != shareCode {
panic(result.Unauthorized("提取码错误!"))
}
}
}
//验证 shareRootMatter是否在被分享。 //验证 shareRootMatter是否在被分享。
shareRootMatter := this.matterDao.CheckByUuid(shareRootUuid) shareRootMatter := this.matterDao.CheckByUuid(shareRootUuid)

View File

@ -282,37 +282,26 @@ func (this *ShareController) CheckShare(writer http.ResponseWriter, request *htt
//如果是根目录那么就传入root. //如果是根目录那么就传入root.
shareUuid := request.FormValue("shareUuid") shareUuid := request.FormValue("shareUuid")
code := request.FormValue("code") code := request.FormValue("code")
share := this.shareDao.CheckByUuid(shareUuid)
//如果是自己的分享,可以不要提取码
user := this.findUser(writer, request) user := this.findUser(writer, request)
if user == nil {
if share.Code != code {
panic(result.Unauthorized("提取码错误!"))
}
} else {
if user.Uuid != share.UserUuid {
if share.Code != code {
panic(result.Unauthorized("提取码错误!"))
}
}
}
return share
return this.shareService.CheckShare(shareUuid, code, user)
} }
//浏览某个分享中的文件 //浏览某个分享中的文件
func (this *ShareController) Browse(writer http.ResponseWriter, request *http.Request) *result.WebResult { func (this *ShareController) Browse(writer http.ResponseWriter, request *http.Request) *result.WebResult {
//要求传参shareUuid,code //如果是根目录那么就传入root.
share := this.CheckShare(writer, request) shareUuid := request.FormValue("shareUuid")
bridges := this.bridgeDao.ListByShareUuid(share.Uuid) code := request.FormValue("code")
//当前查看的puuid。 puuid=root表示查看分享的根目录其余表示查看某个文件夹下的文件。 //当前查看的puuid。 puuid=root表示查看分享的根目录其余表示查看某个文件夹下的文件。
puuid := request.FormValue("puuid") puuid := request.FormValue("puuid")
rootUuid := request.FormValue("rootUuid") rootUuid := request.FormValue("rootUuid")
user := this.findUser(writer, request)
share := this.shareService.CheckShare(shareUuid, code, user)
bridges := this.bridgeDao.ListByShareUuid(share.Uuid)
if puuid == "" { if puuid == "" {
puuid = MATTER_ROOT puuid = MATTER_ROOT
} }

View File

@ -2,6 +2,8 @@ package rest
import ( import (
"github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/core"
"github.com/eyebluecn/tank/code/tool/result"
"time"
) )
//@Service //@Service
@ -35,3 +37,26 @@ func (this *ShareService) Detail(uuid string) *Share {
return share return share
} }
//验证一个shareUuid和shareCode是否匹配和有权限。
func (this *ShareService) CheckShare(shareUuid string, code string, user *User) *Share {
share := this.shareDao.CheckByUuid(shareUuid)
//如果是自己的分享,可以不要提取码
if user == nil || user.Uuid != share.UserUuid {
//没有登录,或者查看的不是自己的分享,要求有验证码
if code == "" {
panic(result.CustomWebResult(result.NEED_SHARE_CODE, "提取码必填"))
} else if share.Code != code {
panic(result.CustomWebResult(result.SHARE_CODE_ERROR, "提取码错误"))
} else {
if !share.ExpireInfinity {
if share.ExpireTime.Before(time.Now()) {
panic(result.BadRequest("分享已过期"))
}
}
}
}
return share
}

View File

@ -200,7 +200,7 @@ func (this *TankApplication) HandleMirror() {
return return
} }
if webResult.Code == result.CODE_WRAPPER_OK.Code { if webResult.Code == result.OK.Code {
fmt.Println("success") fmt.Println("success")
} else { } else {
fmt.Printf("error %s\r\n", webResult.Msg) fmt.Printf("error %s\r\n", webResult.Msg)
@ -247,7 +247,7 @@ func (this *TankApplication) HandleCrawl() {
return return
} }
if webResult.Code == result.CODE_WRAPPER_OK.Code { if webResult.Code == result.OK.Code {
fmt.Println("success") fmt.Println("success")
} else { } else {
fmt.Printf("error %s\r\n", webResult.Msg) fmt.Printf("error %s\r\n", webResult.Msg)

View File

@ -102,7 +102,7 @@ func (this *TankRouter) GlobalPanicHandler(writer http.ResponseWriter, request *
var webResult *result.WebResult = nil var webResult *result.WebResult = nil
if value, ok := err.(string); ok { if value, ok := err.(string); ok {
//一个字符串,默认是请求错误。 //一个字符串,默认是请求错误。
webResult = result.CustomWebResult(result.CODE_WRAPPER_BAD_REQUEST, value) webResult = result.CustomWebResult(result.BAD_REQUEST, value)
} else if value, ok := err.(*result.WebResult); ok { } else if value, ok := err.(*result.WebResult); ok {
//一个WebResult对象 //一个WebResult对象
webResult = value webResult = value
@ -111,10 +111,10 @@ func (this *TankRouter) GlobalPanicHandler(writer http.ResponseWriter, request *
webResult = result.ConstWebResult(value) webResult = result.ConstWebResult(value)
} else if value, ok := err.(error); ok { } else if value, ok := err.(error); ok {
//一个普通的错误对象 //一个普通的错误对象
webResult = result.CustomWebResult(result.CODE_WRAPPER_UNKNOWN, value.Error()) webResult = result.CustomWebResult(result.UNKNOWN, value.Error())
} else { } else {
//其他不能识别的内容 //其他不能识别的内容
webResult = result.ConstWebResult(result.CODE_WRAPPER_UNKNOWN) webResult = result.ConstWebResult(result.UNKNOWN)
} }
//修改http code码 //修改http code码
@ -176,7 +176,7 @@ func (this *TankRouter) ServeHTTP(writer http.ResponseWriter, request *http.Requ
} }
if !canHandle { if !canHandle {
panic(result.CustomWebResult(result.CODE_WRAPPER_NOT_FOUND, fmt.Sprintf("没有找到能够处理%s的方法", path))) panic(result.CustomWebResult(result.NOT_FOUND, fmt.Sprintf("没有找到能够处理%s的方法", path)))
} }
} }
@ -190,7 +190,7 @@ func (this *TankRouter) ServeHTTP(writer http.ResponseWriter, request *http.Requ
if handler, ok := this.installRouteMap[path]; ok { if handler, ok := this.installRouteMap[path]; ok {
handler(writer, request) handler(writer, request)
} else { } else {
panic(result.ConstWebResult(result.CODE_WRAPPER_NOT_INSTALLED)) panic(result.ConstWebResult(result.NOT_INSTALLED))
} }
} }

View File

@ -3,7 +3,6 @@ package cache
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/eyebluecn/tank/code/tool/util"
"sort" "sort"
"sync" "sync"
"time" "time"
@ -148,6 +147,18 @@ func (table *Table) SetDeleteCallback(f func(*Item)) {
table.deleteCallback = f table.deleteCallback = f
} }
//带有panic恢复的方法
func (table *Table) RunWithRecovery(f func()) {
defer func() {
if err := recover(); err != nil {
//core.LOGGER.Error("异步任务错误: %v", err)
}
}()
//执行函数
f()
}
//终结检查,被自调整的时间触发 //终结检查,被自调整的时间触发
func (table *Table) checkExpire() { func (table *Table) checkExpire() {
table.Lock() table.Lock()
@ -197,7 +208,7 @@ func (table *Table) checkExpire() {
table.cleanupInterval = smallestDuration table.cleanupInterval = smallestDuration
if smallestDuration > 0 { if smallestDuration > 0 {
table.cleanupTimer = time.AfterFunc(smallestDuration, func() { table.cleanupTimer = time.AfterFunc(smallestDuration, func() {
go util.RunWithRecovery(table.checkExpire) go table.RunWithRecovery(table.checkExpire)
}) })
} }
table.Unlock() table.Unlock()
@ -387,8 +398,7 @@ func (table *Table) MostAccessed(count int64) []*Item {
// 打印日志 // 打印日志
func (table *Table) log(format string, v ...interface{}) { func (table *Table) log(format string, v ...interface{}) {
//TODO: 全局日志记录 //core.LOGGER.Info(format, v...)
//LOGGER.Info(format, v...)
} }
//新建一个缓存Table //新建一个缓存Table

View File

@ -286,7 +286,7 @@ func DownloadFile(
if size >= 0 { if size >= 0 {
ranges, err := ParseRange(rangeReq, size) ranges, err := ParseRange(rangeReq, size)
if err != nil { if err != nil {
panic(result.CustomWebResult(result.CODE_WRAPPER_RANGE_NOT_SATISFIABLE, "range header出错")) panic(result.CustomWebResult(result.RANGE_NOT_SATISFIABLE, "range header出错"))
} }
if SumRangesSize(ranges) > size { if SumRangesSize(ranges) > size {
// The total number of bytes in all the ranges // The total number of bytes in all the ranges
@ -310,7 +310,7 @@ func DownloadFile(
// be sent using the multipart/byteranges media type." // be sent using the multipart/byteranges media type."
ra := ranges[0] ra := ranges[0]
if _, err := diskFile.Seek(ra.start, io.SeekStart); err != nil { if _, err := diskFile.Seek(ra.start, io.SeekStart); err != nil {
panic(result.CustomWebResult(result.CODE_WRAPPER_RANGE_NOT_SATISFIABLE, "range header出错")) panic(result.CustomWebResult(result.RANGE_NOT_SATISFIABLE, "range header出错"))
} }
sendSize = ra.length sendSize = ra.length
code = http.StatusPartialContent code = http.StatusPartialContent

View File

@ -22,55 +22,61 @@ type CodeWrapper struct {
} }
var ( var (
CODE_WRAPPER_OK = &CodeWrapper{Code: "OK", HttpStatus: http.StatusOK, Description: "成功"} OK = &CodeWrapper{Code: "OK", HttpStatus: http.StatusOK, Description: "成功"}
CODE_WRAPPER_BAD_REQUEST = &CodeWrapper{Code: "BAD_REQUEST", HttpStatus: http.StatusBadRequest, Description: "请求不合法"} BAD_REQUEST = &CodeWrapper{Code: "BAD_REQUEST", HttpStatus: http.StatusBadRequest, Description: "请求不合法"}
CODE_WRAPPER_CAPTCHA_ERROR = &CodeWrapper{Code: "CAPTCHA_ERROR", HttpStatus: http.StatusBadRequest, Description: "验证码错误"} CAPTCHA_ERROR = &CodeWrapper{Code: "CAPTCHA_ERROR", HttpStatus: http.StatusBadRequest, Description: "验证码错误"}
CODE_WRAPPER_NEED_CAPTCHA = &CodeWrapper{Code: "NEED_CAPTCHA", HttpStatus: http.StatusBadRequest, Description: "验证码必填"} NEED_CAPTCHA = &CodeWrapper{Code: "NEED_CAPTCHA", HttpStatus: http.StatusBadRequest, Description: "验证码必填"}
CODE_WRAPPER_USERNAME_PASSWORD_ERROR = &CodeWrapper{Code: "USERNAME_PASSWORD_ERROR", HttpStatus: http.StatusBadRequest, Description: "用户名或密码错误"} NEED_SHARE_CODE = &CodeWrapper{Code: "NEED_SHARE_CODE", HttpStatus: http.StatusUnauthorized, Description: "分享提取码必填"}
CODE_WRAPPER_PARAMS_ERROR = &CodeWrapper{Code: "PARAMS_ERROR", HttpStatus: http.StatusBadRequest, Description: "用户名或密码错误"} SHARE_CODE_ERROR = &CodeWrapper{Code: "SHARE_CODE_ERROR", HttpStatus: http.StatusUnauthorized, Description: "分享提取码错误"}
CODE_WRAPPER_LOGIN = &CodeWrapper{Code: "LOGIN", HttpStatus: http.StatusUnauthorized, Description: "未登录,禁止访问"} USERNAME_PASSWORD_ERROR = &CodeWrapper{Code: "USERNAME_PASSWORD_ERROR", HttpStatus: http.StatusBadRequest, Description: "用户名或密码错误"}
CODE_WRAPPER_LOGIN_EXPIRE = &CodeWrapper{Code: "LOGIN_EXPIRE", HttpStatus: http.StatusUnauthorized, Description: "登录过期,请重新登录"} PARAMS_ERROR = &CodeWrapper{Code: "PARAMS_ERROR", HttpStatus: http.StatusBadRequest, Description: "用户名或密码错误"}
CODE_WRAPPER_USER_DISABLED = &CodeWrapper{Code: "USER_DISABLED", HttpStatus: http.StatusForbidden, Description: "账户被禁用,禁止访问"} LOGIN = &CodeWrapper{Code: "LOGIN", HttpStatus: http.StatusUnauthorized, Description: "未登录,禁止访问"}
CODE_WRAPPER_UNAUTHORIZED = &CodeWrapper{Code: "UNAUTHORIZED", HttpStatus: http.StatusUnauthorized, Description: "没有权限,禁止访问"} LOGIN_EXPIRE = &CodeWrapper{Code: "LOGIN_EXPIRE", HttpStatus: http.StatusUnauthorized, Description: "登录过期,请重新登录"}
CODE_WRAPPER_NOT_FOUND = &CodeWrapper{Code: "NOT_FOUND", HttpStatus: http.StatusNotFound, Description: "内容不存在"} USER_DISABLED = &CodeWrapper{Code: "USER_DISABLED", HttpStatus: http.StatusForbidden, Description: "账户被禁用,禁止访问"}
CODE_WRAPPER_RANGE_NOT_SATISFIABLE = &CodeWrapper{Code: "RANGE_NOT_SATISFIABLE", HttpStatus: http.StatusRequestedRangeNotSatisfiable, Description: "文件范围读取错误"} UNAUTHORIZED = &CodeWrapper{Code: "UNAUTHORIZED", HttpStatus: http.StatusUnauthorized, Description: "没有权限,禁止访问"}
CODE_WRAPPER_NOT_INSTALLED = &CodeWrapper{Code: "NOT_INSTALLED", HttpStatus: http.StatusInternalServerError, Description: "系统尚未安装"} NOT_FOUND = &CodeWrapper{Code: "NOT_FOUND", HttpStatus: http.StatusNotFound, Description: "内容不存在"}
CODE_WRAPPER_SERVER = &CodeWrapper{Code: "SERVER", HttpStatus: http.StatusInternalServerError, Description: "服务器出错"} RANGE_NOT_SATISFIABLE = &CodeWrapper{Code: "RANGE_NOT_SATISFIABLE", HttpStatus: http.StatusRequestedRangeNotSatisfiable, Description: "文件范围读取错误"}
CODE_WRAPPER_UNKNOWN = &CodeWrapper{Code: "UNKNOWN", HttpStatus: http.StatusInternalServerError, Description: "服务器未知错误"} NOT_INSTALLED = &CodeWrapper{Code: "NOT_INSTALLED", HttpStatus: http.StatusInternalServerError, Description: "系统尚未安装"}
SERVER = &CodeWrapper{Code: "SERVER", HttpStatus: http.StatusInternalServerError, Description: "服务器出错"}
UNKNOWN = &CodeWrapper{Code: "UNKNOWN", HttpStatus: http.StatusInternalServerError, Description: "服务器未知错误"}
) )
//根据 CodeWrapper来获取对应的HttpStatus //根据 CodeWrapper来获取对应的HttpStatus
func FetchHttpStatus(code string) int { func FetchHttpStatus(code string) int {
if code == CODE_WRAPPER_OK.Code { if code == OK.Code {
return CODE_WRAPPER_OK.HttpStatus return OK.HttpStatus
} else if code == CODE_WRAPPER_BAD_REQUEST.Code { } else if code == BAD_REQUEST.Code {
return CODE_WRAPPER_BAD_REQUEST.HttpStatus return BAD_REQUEST.HttpStatus
} else if code == CODE_WRAPPER_CAPTCHA_ERROR.Code { } else if code == CAPTCHA_ERROR.Code {
return CODE_WRAPPER_CAPTCHA_ERROR.HttpStatus return CAPTCHA_ERROR.HttpStatus
} else if code == CODE_WRAPPER_NEED_CAPTCHA.Code { } else if code == NEED_CAPTCHA.Code {
return CODE_WRAPPER_NEED_CAPTCHA.HttpStatus return NEED_CAPTCHA.HttpStatus
} else if code == CODE_WRAPPER_USERNAME_PASSWORD_ERROR.Code { } else if code == NEED_SHARE_CODE.Code {
return CODE_WRAPPER_USERNAME_PASSWORD_ERROR.HttpStatus return NEED_SHARE_CODE.HttpStatus
} else if code == CODE_WRAPPER_PARAMS_ERROR.Code { } else if code == SHARE_CODE_ERROR.Code {
return CODE_WRAPPER_PARAMS_ERROR.HttpStatus return SHARE_CODE_ERROR.HttpStatus
} else if code == CODE_WRAPPER_LOGIN.Code { } else if code == USERNAME_PASSWORD_ERROR.Code {
return CODE_WRAPPER_LOGIN.HttpStatus return USERNAME_PASSWORD_ERROR.HttpStatus
} else if code == CODE_WRAPPER_LOGIN_EXPIRE.Code { } else if code == PARAMS_ERROR.Code {
return CODE_WRAPPER_LOGIN_EXPIRE.HttpStatus return PARAMS_ERROR.HttpStatus
} else if code == CODE_WRAPPER_USER_DISABLED.Code { } else if code == LOGIN.Code {
return CODE_WRAPPER_USER_DISABLED.HttpStatus return LOGIN.HttpStatus
} else if code == CODE_WRAPPER_UNAUTHORIZED.Code { } else if code == LOGIN_EXPIRE.Code {
return CODE_WRAPPER_UNAUTHORIZED.HttpStatus return LOGIN_EXPIRE.HttpStatus
} else if code == CODE_WRAPPER_NOT_FOUND.Code { } else if code == USER_DISABLED.Code {
return CODE_WRAPPER_NOT_FOUND.HttpStatus return USER_DISABLED.HttpStatus
} else if code == CODE_WRAPPER_RANGE_NOT_SATISFIABLE.Code { } else if code == UNAUTHORIZED.Code {
return CODE_WRAPPER_RANGE_NOT_SATISFIABLE.HttpStatus return UNAUTHORIZED.HttpStatus
} else if code == CODE_WRAPPER_NOT_INSTALLED.Code { } else if code == NOT_FOUND.Code {
return CODE_WRAPPER_NOT_INSTALLED.HttpStatus return NOT_FOUND.HttpStatus
} else if code == CODE_WRAPPER_SERVER.Code { } else if code == RANGE_NOT_SATISFIABLE.Code {
return CODE_WRAPPER_SERVER.HttpStatus return RANGE_NOT_SATISFIABLE.HttpStatus
} else if code == NOT_INSTALLED.Code {
return NOT_INSTALLED.HttpStatus
} else if code == SERVER.Code {
return SERVER.HttpStatus
} else { } else {
return CODE_WRAPPER_UNKNOWN.HttpStatus return UNKNOWN.HttpStatus
} }
} }
@ -85,6 +91,9 @@ func ConstWebResult(codeWrapper *CodeWrapper) *WebResult {
func CustomWebResult(codeWrapper *CodeWrapper, description string) *WebResult { func CustomWebResult(codeWrapper *CodeWrapper, description string) *WebResult {
if description == "" {
description = codeWrapper.Description
}
wr := &WebResult{ wr := &WebResult{
Code: codeWrapper.Code, Code: codeWrapper.Code,
Msg: description, Msg: description,
@ -94,23 +103,23 @@ func CustomWebResult(codeWrapper *CodeWrapper, description string) *WebResult {
//请求参数有问题 //请求参数有问题
func BadRequest(format string, v ...interface{}) *WebResult { func BadRequest(format string, v ...interface{}) *WebResult {
return CustomWebResult(CODE_WRAPPER_BAD_REQUEST, fmt.Sprintf(format, v...)) return CustomWebResult(BAD_REQUEST, fmt.Sprintf(format, v...))
} }
//没有权限 //没有权限
func Unauthorized(format string, v ...interface{}) *WebResult { func Unauthorized(format string, v ...interface{}) *WebResult {
return CustomWebResult(CODE_WRAPPER_UNAUTHORIZED, fmt.Sprintf(format, v...)) return CustomWebResult(UNAUTHORIZED, fmt.Sprintf(format, v...))
} }
//没有找到 //没有找到
func NotFound(format string, v ...interface{}) *WebResult { func NotFound(format string, v ...interface{}) *WebResult {
return CustomWebResult(CODE_WRAPPER_NOT_FOUND, fmt.Sprintf(format, v...)) return CustomWebResult(NOT_FOUND, fmt.Sprintf(format, v...))
} }
//服务器内部出问题 //服务器内部出问题
func Server(format string, v ...interface{}) *WebResult { func Server(format string, v ...interface{}) *WebResult {
return CustomWebResult(CODE_WRAPPER_SERVER, fmt.Sprintf(format, v...)) return CustomWebResult(SERVER, fmt.Sprintf(format, v...))
} }
//所有的数据库错误情况 //所有的数据库错误情况