interceptor.ts 3.8 KB
Newer Older
yanshaowei's avatar
yanshaowei committed
1 2 3 4 5 6 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 33 34 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 82 83 84 85 86 87 88 89
import axios from 'axios';
import { Environment } from '@/environments/environment';


/**
 * 拦截器
 *
 * @export
 * @class Interceptors
 */
export class Interceptors {

    /**
     * 路由对象
     *
     * @private
     * @type {*}
     * @memberof Interceptors
     */
    private router: any

    /**
     * 拦截器实现接口
     *
     * @private
     * @memberof Interceptors
     */
    private intercept(): void {
        axios.interceptors.request.use((config: any) => {
            const rootapp: any = this.router.app;
            const appdata: string = rootapp.$store.getters.getAppData();
            if (!Object.is(appdata, '')) {
                config.headers.srfappdata = appdata;
            }
            if (window.localStorage.getItem('token')) {
                const token = window.localStorage.getItem('token');
                config.headers.Authorization = `Bearer ${token}`;
            }
            if (!config.url.startsWith('https://') && !config.url.startsWith('http://')) {
                config.url = Environment.BaseUrl + config.url;
            }
            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 (_data && _data.status === 401) {
                this.doNoLogin(_data.data);
            }
            if (res.status === 404) {
                this.router.push({ path: '/404' });
            } else if (res.status === 500) {
                this.router.push({ path: '/500' });
            }

            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 {
90 91 92 93 94 95 96 97 98
            if(Environment.isSsoLoginModel){
                // 单点登录
                window.location.href = Environment.ssoLoginAddr+'?service='+window.location.href;
            }else{
                // 普通应用登录  
                if (Object.is(this.router.currentRoute.name, 'login')) {
                    return;
                }
                this.router.push({ name: 'login', query: { redirect: this.router.currentRoute.fullPath } });
yanshaowei's avatar
yanshaowei committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
            }
        }
    }


    /**
     * 构建对象
     * 
     * @memberof Interceptors
     */
    private constructor(router: any) {
        this.router = router;
        this.intercept();
    }

    /**
     * 初始化变量
     *
     * @private
     * @static
     * @type {Interceptors}
     * @memberof Interceptors
     */
    private static interceptors: Interceptors;

    /**
     * 获取单例对象
     *
     * @static
     * @returns {Interceptors}
     * @memberof Interceptors
     */
    public static getInstance(router: any): Interceptors {
        if (!Interceptors.interceptors) {
            Interceptors.interceptors = new Interceptors(router);
        }
        return this.interceptors;
    }
}