From 9057e04efad1b7d61c77a72e5c37a504d0aee935 Mon Sep 17 00:00:00 2001 From: doum <doum> Date: 星期五, 26 九月 2025 09:24:03 +0800 Subject: [PATCH] H5静态化 --- admin/src/main.js | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 150 insertions(+), 0 deletions(-) diff --git a/admin/src/main.js b/admin/src/main.js new file mode 100644 index 0000000..415b1cc --- /dev/null +++ b/admin/src/main.js @@ -0,0 +1,150 @@ +import Vue from 'vue' +import App from './App.vue' +import router from './router' +import store from './store' +import ElementUI from 'element-ui' +import './assets/style/element-variables.scss' +import VueClipboard from 'vue-clipboard2' +import directives from './directives' +import filters from './filters' +import plugins from './plugins' +import { mapState, mapMutations } from 'vuex' +import { fetchMenuTree } from './api/system/menu' +import Treeselect from '@riophae/vue-treeselect' +import '@riophae/vue-treeselect/dist/vue-treeselect.css' +Vue.component('treeselect', Treeselect) +Vue.config.productionTip = false +Vue.use(ElementUI, { + size: 'small' +}) +Vue.use(VueClipboard) +Vue.use(directives) +Vue.use(filters) +Vue.use(plugins) + +new Vue({ + data: { + loading: false + }, + router, + store, + computed: { + ...mapState(['userInfo', 'homePage']) + }, + watch: { + async userInfo () { + if (this.userInfo == null) { + return + } + await this.initRoutes() + } + }, + methods: { + ...mapMutations(['switchCollapseMenu', 'setHomePage']), + /** + * 鍒濆鍖栨湰鍦伴厤缃� + */ + initLocalConfig () { + // 鑿滃崟鐘舵�侀厤缃� + const menuStatus = window.localStorage.getItem('MENU_STATUS') + if (menuStatus != null) { + this.switchCollapseMenu(menuStatus === 'true') + } + }, + /** + * 鍒濆鍖栬矾鐢� + * + * @returns {Promise<void>} + */ + async initRoutes () { + if (this.loading || this.userInfo == null) { + return + } + this.loading = true + // 閲嶇疆鑿滃崟 + this.$store.commit('resetMenus') + // 鑾峰彇鑿滃崟 + const storeMenus = this.$store.state.menuData.list + if (storeMenus.length > 0 && this.homePage == null) { + this.setHomePage(storeMenus[0]) + } + await fetchMenuTree() + .then(menus => { + // 娣诲姞鑿滃崟 + storeMenus.push.apply(storeMenus, menus) + // 娣诲姞璺敱 + this.__addRouters(storeMenus) + // 404 + router.addRoute({ + path: '*', + redirect: '/not-found' + }) + // 棣栭〉 + router.addRoute({ + name: 'index', + path: '/', + redirect: this.homePage.url + }) + // 璺敱鍔犺浇瀹屾垚鍚庯紝濡傛灉璁块棶鐨勬槸/锛岃烦杞嚦鍔ㄦ�佽瘑鍒殑棣栭〉 + if (this.$route.path === '/') { + this.$router.push(this.homePage.url) + } + }) + .catch(e => { + throw e + }) + .finally(() => { + this.loading = false + }) + }, + /** + * 鏂板缓璺敱 + * + * @param routes 闇�娣诲姞鐨勮矾鐢� + * @param parents 闇�娣诲姞鍒扮殑鐩爣鍒楄〃 + * @private + */ + __addRouters (routes, parents = []) { + if (routes == null || routes.length === 0) { + return + } + const rs = router.getRoutes() + for (const route of routes) { + const parentsDump = JSON.parse(JSON.stringify(parents)) + parentsDump.push(route) + if (route.url == null || route.url === '') { + this.__addRouters(route.children, parentsDump) + continue + } + if (rs.findIndex(r => r.path === route.url) > -1) { + this.__addRouters(route.children, parentsDump) + continue + } + if (this.homePage == null) { + this.setHomePage(route) + } + router.addRoute('layout', { + path: route.url, + name: route.label, + meta: { + title: route.label, + paths: [...parents.map(p => p.label), route.label] + }, + component: () => import('@/views' + route.url) + }) + this.__addRouters(route.children, parentsDump) + } + } + }, + async created () { + if (this.userInfo == null) { + return + } + await this.initRoutes() + .catch(() => {}) + }, + mounted () { + this.initLocalConfig() + }, + render: h => h(App) +}).$mount('#app') -- Gitblit v1.9.3