/*
 * @Author: linwl
 * @Date: 2020-04-13 14:47:59
 * @LastEditTime: 2023-02-24 14:54:19
 * @LastEditors: JinxChen
 * @Description: axios请求配置
 * @FilePath: \TelpoH5FrontendWeb\src\http\requestPolymer.js
 */
import axios from 'axios';
import prefix from '@/store/prefix';
const httpProxyPrefix = process.env.VUE_APP_BASE_API + 'telpopay/';

// create an axios instance  axios.create创建一个实例
const service = axios.create({
    // baseURL: process.env.VUE_APP_BASE_API,
    baseURL: httpProxyPrefix,
    // timeout: 5000 // request timeout
});

const errorHandler = (error) => {
    // const status = get(error, 'response.status');
    const status = error.response.status;
    console.log(status);
    switch (status) {
        case 400: error.message = '请求错误'; break;
        case 401: error.message = '未授权，请登录'; break;
        case 403: error.message = '拒绝访问'; break;
        case 404: error.message = '请求地址出错'; break;
        case 408: error.message = '请求超时'; break;
        case 500: error.message = '服务器内部错误'; break;
        case 501: error.message = '服务未实现'; break;
        case 502: error.message = '网关错误'; break;
        case 503: error.message = '服务器不可用'; break;
        case 504: error.message = '网关超时'; break;
        case 505: error.message = 'HTTP版本不受支持'; break;
        default: break;
    }
    return Promise.reject(error);
};

// 请求拦截器,增加  token interceptors拦截
service.interceptors.request.use(
    request => {
        /* if (localStorage.getItem('token')) {

        } */
        if (localStorage.getItem(prefix + 'token')) {
            request.headers.token = localStorage.getItem(prefix + 'token');
        }
        console.log(localStorage.getItem(prefix + 'token'));
        return request;
    },
    errorHandler
);

// response interceptor
service.interceptors.response.use(
    response => {
        // const res = response.data
       /*  if (response.data.message !== null && response.data.message.indexOf('无效的Access Token') >= 0) { // 登录过期，未建立统一错误码，用 message 暂代
            // 登录过期
            NotifyService.notify({ type: 'warning', message: `请重新登录，${response.data.message}` });
            setTimeout(() => router.push('/login'), 1500);
        } */
        return response;
    },
    errorHandler
);

export default service;
