Browse Source

feat: 增加验证失败之后的跳转

pull/9/head
zhengkunwang223 2 years ago
parent
commit
94281558ae
  1. 1
      .gitignore
  2. 1
      backend/constant/errs.go
  3. 3
      backend/i18n/lang/en.yaml
  4. 3
      backend/i18n/lang/zh.yaml
  5. 2
      backend/middleware/csrf.go
  6. 4
      backend/middleware/session.go
  7. 33
      frontend/src/api/helper/check-status.ts
  8. 2
      frontend/src/api/index.ts
  9. 1
      frontend/src/enums/http-enum.ts
  10. 7
      frontend/src/lang/modules/en.ts
  11. 7
      frontend/src/lang/modules/zh.ts

1
.gitignore vendored

@ -4,6 +4,7 @@
*.dll
*.so
*.dylib
.idea
# Test binary, built with `go test -c`
*.test

1
backend/constant/errs.go

@ -32,4 +32,5 @@ var (
ErrTypeInvalidParams = "ErrInvalidParams"
ErrTypeToken = "ErrToken"
ErrTypeTokenTimeOut = "ErrTokenTimeOut"
ErrTypeNotLogin = "ErrNotLogin"
)

3
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 }}"
ErrStructTransform: "Type conversion failure: {{ .detail }}"
ErrTypeNotLogin: "User is not Login"

3
backend/i18n/lang/zh.yaml

@ -6,4 +6,5 @@ ErrCaptchaCode: "错误的验证码信息"
ErrInternalServer: "服务内部错误: {{ .detail }}"
ErrRecordExist: "记录已存在: {{ .detail }}"
ErrRecordNotFound: "记录未能找到: {{ .detail }}"
ErrStructTransform: "类型转换失败: {{ .detail }}"
ErrStructTransform: "类型转换失败: {{ .detail }}"
ErrTypeNotLogin: "用户未登录"

2
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)

4
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()

33
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('你所访问的资源不存在!');
break;
case 405:
ElMessage.error('请求方式错误!请您稍后重试');
ElMessage.error(i18n.global.t('commons.res.notFound'));
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'));
}
};

2
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',

1
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',
}

7
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: {

7
frontend/src/lang/modules/zh.ts

@ -39,6 +39,13 @@ export default {
commonName: '支持英文、中文、数字、.-_,长度1-30',
email: '邮箱格式错误',
},
res: {
paramError: '请求失败,请稍后重试!',
forbidden: '当前用户无权限',
serverError: '服务异常',
notFound: '资源不存在',
commonError: '请求失败',
},
},
business: {
user: {

Loading…
Cancel
Save