Skip to content

Commit

Permalink
refactor(projects): 动态路由权限完善
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Apr 28, 2022
1 parent 401f0c7 commit 55ddc9c
Show file tree
Hide file tree
Showing 36 changed files with 398 additions and 709 deletions.
4 changes: 2 additions & 2 deletions .env.production
@@ -1,6 +1,6 @@
VITE_VISUALIZER=true
VITE_VISUALIZER=false

VITE_COMPRESS=true
VITE_COMPRESS=false

# gzip | brotliCompress | deflate | deflateRaw
VITE_COMPRESS_TYPE=gzip
38 changes: 8 additions & 30 deletions .eslintrc.js
Expand Up @@ -55,12 +55,11 @@ module.exports = {
group: 'external',
position: 'before'
},
// ui framework, such as "naive-ui"
// {
// pattern: 'naive-ui',
// group: 'external',
// position: 'before'
// },
{
pattern: 'naive-ui',
group: 'external',
position: 'before'
},
{
pattern: '@/config',
group: 'internal',
Expand Down Expand Up @@ -142,13 +141,7 @@ module.exports = {
position: 'before'
}
],
pathGroupsExcludedImportTypes: [
'vue',
'vue-router',
'vuex',
'pinia'
// 'naive-ui'
]
pathGroupsExcludedImportTypes: ['vue', 'vue-router', 'vuex', 'pinia', 'naive-ui']
}
],
'import/no-unresolved': 'off',
Expand All @@ -171,17 +164,7 @@ module.exports = {
ignores: ['index']
}
],
'@typescript-eslint/ban-types': [
'error',
{
types: {
'{}': {
message: 'Use object instead',
fixWith: 'object'
}
}
}
],
'@typescript-eslint/ban-types': 'off',
'@typescript-eslint/no-empty-interface': [
'error',
{
Expand All @@ -192,17 +175,12 @@ module.exports = {
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-shadow': 'error',
'@typescript-eslint/no-unused-vars': ['warn', { ignoreRestSiblings: true, varsIgnorePattern: '^_' }],
'@typescript-eslint/no-use-before-define': ['error', { classes: true, functions: false, typedefs: false }]
'@typescript-eslint/no-use-before-define': ['warn', { classes: true, functions: false, typedefs: false }]
},
overrides: [
{
files: ['*.vue'],
parser: 'vue-eslint-parser',
parserOptions: {
parser: '@typescript-eslint/parser'
},
rules: {
'no-unused-vars': 'off',
'no-undef': 'off'
}
},
Expand Down
4 changes: 2 additions & 2 deletions .vscode/settings.json
Expand Up @@ -33,7 +33,6 @@
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"javascript.updateImportsOnFileMove.enabled": "always",
"[javascriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
Expand Down Expand Up @@ -72,5 +71,6 @@
"business": "core",
"request": "api",
"adapter": "middleware"
}
},
"unocss.root": "src"
}
135 changes: 85 additions & 50 deletions mock/api/auth.ts
@@ -1,9 +1,10 @@
import type { MockMethod } from 'vite-plugin-mock';
import { userModel } from '../model';

const token: ApiAuth.Token = {
token: '__TEMP_TOKEN__',
refreshToken: '__TEMP_REFRESH_TOKEN__'
};
/** 参数错误的状态码 */
const ERROR_PARAM_CODE = 10000;

const ERROR_PARAM_MSG = '参数校验失败!';

const apis: MockMethod[] = [
// 获取验证码
Expand All @@ -18,73 +19,107 @@ const apis: MockMethod[] = [
};
}
},
// 密码登录
{
url: '/mock/loginByPwd',
method: 'post',
response: (): Service.MockServiceResult<ApiAuth.Token> => {
return {
code: 200,
message: 'ok',
data: token
};
}
},
// 验证码登录
// 用户+密码 登录
{
url: '/mock/loginByCode',
url: '/mock/login',
method: 'post',
response: (): Service.MockServiceResult<ApiAuth.Token> => {
response: (options: Service.MockOption): Service.MockServiceResult<ApiAuth.Token | null> => {
const { userName = undefined, password = undefined } = options.body;

if (!userName || !password) {
return {
code: ERROR_PARAM_CODE,
message: ERROR_PARAM_MSG,
data: null
};
}

const findItem = userModel.find(item => item.userName === userName && item.password === password);

if (findItem) {
return {
code: 200,
message: 'ok',
data: {
token: findItem.token,
refreshToken: findItem.refreshToken
}
};
}
return {
code: 200,
message: 'ok',
data: token
code: 1000,
message: '用户名或密码错误!',
data: null
};
}
},
// 获取用户信息(请求头携带token)
// 获取用户信息(请求头携带token, 根据token获取用户信息)
{
url: '/mock/getUserInfo',
method: 'get',
response: (): Service.MockServiceResult<ApiAuth.UserInfo> => {
return {
code: 200,
message: 'ok',
data: {
userId: '0',
userName: 'Soybean',
userPhone: '15170283876',
userRole: 'super'
}
};
}
},
{
url: '/mock/testToken',
method: 'post',
response: (option: Service.MockOption): Service.MockServiceResult<true | null> => {
if (option.headers?.authorization !== token.token) {
response: (options: Service.MockOption): Service.MockServiceResult<ApiAuth.UserInfo | null> => {
// 这里的mock插件得到的字段是authorization, 前端传递的是Authorization字段
const { authorization = '' } = options.headers;
const REFRESH_TOKEN_CODE = 66666;

if (!authorization) {
return {
code: 66666,
message: 'token 失效',
code: REFRESH_TOKEN_CODE,
message: '用户已失效或不存在!',
data: null
};
}
const userInfo: Auth.UserInfo = {
userId: '',
userName: '',
userRole: 'user'
};
const isInUser = userModel.some(item => {
const flag = item.token === authorization;
if (flag) {
const { userId: itemUserId, userName, userRole } = item;
Object.assign(userInfo, { userId: itemUserId, userName, userRole });
}
return flag;
});

if (isInUser) {
return {
code: 200,
message: 'ok',
data: userInfo
};
}

return {
code: 200,
message: 'ok',
data: true
code: REFRESH_TOKEN_CODE,
message: '用户信息异常!',
data: null
};
}
},
{
url: '/mock/updateToken',
method: 'post',
response: (): Service.MockServiceResult<ApiAuth.Token> => {
response: (options: Service.MockOption): Service.MockServiceResult<ApiAuth.Token | null> => {
const { refreshToken = '' } = options.body;

const findItem = userModel.find(item => item.refreshToken === refreshToken);

if (findItem) {
return {
code: 200,
message: 'ok',
data: {
token: findItem.token,
refreshToken: findItem.refreshToken
}
};
}
return {
code: 200,
message: 'ok',
data: token
code: 3000,
message: '用户已失效或不存在!',
data: null
};
}
}
Expand Down

1 comment on commit 55ddc9c

@vercel
Copy link

@vercel vercel bot commented on 55ddc9c Apr 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.