/*
 * @Author: linwl
 * @Date: 2020-04-13 14:47:59
 * @LastEditTime: 2023-03-13 16:35:17
 * @LastEditors: JinxChen
 * @Description: axios请求配置
 * @FilePath: \TelpoH5FrontendWeb\src\http\webapi.js
 */
import axios from 'axios';
const httpProxyPrefix = process.env.VUE_APP_BASE_API + 'webapi';

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

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('webapiToken')) {

        } */
        request.headers.AuthToken = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJVc2VySW5mbyI6eyJVc2VySWQiOiJjZTQzOWU1Yy03NWVjLTRhMTEtYWJmMC02YTdhM2IzY2UwMGQiLCJMb2dpbk5hbWUiOiIxODI3NzQyNjcxMiIsIkxvZ2luVHlwZSI6MX0sIkV4cCI6MTY4NjY0NTI1MTQ4NC4wfQ.cm3AblDTivfbuJ-OgDAglWVsJFWiJZyrpt_BmNq499g';
        return request;
    },
    errorHandler
);

// response interceptor
service.interceptors.response.use(
    response => {
        return response;
    },
    errorHandler
);

export default service;
