interceptor.ts 4.4 KB
Newer Older
zcdtk's avatar
zcdtk committed
1 2 3
import { Store } from 'vuex';
import axios from 'axios';
import Router from 'vue-router';
zcdtk's avatar
zcdtk committed
4
import i18n from '@/locale';
zcdtk's avatar
zcdtk committed
5
import ignoreProxyMap from './ignore-proxy';
6
import { Loading } from '@/ibiz-core/utils';
zcdtk's avatar
zcdtk committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/**
 * 拦截器
 *
 * @export
 * @class Interceptors
 */
export class Interceptors {

    /**
     * 路由对象
     *
     * @private
     * @type {(Router | any)}
     * @memberof Interceptors
     */
    private router: Router | any;

    /**
     * 缓存对象
     *
     * @private
     * @type {(Store<any> | any)}
     * @memberof Interceptors
     */
    private store: Store<any> | any;

33 34
    

zcdtk's avatar
zcdtk committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    /**
     *  单列对象
     *
     * @private
     * @static
     * @type {LoadAppData}
     * @memberof Interceptors
     */
    private static readonly instance: Interceptors = new Interceptors();

    /**
     * Creates an instance of Interceptors.
     * 私有构造,拒绝通过 new 创建对象
     * 
     * @memberof Interceptors
     */
    private constructor() {
        if (Interceptors.instance) {
            return Interceptors.instance;
        } else {
            this.intercept();
        }
    }

    /**
     * 获取 LoadAppData 单例对象
     *
     * @static
     * @param {Router} route
     * @param {Store<any>} store
     * @returns {Interceptors}
     * @memberof Interceptors
     */
    public static getInstance(route: Router, store: Store<any>): Interceptors {
        this.instance.router = route;
        this.instance.store = store;
        return this.instance;
    }

    /**
     * 拦截器实现接口
     *
     * @private
     * @memberof Interceptors
     */
    private intercept(): void {
        axios.interceptors.request.use((config: any) => {
zcdtk's avatar
zcdtk committed
82 83 84
            let appdata: any = this.store.getters.getAppData();;
            if (appdata && appdata.context) {
                config.headers['srforgsectorid'] = appdata.context.srforgsectorid;
zcdtk's avatar
zcdtk committed
85 86 87 88 89
            }
            if (window.localStorage.getItem('token')) {
                const token = window.localStorage.getItem('token');
                config.headers.Authorization = `Bearer ${token}`;
            }
zcdtk's avatar
zcdtk committed
90
            config.headers['Accept-Language'] =  i18n.locale;
zcdtk's avatar
zcdtk committed
91 92 93 94
            
            // 混合 app 代理处理
            if (Object.is(process.env.VUE_APP_CURRENTMODE, 'hybridapp') && !config.url.startsWith('https://') && !config.url.startsWith('http://')) {
                if (!ignoreProxyMap.has(config.url)) {
zcdtk's avatar
zcdtk committed
95
                    config.url = process.env.VUE_APP_PROXY + config.url;
zcdtk's avatar
zcdtk committed
96 97
                }
            }
zcdtk's avatar
zcdtk committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111
            return config;
        }, (error: any) => {
            return Promise.reject(error);
        });

        axios.interceptors.response.use((response: any) => {
            return response;
        }, (error: any) => {
            error = error ? error : { response: {} };
            // tslint:disable-next-line:prefer-const
            let { response: res } = error;
            let { data: _data } = res;

            if (res.status === 401) {
112
                // Loading.hidden();
zcdtk's avatar
zcdtk committed
113 114 115
                this.doNoLogin(_data.data);
            }
            if (res.status === 404) {
116
                // Loading.hidden();
117
                this.router.push({ path: '/404' });
zcdtk's avatar
zcdtk committed
118
            } else if (res.status === 500) {
119
                // Loading.hidden();
120
                this.router.push({ path: '/500' });
zcdtk's avatar
zcdtk committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
            }

            return Promise.reject(res);
        });
    }

    /**
     * 处理未登录异常情况
     *
     * @private
     * @param {*} [data={}]
     * @memberof Interceptors
     */
    private doNoLogin(data: any = {}): void {
        if (data.loginurl && !Object.is(data.loginurl, '') && data.originurl && !Object.is(data.originurl, '')) {
            let _url = encodeURIComponent(encodeURIComponent(window.location.href));
            let loginurl: string = data.loginurl;
            const originurl: string = data.originurl;

            if (originurl.indexOf('?') === -1) {
                _url = `${encodeURIComponent('?RU=')}${_url}`;
            } else {
                _url = `${encodeURIComponent('&RU=')}${_url}`;
            }
            loginurl = `${loginurl}${_url}`;

            window.location.href = loginurl;
        } else {
            if (Object.is(this.router.currentRoute.name, 'login')) {
                return;
            }
            this.router.push({ name: 'login', query: { redirect: this.router.currentRoute.fullPath } });
        }
    }

}