Skip to content

Commit 76d979a

Browse files
authored
Optimize (#442)
* feat(*): Continuously optimize code re #440 * improvement(*): optimze code as usual re $440
1 parent 3c548ab commit 76d979a

File tree

14 files changed

+120
-109
lines changed

14 files changed

+120
-109
lines changed

package-lock.json

Lines changed: 24 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
"@antv/g2plot": "^0.11.35",
1717
"@babel/polyfill": "^7.4.4",
1818
"@tinymce/tinymce-vue": "^2.0.0",
19-
"axios": "~0.18.0",
19+
"axios": "^0.21.1",
2020
"core-js": "^3.6.5",
2121
"dayjs": "^1.10.4",
22-
"element-plus": "^1.0.2-beta.36",
22+
"element-plus": "^1.0.2-beta.39",
2323
"event-source-polyfill": "^1.0.7",
2424
"fastscan": "^1.0.4",
2525
"good-storage": "^1.1.0",

src/component/layout/app-main.vue

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
<template>
22
<section class="container">
33
<div class="wrapper" id="wrapper">
4-
<transition name="fade-transform" mode="out-in"> <router-view></router-view> </transition>
4+
<router-view v-slot="{ Component }">
5+
<transition appear name="fade-transform" mode="out-in">
6+
<component :is="Component" />
7+
</transition>
8+
</router-view>
59
</div>
610
</section>
711
</template>
812

913
<script>
1014
export default {
1115
name: 'AppMain',
12-
watch: {
13-
$route() {
14-
if (this.$previewInstance) {
15-
this.$previewInstance.destroy()
16-
}
17-
},
18-
},
1916
}
2017
</script>
2118

src/component/layout/nav-bar.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
</template>
2323

2424
<script>
25-
import User from './user'
2625
import store from '@/store'
2726
import Config from '@/config'
27+
import { getToken } from '@/lin/util/token'
28+
import User from './user'
2829
import ClearTab from './clear-tab'
2930
import Breadcrumb from './breadcrumb'
3031
import Screenfull from './screen-full'
31-
import { getToken } from '@/lin/util/token'
3232
3333
export default {
3434
name: 'NavBar',

src/config/stage/index.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import Utils from '@/lin/util/util'
12
import adminConfig from './admin'
23
import bookConfig from './book' // 引入图书管理路由文件
34
import pluginsConfig from './plugin'
4-
import Utils from '@/lin/util/util'
55

66
// eslint-disable-next-line import/no-mutable-exports
77
let homeRouter = [
@@ -48,9 +48,20 @@ let homeRouter = [
4848
adminConfig,
4949
]
5050

51+
// 接入插件
5152
const plugins = [...pluginsConfig]
53+
filterPlugin(homeRouter)
54+
homeRouter = homeRouter.concat(plugins)
5255

53-
// 筛除已经被添加的插件
56+
// 处理顺序
57+
homeRouter = Utils.sortByOrder(homeRouter)
58+
deepReduceName(homeRouter)
59+
60+
export default homeRouter
61+
62+
/**
63+
* 筛除已经被添加的插件
64+
*/
5465
function filterPlugin(data) {
5566
if (plugins.length === 0) {
5667
return
@@ -70,16 +81,10 @@ function filterPlugin(data) {
7081
}
7182
}
7283

73-
filterPlugin(homeRouter)
74-
75-
homeRouter = homeRouter.concat(plugins)
76-
77-
// 处理顺序
78-
homeRouter = Utils.sortByOrder(homeRouter)
7984
/**
8085
* 使用 Symbol 处理 name 字段, 保证唯一性
8186
*/
82-
const deepReduceName = target => {
87+
function deepReduceName(target) {
8388
if (Array.isArray(target)) {
8489
target.forEach(item => {
8590
if (typeof item !== 'object') {
@@ -105,7 +110,3 @@ const deepReduceName = target => {
105110
}
106111
}
107112
}
108-
109-
deepReduceName(homeRouter)
110-
111-
export default homeRouter

src/lin/model/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import store from '@/store'
12
import _axios, { post, get, put } from '@/lin/plugin/axios'
23
import { saveTokens } from '../util/token'
3-
import store from '@/store'
44

55
export default class User {
66
/**

src/lin/util/sse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default class Sse {
4545
this.source.addEventListener(eventName, event => {
4646
// console.log('receive one message: ', event.data)
4747
// console.log('receive one message: ', event.lastEventId)
48-
store.commit('ADD_UNREAD_MESSAGE', { data: event.data, id: event.lastEventId })
48+
store.commit('MARK_UNREAD_MESSAGE', { data: event.data, id: event.lastEventId })
4949
ElMessage.warning(JSON.parse(event.data).message)
5050
})
5151
}

src/store/action.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ export default {
1313

1414
readMessage({ commit }, message) {
1515
commit(types.REMOVE_UNREAD_MESSAGE, message.id)
16-
commit(types.ADD_READED_MESSAGE, message)
16+
commit(types.MARK_READ_MESSAGE, message)
1717
},
1818
}

src/store/getter.js

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,6 @@ import Util from '@/lin/util/util'
22

33
let stageMap = {}
44

5-
const deepTravel = (obj, fuc) => {
6-
if (Array.isArray(obj)) {
7-
obj.forEach(item => {
8-
deepTravel(item, fuc)
9-
})
10-
return
11-
}
12-
if (obj && obj.children) {
13-
fuc(obj)
14-
deepTravel(obj.children, fuc)
15-
return
16-
}
17-
if (obj.name) {
18-
fuc(obj)
19-
}
20-
}
21-
225
export const loggedIn = state => state.loggedIn
236

247
export const user = state => state.user
@@ -27,43 +10,6 @@ export const alreadyReadMessages = state => state.alreadyReadMessages
2710

2811
export const unreadMessages = state => state.unreadMessages
2912

30-
/**
31-
* 在侧边栏展示时,如果当前路由 children 属性为空,则删除该路由
32-
* @param {*} arr 路由配置项数据
33-
*/
34-
function IterationDelateMenuChildren(arr) {
35-
if (arr.length) {
36-
// eslint-disable-next-line no-unused-vars
37-
for (const i in arr) {
38-
if (arr[i].children && !arr[i].children.length) {
39-
delete arr[i]
40-
} else if (arr[i].children && arr[i].children.length) {
41-
IterationDelateMenuChildren(arr[i].children)
42-
}
43-
}
44-
}
45-
return arr
46-
}
47-
48-
/**
49-
* Shaking 掉无权限路由
50-
* @param {array} stageConfig 路由配置项数据
51-
* @param {array} permissions 当前登录管理员所拥有的权限集合
52-
* @param {object} currentUser 当前登录管理员
53-
*/
54-
function permissionShaking(stageConfig, permissions, currentUser) {
55-
const shookConfig = stageConfig.filter(route => {
56-
if (Util.hasPermission(permissions, route, currentUser)) {
57-
if (route.children && route.children.length) {
58-
route.children = permissionShaking(route.children, permissions, currentUser)
59-
}
60-
return true
61-
}
62-
return false
63-
})
64-
return IterationDelateMenuChildren(shookConfig)
65-
}
66-
6713
/**
6814
* 获取有权限的舞台配置
6915
* @param {*} state
@@ -216,3 +162,62 @@ export const getStageInfo = state => {
216162
return stageInfo
217163
}
218164
}
165+
166+
/**
167+
* 递归
168+
* @param {*} obj
169+
* @param {*} fuc
170+
*/
171+
function deepTravel(obj, fuc) {
172+
if (Array.isArray(obj)) {
173+
obj.forEach(item => {
174+
deepTravel(item, fuc)
175+
})
176+
return
177+
}
178+
if (obj && obj.children) {
179+
fuc(obj)
180+
deepTravel(obj.children, fuc)
181+
return
182+
}
183+
if (obj.name) {
184+
fuc(obj)
185+
}
186+
}
187+
188+
/**
189+
* 在侧边栏展示时,如果当前路由 children 属性为空,则删除该路由
190+
* @param {*} arr 路由配置项数据
191+
*/
192+
function IterationDelateMenuChildren(arr) {
193+
if (arr.length) {
194+
// eslint-disable-next-line no-unused-vars
195+
for (const i in arr) {
196+
if (arr[i].children && !arr[i].children.length) {
197+
delete arr[i]
198+
} else if (arr[i].children && arr[i].children.length) {
199+
IterationDelateMenuChildren(arr[i].children)
200+
}
201+
}
202+
}
203+
return arr
204+
}
205+
206+
/**
207+
* Shaking 掉无权限路由
208+
* @param {array} stageConfig 路由配置项数据
209+
* @param {array} permissions 当前登录管理员所拥有的权限集合
210+
* @param {object} currentUser 当前登录管理员
211+
*/
212+
function permissionShaking(stageConfig, permissions, currentUser) {
213+
const shookConfig = stageConfig.filter(route => {
214+
if (Util.hasPermission(permissions, route, currentUser)) {
215+
if (route.children && route.children.length) {
216+
route.children = permissionShaking(route.children, permissions, currentUser)
217+
}
218+
return true
219+
}
220+
return false
221+
})
222+
return IterationDelateMenuChildren(shookConfig)
223+
}

src/store/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createStore, createLogger } from 'vuex'
22
import VuexPersistence from 'vuex-persist'
3+
34
import mutations from './mutation'
45
import state from './state'
56
import * as getters from './getter'
@@ -8,9 +9,8 @@ import actions from './action'
89
const vuexLocal = new VuexPersistence({
910
storage: window.localStorage,
1011
reducer: stateData => ({
11-
// eslint-disable-line
12-
loggedIn: stateData.loggedIn,
1312
user: stateData.user,
13+
loggedIn: stateData.loggedIn,
1414
permissions: stateData.permissions,
1515
}),
1616
})
@@ -20,8 +20,8 @@ const debug = process.env.NODE_ENV !== 'production'
2020
export default createStore({
2121
state,
2222
getters,
23-
mutations,
2423
actions,
25-
plugins: debug ? [vuexLocal.plugin, createLogger()] : [vuexLocal.plugin],
24+
mutations,
2625
strict: debug,
26+
plugins: debug ? [vuexLocal.plugin, createLogger()] : [vuexLocal.plugin],
2727
})

0 commit comments

Comments
 (0)