Authored by zhangwenxue

style: apply eslint

... ... @@ -15,7 +15,7 @@ Vue.use(yohoPluginAuth);
Vue.use(yohoPluginCache);
Vue.render({
el: '#app',
template: '<App/>',
components: {App}
el: '#app',
template: '<App/>',
components: { App },
});
... ...
... ... @@ -7,136 +7,133 @@ import UserService from 'services/user/user-service';
import crypto from 'util/crypto';
export default {
updateUser(Vue, user, purviews) {
Vue.$store.set(Vue.$config.storeKeys.user, crypto.aesEncrypt(user));
Vue.prop('purviews', purviews.deep);
Vue.prop('oriPurviews', purviews.ori);
Vue.prop('user', user);
Vue.prop('isLogin', true);
},
updateUser(Vue, user, purviews) {
Vue.$store.set(Vue.$config.storeKeys.user, crypto.aesEncrypt(user));
Vue.prop('purviews', purviews.deep);
Vue.prop('oriPurviews', purviews.ori);
Vue.prop('user', user);
Vue.prop('isLogin', true);
},
// 权限验证
checkPurview() {
return Promise.resolve();
// 权限验证
checkPurview() {
return Promise.resolve();
// let pur = _.find(Vue.$purviews, p => p.menu_url === to.path);
// let pur = _.find(Vue.$purviews, p => p.menu_url === to.path);
// if (pur) {
// return Promise.resolve();
// }
// return Promise.reject();
},
initPurview(Vue, user) {
return this.userService.purviews().then((purviews) => {
this.updateUser(Vue, user, purviews);
});
},
initConfig(Vue) {
return this.userService.config().then((data) => {
Object.assign(Vue.$config, data);
Vue.prop('config', Vue.$config);
});
},
install(Vue) {
this.userService = new UserService();
Vue.beforeRender((next) => {
let user = Vue.$store.get(Vue.$config.storeKeys.user);
let isLogin = Vue.$cookie.get('_isLogin');
if (isLogin && user) {
try {
user = crypto.aesDecrypt(user, Object);
return Promise.all([
this.initPurview(Vue, user),
this.initConfig(Vue)
]).then(() => {
next();
}, () => {
next();
});
} catch (e) {
Vue.logout();
}
}
next();
});
// if (pur) {
// return Promise.resolve();
// }
// return Promise.reject();
},
initPurview(Vue, user) {
return this.userService.purviews().then(purviews => {
this.updateUser(Vue, user, purviews);
});
},
initConfig(Vue) {
return this.userService.config().then(data => {
Object.assign(Vue.$config, data);
Vue.prop('config', Vue.$config);
});
},
install(Vue) {
this.userService = new UserService();
// 路由权限控制
Vue.$router.beforeEach((to, from, next) => {
Vue.beforeRender(next => {
let user = Vue.$store.get(Vue.$config.storeKeys.user);
const isLogin = Vue.$cookie.get('_isLogin');
// 需要修改密码去修改密码
if (Vue.$store.get('needUpdate') && to.name !== 'login' && to.name !== 'user.password') {
return next('/user/password.html');
if (isLogin && user) {
try {
user = crypto.aesDecrypt(user, Object);
return Promise.all([this.initPurview(Vue, user), this.initConfig(Vue)]).then(
() => {
next();
},
() => {
next();
}
);
} catch (e) {
Vue.logout();
}
}
next();
});
// 无权限控制理由直接pass
let authPass = _.get(_.last(to.matched), 'meta.authPass', false);
// 路由权限控制
Vue.$router.beforeEach((to, from, next) => {
// 需要修改密码去修改密码
if (Vue.$store.get('needUpdate') && to.name !== 'login' && to.name !== 'user.password') {
return next('/user/password.html');
}
if (authPass) {
// 已登录跳转到首页
if (to.name === 'login' && Vue.$isLogin) {
return next('/');
}
return next();
}
// 无权限控制理由直接pass
const authPass = _.get(_.last(to.matched), 'meta.authPass', false);
// 未登录去登录
if (!Vue.$isLogin) {
return next('/login.html');
}
if (authPass) {
// 已登录跳转到首页
if (to.name === 'login' && Vue.$isLogin) {
return next('/');
}
return next();
}
// 未登录去登录
if (!Vue.$isLogin) {
return next('/login.html');
}
return this.checkPurview(Vue, to).then(
() => {
return next();
},
() => {
return next('/401.html');
}
);
});
return this.checkPurview(Vue, to).then(() => {
return next();
}, () => {
return next('/401.html');
Vue.passport = {
local: (username, password, captcha) => {
return this.userService.login(username, password, captcha).then(res => {
if (res.code === 200) {
return Promise.all([this.initPurview(Vue, res.data), this.initConfig(Vue)]).then(() => {
if (res.data.needUpdate) {
Vue.$store.set('needUpdate', true);
}
return res.data;
});
}
return Promise.reject(res);
});
Vue.passport = {
local: (username, password, captcha) => {
return this.userService.login(username, password, captcha).then((res) => {
if (res.code === 200) {
return Promise.all([
this.initPurview(Vue, res.data),
this.initConfig(Vue)
]).then(() => {
if (res.data.needUpdate) {
Vue.$store.set('needUpdate', true);
}
return res.data;
});
}
return Promise.reject(res);
});
}
};
Vue.switchShop = shopsId => {
Vue.$store.set(Vue.$config.storeKeys.user, crypto.aesEncrypt(Vue.$user));
Vue.$cookie.set('_sign', shopsId, {
path: '/'
});
};
Vue.logout = () => {
_.each(Vue.$config.storeKeys, Vue.$store.remove);
Vue.prop('user', void 0);
Vue.prop('isLogin', void 0);
Vue.prop('purviews', void 0);
Vue.$store.remove('needUpdate');
axios.post('/logout');
Vue.$router.push('/login.html');
};
axios.defaults.validateStatus = (status) => {
if (status === 401) {
Vue.logout();
return false;
}
if (status === 403) {
Vue.$store.set('needUpdate', true);
return false;
}
return true;
};
}
},
};
Vue.switchShop = shopsId => {
Vue.$store.set(Vue.$config.storeKeys.user, crypto.aesEncrypt(Vue.$user));
Vue.$cookie.set('_sign', shopsId, {
path: '/',
});
};
Vue.logout = () => {
_.each(Vue.$config.storeKeys, Vue.$store.remove);
Vue.prop('user', void 0);
Vue.prop('isLogin', void 0);
Vue.prop('purviews', void 0);
Vue.$store.remove('needUpdate');
axios.post('/logout');
Vue.$router.push('/login.html');
};
axios.defaults.validateStatus = status => {
if (status === 401) {
Vue.logout();
return false;
}
if (status === 403) {
Vue.$store.set('needUpdate', true);
return false;
}
return true;
};
},
};
... ...
... ... @@ -5,57 +5,59 @@ import common from '../pages/common';
import _ from 'lodash';
export default {
loadRouters(rous, paths, children) {
if (_.has(rous, 'path')) {
let ps = _.flattenDeep(paths).filter(p => p);
loadRouters(rous, paths, children) {
if (_.has(rous, 'path')) {
let ps = _.flattenDeep(paths).filter(p => p);
if (_.last(ps) === rous.name) {
ps = _.dropRight(ps);
}
if (!children) {
if (rous.path) {
rous.path = _.join(ps, '/') + (rous.path[0] === '/' ? '' : '/') + rous.path;
} else {
rous.path = _.join(ps, '/') + '.html';
}
}
rous.name = _.join(_.concat(ps, [rous.name]), '.');
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]);
});
if (_.last(ps) === rous.name) {
ps = _.dropRight(ps);
}
if (!children) {
if (rous.path) {
rous.path = _.join(ps, '/') + (rous.path[0] === '/' ? '' : '/') + rous.path;
} else {
return _.map(rous, (rou, k) => {
return this.loadRouters(rou, [paths, k]);
});
rous.path = _.join(ps, '/') + '.html';
}
},
install(Vue) {
let childRouters = _.flattenDeep(this.loadRouters(pageRoutes));
}
rous.name = _.join(_.concat(ps, [rous.name]), '.');
if (Vue.$config.homePage) {
let homePage = _.find(childRouters, router => router.name === Vue.$config.homePage);
homePage && (homePage.path = '/');
}
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) {
const childRouters = _.flattenDeep(this.loadRouters(pageRoutes));
let routes = [{
path: '/',
component: layout,
children: childRouters
}];
if (Vue.$config.homePage) {
const homePage = _.find(childRouters, router => router.name === Vue.$config.homePage);
routes = routes.concat(common);
Vue.$router = new Router({
routes: routes,
mode: Vue.$config.historyMode
});
homePage && (homePage.path = '/');
}
let routes = [
{
path: '/',
component: layout,
children: childRouters,
},
];
routes = routes.concat(common);
Vue.$router = new Router({
routes,
mode: Vue.$config.historyMode,
});
},
};
... ...
... ... @@ -2,51 +2,51 @@ import _ from 'lodash';
import Service from '../service';
class UserService extends Service {
login(username, password, captcha) {
return this.post('/login', {
username,
password,
captcha
});
}
update(params) {
return this.post('/updatePwd', params);
}
purviews() {
return this.post('/erp/getPurview', {
platform_id: 4
}).then(res => {
return {ori: res.data, deep: this.deepList(res.data)};
});
}
config() {
return this.post('/config');
}
deepList(purviews) {
let purs = [];
login(username, password, captcha) {
return this.post('/login', {
username,
password,
captcha,
});
}
update(params) {
return this.post('/updatePwd', params);
}
purviews() {
return this.post('/erp/getPurview', {
platform_id: 4,
}).then(res => {
return { ori: res.data, deep: this.deepList(res.data) };
});
}
config() {
return this.post('/config');
}
deepList(purviews) {
let purs = [];
_.each(purviews, pur => {
let sub = pur.sub;
_.each(purviews, pur => {
const sub = pur.sub;
purs.push(pur);
if (sub && sub.length) {
let subs = this.deepList(sub);
purs.push(pur);
if (sub && sub.length) {
const subs = this.deepList(sub);
_.each(subs, s => {
s.pMenu = pur;
});
purs = purs.concat(...subs);
}
});
return purs;
}
switchShop(shopId) {
return this.post('/switchShop', {
shopId
_.each(subs, s => {
s.pMenu = pur;
});
}
geeCaptcha() {
return this.get('/geeCaptcha?r=' + Math.random());
}
purs = purs.concat(...subs);
}
});
return purs;
}
switchShop(shopId) {
return this.post('/switchShop', {
shopId,
});
}
geeCaptcha() {
return this.get('/geeCaptcha?r=' + Math.random());
}
}
export default UserService;
... ...