 //测试地址
  /* const baseURL = 'http://api.dms.66km.com.cn/' 
 const burl2='http://api.dms.66km.com.cn/' */
/* const baseURL = 'http://192.168.1.18:20187/'
 const burl2='http://192.168.1.18:20187/' */
//66正式
/* const baseURL = 'https://apidms.66km.com/' 
 const burl2='https://apidms.66km.com/' */ 
//欧洲维修
 /* const baseURL = 'https://edms.eurorepar.cn/' 
 const burl2='https://edms.eurorepar.cn/' */
 //京猫虎
/* const baseURL = 'http://apidms.jcatt.cn/'
  const burl2='http://apidms.jcatt.cn/' */ 
 //安徒生
/* const baseURL = 'http://dms-api.lhrhy.cn/'
 const burl2='http://dms-api.lhrhy.cn/' */
/* const baseURL = 'http://store-api.qdbtl.cn/'
 const burl2='http://store-api.qdbtl.cn/' */
 /* const baseURL = 'https://store-api.qdbtl.cn/'
 const burl2='https://store-api.qdbtl.cn/' */ 
/* const baseURL = '//store-api.qdbtl.cn/'
 const burl2='//store-api.qdbtl.cn/' */
// import md5 from './MD5.js'

 //首配
 /* const baseURL = 'http://dms.shoupeiyun.com/'
 const burl2='http://dms.shoupeiyun.com/' */
 //驰湃
 /* const baseURL = 'http://dmsapi.chipaioil.com/'
 const burl2='http://dmsapi.chipaioil.com/' */ 
 //宝顺
 /* const baseURL = 'http://dmsapi.baoshuncar.com/'
 const burl2='http://dmsapi.baoshuncar.com/' */
 //8. 汇捷
  /* const baseURL = 'http://apidms.dqcarlink.com/'
 const burl2='http://apidms.dqcarlink.com/' */
 // 英冠
 /* const baseURL = 'http://dmsapi.cnjla.com/'
  const burl2='http://dmsapi.cnjla.com/' */
  //极致工匠
   const baseURL = 'http://dmsapi.jizhigongjiang.com/'
   const burl2='http://dmsapi.jizhigongjiang.com/' 
  //美勒
  /* const baseURL = 'http://dmsapi.mljqp.com/'
  const burl2='http://dmsapi.mljqp.com/' */
 // 欧洲维修申请
 const europeUrl='https://edms.eurorepar.cn/' 
 
const fetchPost = (url = '', params = {}, header = {
}) => {
    return new Promise((resolve, reject) => {
		header['Content-Type']='application/x-www-form-urlencoded';
		header['code'] = code
		params = {
		  ...params,
		  _t: new Date().getTime()
		}
        uni.request({
            method: 'POST',
            url: baseURL + url,
            data: params,
            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 fetchGet = (url = '', params = {}, header = {
}) => {
    return new Promise((resolve, reject) => {
		header['Content-Type']='application/x-www-form-urlencoded';
		header['code'] = code
		params = {
		  ...params,
		  _t: new Date().getTime()
		}
        uni.request({
            method: 'GET',
            url: baseURL + url,
            data: params,
            header: header,
            dataType: 'json',         
        }).then((response) => {
           setTimeout(function() {
                uni.hideLoading();
            }, 200); 
			let [error, res] = response;
            resolve(res.data);
        }).catch(error => {
            let [err, res] = error;
            reject(err)
        }) 
    });
}
 

const http = (url = '', date = {}, type = 'POST', header = {
}) => {
    return new Promise((resolve, reject) => {
		header={};
		
		
		//header['token']='98A9FED8814B457288166BDF623F33D1';
		//header['uid']='795957B6-8F4B-4E7F-96FE-1F26E5BA21A1'; 
		header['Content-Type']='application/x-www-form-urlencoded';
		console.log(baseURL + url)
        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: 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)
	})
}
const europehttp = (url = '', date = {}, type = 'POST', header = {
}) => {
    return new Promise((resolve, reject) => {
		header={};
		
		
		//header['token']='98A9FED8814B457288166BDF623F33D1';
		//header['uid']='795957B6-8F4B-4E7F-96FE-1F26E5BA21A1'; 
		header['Content-Type']='application/x-www-form-urlencoded';
		console.log(europeUrl + url)
        uni.request({
            method: type,
            url: europeUrl + 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)
        }) 
    });
}


export default {
	http,baseURL,fetchGet,fetchPost,europehttp
}