//测试地址
//const baseUrl = 'http://api.dms.66km.com.cn/'
//const baseUrl = 'http://192.168.0.121:20187/'
//const baseUrl = 'http://192.168.0.126:20187/'
//正式
//const baseUrl = 'https://apidms.66km.com/'
//正式地址
import $store from '../store'
const extConfig = uni.getExtConfigSync();		
console.log(extConfig)
const baseUrl=extConfig.url+'/'
//const baseUrl='http://192.168.0.131:20189/'
//const baseUrl='https://store-api.qdbtl.cn/'
//const baseUrl='https://apidms.66km.com/'
//wxc023ff04deb28f2d正式  wx33053a645546ec31 E37BB296-5A08-4534-859D-B351BA611AF9 EEADACCD-8A19-499D-8AD7-6975D2C93243 
//测试地址
//const webUrl = 'http://dms.32che.cn/'
//正式
const webUrl = 'https://webwx.66km.com/'
console.log(baseUrl)
const http = (url = '', date = {}, type = 'POST', header = {
}) => {
    return new Promise((resolve, reject) => {
		header={};
		//console.log(baseUrl)
		console.log($store.state.wxOpenData.loginInfo)
		//const wxOpenData = uni.getStorageSync("wxOpenData");
		var wxOpenData=$store.state.wxOpenData
		if(wxOpenData.loginInfo){
			header['token']=wxOpenData.loginInfo.token;
			header['uid']=wxOpenData.loginInfo.uid; 
		}
		//console.log($store.state.carInfo)
		//header['token']='98A9FED8814B457288166BDF623F33D1';
		//header['uid']='39415962-C9D3-466D-A73F-5B5429A8DB2A'; 
		header['Content-Type']='application/x-www-form-urlencoded';
		//console.log(header)
        uni.request({
            method: type,
            url: baseUrl + url,
            data: date,
            header: header,
            dataType: 'json',         
        }).then((response) => {
           /* setTimeout(function() {
                uni.hideLoading();
            }, 200); */
            let [error, res] = response;
		 
			if(res.data.code==401||res.data.code==403){
				//  uni.showToast({
				//      title: res.data.msg,
				//  	 icon:'none',
				//      duration: 3000,
				//  });
				 uni.clearStorageSync()
				//  uni.navigateTo({
				// 	url:'../login/login'
				// }) 
			}else if(res.data.code==409){
				 // getToken(url,date,type,header)
			}
			if(res.data.code==1){
				  uni.showToast({
				      title: res.data.msg,
				  	 icon:'none',
				      duration: 4000,
				  });
			}
            resolve(res.data);
        }).catch(error => {
            let [err, res] = error;
            reject(err)
        }) 
    });
}
// 请求图片流
const httpBinary = (url = '', date = {}, type = 'POST', header = {
}) => {
    return new Promise((resolve, reject) => {
		header={};
		//const wxOpenData = uni.getStorageSync("wxOpenData");
		var wxOpenData=$store.state.wxOpenData
		if(wxOpenData){	
			header['token']=wxOpenData.loginInfo.token;
			header['uid']=wxOpenData.loginInfo.uid; 
		}
		//header['token']='98A9FED8814B457288166BDF623F33D1';
		//header['uid']='39415962-C9D3-466D-A73F-5B5429A8DB2A'; 
		header['Content-Type']='application/x-www-form-urlencoded';
		//console.log(header)
        uni.request({
            method: type,
            url: baseUrl + url,
            data: date,
            header: header,
            dataType: 'json', 
			responseType: "arraybuffer",
        }).then((response) => {
           /* setTimeout(function() {
                uni.hideLoading();
            }, 200); */
            let [error, res] = response;
		 
			if(res.data.code==401||res.data.code==403){
				 uni.showToast({
				     title: res.data.msg,
				 	 icon:'none',
				     duration: 3000,
				 });
				 uni.clearStorageSync()
				 uni.navigateTo({
					url:'../login/login'
				}) 
			}else if(res.data.code==409){
				 // getToken(url,date,type,header)
			}
			
            resolve(res.data);
        }).catch(error => {
			uni.hideLoading();
            let [err, res] = error;
            reject(err)
        }) 
    });
}

function getToken(url,date,type,header){
	var header={};
    //var userId=uni.getStorageSync("devData").userId;
	//var devData=uni.getStorageSync("devData")
	header['Content-Type']='application/x-www-form-urlencoded';
	uni.request({
	    method: "POST",
	    url: baseUrl + '/sys/getTokenByUserID',
	    data: {
			userId:userId
		},
	    header: header,
	    dataType: 'json',         
	}).then((response) => {
	    let [error, res] = response;
		var accessToken=res.data.data.accessToken.accessToken;
		devData.accessToken=accessToken;
		uni.setStorage({
			   key: 'devData',
			   data:devData,
			   success: function () {
				   refresh(url,date,type,header) //刷新接口
			   }
		 });
		  // console.log(accessToken)
	}).catch(error => {
	   
	}) 
}

function refresh(url,date,type,header){
	uni.request({
	    method: type,
	    url: baseUrl + url,
	    data: date,
	    header: header,
	    dataType: 'json',         
	}).then((response) => {
	   setTimeout(function() {
	        uni.hideLoading();
	    }, 200); 
	    let [error, res] = response;
	  
		if(res.data.code==403){
			uni.showToast({
			     title: res.data.msg,
			 	 icon:'none',
			     duration: 3000,
			 });
			 uni.navigateTo({
				url:'../login/login'
			}) 
		}
	    resolve(res.data);
	}).catch(error => {
	    let [err, res] = error;
	    reject(err)
	})
}

export default {
	http,baseUrl,httpBinary,webUrl
}