const baseUrl = 'http://58.56.15.138:20201/' 
const burl2='http://58.56.15.138:20201/' 
//const baseUrl = 'http://58.56.15.138:20132/' 
//  const baseUrl = 'https://apiclt.66km.com/'
// const burl2='https://apiclt.66km.com/'

import md5 from './MD5.js'


/* const http = (url = '', date = {}, type = 'POST', header = {
}) => {
    return new Promise((resolve, reject) => {
		console.log(burl2 + url)
		//header['Content-Type']='application/x-www-form-urlencoded';
		header['actionFromMiniApp']=1;
        uni.request({
            method: type,
            url: burl2 + 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.clearStorageSync()
				 uni.navigateTo({
					url:'../login/login'
				}) 
			}else if(res.data.code==409){
				  getToken(url,date,type,header)
			}
          		
            resolve(res.data);
        }).catch(error => {
            let [err, res] = error;
            reject(err)
        }) 
    });
}
 */

const http = (url = '', date = {}, type = 'POST', header = {
}) => {
    return new Promise((resolve, reject) => {
		console.log(burl2 + url)
		header={};
		const logodata = uni.getStorageSync("logodata");
		header['token']=logodata.token;
		header['uid']=logodata.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',         
        }).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 => {
            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: burl2 + '/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: burl2 + 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
}