yoho-plugin-core.js 3.84 KB
/**
 * 插件
 */
import Router from 'vue-router';
import Promise from 'promise-polyfill';
import iView from 'iview';
import 'iview/dist/styles/iview.css';
import 'iview/dist/styles/fonts/ionicons.eot';
import 'iview/dist/styles/fonts/ionicons.svg';
import 'iview/dist/styles/fonts/ionicons.ttf';
import 'iview/dist/styles/fonts/ionicons.woff';
import '../statics/fonts/iconfont.css';
import store from 'yoho-store';
import cookie from 'yoho-cookie';
import components from '../components';
import axios from 'axios';
import config from 'config';
import _ from 'lodash';

export default {
    loadGlobalComponents(Vue) {
        _.each(components, componentModules => {
            _.each(componentModules, component => {
                if (component.length) {
                    Vue.component(component[0], component[1]);
                } else {
                    Vue.component(component.name, component);
                }
            });
        });
    },
    defineVueProp(Vue) {
        Vue.prop = (key, value) => {
            Vue[`$${key}`] = Vue.prototype[`$${key}`] = value;
        };
        Vue.beforeRenderHooks = [];
        Vue.beforeRender = (fn) => {
            Vue.beforeRenderHooks.push(fn);
        };
        Vue.render = opts => {
            return new Promise(resolve => {
                if (Vue.beforeRenderHooks.length) {
                    let step = index => {
                        if (index >= Vue.beforeRenderHooks.length) {
                            resolve();
                        } else {
                            Vue.beforeRenderHooks[index](() => {
                                step(index + 1);
                            });
                        }
                    };

                    step(0);
                } else {
                    resolve();
                }
            }).then(() => {
                return new Vue(Object.assign(opts, {
                    router: Vue.$router
                }));
            });
        };
    },
    compatible() {
        // 兼容IE的Function没有name属性为题,为了修复iView的bug
        if (!(function f() {}).name) {
            Object.defineProperty(Function.prototype, 'name', { //eslint-disable-line
                get: function() {
                    let name = (this.toString().match(/^function\s*([^\s(]+)/) || [])[1];

                    Object.defineProperty(this, 'name', { value: name });
                    return name;
                }
            });
        }

        // 使用了webpack code spliting IE下需要promise ployfill
        if (!window.Promise) {
            window.Promise = Promise;
        }
    },
    install(Vue) {
        // 定义Vue全局属性
        this.defineVueProp(Vue);

        // 加载核心组件
        this.loadGlobalComponents(Vue);

        // 兼容
        this.compatible();

        // 加载核心插件
        Vue.use(iView);
        Vue.use(Router);

        // 附加Vue原型属性
        Vue.prop('config', config[process.env.NODE_ENV]);
        Vue.prop('store', store);
        Vue.prop('cookie', cookie);

        // 设置axios默认参数
        axios.defaults.baseURL = Vue.$config.axiosBaseUrl;
        axios.defaults.responseType = Vue.$config.axiosResponseType;
        axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

        axios.interceptors.response.use(response => {
            if (response.data.code !== 200) {
                iView.Message.error(response.data.message);
                return Promise.reject({response});
            }

            if (response.status >= 200 && response.status < 300) {
                return response;
            }

            iView.Message.error('接口异常');
            return Promise.reject({response});
        }, error => {
            iView.Message.error('接口异常');
            return Promise.reject(error);
        });
    }
};