Skip to content

Commit

Permalink
feature: 添加切换权限功能
Browse files Browse the repository at this point in the history
  • Loading branch information
jsxiaosi committed Jun 14, 2022
1 parent 2b32625 commit 1070948
Show file tree
Hide file tree
Showing 17 changed files with 180 additions and 69 deletions.
20 changes: 19 additions & 1 deletion mock/demo/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ const userInfo = {
power: 'admin',
};

const userInfo2 = {
name: 'test',
userid: '00000002',
email: '[email protected]',
signature: '小啊小啊浪',
introduction: '一个只会喝蜂蜜绿的小前端',
title: '咪咪咪',
token: '',
power: 'test',
};

export default [
{
url: '/mock_api/login',
Expand All @@ -25,9 +36,16 @@ export default [
code: 1,
message: 'ok',
};
} else if (username == 'test' && password == 'test123') {
userInfo2.token = genID(16);
return {
data: userInfo2,
code: 1,
message: 'ok',
};
} else {
return {
data: userInfo,
data: null,
code: -1,
message: '账号密码错误',
};
Expand Down
43 changes: 28 additions & 15 deletions mock/demo/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const power = [
},
{
path: '/echarts',
name: 'echarts',
name: 'RtEcharts',

children: [
{
Expand All @@ -68,7 +68,7 @@ const power = [
},
{
path: '/editor',
name: 'RtEcharts',
name: 'RtEditor',
children: [
{
path: 'rich-text',
Expand All @@ -84,6 +84,23 @@ const power = [
},
],
},
{
path: '/system',
name: 'RtSystem',
children: [
{
path: 'power',
name: 'RtPower',
},
{
path: 'guide',
name: 'RtGuide',
},
],
},
];

const adminRoute = [
{
path: '/useradmin',
name: 'RtUseradmin',
Expand Down Expand Up @@ -136,16 +153,6 @@ const power = [
},
],
},
{
path: '/guide',
name: 'RtSystem',
children: [
{
path: '',
name: 'RtGuide',
},
],
},
{
path: '/external-link',
name: 'RtExternal',
Expand Down Expand Up @@ -178,15 +185,21 @@ export default [
const { name } = body;
if (name == 'admin') {
return {
data: power,
data: [...power, ...adminRoute],
code: 1,
message: 'ok',
};
} else if (name == 'test') {
return {
data: [...power],
code: 1,
message: 'ok',
};
} else {
return {
data: power,
data: [],
code: -1,
message: '账号密码错误',
message: '账号错误',
};
}
},
Expand Down
48 changes: 24 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,79 +24,79 @@
"build:push": "npm run build && npm run push"
},
"dependencies": {
"@element-plus/icons-vue": "^1.1.4",
"@logicflow/core": "^1.1.16",
"@logicflow/extension": "^1.1.16",
"@element-plus/icons-vue": "^2.0.4",
"@logicflow/core": "^1.1.20",
"@logicflow/extension": "^1.1.20",
"axios": "^0.27.2",
"echarts": "^5.3.2",
"element-plus": "^2.2.0",
"element-plus": "^2.2.5",
"es6-promise": "^4.2.8",
"i18next": "^21.8.3",
"i18next": "^21.8.9",
"image-conversion": "^2.1.1",
"intro.js": "^5.1.0",
"lodash-es": "^4.17.21",
"marked": "^4.0.16",
"marked": "^4.0.17",
"mockjs": "^1.1.0",
"pinia": "^2.0.14",
"sortablejs": "^1.15.0",
"vue": "^3.2.34",
"vue": "^3.2.37",
"vue-i18n": "^9.1.10",
"vue-router": "^4.0.15",
"vue-router": "^4.0.16",
"vue-types": "^4.1.1",
"vuex": "^4.0.2",
"wangeditor": "^4.7.15"
},
"devDependencies": {
"@commitlint/cli": "^17.0.0",
"@commitlint/config-conventional": "^17.0.0",
"@commitlint/cli": "^17.0.2",
"@commitlint/config-conventional": "^17.0.2",
"@ehutch79/vite-eslint": "^0.0.1",
"@types/core-js": "^2.5.5",
"@types/intro.js": "^3.0.2",
"@types/lodash-es": "^4.17.6",
"@types/marked": "^4.0.3",
"@types/node": "^17.0.35",
"@types/node": "^17.0.42",
"@types/sortablejs": "^1.13.0",
"@typescript-eslint/eslint-plugin": "^5.25.0",
"@typescript-eslint/parser": "^5.25.0",
"@typescript-eslint/eslint-plugin": "^5.27.1",
"@typescript-eslint/parser": "^5.27.1",
"@vitejs/plugin-vue": "^2.3.3",
"@vitejs/plugin-vue-jsx": "^1.3.10",
"@vue/compiler-sfc": "^3.2.34",
"@vue/compiler-sfc": "^3.2.37",
"autoprefixer": "^10.4.7",
"babel-eslint": "^10.1.0",
"commitizen": "^4.2.4",
"cz-conventional-changelog": "^3.3.0",
"cz-customizable": "^6.3.0",
"eslint": "^8.15.0",
"eslint": "^8.17.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-promise": "^6.0.0",
"eslint-plugin-vue": "^9.0.1",
"http-server": "^14.1.0",
"eslint-plugin-vue": "^9.1.1",
"http-server": "^14.1.1",
"husky": "^8.0.1",
"lint-staged": "^12.4.1",
"lint-staged": "^13.0.1",
"postcss-html": "^1.4.1",
"postcss-import": "^14.1.0",
"prettier": "2.6.2",
"pretty-quick": "^3.1.3",
"sass": "^1.51.0",
"stylelint": "^14.8.2",
"sass": "^1.52.3",
"stylelint": "^14.9.1",
"stylelint-config-prettier": "^9.0.3",
"stylelint-config-recess-order": "^3.0.0",
"stylelint-config-standard": "^25.0.0",
"stylelint-config-standard": "^26.0.0",
"stylelint-order": "^5.0.0",
"stylelint-scss": "^4.2.0",
"typescript": "^4.6.4",
"typescript": "^4.7.3",
"unplugin-element-plus": "^0.4.0",
"vite": "^2.9.9",
"vite": "^2.9.12",
"vite-plugin-compression": "^0.5.1",
"vite-plugin-linter": "^1.2.0",
"vite-plugin-mock": "^2.9.6",
"vite-plugin-pwa": "^0.12.0",
"vite-plugin-style-import": "^2.0.0",
"vite-plugin-svg-icons": "^2.0.1",
"vue-tsc": "^0.34.15"
"vue-tsc": "^0.37.5"
},
"license": "MIT",
"config": {
Expand Down
Binary file added src/assets/login/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions src/components/Application/AppAccount.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<template>
<div class="account">
<el-dropdown trigger="click" @command="command">
<img src="@/assets/login/logo.png" class="wave" />
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item command="signOut">退出登录</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
</div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router';
const router = useRouter();
const command = (value: string) => {
console.log(value);
if (value === 'signOut') {
localStorage.removeItem('userInfo');
router.push('/login');
}
};
</script>

<style lang="scss" scoped>
.account {
margin-left: 12px;
.wave {
width: 30px;
height: 30px;
border-radius: 50%;
}
}
</style>
2 changes: 2 additions & 0 deletions src/components/Application/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { withInstall } from '@/utils';
import appLocale from './AppLocale.vue';
import appTheme from './AppTheme.vue';
import appAccount from './AppAccount.vue';

export const AppLocale = withInstall(appLocale);
export const AppTheme = withInstall(appTheme);
export const AppAccount = withInstall(appAccount);
5 changes: 3 additions & 2 deletions src/layouts/pageLayouts/components/Navbart/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<div class="navbar-right">
<AppTheme></AppTheme>
<AppLocale class="icon"></AppLocale>
<AppAccount></AppAccount>
<SvgIcon class="icon" name="iEL-setting" @click="drawer = true"></SvgIcon>
</div>

Expand All @@ -37,7 +38,7 @@
// import { ref } from 'vue'
import Sidebar from '../../components/Sidebar/index.vue';
import Setting from '../../components/Seting/index.vue';
import { AppLocale, AppTheme } from '@/components/Application';
import { AppLocale, AppTheme, AppAccount } from '@/components/Application';
import SvgIcon from '@/components/SvgIcon/index.vue';
import { ref } from 'vue';
Expand Down Expand Up @@ -100,7 +101,7 @@
.icon {
margin-left: 12px;
font-size: 18px;
font-size: 20px;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/pageLayouts/components/Sidebar/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import { getAppCollapseMenu } from '@/hooks/userAppWindow';
import { usePermissionStoreHook } from '@/store/modules/permission';
const routeModuleList = usePermissionStoreHook().wholeMenus;
const routeModuleList = computed(() => usePermissionStoreHook().wholeMenus);
defineProps({
mode: {
Expand Down
2 changes: 2 additions & 0 deletions src/locales/en/modules/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const route = {
userList: 'UserList',
refSyntax: 'RefSyntax',
userDateil: 'UserDateil',
system: 'System',
power: 'Power',
guide: 'GuidePage',
thirdParty: 'Third party',
nested: 'Nested Route',
Expand Down
2 changes: 2 additions & 0 deletions src/locales/zh-ch/modules/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const route = {
userList: '用户列表',
refSyntax: 'ref语法糖',
userDateil: '用户详情',
system: '系统',
power: '权限',
guide: '引导页',
thirdParty: '第三方',
nested: '嵌套路由',
Expand Down
17 changes: 10 additions & 7 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,28 @@ router.beforeEach((to, from, next) => {
}

const userInfo = localStorage.getItem('userInfo');

if (userInfo) {
if (from.name) {
next();
// 已登陆状态不允许去登录页
if (to.path === '/login') {
next({ path: from.path });
} else {
next();
}
} else {
if (usePermissionStoreHook().wholeMenus.length === 0) {
initAsyncRoute(JSON.parse(userInfo).power || '').then(() => {
if (router.hasRoute(to.name || '')) {
initAsyncRoute(JSON.parse(userInfo).power || '').then((res) => {
if (res.length) {
router.push(to.path);
} else {
router.push('/');
localStorage.removeItem('userInfo');
router.push('login');
}
});
} else {
next();
}
}
// if (to.path === '/login') next(from.path);
// else next();
} else {
if (to.path !== '/login') {
next({ path: '/login' });
Expand Down
17 changes: 11 additions & 6 deletions src/router/modules/otherRoute/otherRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,22 @@ const safeManagerRoutes: Array<AppRouteRecordRaw> = [
],
},
{
path: '/guide',
path: '/system',
component: Layout,
redirect: '/guide',
redirect: '/system/power',
name: 'RtSystem',
alwaysShow: false,
meta: { title: '', icon: 'guide' },
meta: { title: 'route.pathName.system', icon: 'guide' },
children: [
{
path: '',
path: 'power',
name: 'RtPower',
component: () => import('@/views/system/power/index.vue'),
meta: { title: t('route.pathName.power') },
},
{
path: 'guide',
name: 'RtGuide',
component: () => import('@/views/guide/index.vue'),
component: () => import('@/views/system/guide/index.vue'),
meta: { title: t('route.pathName.guide') },
},
],
Expand Down
Loading

0 comments on commit 1070948

Please sign in to comment.