From 46256439da5d055a28995cc9a69f68e3deb94006 Mon Sep 17 00:00:00 2001 From: zicla Date: Fri, 26 Apr 2019 02:50:32 +0800 Subject: [PATCH] Refine all the panic method. --- rest/bean.go | 21 --------------------- rest/dav_controller.go | 4 ++-- rest/dav_service.go | 23 ++++++++++++----------- rest/footprint_controller.go | 4 ++-- rest/image_cache_controller.go | 11 ++++++----- rest/install_controller.go | 32 ++++++++++++++++---------------- rest/matter_controller.go | 24 ++++++++++++------------ rest/matter_dao.go | 12 ++++++------ rest/matter_service.go | 20 ++++++++++---------- rest/preference_controller.go | 2 +- rest/result/web_result.go | 16 ++++++++++++++++ rest/user_controller.go | 26 +++++++++++++------------- rest/user_service.go | 3 ++- 13 files changed, 98 insertions(+), 100 deletions(-) diff --git a/rest/bean.go b/rest/bean.go index 97417a2..052ff95 100644 --- a/rest/bean.go +++ b/rest/bean.go @@ -1,7 +1,6 @@ package rest import ( - "fmt" "net/http" "tank/rest/config" "tank/rest/logger" @@ -44,26 +43,6 @@ func (this *Bean) PanicError(err error) { } } -//请求参数有问题 -func (this *Bean) PanicBadRequest(format string, v ...interface{}) { - panic(result.CustomWebResult(result.CODE_WRAPPER_BAD_REQUEST, fmt.Sprintf(format, v...))) -} - -//没有权限 -func (this *Bean) PanicUnauthorized(format string, v ...interface{}) { - panic(result.CustomWebResult(result.CODE_WRAPPER_UNAUTHORIZED, fmt.Sprintf(format, v...))) -} - -//没有找到 -func (this *Bean) PanicNotFound(format string, v ...interface{}) { - panic(result.CustomWebResult(result.CODE_WRAPPER_NOT_FOUND, fmt.Sprintf(format, v...))) -} - -//服务器内部出问题 -func (this *Bean) PanicServer(format string, v ...interface{}) { - panic(result.CustomWebResult(result.CODE_WRAPPER_SERVER, fmt.Sprintf(format, v...))) -} - //能找到一个user就找到一个 func (this *Bean) findUser(writer http.ResponseWriter, request *http.Request) *User { diff --git a/rest/dav_controller.go b/rest/dav_controller.go index 20d3757..438497f 100644 --- a/rest/dav_controller.go +++ b/rest/dav_controller.go @@ -85,10 +85,10 @@ func (this *DavController) CheckCurrentUser(writer http.ResponseWriter, request user := this.userDao.FindByUsername(username) if user == nil { - this.PanicBadRequest("邮箱或密码错误") + panic(result.BadRequest("邮箱或密码错误")) } else { if !tool.MatchBcrypt(password, user.Password) { - this.PanicBadRequest("邮箱或密码错误") + panic(result.BadRequest("邮箱或密码错误")) } } diff --git a/rest/dav_service.go b/rest/dav_service.go index ffa1fa2..41f7aa7 100644 --- a/rest/dav_service.go +++ b/rest/dav_service.go @@ -9,6 +9,7 @@ import ( "strings" "tank/rest/dav" "tank/rest/dav/xml" + "tank/rest/result" "tank/rest/tool" ) @@ -57,7 +58,7 @@ func (this *DavService) ParseDepth(request *http.Request) int { return 1 } } else { - this.PanicBadRequest("必须指定Header Depth") + panic(result.BadRequest("必须指定Header Depth")) } return depth } @@ -106,7 +107,7 @@ func (this *DavService) PropstatsFromXmlNames(user *User, matter *Matter, xmlNam } if len(properties) == 0 { - this.PanicBadRequest("请求的属性项无法解析!") + panic(result.BadRequest("请求的属性项无法解析!")) } okPropstat := dav.Propstat{Status: http.StatusOK, Props: properties} @@ -135,7 +136,7 @@ func (this *DavService) Propstats(user *User, matter *Matter, propfind *dav.Prop propstats := make([]dav.Propstat, 0) if propfind.Propname != nil { - this.PanicBadRequest("propfind.Propname != nil 尚未处理") + panic(result.BadRequest("propfind.Propname != nil 尚未处理")) } else if propfind.Allprop != nil { //TODO: 如果include中还有内容,那么包含进去。 @@ -312,7 +313,7 @@ func (this *DavService) prepareMoveCopy( var destinationPath string if destinationStr == "" { - this.PanicBadRequest("Header Destination必填") + panic(result.BadRequest("Header Destination必填")) } //如果是重命名,那么就不是http开头了。 @@ -322,7 +323,7 @@ func (this *DavService) prepareMoveCopy( destinationUrl, err := url.Parse(destinationStr) this.PanicError(err) if destinationUrl.Host != request.Host { - this.PanicBadRequest("Destination Host不一致. %s %s != %s", destinationStr, destinationUrl.Host, request.Host) + panic(result.BadRequest("Destination Host不一致. %s %s != %s", destinationStr, destinationUrl.Host, request.Host)) } fullDestinationPath = destinationUrl.Path } @@ -337,7 +338,7 @@ func (this *DavService) prepareMoveCopy( if len(strs) == 2 { destinationPath = strs[1] } else { - this.PanicBadRequest("目标前缀必须为:%s", WEBDAV_PREFFIX) + panic(result.BadRequest("目标前缀必须为:%s", WEBDAV_PREFFIX)) } destinationName = tool.GetFilenameOfPath(destinationPath) @@ -360,7 +361,7 @@ func (this *DavService) prepareMoveCopy( //如果是空或者/就是请求根目录 if srcMatter.Uuid == MATTER_ROOT { - this.PanicBadRequest("你不能移动根目录!") + panic(result.BadRequest("你不能移动根目录!")) } //寻找目标文件夹matter @@ -404,19 +405,19 @@ func (this *DavService) HandleCopy(writer http.ResponseWriter, request *http.Req //加锁 func (this *DavService) HandleLock(writer http.ResponseWriter, request *http.Request, user *User, subPath string) { - this.PanicBadRequest("不支持LOCK方法") + panic(result.BadRequest("不支持LOCK方法")) } //解锁 func (this *DavService) HandleUnlock(writer http.ResponseWriter, request *http.Request, user *User, subPath string) { - this.PanicBadRequest("不支持UNLOCK方法") + panic(result.BadRequest("不支持UNLOCK方法")) } //修改文件属性 func (this *DavService) HandleProppatch(writer http.ResponseWriter, request *http.Request, user *User, subPath string) { - this.PanicBadRequest("不支持PROPPATCH方法") + panic(result.BadRequest("不支持PROPPATCH方法")) } //处理所有的请求 @@ -480,7 +481,7 @@ func (this *DavService) HandleDav(writer http.ResponseWriter, request *http.Requ } else { - this.PanicBadRequest("该方法还不支持。%s", method) + panic(result.BadRequest("该方法还不支持。%s", method)) } diff --git a/rest/footprint_controller.go b/rest/footprint_controller.go index c221a5b..53e45ad 100644 --- a/rest/footprint_controller.go +++ b/rest/footprint_controller.go @@ -47,7 +47,7 @@ func (this *FootprintController) Detail(writer http.ResponseWriter, request *htt uuid := request.FormValue("uuid") if uuid == "" { - this.PanicBadRequest("图片缓存的uuid必填") + panic(result.BadRequest("图片缓存的uuid必填")) } footprint := this.footprintService.Detail(uuid) @@ -113,7 +113,7 @@ func (this *FootprintController) Delete(writer http.ResponseWriter, request *htt uuid := request.FormValue("uuid") if uuid == "" { - this.PanicBadRequest("uuid必填") + panic(result.BadRequest("uuid必填")) } footprint := this.footprintDao.FindByUuid(uuid) diff --git a/rest/image_cache_controller.go b/rest/image_cache_controller.go index 6915659..af99341 100644 --- a/rest/image_cache_controller.go +++ b/rest/image_cache_controller.go @@ -49,7 +49,7 @@ func (this *ImageCacheController) Detail(writer http.ResponseWriter, request *ht uuid := request.FormValue("uuid") if uuid == "" { - this.PanicBadRequest("图片缓存的uuid必填") + panic(result.BadRequest("图片缓存的uuid必填")) } imageCache := this.imageCacheService.Detail(uuid) @@ -127,7 +127,7 @@ func (this *ImageCacheController) Delete(writer http.ResponseWriter, request *ht uuid := request.FormValue("uuid") if uuid == "" { - this.PanicBadRequest("图片缓存的uuid必填") + panic(result.BadRequest("图片缓存的uuid必填")) } imageCache := this.imageCacheDao.FindByUuid(uuid) @@ -135,7 +135,8 @@ func (this *ImageCacheController) Delete(writer http.ResponseWriter, request *ht //判断图片缓存的所属人是否正确 user := this.checkUser(writer, request) if user.Role != USER_ROLE_ADMINISTRATOR && imageCache.UserUuid != user.Uuid { - this.PanicUnauthorized("没有权限") + + panic(result.Unauthorized("没有权限")) } this.imageCacheDao.Delete(imageCache) @@ -148,7 +149,7 @@ func (this *ImageCacheController) DeleteBatch(writer http.ResponseWriter, reques uuids := request.FormValue("uuids") if uuids == "" { - this.PanicBadRequest("图片缓存的uuids必填") + panic(result.BadRequest("图片缓存的uuids必填")) } uuidArray := strings.Split(uuids, ",") @@ -160,7 +161,7 @@ func (this *ImageCacheController) DeleteBatch(writer http.ResponseWriter, reques //判断图片缓存的所属人是否正确 user := this.checkUser(writer, request) if user.Role != USER_ROLE_ADMINISTRATOR && imageCache.UserUuid != user.Uuid { - this.PanicUnauthorized("没有权限") + panic(result.Unauthorized("没有权限")) } this.imageCacheDao.Delete(imageCache) diff --git a/rest/install_controller.go b/rest/install_controller.go index ed436b5..d9f75be 100644 --- a/rest/install_controller.go +++ b/rest/install_controller.go @@ -130,7 +130,7 @@ func (this *InstallController) getCreateSQLFromFile(tableName string) string { filePath := homePath + "/db/" + tableName + ".sql" exists, err := tool.PathExists(filePath) if err != nil { - this.PanicServer("从安装目录判断建表语句文件是否存在时出错!") + panic(result.Server("从安装目录判断建表语句文件是否存在时出错!")) } //2. 从GOPATH下面去找,因为可能是开发环境 @@ -142,11 +142,11 @@ func (this *InstallController) getCreateSQLFromFile(tableName string) string { filePath = build.Default.GOPATH + "/src/tank/build/db/" + tableName + ".sql" exists, err = tool.PathExists(filePath) if err != nil { - this.PanicServer("从GOPATH判断建表语句文件是否存在时出错!") + panic(result.Server("从GOPATH判断建表语句文件是否存在时出错!")) } if !exists { - this.PanicServer(fmt.Sprintf("%s 或 %s 均不存在,请检查你的安装情况。", filePath1, filePath)) + panic(result.Server("%s 或 %s 均不存在,请检查你的安装情况。", filePath1, filePath)) } } @@ -219,10 +219,10 @@ func (this *InstallController) validateTableMetaList(tableInfoList []*InstallTab strs = append(strs, v.DBName) } - this.PanicBadRequest(fmt.Sprintf("%s 表的以下字段缺失:%v", tableInfo.Name, strs)) + panic(result.BadRequest(fmt.Sprintf("%s 表的以下字段缺失:%v", tableInfo.Name, strs))) } } else { - this.PanicBadRequest(tableInfo.Name + "表不存在") + panic(result.BadRequest(tableInfo.Name + "表不存在")) } } @@ -309,15 +309,15 @@ func (this *InstallController) CreateAdmin(writer http.ResponseWriter, request * //验证超级管理员的信息 if m, _ := regexp.MatchString(`^[0-9a-zA-Z_]+$`, adminUsername); !m { - this.PanicBadRequest(`超级管理员用户名必填,且只能包含字母,数字和'_''`) + panic(result.BadRequest(`超级管理员用户名必填,且只能包含字母,数字和'_''`)) } if len(adminPassword) < 6 { - this.PanicBadRequest(`超级管理员密码长度至少为6位`) + panic(result.BadRequest(`超级管理员密码长度至少为6位`)) } if adminEmail == "" { - this.PanicBadRequest(`超级管理员邮箱必填`) + panic(result.BadRequest(`超级管理员邮箱必填`)) } //检查是否有重复。 @@ -325,14 +325,14 @@ func (this *InstallController) CreateAdmin(writer http.ResponseWriter, request * db1 := db.Model(&User{}).Where("email = ?", adminEmail).Count(&count1) this.PanicError(db1.Error) if count1 > 0 { - this.PanicBadRequest(`该邮箱已存在`) + panic(result.BadRequest(`该邮箱已存在`)) } var count2 int64 db2 := db.Model(&User{}).Where("username = ?", adminUsername).Count(&count2) this.PanicError(db2.Error) if count2 > 0 { - this.PanicBadRequest(`该用户名已存在`) + panic(result.BadRequest(`该用户名已存在`)) } user := &User{} @@ -369,24 +369,24 @@ func (this *InstallController) ValidateAdmin(writer http.ResponseWriter, request //验证超级管理员的信息 if adminEmail == "" { - this.PanicBadRequest(`超级管理员邮箱必填`) + panic(result.BadRequest(`超级管理员邮箱必填`)) } if len(adminPassword) < 6 { - this.PanicBadRequest(`超级管理员密码长度至少为6位`) + panic(result.BadRequest(`超级管理员密码长度至少为6位`)) } var existEmailUser = &User{} db = db.Where(&User{Email: adminEmail}).First(existEmailUser) if db.Error != nil { - this.PanicBadRequest(fmt.Sprintf("%s对应的用户不存在", adminEmail)) + panic(result.BadRequest(fmt.Sprintf("%s对应的用户不存在", adminEmail))) } if !tool.MatchBcrypt(adminPassword, existEmailUser.Password) { - this.PanicBadRequest("邮箱或密码错误") + panic(result.BadRequest("邮箱或密码错误")) } if existEmailUser.Role != USER_ROLE_ADMINISTRATOR { - this.PanicBadRequest("该账号不是管理员") + panic(result.BadRequest("该账号不是管理员")) } return this.Success("OK") @@ -422,7 +422,7 @@ func (this *InstallController) Finish(writer http.ResponseWriter, request *http. db1 := db.Model(&User{}).Where("role = ?", USER_ROLE_ADMINISTRATOR).Count(&count1) this.PanicError(db1.Error) if count1 == 0 { - this.PanicBadRequest(`请至少配置一名管理员`) + panic(result.BadRequest(`请至少配置一名管理员`)) } var configItem = &config.ConfigItem{ diff --git a/rest/matter_controller.go b/rest/matter_controller.go index a15930a..ce44a3d 100644 --- a/rest/matter_controller.go +++ b/rest/matter_controller.go @@ -72,7 +72,7 @@ func (this *MatterController) Detail(writer http.ResponseWriter, request *http.R uuid := request.FormValue("uuid") if uuid == "" { - this.PanicBadRequest("文件的uuid必填") + panic(result.BadRequest("文件的uuid必填")) } matter := this.matterService.Detail(uuid) @@ -284,7 +284,7 @@ func (this *MatterController) Delete(writer http.ResponseWriter, request *http.R uuid := request.FormValue("uuid") if uuid == "" { - this.PanicBadRequest("文件的uuid必填") + panic(result.BadRequest("文件的uuid必填")) } matter := this.matterDao.CheckByUuid(uuid) @@ -292,7 +292,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 { - this.PanicUnauthorized("没有权限") + panic(result.Unauthorized("没有权限")) } this.matterService.AtomicDelete(matter) @@ -305,7 +305,7 @@ func (this *MatterController) DeleteBatch(writer http.ResponseWriter, request *h uuids := request.FormValue("uuids") if uuids == "" { - this.PanicBadRequest("文件的uuids必填") + panic(result.BadRequest("文件的uuids必填")) } uuidArray := strings.Split(uuids, ",") @@ -323,7 +323,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 { - this.PanicUnauthorized("没有权限") + panic(result.Unauthorized("没有权限")) } this.matterService.AtomicDelete(matter) @@ -345,7 +345,7 @@ func (this *MatterController) Rename(writer http.ResponseWriter, request *http.R matter := this.matterDao.CheckByUuid(uuid) if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { - this.PanicUnauthorized("没有权限") + panic(result.Unauthorized("没有权限")) } this.matterService.AtomicRename(matter, name, user) @@ -371,7 +371,7 @@ func (this *MatterController) ChangePrivacy(writer http.ResponseWriter, request //权限验证 user := this.checkUser(writer, request) if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { - this.PanicUnauthorized("没有权限") + panic(result.Unauthorized("没有权限")) } matter.Privacy = privacy @@ -390,7 +390,7 @@ func (this *MatterController) Move(writer http.ResponseWriter, request *http.Req var srcUuids []string //验证参数。 if srcUuidsStr == "" { - this.PanicBadRequest("srcUuids参数必填") + panic(result.BadRequest("srcUuids参数必填")) } else { srcUuids = strings.Split(srcUuidsStr, ",") } @@ -405,11 +405,11 @@ func (this *MatterController) Move(writer http.ResponseWriter, request *http.Req //验证dest是否有问题 var destMatter = this.matterDao.CheckWithRootByUuid(destUuid, user) if !destMatter.Dir { - this.PanicBadRequest("目标不是文件夹") + panic(result.BadRequest("目标不是文件夹")) } if user.Role != USER_ROLE_ADMINISTRATOR && destMatter.UserUuid != user.Uuid { - this.PanicUnauthorized("没有权限") + panic(result.Unauthorized("没有权限")) } var srcMatters []*Matter @@ -419,14 +419,14 @@ func (this *MatterController) Move(writer http.ResponseWriter, request *http.Req srcMatter := this.matterDao.CheckByUuid(uuid) if srcMatter.Puuid == destMatter.Uuid { - this.PanicBadRequest("没有进行移动,操作无效!") + panic(result.BadRequest("没有进行移动,操作无效!")) } //判断同级文件夹中是否有同名的文件 count := this.matterDao.CountByUserUuidAndPuuidAndDirAndName(user.Uuid, destMatter.Uuid, srcMatter.Dir, srcMatter.Name) if count > 0 { - this.PanicBadRequest("【" + srcMatter.Name + "】在目标文件夹已经存在了,操作失败。") + panic(result.BadRequest("【" + srcMatter.Name + "】在目标文件夹已经存在了,操作失败。")) } //判断和目标文件夹是否是同一个主人。 diff --git a/rest/matter_dao.go b/rest/matter_dao.go index 87e750c..5bdf9d9 100644 --- a/rest/matter_dao.go +++ b/rest/matter_dao.go @@ -46,7 +46,7 @@ func (this *MatterDao) FindByUuid(uuid string) *Matter { func (this *MatterDao) CheckByUuid(uuid string) *Matter { matter := this.FindByUuid(uuid) if matter == nil { - this.PanicNotFound("%s 对应的matter不存在", uuid) + panic(result.NotFound("%s 对应的matter不存在", uuid)) } return matter } @@ -55,13 +55,13 @@ func (this *MatterDao) CheckByUuid(uuid string) *Matter { func (this *MatterDao) CheckWithRootByUuid(uuid string, user *User) *Matter { if uuid == "" { - this.PanicBadRequest("uuid cannot be nil.") + panic(result.BadRequest("uuid cannot be nil.")) } var matter *Matter if uuid == MATTER_ROOT { if user == nil { - this.PanicBadRequest("user cannot be nil.") + panic(result.BadRequest("user cannot be nil.")) } matter = NewRootMatter(user) } else { @@ -77,7 +77,7 @@ func (this *MatterDao) CheckWithRootByPath(path string, user *User) *Matter { var matter *Matter if user == nil { - this.PanicBadRequest("user cannot be nil.") + panic(result.BadRequest("user cannot be nil.")) } //目标文件夹matter @@ -349,11 +349,11 @@ func (this *MatterDao) findByUserUuidAndPath(userUuid string, path string) *Matt func (this *MatterDao) checkByUserUuidAndPath(userUuid string, path string) *Matter { if path == "" { - this.PanicBadRequest("path 不能为空") + panic(result.BadRequest("path 不能为空")) } matter := this.findByUserUuidAndPath(userUuid, path) if matter == nil { - this.PanicNotFound("path = %s 不存在", path) + panic(result.NotFound("path = %s 不存在", path)) } return matter diff --git a/rest/matter_service.go b/rest/matter_service.go index ddede10..bbfb6b9 100644 --- a/rest/matter_service.go +++ b/rest/matter_service.go @@ -277,7 +277,7 @@ func (this *MatterService) handleOverwrite(userUuid string, destinationPath stri //要求覆盖。那么删除。 this.matterDao.Delete(destMatter) } else { - this.PanicBadRequest("%s已经存在,操作失败!", destMatter.Path) + panic(result.BadRequest("%s已经存在,操作失败!", destMatter.Path)) } } @@ -291,7 +291,7 @@ func (this *MatterService) move(srcMatter *Matter, destDirMatter *Matter) { } if !destDirMatter.Dir { - this.PanicBadRequest("目标必须为文件夹") + panic(result.BadRequest("目标必须为文件夹")) } if srcMatter.Dir { @@ -352,7 +352,7 @@ func (this *MatterService) AtomicMove(srcMatter *Matter, destDirMatter *Matter, panic(result.BadRequest("destDirMatter cannot be nil.")) } if !destDirMatter.Dir { - this.PanicBadRequest("目标必须为文件夹") + panic(result.BadRequest("目标必须为文件夹")) } //文件夹不能把自己移入到自己中,也不可以移入到自己的子文件夹下。 @@ -389,7 +389,7 @@ func (this *MatterService) AtomicMoveBatch(srcMatters []*Matter, destDirMatter * } if !destDirMatter.Dir { - this.PanicBadRequest("目标必须为文件夹") + panic(result.BadRequest("目标必须为文件夹")) } //文件夹不能把自己移入到自己中,也不可以移入到自己的子文件夹下。 @@ -477,7 +477,7 @@ func (this *MatterService) AtomicCopy(srcMatter *Matter, destDirMatter *Matter, defer this.userService.MatterUnlock(srcMatter.UserUuid) if !destDirMatter.Dir { - this.PanicBadRequest("目标必须为文件夹") + panic(result.BadRequest("目标必须为文件夹")) } destinationPath := destDirMatter.Path + "/" + name @@ -490,7 +490,7 @@ func (this *MatterService) AtomicCopy(srcMatter *Matter, destDirMatter *Matter, func (this *MatterService) AtomicRename(matter *Matter, name string, user *User) { if user == nil { - this.PanicBadRequest("user cannot be nil") + panic(result.BadRequest("user cannot be nil")) } //操作锁 @@ -499,10 +499,10 @@ func (this *MatterService) AtomicRename(matter *Matter, name string, user *User) //验证参数。 if name == "" { - this.PanicBadRequest("name参数必填") + panic(result.BadRequest("name参数必填")) } if m, _ := regexp.MatchString(`[<>|*?/\\]`, name); m { - this.PanicBadRequest(`名称中不能包含以下特殊符号:< > | * ? / \`) + panic(result.BadRequest(`名称中不能包含以下特殊符号:< > | * ? / \`)) } if len(name) > 200 { @@ -510,14 +510,14 @@ func (this *MatterService) AtomicRename(matter *Matter, name string, user *User) } if name == matter.Name { - this.PanicBadRequest("新名称和旧名称一样,操作失败!") + panic(result.BadRequest("新名称和旧名称一样,操作失败!")) } //判断同级文件夹中是否有同名的文件 count := this.matterDao.CountByUserUuidAndPuuidAndDirAndName(user.Uuid, matter.Puuid, matter.Dir, name) if count > 0 { - this.PanicBadRequest("【" + name + "】已经存在了,请使用其他名称。") + panic(result.BadRequest("【" + name + "】已经存在了,请使用其他名称。")) } if matter.Dir { diff --git a/rest/preference_controller.go b/rest/preference_controller.go index 94d2a3e..f228705 100644 --- a/rest/preference_controller.go +++ b/rest/preference_controller.go @@ -92,7 +92,7 @@ func (this *PreferenceController) SystemCleanup(writer http.ResponseWriter, requ password := request.FormValue("password") if !tool.MatchBcrypt(password, user.Password) { - this.PanicBadRequest("密码错误,不能执行!") + panic(result.BadRequest("密码错误,不能执行!")) } for _, bean := range CONTEXT.BeanMap { diff --git a/rest/result/web_result.go b/rest/result/web_result.go index 779e254..4118d82 100644 --- a/rest/result/web_result.go +++ b/rest/result/web_result.go @@ -97,6 +97,22 @@ func BadRequest(format string, v ...interface{}) *WebResult { return CustomWebResult(CODE_WRAPPER_BAD_REQUEST, fmt.Sprintf(format, v...)) } +//没有权限 +func Unauthorized(format string, v ...interface{}) *WebResult { + return CustomWebResult(CODE_WRAPPER_UNAUTHORIZED, fmt.Sprintf(format, v...)) +} + +//没有找到 +func NotFound(format string, v ...interface{}) *WebResult { + return CustomWebResult(CODE_WRAPPER_NOT_FOUND, fmt.Sprintf(format, v...)) + +} + +//服务器内部出问题 +func Server(format string, v ...interface{}) *WebResult { + return CustomWebResult(CODE_WRAPPER_SERVER, fmt.Sprintf(format, v...)) +} + //所有的数据库错误情况 var ( DB_ERROR_DUPLICATE_KEY = "Error 1062: Duplicate entry" diff --git a/rest/user_controller.go b/rest/user_controller.go index 04a1f5b..5040bfd 100644 --- a/rest/user_controller.go +++ b/rest/user_controller.go @@ -51,18 +51,18 @@ func (this *UserController) Login(writer http.ResponseWriter, request *http.Requ if "" == email || "" == password { - this.PanicBadRequest("请输入邮箱和密码") + panic(result.BadRequest("请输入邮箱和密码")) } user := this.userDao.FindByEmail(email) if user == nil { - this.PanicBadRequest("邮箱或密码错误") + panic(result.BadRequest("邮箱或密码错误")) } else { if !tool.MatchBcrypt(password, user.Password) { - this.PanicBadRequest("邮箱或密码错误") + panic(result.BadRequest("邮箱或密码错误")) } } @@ -188,7 +188,7 @@ func (this *UserController) Edit(writer http.ResponseWriter, request *http.Reque user.SizeLimit = sizeLimit } else { if currentUser.Uuid != uuid { - this.PanicUnauthorized("没有权限") + panic(result.Unauthorized("没有权限")) } } @@ -310,11 +310,11 @@ func (this *UserController) Disable(writer http.ResponseWriter, request *http.Re loginUser := this.checkUser(writer, request) if uuid == loginUser.Uuid { - this.PanicBadRequest("你不能操作自己的状态。") + panic(result.BadRequest("你不能操作自己的状态。")) } if user.Status == USER_STATUS_DISABLED { - this.PanicBadRequest("用户已经被禁用,操作无效。") + panic(result.BadRequest("用户已经被禁用,操作无效。")) } user.Status = USER_STATUS_DISABLED @@ -333,11 +333,11 @@ func (this *UserController) Enable(writer http.ResponseWriter, request *http.Req user := this.userDao.CheckByUuid(uuid) loginUser := this.checkUser(writer, request) if uuid == loginUser.Uuid { - this.PanicBadRequest("你不能操作自己的状态。") + panic(result.BadRequest("你不能操作自己的状态。")) } if user.Status == USER_STATUS_OK { - this.PanicBadRequest("用户已经是正常状态,操作无效。") + panic(result.BadRequest("用户已经是正常状态,操作无效。")) } user.Status = USER_STATUS_OK @@ -354,7 +354,7 @@ func (this *UserController) ChangePassword(writer http.ResponseWriter, request * oldPassword := request.FormValue("oldPassword") newPassword := request.FormValue("newPassword") if oldPassword == "" || newPassword == "" { - this.PanicBadRequest("旧密码和新密码都不能为空") + panic(result.BadRequest("旧密码和新密码都不能为空")) } user := this.checkUser(writer, request) @@ -365,7 +365,7 @@ func (this *UserController) ChangePassword(writer http.ResponseWriter, request * } if !tool.MatchBcrypt(oldPassword, user.Password) { - this.PanicBadRequest("旧密码不正确!") + panic(result.BadRequest("旧密码不正确!")) } user.Password = tool.GetBcrypt(newPassword) @@ -381,16 +381,16 @@ func (this *UserController) ResetPassword(writer http.ResponseWriter, request *h userUuid := request.FormValue("userUuid") password := request.FormValue("password") if userUuid == "" { - this.PanicBadRequest("用户不能为空") + panic(result.BadRequest("用户不能为空")) } if password == "" { - this.PanicBadRequest("密码不能为空") + panic(result.BadRequest("密码不能为空")) } currentUser := this.checkUser(writer, request) if currentUser.Role != USER_ROLE_ADMINISTRATOR { - this.PanicUnauthorized("没有权限") + panic(result.Unauthorized("没有权限")) } user := this.userDao.CheckByUuid(userUuid) diff --git a/rest/user_service.go b/rest/user_service.go index a358fa1..c4ec74f 100644 --- a/rest/user_service.go +++ b/rest/user_service.go @@ -4,6 +4,7 @@ import ( "net/http" "tank/rest/cache" "tank/rest/config" + "tank/rest/result" "time" ) @@ -49,7 +50,7 @@ func (this *UserService) MatterLock(userUuid string) { //当前被锁住了。 if cacheItem != nil && cacheItem.Data() != nil { - this.PanicBadRequest("当前正在进行文件操作,请稍后再试!") + panic(result.BadRequest("当前正在进行文件操作,请稍后再试!")) } //添加一把新锁,有效期为12小时