Authored by 陈峰

重构路由规则

Showing 43 changed files with 262 additions and 226 deletions
import Vue from 'vue';
import Router from 'vue-router';
import Promise from 'promise-polyfill';
import App from './pages/app';
import Routers from './pages';
import yohoPluginCore from './plugins/yoho-plugin-core';
import yohoPluginRouter from './plugins/yoho-plugin-router';
import yohoPluginAuth from './plugins/yoho-plugin-auth';
import './filters';
import './directives';
... ... @@ -27,17 +26,13 @@ if (!window.Promise) {
window.Promise = Promise;
}
let router = new Router({
routes: Routers,
mode: 'history'
});
Vue.use(yohoPluginCore, {router});
Vue.use(yohoPluginCore);
Vue.use(yohoPluginRouter);
Vue.use(yohoPluginAuth);
Vue.render({
el: '#app',
router,
template: '<App/>',
components: {App}
});
... ...
export default {
historyMode: 'history',
axiosBaseUrl: '/Api',
axiosResponseType: 'json',
storeKeys: {
user: '_user',
purview: '_purview',
}
};
... ...
import login from './login';
let routers = [login];
routers.forEach(router => {
router.name = `auth.${router.name}`;
});
export default routers;
const home = r => require.ensure([], () => r(require('./login')), 'auth.login');
export default {
path: '/login.html',
name: 'login',
component: home,
meta: {
authPass: true
}
};
const page = r => require.ensure([], () => r(require('./error')), 'error');
const home = r => require.ensure([], () => r(require('./login')), 'login');
const error = r => require.ensure([], () => r(require('./error')), 'error');
const router = [{
path: '/login.html',
name: 'login',
component: home,
meta: {
authPass: true
}
}, {
path: '/401.html',
name: 'error.401',
component: page,
component: error,
meta: {
authPass: true
}
}, {
path: '*',
name: 'error.404',
component: page,
component: error,
meta: {
authPass: true
}
}, {
path: '/500.html',
name: 'error.500',
component: page,
component: error,
meta: {
authPass: true
}
... ...
/**
* Created by TaoHuang on 2017/5/5.
*/
<template>
<LayoutBody>
<IFrame src="http://shops.yohobuy.com/finance/clearing/index"></IFrame>
</LayoutBody>
</template>
... ...
const page = r => require.ensure([], () => r(require('./clearing')), 'finance.clearing');
export default {
path: '/clearing.html',
name: 'clearing',
component: page,
meta: {
pageName: '对账单'
}
};
... ...
const page = r => require.ensure([], () => r(require('./balance')), 'finance.balance');
const page = r => require.ensure([], () => r(require('./clearing')), 'finance.clearing');
export default {
path: '/balance.html',
name: 'balance',
path: '/clearing.html',
name: 'clearing',
component: page,
meta: {
pageName: '结算单'
}
};
... ...
import balance from './balance';
import store from './store';
import clearing from './clearing-iframe';
import payment from './payment-iframe';
let routers = [balance, store];
routers.forEach(router => {
router.path = `/finance${router.path}`;
router.name = `finance.${router.name}`;
});
export default routers;
export default {
clearing,
payment
};
... ...
const page = r => require.ensure([], () => r(require('./store')), 'finance.store');
const page = r => require.ensure([], () => r(require('./payment')), 'finance.payment');
export default {
path: '/store.html',
name: 'store',
path: '/payment.html',
name: 'payment',
component: page,
meta: {
pageName: '结算单'
... ...
<template>
<LayoutBody>
<IFrame src="http://shops.yohobuy.com/finance/financepayment/index"></IFrame>
</LayoutBody>
</template>
... ...
const page = r => require.ensure([], () => r(require('./decoration')), 'shop.decoration');
const page = r => require.ensure([], () => r(require('./payment')), 'finance.payment');
export default {
path: '/decoration.html',
name: 'decoration',
path: '/payment.html',
name: 'payment',
component: page,
meta: {
pageName: '店铺装修'
pageName: '结算单'
}
};
... ...
import overview from './overview';
let routers = [overview];
routers.forEach(router => {
router.name = `home.${router.name}`;
});
export default routers;
export default {
overview
};
... ...
import home from './home';
import product from './product';
import shop from './shop';
... ... @@ -6,19 +5,11 @@ import repository from './repository';
import statistics from './statistics';
import finance from './finance';
let routers = [product, home, shop, repository, statistics, finance];
let childrenRoutes = [].concat(...routers);
import layout from './layout';
let routes = [{
path: '/',
component: layout,
children: childrenRoutes
}];
import error from './error';
import auth from './auth';
routes = routes.concat(auth, error);
export default routes;
export default {
product,
home,
shop,
repository,
statistics,
finance
};
... ...
... ... @@ -8,21 +8,21 @@ const step3 = r => require.ensure([], () => r(require('./step3')), 'product.crea
export default [{
path: 'step1.html',
name: 'product.create.step1',
name: 'step1',
component: step1,
meta: {
pageName: '类目选择',
}
}, {
path: 'step2.html',
name: 'product.create.step2',
name: 'step2',
component: step2,
meta: {
pageName: '基础信息',
}
}, {
path: 'step3.html',
name: 'product.create.step3',
name: 'step3',
component: step3,
meta: {
pageName: '网销信息',
... ...
import create from './create';
import edit from './edit';
import show from './show';
import onsale from './onsale';
import offsale from './offsale';
import output from './output';
let routers = [create, edit, show, onsale, offsale, output];
routers.forEach(router => {
router.path = `/product${router.path}`;
router.name = `product.${router.name}`;
});
export default routers;
export default {
create,
edit,
onsale,
offsale,
output
};
... ...
/**
* Created by TaoHuang on 2017/4/28.
*/
const showProduct = r => require.ensure([], () => r(require('../edit/edit.vue')), 'product.show');
export default {
path: '/show/:id',
name: 'show',
component: showProduct
};
<template>
<LayoutBody>
<IFrame src="http://shops.yohobuy.com/storehouse/workorder/index"></IFrame>
</LayoutBody>
</template>
... ...
/**
* 发货入库差异
* Created by TaoHuang on 2017/5/4.
*/
const page = r => require.ensure([], () => r(require('./info')), 'statistics.info');
const page = r => require.ensure([], () => r(require('./diff')), 'repository.diff');
export default {
path: '/info.html',
name: 'info',
path: '/diff.html',
name: 'diff',
component: page,
meta: {
pageName: '经营总览'
pageName: '差异库存'
}
};
\ No newline at end of file
... ...
... ... @@ -2,14 +2,10 @@
* Created by TaoHuang on 2017/5/4.
*/
import diff from './diff';
import diff from './diff-iframe';
import jit from './jit';
let routers = [diff, jit];
routers.forEach(router => {
router.path = `/repository${router.path}`;
router.name = `repository.${router.name}`;
});
export default routers;
export default {
diff,
jit
};
... ...
<template>
<LayoutBody>
<IFrame src="http://shops.yohobuy.com/supplier/shop/decorationDetail/792/1/editor/1576/"></IFrame>
</LayoutBody>
</template>
\ No newline at end of file
... ...
const decoration = r => require.ensure([], () => r(require('./decoration')), 'shop.decoration');
const shops = r => require.ensure([], () => r(require('./decoration')), 'shop.shops');
export default [
{
path: '/shops.html',
name: 'shops',
component: shops,
meta: {
pageName: '店铺列表'
}
},
{
path: '/decoration.html',
name: 'decoration',
component: decoration,
meta: {
pageName: '店铺装修'
}
}
];
... ...
<template>
<LayoutBody>
<IFrame src="http://shops.yohobuy.com/supplier/shop/decorationDetail/792/1/editor/1576/"></IFrame>
</LayoutBody>
</template>
<script>
export default {
created() {
},
data() {
return {
};
},
methods: {
}
};
</script>
<style lang="scss">
</style>
import info from './info';
import decoration from './decoration';
import decoration from './decoration-iframe';
let routers = [info, decoration];
routers.forEach(router => {
router.path = `/shop${router.path}`;
router.name = `shop.${router.name}`;
});
export default routers;
export default {
info,
decoration
};
... ...
/**
* Created by TaoHuang on 2017/5/4.
*/
import overview from './overview-iframe';
import sale from './sale';
import info from './info';
let routers = [info];
routers.forEach(router => {
router.path = `/statistics${router.path}`;
router.name = `statistics.${router.name}`;
});
export default routers;
export default {
overview,
sale
};
... ...
/**
* Created by TaoHuang on 2017/5/4.
*/
<template>
<LayoutBody>
<IFrame src="http://shops.yohobuy.com/supplier/shop/decorationDetail/792/1/editor/1576/"></IFrame>
</LayoutBody>
</template>
<script>
export default {
created() {
},
data() {
return {
};
},
methods: {
}
};
</script>
<style lang="scss">
</style>
\ No newline at end of file
const page = r => require.ensure([], () => r(require('./overview')), 'statistics.overview');
export default {
path: '/overview.html',
name: 'overview',
component: page,
meta: {
pageName: '经营总览'
}
};
... ...
<template>
<LayoutBody>
<IFrame src="http://shops.yohobuy.com/report/businessOverview"></IFrame>
</LayoutBody>
</template>
... ...
const page = r => require.ensure([], () => r(require('./sale')), 'statistics.sale');
export default {
path: '/sale.html',
name: 'sale',
component: page,
meta: {
pageName: '销售统计'
}
};
... ...
<template>
<LayoutBody>
<IFrame src="http://shops.yohobuy.com/report/businessSale"></IFrame>
</LayoutBody>
</template>
... ...
... ... @@ -55,7 +55,7 @@ const plugin = {
if (authPass) {
// 已登录跳转到首页
if (to.name === 'auth.login' && Vue.$isLogin) {
if (to.name === 'login' && Vue.$isLogin) {
return next('/');
}
return next();
... ... @@ -86,7 +86,7 @@ const plugin = {
}
};
Vue.switchShop = shopsId => {
Vue.$cookie.set('shopsId', shopsId, {
Vue.$cookie.set('_sign', shopsId, {
path: '/'
});
};
... ...
/**
* 插件
*/
import iView from 'iview';
import Router from 'vue-router';
import iView from 'iview';
import store from 'yoho-store';
import cookie from 'yoho-cookie';
import components from '../components/global';
... ... @@ -11,32 +11,15 @@ import config from 'config';
import _ from 'lodash';
const plugin = {
install(Vue, options) {
// 加载核心插件
Vue.use(iView);
Vue.use(Router);
Vue.prop = (key, value) => {
Vue[`$${key}`] = Vue.prototype[`$${key}`] = value;
};
// 附加Vue原型属性
if (config) {
Vue.prop('config', config);
}
if (options && options.router) {
Vue.$router = options.router;
}
Vue.prop('store', store);
Vue.prop('cookie', cookie);
// 加载核心组件
loadGlobalComponents(Vue) {
_.each(components, component => {
Vue.component(component.name, component);
});
},
defineVueProp(Vue) {
Vue.prop = (key, value) => {
Vue[`$${key}`] = Vue.prototype[`$${key}`] = value;
};
Vue.beforeRender = [];
Vue.render = opts => {
return new Promise(resolve => {
... ... @@ -56,13 +39,31 @@ const plugin = {
resolve();
}
}).then(() => {
return new Vue(opts);
return new Vue(Object.assign(opts, {
router: Vue.$router
}));
});
};
},
install(Vue) {
// 定义Vue全局属性
this.defineVueProp(Vue);
// 加载核心组件
this.loadGlobalComponents(Vue);
// 加载核心插件
Vue.use(iView);
Vue.use(Router);
// 附加Vue原型属性
Vue.prop('config', config);
Vue.prop('store', store);
Vue.prop('cookie', cookie);
// 设置axios默认参数
axios.defaults.baseURL = '/Api';
axios.defaults.responseType = 'json';
axios.defaults.baseURL = config.axiosBaseUrl;
axios.defaults.responseType = config.axiosResponseType;
}
};
... ...
import Router from 'vue-router';
import config from 'config';
import pageRoutes from '../pages';
import layout from '../pages/layout';
import common from '../pages/common';
import _ from 'lodash';
const plugin = {
loadRouters(rous, paths, children) {
if (_.has(rous, 'path')) {
let ps = _.flattenDeep(paths).filter(p => p);
rous.name = _.join(ps, '.');
if (!children) {
if (rous.path) {
rous.path = _.join(_.dropRight(ps, 1), '/') + (rous.path[0] === '/' ? '' : '/') + rous.path;
} else {
rous.path = _.join(ps, '/') + '.html';
}
}
if (rous.children) {
_.each(rous.children, child => this.loadRouters(child, [paths, child.name], true));
return [rous];
}
return [rous];
}
if (rous.length) {
return _.map(rous, r => {
return this.loadRouters(r, [paths]);
});
} else {
return _.map(rous, (rou, k) => {
return this.loadRouters(rou, [paths, k]);
});
}
},
install(Vue) {
let childRouters = this.loadRouters(pageRoutes);
console.log(_.flattenDeep(childRouters))
let routes = [{
path: '/',
component: layout,
children: _.flattenDeep(childRouters)
}];
routes = routes.concat(common);
Vue.$router = new Router({
routes: routes,
mode: config.historyMode
});
}
};
export default plugin;
... ...
... ... @@ -77,11 +77,18 @@ export default [
sub: [
{
menu_name: '经营总览',
menu_url: '/statistics/info.html',
menu_id: 'statistics.info',
menu_url: '/statistics/overview.html',
menu_id: 'statistics.overview',
status: 1,
id: 41
},
{
menu_name: '销售统计',
menu_url: '/statistics/sale.html',
menu_id: 'statistics.sale',
status: 1,
id: 42
},
],
menu_name: '数据报表',
menu_id: 'statistics',
... ... @@ -91,16 +98,16 @@ export default [
{
sub: [
{
menu_name: '查看明细',
menu_url: '/finance/balance.html',
menu_id: 'finance.balance',
menu_name: '对账单',
menu_url: '/finance/clearing.html',
menu_id: 'finance.clearing',
status: 1,
id: 51
},
{
menu_name: '库存',
menu_url: '/finance/store.html',
menu_id: 'finance.store',
menu_name: '结算单',
menu_url: '/finance/payment.html',
menu_id: 'finance.payment',
status: 1,
id: 52
}
... ...
... ... @@ -68,7 +68,7 @@ class UserController extends Context {
domain: '.yohobuy.com'
});
res.clearCookie('_isLogin');
res.clearCookie('shopsId');
res.clearCookie('_sign');
res.clearCookie('yoho-shop');
res.clearCookie('yoho-shop.sig');
return res.json({
... ... @@ -93,7 +93,7 @@ class UserController extends Context {
context.res.cookie('_isLogin', true, {
path: '/'
});
context.res.cookie('shopsId', user.currentShop.id, {
context.res.cookie('_sign', user.currentShop.id, {
path: '/'
});
}
... ...
... ... @@ -42,7 +42,7 @@ module.exports = (req, res, next) => {
});
}
let userShops = req.user.shops;
let currentShop = _.find(userShops, shop => shop.id === req.cookies.shopsId);
let currentShop = _.find(userShops, shop => shop.id === req.cookies._sign);
if (currentShop) {
let channel = apiMap.split('.')[0];
... ...