From 94281558ae251671737c3fd474f067a56733fa87 Mon Sep 17 00:00:00 2001 From: zhengkunwang223 <31820853+zhengkunwang223@users.noreply.github.com> Date: Wed, 17 Aug 2022 11:47:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E4=B9=8B=E5=90=8E=E7=9A=84=E8=B7=B3=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + backend/constant/errs.go | 1 + backend/i18n/lang/en.yaml | 3 ++- backend/i18n/lang/zh.yaml | 3 ++- backend/middleware/csrf.go | 2 +- backend/middleware/session.go | 4 +-- frontend/src/api/helper/check-status.ts | 33 +++++++------------------ frontend/src/api/index.ts | 2 +- frontend/src/enums/http-enum.ts | 1 + frontend/src/lang/modules/en.ts | 7 ++++++ frontend/src/lang/modules/zh.ts | 7 ++++++ 11 files changed, 34 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index 66fd13c90..0ca2a0068 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.dll *.so *.dylib +.idea # Test binary, built with `go test -c` *.test diff --git a/backend/constant/errs.go b/backend/constant/errs.go index 3163f92f8..83ef571ec 100644 --- a/backend/constant/errs.go +++ b/backend/constant/errs.go @@ -32,4 +32,5 @@ var ( ErrTypeInvalidParams = "ErrInvalidParams" ErrTypeToken = "ErrToken" ErrTypeTokenTimeOut = "ErrTokenTimeOut" + ErrTypeNotLogin = "ErrNotLogin" ) diff --git a/backend/i18n/lang/en.yaml b/backend/i18n/lang/en.yaml index 74af8728f..5e955c163 100644 --- a/backend/i18n/lang/en.yaml +++ b/backend/i18n/lang/en.yaml @@ -6,4 +6,5 @@ ErrCaptchaCode: "The verification code information is incorrect" ErrInternalServer: "Service internal error: {{ .detail }}" ErrRecordExist: "Record already exists: {{ .detail }}" ErrRecordNotFound: "Records not found: {{ .detail }}" -ErrStructTransform: "Type conversion failure: {{ .detail }}" \ No newline at end of file +ErrStructTransform: "Type conversion failure: {{ .detail }}" +ErrTypeNotLogin: "User is not Login" \ No newline at end of file diff --git a/backend/i18n/lang/zh.yaml b/backend/i18n/lang/zh.yaml index ff4c042cb..14fad1bbe 100644 --- a/backend/i18n/lang/zh.yaml +++ b/backend/i18n/lang/zh.yaml @@ -6,4 +6,5 @@ ErrCaptchaCode: "错误的验证码信息" ErrInternalServer: "服务内部错误: {{ .detail }}" ErrRecordExist: "记录已存在: {{ .detail }}" ErrRecordNotFound: "记录未能找到: {{ .detail }}" -ErrStructTransform: "类型转换失败: {{ .detail }}" \ No newline at end of file +ErrStructTransform: "类型转换失败: {{ .detail }}" +ErrTypeNotLogin: "用户未登录" \ No newline at end of file diff --git a/backend/middleware/csrf.go b/backend/middleware/csrf.go index 6b8d18659..4182edb7f 100644 --- a/backend/middleware/csrf.go +++ b/backend/middleware/csrf.go @@ -15,7 +15,7 @@ func CSRF() gin.HandlerFunc { csrf.ErrorHandler(http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusForbidden) - _, _ = w.Write([]byte("message: csrf token invalid")) + _, _ = w.Write([]byte("csrf token invalid")) })), ) return adapter.Wrap(csrfMd) diff --git a/backend/middleware/session.go b/backend/middleware/session.go index 3d3918bc2..ba879cb9f 100644 --- a/backend/middleware/session.go +++ b/backend/middleware/session.go @@ -14,11 +14,11 @@ func SessionAuth() gin.HandlerFunc { } sId, err := c.Cookie(global.CONF.Session.SessionName) if err != nil { - helper.ErrorWithDetail(c, constant.CodeErrUnauthorized, constant.ErrTypeToken, nil) + helper.ErrorWithDetail(c, constant.CodeErrUnauthorized, constant.ErrTypeNotLogin, nil) return } if _, err := global.SESSION.Get(sId); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrUnauthorized, constant.ErrTypeToken, nil) + helper.ErrorWithDetail(c, constant.CodeErrUnauthorized, constant.ErrTypeNotLogin, nil) return } c.Next() diff --git a/frontend/src/api/helper/check-status.ts b/frontend/src/api/helper/check-status.ts index edff72c1a..891d607e8 100644 --- a/frontend/src/api/helper/check-status.ts +++ b/frontend/src/api/helper/check-status.ts @@ -1,4 +1,6 @@ +import i18n from '@/lang'; import { ElMessage } from 'element-plus'; +import router from '@/routers'; /** * @description: 校验网络请求状态码 @@ -8,36 +10,19 @@ import { ElMessage } from 'element-plus'; export const checkStatus = (status: number): void => { switch (status) { case 400: - ElMessage.error('请求失败!请您稍后重试'); - break; - case 401: - ElMessage.error('登录失效!请您重新登录'); - break; - case 403: - ElMessage.error('当前账号无权限访问!'); + ElMessage.error(i18n.global.t('commons.res.paramError')); break; case 404: - ElMessage.error('你所访问的资源不存在!'); + ElMessage.error(i18n.global.t('commons.res.notFound')); break; - case 405: - ElMessage.error('请求方式错误!请您稍后重试'); - break; - case 408: - ElMessage.error('请求超时!请您稍后重试'); + case 403: + router.replace({ path: '/login' }); + ElMessage.error(i18n.global.t('commons.res.forbidden')); break; case 500: - ElMessage.error('服务异常!'); - break; - case 502: - ElMessage.error('网关错误!'); - break; - case 503: - ElMessage.error('服务不可用!'); - break; - case 504: - ElMessage.error('网关超时!'); + ElMessage.error(i18n.global.t('commons.res.serverError')); break; default: - ElMessage.error('请求失败!'); + ElMessage.error(i18n.global.t('commons.res.commonError')); } }; diff --git a/frontend/src/api/index.ts b/frontend/src/api/index.ts index bbd12ff96..f091df17f 100644 --- a/frontend/src/api/index.ts +++ b/frontend/src/api/index.ts @@ -49,7 +49,7 @@ class RequestHttp { } axiosCanceler.removePending(config); tryHideFullScreenLoading(); - if (data.code == ResultEnum.OVERDUE) { + if (data.code == ResultEnum.OVERDUE || data.code == ResultEnum.FORBIDDEN) { ElMessage.error(data.msg); router.replace({ path: '/login', diff --git a/frontend/src/enums/http-enum.ts b/frontend/src/enums/http-enum.ts index cdd38613b..1415ee917 100644 --- a/frontend/src/enums/http-enum.ts +++ b/frontend/src/enums/http-enum.ts @@ -2,6 +2,7 @@ export enum ResultEnum { SUCCESS = 200, ERROR = 500, OVERDUE = 401, + FORBIDDEN = 403, TIMEOUT = 10000, TYPE = 'success', } diff --git a/frontend/src/lang/modules/en.ts b/frontend/src/lang/modules/en.ts index f19bb8d20..2d9dafeac 100644 --- a/frontend/src/lang/modules/en.ts +++ b/frontend/src/lang/modules/en.ts @@ -39,6 +39,13 @@ export default { commonName: 'Support English, Chinese, numbers, .-_, length 1-30', email: 'Email format error', }, + res: { + paramError: 'The request failed, please try again later!', + forbidden: 'The current user has no permission', + serverError: 'Service exception', + notFound: 'The resource does not exist', + commonError: 'The request failed', + }, }, business: { user: { diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index cea59fca4..9899274c1 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -39,6 +39,13 @@ export default { commonName: '支持英文、中文、数字、.-_,长度1-30', email: '邮箱格式错误', }, + res: { + paramError: '请求失败,请稍后重试!', + forbidden: '当前用户无权限', + serverError: '服务异常', + notFound: '资源不存在', + commonError: '请求失败', + }, }, business: { user: {