UNPKG

enc-framework

Version:

enc-framework 核心组件.

312 lines (289 loc) 8.84 kB
import Vue from 'vue' import VueRouter from 'vue-router' import NProgress from 'nprogress' // Progress 进度条 NProgress.configure({ease: 'ease', speed: 1500}); import Page_403 from '../components/common/403' import Page_404 from '../components/common/404' import Page_500 from '../components/common/500' import ipError from '../components/common/ipError' import Home from '../components/home' import AjmConfig from '../utils/ajm-config' // import loginService from "ijiami-oauth/services/loginService"; import loginService from "../services/loginService"; // 使用 router Vue.use(VueRouter) function getBaseRouters(main) { // 基础路由 const BASE_ROUTES = [{ path: '/403', component: Page_403, meta: { isAuth: false } }, { path: '/500', component: Page_500, meta: { isAuth: false } },{ path: '/ipError', component: ipError, meta: { isAuth: false } }, { path: '*', component: main, children: [{ path: "*", component: Page_404, meta: { isAuth: false, hasNav: false } }] }]; return BASE_ROUTES; } const AjmRouter = function(store, options) { this.store = store; // 配置项 this.options = options || {}; // 主页面组件 this.main = this.options.main || "<router-view/>"; // 登录地址 this.homePath= this.options.homePath; this.loginPath = this.options.loginPath || "/"; this.viewFilePath = this.options.viewFilePath || ""; this.oauthUrl = this.options.oauthUrl || ""; this.isRemoteOauth = this.options.isRemoteOauth; this.clientInfo = this.options.clientInfo; let routes = this.options.routes || []; routes.push({ path: '/_home', component: this.main, children: [{ path: "/_home", component: Home, meta: { isAuth: 2, isPanel:false, menuName: "首页", hasNav: false } }] }); this.routes = routes; // 创建路由 this.router = new VueRouter({ //默认路由 routes: this.routes }); // 用户api this.loginApi = this.options.loginApi || {}; this.userApi = this.options.userApi || {}; // 初始化 this._init(); return this; } // 初始化路由对象 AjmRouter.prototype._init = function() { let router = this.router; // 前置导航钩子 router.beforeEach((to, from, next) => { //先判断当前IP是否被限制访问 let loginIp=this.store.state.user.userIp; //为false 不能登录 if(loginIp && !loginIp.result){ if(to.path == '/ipError'){ next() }else{ next('/ipError'); } return } // 获取token let token = this.store.getters.tokenInfo; if (to.path == this.loginPath) { if(token){ //to.path=this.homePath //强制修改密码Bug if(from.path == '/PasswordEditMain/' || localStorage.getItem("isPwd")=='true'){ localStorage.setItem("isPwd","false"); this.loginApi.logout(function () { next(to.path); }); }else{ to.path=this.homePath } } next(); } // isAuth : 1=无登录无授权,2=登录无授权,3=登录并授权 let isAuth = to.meta.isAuth; if ((typeof isAuth == 'undefined') || isAuth === true) { isAuth = 3; }else if(isAuth === false){ isAuth = 1; } if(isAuth == 1){ to.meta.menuPath = to.path; next(); }else if (!token && isAuth != 1) { if (this.isRemoteOauth) { //需要远程登录授权 let code = this.getQueryVariable("code"); if (code) { let url = window.location.href; var query = window.location.search.substring(1); url = url.replace("?" + query, ""); let params = { code: code, grant_type: "authorization_code", redirect_uri: url }; this.loginApi.login(params, function () { loginService.checkIsDefaultPassword().then(resp => { if (resp) { router.push('/PasswordEditMain/'); } else { window.location.href = url; } }); //_this.doHandler(to, from, next); }); } else { next({ path: this.loginPath }); } } else { next({ path: this.loginPath }); } } else { this.doHandler(to, from, next,isAuth); } }); //路由跳转错误 router.onError(function(errorMsg) { router.push({ path: '/500' }); return }); // 后置导航钩子 router.afterEach(transition => { NProgress.done(); }); } AjmRouter.prototype.getQueryVariable = function(variable) { var query = window.location.search.substring(1); var vars = query.split("&"); for (var i = 0; i < vars.length; i++) { var pair = vars[i].split("="); if (pair[0] == variable) { return pair[1]; } } return (false); } AjmRouter.prototype.doHandler = function(to, from, next,isAuth) { NProgress.start(); // 是否存在用户 let userInfo = this.store.getters.userInfo; if (!userInfo) { this.userApi.loadUser().then(resp => { let data = resp.data || resp; this.store.dispatch('user/saveUserInfo', data).then(() => { // 加载权限 this.userApi.loadPermissions().then(resp => { let data = resp.data || resp; this.store.dispatch('permission/savePermissions', data).then(() => { this.checkPermission(to, from, next, isAuth); }); }).catch((err) => { next({ path: this.loginPath }); }); }); }).catch((err) => { next({ path: this.loginPath }); }); } else { this.checkPermission(to, from, next,isAuth); } } // 检查权限 /* 1,要访问的页面是否在返回的菜单权限中 存在 → 跳转要访问的页面 2,不存在跳转到菜单第一个 3, 菜单权限为空 返回403 */ AjmRouter.prototype.checkPermission = function(to, from, next,isAuth) { if (isAuth == 3) { let permissions=this.store.state.permission.permissions||[]; if(permissions.length>0){ if(this.matchPageByPermission(to,permissions)){ to.meta.menuPath = to.path; next(); }else{ this.router.push({path: permissions[0].menuPath}); } return }else{ this.router.push({path: '/403'}); return } let paths = [to.path]; let matched = to.matched; if(matched.length > 0){ paths.push(matched[matched.length - 1].path); return } this.store.dispatch('permission/getPermission', paths).then((permission) => { if (permission) { to.meta.menuPath = permission.menuPath; next(); } else { this.router.push({ //路径 path: '/403' }); } }); } else { to.meta.menuPath = to.path; next(); } } //匹配菜单权限 AjmRouter.prototype.matchPageByPermission = function(to,permissions) { let isInPermission=false for (var i = 0; i < permissions.length; i++) { if(to.path.indexOf(permissions[i].path)==0){ isInPermission= true } } return isInPermission } // 获取路由对象 AjmRouter.prototype.getRouter = function() { return this.router; } // 添加默认路由 AjmRouter.prototype.addRoutes = function(routers) { if (!routers || routers.length <= 0) { return; } this.router.addRoutes(routers); } AjmRouter.prototype.completed = function() { this.addRoutes(getBaseRouters(this.main)); } export default AjmRouter