const baseUrl = 'https://r.ucaret.cn/test-api'; // 开发地/址
// const baseUrl = 'http://5thcg5.natappfree.cc'; // 开发地址
// const baseUrl = 'https://jduniapp.uzosp.com'; // 线上地/址
const httpRequest = (opts, data) => {
    let httpDefaultOpts = {
        url: baseUrl + opts.url,
        data: data,
        method: opts.method,
        header: opts.method == 'get' ? {
            'X-Requested-With': 'XMLHttpRequest',
            "Accept": "application/json",
            "Content-Type": "application/json; charset=UTF-8"
        } : {
            'X-Requested-With': 'XMLHttpRequest',
            "Content-Type": "application/json; charset=UTF-8"
            // 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        },
        dataType: 'json',
    }
    let promise = new Promise(function(resolve, reject) {
        uni.request(httpDefaultOpts).then(
            (res) => {
                resolve(res[1])
            }
        ).catch(
            (response) => {
                reject(response)
            }
        )
    })
    return promise
};
let requestNumber = 0;
//带Token请求
const httpTokenRequest = (opts, data) => {
    let token = uni.getStorageSync('userToken');
    uni.getStorage({
        key: 'token',
        success: function(ress) {
            token = ress.data
        }
    });
    //此token是登录成功后后台返回保存在storage中的
    let httpDefaultOpts = {
        url: baseUrl + opts.url,
        data: data,
        method: opts.method,
        header: opts.method == 'get' ? {
            'Authorization': 'Bearer ' + token,
            'X-Requested-With': 'XMLHttpRequest',
            "Accept": "application/json",
            "Content-Type": "application/json; charset=UTF-8"
            // "Content-Type": "application/json; charset=UTF-8"
        } : {
            'Authorization': 'Bearer ' + token,
            'X-Requested-With': 'XMLHttpRequest',
            "Content-Type": "application/json; charset=UTF-8"
            // 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        },
        dataType: 'json',
    }
    let promise = new Promise(function(resolve, reject) {
        uni.request(httpDefaultOpts).then(
            (res) => {
                console.log(33333333333,requestNumber,baseUrl,res[1])
				if(res[1].data.code == 401){
					if(requestNumber <= 0){
						requestNumber ++
						uni.removeStorageSync('userToken')
						let returnPage= '/pages/main';
						let pageType = "reLaunch"
						uni.navigateTo({
							url:"/pages/login/index/index?returnPage="+returnPage+'&pageType='+pageType,
							success:()=>{
								requestNumber = 0
							}
						})
					}
					return;
				}else if(res[1].data.code == 500){
					// uni.showToast({
					// 	title:res[1].data.msg,
					// 	icon:"none"
					// })
				}
                resolve(res[1])
            }
        ).catch(
            (response) => {
                reject(response)
            }
        )
    })
    return promise
};

export default {
    baseUrl,
    httpRequest,
    httpTokenRequest
}