const install = (Vue, vm) => {
   

    uni.setStorageSync('deptId', 101);
    Vue.prototype.$u.http.setConfig({
        // baseUrl: 'http://61.174.243.28:15861',
        // baseUrl: 'http://192.168.2.46:8080',
        // baseUrl: 'https://che.chuangtewl.com/prod-api',
		// 测试环境
        baseUrl: 'https://dianche.chuantewulian.cn/prod-api',
		// 俞山岛
        // baseUrl: 'https://dche.ccttiot.com/prod-api',
		// 创特
        loadingText: '努力加载中~',  
        loadingTime: 10000,
        // 设置自定义头部content-type
        header: {
            'content-type': 'application/json;charset=UTF-8',
        },
    });
	// 创特:wx3428c498d5061192
	// 俞山岛:	wx4d178f8c80348214
    // 请求拦截部分
    Vue.prototype.$u.http.interceptor.request = (config) => {
        // 引用token
        // 方式一,存放在vuex的token,假设使用了uView封装的vuex方式
        // 见:https://uviewui.com/components/globalVariable.html
        // config.header.token = vm.token;

        // 方式二,如果没有使用uView封装的vuex方法,那么需要使用$store.state获取
        // config.header.token = vm.$store.state.token;

        // 方式三,如果token放在了globalData,通过getApp().globalData获取

        // 方式四,如果token放在了Storage本地存储中,拦截是每次请求都执行的
        // 所以哪怕您重新登录修改了Storage,下一次的请求将会是最新值
        const token = uni.getStorageSync('token');

        // const token = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjQ0ZDljYzBlLThjZjEtNGFkOC05OGFjLThlNThjMWIxYjQ3OSJ9.v-aMkp9t7Z_QfjfsZy6d_1Ng76hPYa0A--SWScMJY9to7UlNv9IxHQnTJylLKdYKGrr8fRZ47Bu12UPm1DgMQg"

        // console.log("我是token", token)
        config.header.Authorization = token;
        // config.header.Tenant-Id=1
        // #ifdef H5
        config.header.Authorization = 'Bearer ' + token;
        // #endif
        // 可以对某个url进行特别处理,此url参数为this.$u.get(url)中的url值
        if (config.url == '/user/login') config.header.noToken = true;
        // 最后需要将config进行return
        return config;

        // 如果return一个false值,则会取消本次请求
        // if(config.url == '/user/rest') return false; // 取消某次请求
    }

    // 响应拦截部分
    Vue.prototype.$u.http.interceptor.response = (res) => {
        // if(res.code == 10022 || res.code == 10023) {
        //     // res为服务端返回值,可能有code,result等字段
        //     // 这里对res.result进行返回,将会在this.$u.post(url).then(res => {})的then回调中的res的到
        //     // 如果配置了originalData为true,请留意这里的返回值
        //     uni.redirectTo({
        //         url:"/pages/login/login",
        //         fail:function(mes){
        //             console.log(mes)
        //         },
        //         success:function(mes){
        //             console.log(mes)
        //         }
        //     })
        //     // return res.result;
        // }
        if (res.code == 401) {
            wx.login({
                success(res) {
                    if (res.code) {
                        console.log('登录!', res);
                        let areaId = uni.getStorageSync('areaId');
                        if (areaId) {
                            vm.$u.post('/loginByopenid?jsCode=' + res.code + '&areaId=' + areaId).then(
                                res => {
                                    uni.hideLoading();
                                    if (res.code == 200) {
                                        uni.setStorageSync('token', res.token);
                                    } else if (res.code == 501) {
                                        uni.showToast({
                                            title: res.msg,
                                            icon: 'none',
                                            duration: 2000
                                        });
                                    } else {
                                        uni.navigateTo({
                                            url: '/pages/login/login'
                                        })
                                    }
                                });
                        }
                    }
                },
            });
        }
        return res;
    }

    // 手动实现超时控制
    const originalRequest = Vue.prototype.$u.http.request;
    Vue.prototype.$u.http.request = (options) => {
        return new Promise((resolve, reject) => {
            const timeout = 20000; // 10秒超时
            const timer = setTimeout(() => {
                resolve( { code: 500, msg: '请求超时' } );
            }, timeout);

            // 执行原始请求
            originalRequest.call(Vue.prototype.$u.http, options).then(response => {
                clearTimeout(timer);
                resolve(response);
            }).catch(error => {
                clearTimeout(timer);
                reject(error);
            });
        });
    };
}

export default {
    install
}