yoho-plugin-purview.js 1.17 KB
/**
 * 权限插件
 */
import _ from 'lodash';
import axios from 'axios';
import purviews from 'purview';

const plugin = {
    install(Vue) {
        // 路由权限
        Vue.mixin({
            beforeRouteEnter(to, from, next) {
                // TODO 获取权限
                if (to.matched && to.matched.length > 0) {
                    let authPass = _.get(_.last(to.matched), 'meta.authPass', false);

                    if (authPass) {
                        return next();
                    }
                }
                let purview = _.get(purviews, to.name.replace(/\./g, '.subPurviews.'));

                if (purview.own) {
                    return next();
                } else {
                    return next('/401');
                }
            }
        });

        // 接口权限
        axios.defaults.validateStatus = (status) => {
            if (status >= 200 && status < 300) {
                return true;
            }
            if (status === 401) {
                Vue.logout();
                return false;
            }
            Vue.$Message.error('接口异常');
            return false;
        };
    }
};

export default plugin;