import { defineNuxtPlugin } from '#app'
import axios from 'axios'
import { getToken } from '@/store/useCookieStore'

export default defineNuxtPlugin(nuxtApp => {
    const service = axios.create({
        //baseURL: 'http://apidev.bjzxtw.org.cn' //测试服务器地址
        // baseURL: 'http://192.168.1.234:9501' //pre服务器地址
        baseURL: 'http://apipre1.bjzxtw.org.cn:29501' //pre服务器地址
        // baseURL: 'http://192.168.1.117:9501'  //冯蕊的服务器地址
        // baseURL: 'http://192.168.1.201:9501'  //刘佳伟的服务器地址
    });

    // 请求拦截器
    service.interceptors.request.use(config => {
        // console.log("请求拦截器",config);
        // 从cookie中获取数据
        const token = getToken()
        //const token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJwaHBlcjY2Ni9qd3QiLCJ1aWQiOjc2LCJ1c2VyX25hbWUiOiIxNzM0MDUzMTQ1NCIsIm1vYmlsZSI6IjE3MzExMTExMTExIiwiZW1haWwiOiIiLCJsZXZlbF9pZCI6MCwidHlwZV9pZCI6MTAwMDAsImp3dF9zY2VuZSI6ImRlZmF1bHQiLCJqdGkiOiJkZWZhdWx0XzY3ZDBmYmQ2OWUwZDk2LjY3MTU1NDM5IiwiaWF0IjoxNzQxNzQ5MjA2LCJuYmYiOjE3NDE3NDkyMDYsImV4cCI6MTc0MTgzNTYwNn0.1pfYged1EOSVaQNdAlaKA_kvBJLgOkWKkk38yog_xoU"
        if (token) {
            config.headers.token = token;
        }
        config.headers.Userurl = 'nwpre.bjzxtw.org.cn';
        return config;
    }, error => {
        console.log(error)
        return Promise.reject(error);
    });

    // 响应拦截器
    service.interceptors.response.use(response => {
        if (response.status >= 200 && response.status <= 210) {
            return response.data;
        } else if (response.status == 404) {
            console.error(response.data.errmsg);
        } else {

        }
    },
        error => {
            // console.log(1);
            console.log('err' + error);
        })

    return {
        provide: {
            axios: service
        }
    }
});

