/* eslint-disable indent */
/*
 * @Author: your name
 * @Date: 2020-10-16 10:38:49
 * @LastEditTime: 2021-12-02 15:50:51
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue3-element-admin\src\plugins\axios.js
 */
"use strict";

import axios from "axios";
import Qs from "qs";
import NProgress from "nprogress";
import {igSuccessUrl} from "../api/config";
import { SET_TOKEN } from "@/store/modules/app/type";

// Full config:  https://github.com/axios/axios#request-config
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
const install = (app, { router, store, opt }) => {
    let config = {
        Global: true,
        // baseURL: process.env.baseURL || process.env.apiUrl || ""
        // timeout: 60 * 1000, // Timeout
        // withCredentials: true, // Check cross-site Access-Control
        // {"userName":"Administrator","pwd":"123456"}
        // userName=Administrator&pwd=123456
    };
    if (VE_ENV.MODE === "production") {
        // config.transformRequest = [(data) => Qs.parse(data)];
    }

    const _axios = axios.create(config);
    let ve_loading;
    let ve_message = null;
    let loadingCount = 0;
    // 请求拦截
    _axios.interceptors.request.use(
        (config) => {
            NProgress.done();
            if (config.Global) {
                if(getIsShow(config.url) == false) {
                    // NProgress.start();
                    ve_loading = app.config.globalProperties.$loading({
                        lock: true,
                        text: "Loading",
                        spinner: "el-icon-loading",
                        background: "rgba(0,0,0,0.1)",
                    });
                }
            }
            loadingCount++;
            //*请求头添加token
            const token = store.getters.token;
            token && (config.headers.token = token);
            const djiCloudToken = store.getters.djiCloudToken;
            djiCloudToken && (config.headers['x-auth-token'] = djiCloudToken);

            // Do something before request is sent
            return config;
        },
        (error) => {
            // Do something with request error
            return Promise.reject(error);
        }
    );

    // Add a response interceptor
    // 响应拦截
    _axios.interceptors.response.use(
        (response) => {
            // TODO 根据响应头更新token
            //store.dispatch(`app/${SET_TOKEN}`, new Date().getTime());

            loadingCount--;
            if (loadingCount <= 0) {
                NProgress.done();
                if(ve_loading)
                ve_loading.close();
            }
            let type = "";
            if (response.data.code == 0 || response.data.code == 1) {
                type = "success";
            } else {
                type = "error";
            }
            //igSuccess 状态为false 才需要成功提示
            if(type == "error" && getIsShow(response.request.responseURL) == false){
                if (ve_message) {
                    ve_message.close();
                    ve_message = null;
                }
                if(response.data.msg)
                    ve_message = app.config.globalProperties.$message({
                        type,
                        message: response.data.msg,
                    });
            }
            // Do something with response data
            return response.data;
        },
        (error) => {
            loadingCount--;
            if (loadingCount <= 0) {
                NProgress.done();
                ve_loading.close();
            }
            if (error && error.response) {
                let message = "";
                switch (error.response.status) {
                    case 400:
                        message = "请求错误";
                        break;
                    case 401: {
                        message = "未授权，请登录";
                        router.replace({
                            name: "Login",
                        });
                        break;
                    }
                    case 403:
                        message = "没有权限，拒绝访问";
                        break;
                    case 404:
                        message = `请求地址出错`;
                        break;
                    case 408:
                        message = "请求超时";
                        break;
                    case 500:
                        message = "服务器内部错误";
                        break;
                    case 501:
                        message = "服务未实现";
                        break;
                    case 502:
                        message = "网关错误";
                        break;
                    case 503:
                        message = "服务不可用";
                        break;
                    case 504:
                        message = "网关超时";
                        break;
                    case 505:
                        message = "HTTP版本不受支持";
                        break;
                    default:
                        break;
                }
                if (ve_message) {
                    ve_message.close();
                    ve_message = null;
                }
                ve_message = app.config.globalProperties.$message({
                    message,
                    type: "error",
                });
            }
            // Do something with response error
            return Promise.reject(error);
        }
    );

    function getIsShow(url){
        //请求成功的时候过滤不需要提示的请求
            for(let s of igSuccessUrl){
                if(url.indexOf(s) > -1){
                    return true;
                }
            }
        return false;
    }


    const method = {
        post: (url, p, config) => {
            return _axios.post(url, p, config)
        },
        get: (url, p, config) =>{
            return _axios.get(url, Object.assign(config, { params: p }))
        },
        put: (url, p, config) =>{
            return _axios.put(url, p, config)
        },
        delete: (url, p, config) =>{
            return _axios.delete(url, config)
        },
    };

    let api = {};
    const files = require.context("@/api/modules", false, /\.js$/);
    files.keys().forEach((key) => {
        const fileName = key.replace(/(\.\/|\.js)/g, "");
        api[fileName] = {};
        let obj = files(key);
        Object.keys(obj).forEach((item) => {
            api[fileName][item] = (p, config = {}) => {
                if(VE_ENV.MOCK === "True" && obj[item].mockCondition && obj[item].mockCondition(p)){
                    return obj[item].mockFun(p);
                }
                return method[obj[item].type](pathVariable(obj[item].url, p ), p, config);
            };
        });
    });

    window[opt] = api;
    app.config.globalProperties[opt] = api;
};

function pathVariable(url,obj){
    let prefix = url.indexOf("{")
    if(prefix > -1){
        let suffix = url.indexOf("}")
        let key = url.substr(prefix + 1, suffix - prefix - 1)
        url = url.replace("{"+key+"}",obj[key])
        return pathVariable(url, obj);
    }
    return url;
}

export default { install };
