Commit ea21c88e authored by 水落(YangLei)'s avatar 水落(YangLei)

feat: 路由逻辑处理

parent 42e2fa95
import { request, METHOD } from '@/utils';
export function getUserDetailInfoApi() {
return request('/api/v1/detail', METHOD.GET);
}
<template> <template>
<a-layout :class="['admin-layout', 'beauty-scroll']"> <a-layout :class="['admin-layout', 'beauty-scroll']">
<drawer v-if="isMobile" v-model="drawerOpen"> <drawer v-if="isMobile" v-model="drawerOpen">
<side-menu :theme="theme.mode" :menuData="menuData" :collapsed="false" :collapsible="false" @menuSelect="onMenuSelect" /> <side-menu
:theme="theme.mode"
:menuData="menuData"
:collapsed="false"
:collapsible="false"
@menuSelect="onMenuSelect"
/>
</drawer> </drawer>
<side-menu :class="[fixedSideBar ? 'fixed-side' : '']" :theme="theme.mode" v-else-if="layout === 'side' || layout === 'mix'" :menuData="sideMenuData" :collapsed="collapsed" :collapsible="true" /> <side-menu
:class="[fixedSideBar ? 'fixed-side' : '']"
:theme="theme.mode"
v-else-if="layout === 'side' || layout === 'mix'"
:menuData="sideMenuData"
:collapsed="collapsed"
:collapsible="true"
/>
<div v-if="fixedSideBar && !isMobile" :style="`width: ${sideMenuWidth}; min-width: ${sideMenuWidth};max-width: ${sideMenuWidth};`" class="virtual-side"> <div
</div> v-if="fixedSideBar && !isMobile"
:style="`width: ${sideMenuWidth}; min-width: ${sideMenuWidth};max-width: ${sideMenuWidth};`"
class="virtual-side"
></div>
<drawer v-if="!hideSettingExtend" v-model="showSetting" placement="right"> <drawer v-if="!hideSettingExtend" v-model="showSetting" placement="right">
<div class="setting" slot="handler"> <div class="setting" slot="handler">
...@@ -17,8 +33,20 @@ ...@@ -17,8 +33,20 @@
</drawer> </drawer>
<a-layout class="admin-layout-main beauty-scroll"> <a-layout class="admin-layout-main beauty-scroll">
<layout-top-header :class="[{'fixed-tabs': fixedTabs, 'fixed-header': fixedHeader, 'multi-page': multiPage}]" :style="headerStyle" :menuData="headMenuData" :collapsed="collapsed" @toggleCollapse="toggleCollapse" /> <layout-top-header
<a-layout-header :class="['virtual-header', {'fixed-tabs' : fixedTabs, 'fixed-header': fixedHeader, 'multi-page': multiPage}]" v-show="fixedHeader"></a-layout-header> :class="[{ 'fixed-tabs': fixedTabs, 'fixed-header': fixedHeader, 'multi-page': multiPage }]"
:style="headerStyle"
:menuData="headMenuData"
:collapsed="collapsed"
@toggleCollapse="toggleCollapse"
/>
<a-layout-header
:class="[
'virtual-header',
{ 'fixed-tabs': fixedTabs, 'fixed-header': fixedHeader, 'multi-page': multiPage },
]"
v-show="fixedHeader"
></a-layout-header>
<a-layout-content class="admin-layout-content" :style="`min-height: ${minHeight}px;`"> <a-layout-content class="admin-layout-content" :style="`min-height: ${minHeight}px;`">
<div style="position: relative"> <div style="position: relative">
<slot></slot> <slot></slot>
...@@ -126,7 +154,7 @@ export default { ...@@ -126,7 +154,7 @@ export default {
matched = matched.slice(0, matched.length - 1); matched = matched.slice(0, matched.length - 1);
const { firstMenu } = this; const { firstMenu } = this;
for (let menu of firstMenu) { for (let menu of firstMenu) {
if (matched.findIndex((item) => item.path === menu.fullPath) !== -1) { if (matched.findIndex(item => item.path === menu.fullPath) !== -1) {
this.setActivatedFirst(menu.fullPath); this.setActivatedFirst(menu.fullPath);
break; break;
} }
......
...@@ -78,7 +78,7 @@ ...@@ -78,7 +78,7 @@
<script> <script>
import CommonLayout from '@/pages/frame/layouts/CommonLayout'; import CommonLayout from '@/pages/frame/layouts/CommonLayout';
import { login } from '@/pages/frame/services/accountService'; import { login } from '@/pages/frame/services/accountService';
import { setToken, clearToken } from '@/utils/requestUtil'; import { setToken, clearToken, setUserId } from '@/utils';
import { loadRoutes } from '@/utils/routerUtil'; import { loadRoutes } from '@/utils/routerUtil';
import { mapMutations } from 'vuex'; import { mapMutations } from 'vuex';
import loginI18n from './i18n'; import loginI18n from './i18n';
...@@ -120,19 +120,9 @@ export default { ...@@ -120,19 +120,9 @@ export default {
afterLogin(result) { afterLogin(result) {
this.logging = false; this.logging = false;
const { token, userId } = result; const { token, userId } = result;
console.log(token);
setToken(token); setToken(token);
setUserId(userId);
this.$router.replace('/dashboard/workbench'); this.$router.replace('/dashboard/workbench');
// this.setUser(user);
// this.setPermissions(permissions);
// this.setRoles(roles);
// setAuthorization({
// token: loginResult.data.token,
// expireAt: new Date(loginResult.data.expireAt),
// });
// // 获取路由配置
// loadRoutes(routers);
}, },
}, },
}; };
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
</template> </template>
<script> <script>
import PageLayout from '../../layouts/PageLayout'; import PageLayout from '../../layouts/PageLayout.vue';
import PageToggleTransition from '@/components/transition/PageToggleTransition'; import PageToggleTransition from '@/components/transition/PageToggleTransition';
import { mapState } from 'vuex'; import { mapState } from 'vuex';
......
<template>
<h1>Home</h1>
</template>
...@@ -26,32 +26,17 @@ ...@@ -26,32 +26,17 @@
</template> </template>
<script> <script>
import { convertListToTree } from '@/utils';
export default { export default {
data: () => ({ data: () => ({
add: {}, add: {},
addBtn: { text: '新建', title: '菜单配置' }, addBtn: { text: '新建', title: '菜单配置' },
sortOrder: 'ascend', sortOrder: 'ascend',
}), }),
methods: { methods: {
formatData(data = []) { formatData: convertListToTree,
let menu,
res = [],
tmp = data.slice();
while ((menu = tmp.pop())) {
if (menu.parentMenuId === 0) res.push(menu);
else {
for (const resetMenu of data) {
if (menu.parentMenuId === resetMenu.menuId) {
resetMenu.children
? resetMenu.children.push(menu)
: (resetMenu.children = [menu]);
}
}
}
}
console.log(res);
return res;
},
sorter(pre, next) { sorter(pre, next) {
return pre.viewIndex - next.viewIndex; return pre.viewIndex - next.viewIndex;
}, },
......
import routesI18n from '@/router/i18n' import routesI18n from '@/router/i18n';
import {getI18nKey} from '@/utils/routerUtil' import { getI18nKey } from '@/utils/routerUtil';
/** /**
* 根据 router options 配置生成 国际化语言 * 根据 router options 配置生成 国际化语言
...@@ -10,22 +10,28 @@ import {getI18nKey} from '@/utils/routerUtil' ...@@ -10,22 +10,28 @@ import {getI18nKey} from '@/utils/routerUtil'
*/ */
function generateI18n(lang, routes, valueKey) { function generateI18n(lang, routes, valueKey) {
routes.forEach(route => { routes.forEach(route => {
let keys = getI18nKey(route.fullPath).split('.') let keys = getI18nKey(route.fullPath).split('.');
let value = valueKey === 'path' ? route[valueKey].split('/').filter(item => !item.startsWith(':') && item != '').join('.') : route[valueKey] console.log(keys);
let value =
valueKey === 'path'
? route[valueKey]
.split('/')
.filter(item => !item.startsWith(':') && item != '')
.join('.')
: route[valueKey];
if (value.includes('_')) { if (value.includes('_')) {
value = value.replace('_', ' '); value = value.replace('_', ' ');
value = value.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase()); value = value.toLowerCase().replace(/( |^)[a-z]/g, L => L.toUpperCase());
} } else {
else { value = value.charAt(0).toUpperCase() + value.toLowerCase().substring(1);
value = value.charAt(0).toUpperCase() + value.toLowerCase().substring(1)
} }
lang.assignProps(keys, value) lang.assignProps(keys, value);
if (route.children) { if (route.children) {
generateI18n(lang, route.children, valueKey) generateI18n(lang, route.children, valueKey);
} }
}) });
return lang return lang;
} }
/** /**
...@@ -35,12 +41,16 @@ function generateI18n(lang, routes, valueKey) { ...@@ -35,12 +41,16 @@ function generateI18n(lang, routes, valueKey) {
*/ */
function formatFullPath(routes, parentPath = '') { function formatFullPath(routes, parentPath = '') {
routes.forEach(route => { routes.forEach(route => {
let isFullPath = route.path.substring(0, 1) === '/' let isFullPath = route.path.substring(0, 1) === '/';
route.fullPath = isFullPath ? route.path : (parentPath === '/' ? parentPath + route.path : parentPath + '/' + route.path) route.fullPath = isFullPath
? route.path
: parentPath === '/'
? parentPath + route.path
: parentPath + '/' + route.path;
if (route.children) { if (route.children) {
formatFullPath(route.children, route.fullPath) formatFullPath(route.children, route.fullPath);
} }
}) });
} }
/** /**
...@@ -49,16 +59,16 @@ function formatFullPath(routes, parentPath = '') { ...@@ -49,16 +59,16 @@ function formatFullPath(routes, parentPath = '') {
* @param routes * @param routes
*/ */
function mergeI18nFromRoutes(i18n, routes) { function mergeI18nFromRoutes(i18n, routes) {
formatFullPath(routes) formatFullPath(routes);
const zh_CN = generateI18n(new Object(), routes, 'name') const zh_CN = generateI18n(new Object(), routes, 'name');
const en_US = generateI18n(new Object(), routes, 'path') const en_US = generateI18n(new Object(), routes, 'path');
i18n.mergeLocaleMessage('zh_CN', zh_CN) i18n.mergeLocaleMessage('zh_CN', zh_CN);
i18n.mergeLocaleMessage('en_US', en_US) i18n.mergeLocaleMessage('en_US', en_US);
const messages = routesI18n.messages const messages = routesI18n.messages;
Object.keys(messages).forEach(lang => { Object.keys(messages).forEach(lang => {
i18n.mergeLocaleMessage(lang, messages[lang]) i18n.mergeLocaleMessage(lang, messages[lang]);
}) });
} }
/** /**
...@@ -71,22 +81,19 @@ Object.defineProperty(Object.prototype, 'assignProps', { ...@@ -71,22 +81,19 @@ Object.defineProperty(Object.prototype, 'assignProps', {
writable: false, writable: false,
enumerable: false, enumerable: false,
configurable: true, configurable: true,
value: function (keys, value) { value: function(keys, value) {
let props = this let props = this;
for (let i = 0; i < keys.length; i++) { for (let i = 0; i < keys.length; i++) {
let key = keys[i] let key = keys[i];
if (i == keys.length - 1) { if (i == keys.length - 1) {
props[key] = value props[key] = value;
} else { } else {
props[key] = props[key] == undefined ? {} : props[key] props[key] = props[key] == undefined ? {} : props[key];
props = props[key] props = props[key];
} }
} }
return this return this;
} },
}) });
export { export { mergeI18nFromRoutes, formatFullPath };
mergeI18nFromRoutes,
formatFullPath
}
export { default as langUtil } from './langUtils'; export { default as langUtil } from './langUtils';
export * from './requestUtil';
const USERID_KEY = 'userId';
export function getUserId() {
return window.sessionStorage.getItem(USERID_KEY) || '';
}
export function setUserId(val) {
window.sessionStorage.setItem(USERID_KEY, val);
}
/**
* 转变菜单列表为tree结构
* @param {Array} menuList 菜单列表
*/
export function convertListToTree(menuList) {
const tempMenu = [...menuList];
for (const menu of menuList) {
if (menu.parentMenuId === 0) continue;
const parent = menuList.find(m => m.menuId === menu.parentMenuId);
parent.children ? parent.children.push(menu) : (parent.children = [menu]);
}
return tempMenu.filter(m => m.parentMenuId === 0);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment