yoho-plugin-auth.js 3.23 KB
/**
 * 权限插件
 */
import _ from 'lodash';
import axios from 'axios';
import userService from 'user-service';
import Rsa from 'rsa';

const plugin = {
    updateUser(Vue, user, purviews) {
        Vue.$store.set(Vue.$config.storeKeys.user, Rsa.encrypt(user));
        Vue.prop('user', user);
        Vue.prop('isLogin', true);
        Vue.prop('purviews', purviews.deep);
        Vue.prop('oriPurviews', purviews.ori);
    },

    // 权限验证
    checkPurview() {
        return Promise.resolve();

        // let pUrl = `/${_.split(to.name, '.').join('/')}`;

        // let pur = _.find(purviews, p => p.menu_url === pUrl);

        // if (pur) {
        //     return Promise.resolve();
        // }
        // return Promise.reject();
    },
    initPurview(Vue, user) {
        return userService.purviews().then((purviews) => {
            this.updateUser(Vue, user, purviews);
        });
    },
    install(Vue) {
        Vue.beforeRender((next) => {
            let user = Vue.$store.get(Vue.$config.storeKeys.user);
            let isLogin = Vue.$cookie.get('_isLogin');

            if (isLogin && user) {
                user = Rsa.decrypt(user, Object);
                return this.initPurview(Vue, user).then(() => {
                    next();
                });
            }
            next();
        });

        // 路由权限控制
        Vue.$router.beforeEach((to, from, next) => {
            // 无权限控制理由直接pass
            let authPass = _.get(_.last(to.matched), 'meta.authPass', false);

            if (authPass) {
                // 已登录跳转到首页
                if (to.name === 'login' && Vue.$isLogin) {
                    return next('/');
                }
                return next();
            }

            // 未登录去登录
            if (!Vue.$isLogin) {
                return next('/login.html');
            }

            return this.checkPurview(Vue.$purviews, to).then(() => {
                return next();
            }, () => {
                return next('/401.html');
            });
        });

        Vue.passport = {
            local: (username, password) => {
                return userService.login(username, password).then((res) => {
                    if (res.code === 200) {
                        return this.initPurview(Vue, res.data).then(() => {
                            return res.data;
                        });
                    }
                    return Promise.reject(res);
                });
            }
        };
        Vue.switchShop = shopsId => {
            Vue.$store.set(Vue.$config.storeKeys.user, Rsa.encrypt(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);
            axios.post('/logout');
            Vue.$router.push('/login.html');
        };
        axios.defaults.validateStatus = (status) => {
            if (status === 401) {
                Vue.logout();
                return false;
            }
            return true;
        };
    }
};

export default plugin;