init
This commit is contained in:
95
src/index.js
95
src/index.js
@@ -3,6 +3,14 @@
|
||||
* Version: 1.0.0
|
||||
*/
|
||||
|
||||
// 引入核心依赖
|
||||
import Vue from 'vue'
|
||||
import VueRouter from 'vue-router'
|
||||
import Vuex from 'vuex'
|
||||
import ViewUI from 'view-design'
|
||||
|
||||
// 引入样式
|
||||
import 'view-design/dist/styles/iview.css'
|
||||
import './assets/css/animate.css'
|
||||
import './assets/css/base.less'
|
||||
import './assets/css/ivewExpand.less'
|
||||
@@ -326,6 +334,93 @@ class AdminFramework {
|
||||
Vue.component(name, components[name])
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Create app with simplified API (推荐使用)
|
||||
* @param {Object} config - application config
|
||||
* @param {String} config.title - application title
|
||||
* @param {String} config.apiUrl - API base URL
|
||||
* @param {String} config.uploadUrl - upload URL (可选,默认为 apiUrl + 'upload')
|
||||
* @param {Object} config.componentMap - custom component map (optional)
|
||||
* @param {Function} config.onReady - callback when app is ready (optional)
|
||||
* @returns {Object} Vue instance
|
||||
*/
|
||||
createApp(config = {}) {
|
||||
// Auto install framework using internal dependencies
|
||||
if (!this.installed) {
|
||||
const { componentMap, ...appConfig } = config
|
||||
|
||||
// 如果没有提供 uploadUrl,自动从 apiUrl 推导
|
||||
if (!appConfig.uploadUrl && appConfig.apiUrl) {
|
||||
appConfig.uploadUrl = appConfig.apiUrl + (appConfig.apiUrl.endsWith('/') ? 'upload' : '/upload')
|
||||
}
|
||||
|
||||
this.install(Vue, {
|
||||
config: appConfig,
|
||||
ViewUI,
|
||||
VueRouter,
|
||||
Vuex,
|
||||
createPersistedState: null,
|
||||
componentMap: componentMap || {}
|
||||
})
|
||||
}
|
||||
|
||||
// Create Vue instance with auto menu/title restoration
|
||||
const app = new Vue({
|
||||
router: this.router,
|
||||
store: this.store,
|
||||
render: h => h('router-view'),
|
||||
async created() {
|
||||
console.log('=== Admin Framework App Started ===')
|
||||
console.log('Framework Version:', framework.version)
|
||||
console.log('Config:', this.$config)
|
||||
|
||||
// Auto restore menu and title on refresh
|
||||
const token = this.$store.state.user.token
|
||||
const authorityMenus = localStorage.getItem('authorityMenus')
|
||||
|
||||
if (token && authorityMenus) {
|
||||
console.log('Restoring menu and title...')
|
||||
try {
|
||||
// Restore menu
|
||||
await this.$store.dispatch('user/setAuthorityMenus', {
|
||||
Main: framework.Main,
|
||||
ParentView: framework.ParentView,
|
||||
Page404: framework.Page404,
|
||||
authorityMenus: authorityMenus
|
||||
})
|
||||
console.log('Menu restored')
|
||||
|
||||
// Get system title
|
||||
await this.$store.dispatch('app/getSysTitle', {
|
||||
defaultTitle: this.$config.title,
|
||||
defaultLogo: ''
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Restore failed:', error)
|
||||
}
|
||||
} else {
|
||||
// Not logged in, use default title
|
||||
console.log('Not logged in, using default title')
|
||||
document.title = this.$config.title
|
||||
}
|
||||
|
||||
// Call user callback if provided
|
||||
if (config.onReady && typeof config.onReady === 'function') {
|
||||
config.onReady.call(this)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Expose to global for debugging
|
||||
if (typeof window !== 'undefined') {
|
||||
window.app = app
|
||||
window.rootVue = app
|
||||
window.framework = framework
|
||||
}
|
||||
|
||||
return app
|
||||
}
|
||||
}
|
||||
|
||||
const framework = new AdminFramework()
|
||||
|
||||
@@ -57,6 +57,25 @@ export const setupRouterGuards = (router, ViewUI, homeName = 'home') => {
|
||||
next({ name: homeName })
|
||||
}
|
||||
} else {
|
||||
// 已登录,检查路由是否存在
|
||||
if (token && to.matched.length === 0 && to.path !== '/') {
|
||||
// 路由不存在,可能是动态路由还未加载完成
|
||||
console.warn('路由未找到:', to.name || to.path, ',尝试等待动态路由加载')
|
||||
|
||||
// 等待一小段时间后重试
|
||||
setTimeout(() => {
|
||||
// 检查路由是否已经存在
|
||||
const route = router.resolve(to.path)
|
||||
if (route && route.matched.length > 0) {
|
||||
next({ path: to.path, replace: true })
|
||||
} else {
|
||||
console.error('路由仍然不存在:', to.name || to.path, ',跳转到首页')
|
||||
next({ name: homeName, replace: true })
|
||||
}
|
||||
}, 150)
|
||||
return
|
||||
}
|
||||
|
||||
// 其他情况 → 允许访问
|
||||
next()
|
||||
}
|
||||
|
||||
@@ -104,9 +104,11 @@ export default {
|
||||
commit('setMenuList', mainMenu.children)
|
||||
|
||||
// 动态添加路由(重要!解决登录后点击菜单空白的问题)
|
||||
if (this.router) {
|
||||
const routes =this.router.options.routes
|
||||
|
||||
// 从 window.framework 获取 router 实例
|
||||
const router = window.framework && window.framework.router
|
||||
if (router) {
|
||||
const routes = router.options.routes
|
||||
|
||||
// 查找并移除旧的主路由
|
||||
const mainRouteIndex = routes.findIndex(r => r.path === '/')
|
||||
if (mainRouteIndex !== -1) {
|
||||
@@ -116,6 +118,11 @@ export default {
|
||||
// 添加新的主路由
|
||||
router.addRoute(mainMenu)
|
||||
console.log('动态路由已添加,redirect 设置为:', mainMenu.redirect)
|
||||
|
||||
// 等待路由更新完成
|
||||
await new Promise(resolve => setTimeout(resolve, 100))
|
||||
} else {
|
||||
console.warn('未找到 router 实例,动态路由添加失败')
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user