08335 / hivui-platform-template
hivui平台项目模板
Newer
Older
hivui-platform-template / project / hivuiBirt / funclib / AxiosUtil.js
/**
 * 请求工具类,临时方案,待完善
 * @author cls
 * @date 2021-01-30
 */
 import {baseURL,projectName,requestUrl} from '@birt/config'
 import Message from '@birt/components/vue-m-message'
import axios from 'axios';
import { getToken } from '@birt/utils/auth'
var pn = projectName || 'birt'
//请求状态码属性
const STATUS_KEY = "status";
//请求成功状态码
const STATUS_SUCCESS = 200;
//超时
const STATUS_TIMEOUT = 418;
// 检测登录超时
function checkTimeout(res) {
    if (res[STATUS_KEY] == STATUS_TIMEOUT) {
        // to re-login
        MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
            confirmButtonText: 'Re-Login',
            cancelButtonText: 'Cancel',
            type: 'warning'
        }).then(() => {
            console.log("这里处理超时登录");
        })
        return true;
    }
    return false
}
debugger
axios.baseURL = baseURL
axios.interceptors.request.use(
    config => {
        // "3d3fe30c-3d99-41e8-9aea-addd8528aa07"
        // config.headers["Authorization"] = "bearer " + "b9e6a365-c551-4551-b258-c56e3acd0eed";
        if(getToken()){
            config.headers["Authorization"] = "bearer " + getToken();
        }else{
            //没有token,跳转登录页
            if(window.HIVUI_SETTING.loginUrl){
                location=window.HIVUI_SETTING.loginUrl;
            }else{
                location="/"+projectName+"/hivuiLogin/index.html#/";
            }
        }
        if (!config.url.startsWith("/api") && config.url.indexOf(requestUrl) == -1) {
            config.url = requestUrl + config.url;
        }
        if(config.url.indexOf(pn) == -1){
            if(config.url.indexOf('pn=birt') != -1){
                var url = config.url.replace('birt', pn)
                console.log('url', url)
                config.url = url 
            }else if(config.url.indexOf('pn=false') != -1){
                var url = config.url.replace('false', pn)
                console.log('url', url)
                config.url = url 
            }else {
                if (config.url.indexOf('?') == -1) {
                    config.url += "?pn=" + pn;
                }else {
                    config.url += "&?pn=" + pn
                }
            }
        }
    return config
        return config
    },
    error => {
        console.log(error)
        return Promise.reject(error)
    }
)

axios.interceptors.response.use(function (response) {
    // 因返回mock数据包格式不统一,做兼容处理,枚举接口未返回status:200
    var res = (response[STATUS_KEY] && response.dataPack) ? response : response.data;
    res = (res || response.dataPack) || response;
    let status = res[STATUS_KEY];
    if (status != STATUS_SUCCESS && status != 207) {
        if (!checkTimeout(res)) {
            let msgType = "error";
            if (res == 700) {
                msgType = "info";
            }
            Message({
                message: res.msg || 'error',
                type: msgType,
                duration: 5 * 1000
            })
        }
        return Promise.reject(res.msg || 'error')
    } else if (res[STATUS_KEY] == 401) {
        Message({
          message: 'token失效',
          type: 'error',
          duration: 5 * 1000
        })
      } else {
        return res
      }
}, function (error) {
    const res = error.response.data;
    if (!checkTimeout(res)) {
        Message({
            showClose: true,
            message: res.msg || '未知错误',
            type: 'error',
            duration: 5 * 1000
        })
        return Promise.reject(error)
    }
});

export default axios;