import { getHashQuery } from '../utils/index.js'
var url='http://'+getHashQuery('url')+'/'
//const baseUrl = url
console.log(getHashQuery('url'))
if(getHashQuery('url')){
	var baseUrl =url
}else{
  var baseUrl = 'http://api.db.66km.cn/'	
 //  var baseUrl = 'http://192.168.1.22:20012/'
  //var baseUrl = 'http://testmanageapi.66km.cn/'
   
}

const burl2=url 
//baseUrl = 'http://crm.eurorepar.cn/'
import md5 from './MD5.js'
var token=getHashQuery('token')
 token = decodeURIComponent(token).replace(/[\r\n]/g, '').trim()
console.log(token)
var uid=getHashQuery('uid')
console.log(uid)

const http = (url = '', date = {}, type = 'POST', header = {
}) => {
    return new Promise((resolve, reject) => {
		header={};
		
		if(token){
			//header['accessToken']=token;
			header['token']=token;
			//header['uid']=uid; 
		}else{
			header['accessToken']='';
			header['uid']=''; 
		}
		
		//header['token']='DD5F30F1CCC04E6B941929D07239D7FF';
		//header['uid']='54E7CAA8-BCE1-4084-8A1B-EE737F4BCE0B'; 
		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;
			//console.log(res.data)
			if(res.data.code==403){
				uni.showToast({
				 	     title: res.data.msg,
				 	 	 icon:'none',
				 	     duration: 3000,
				 	 });
			}else{
				if(res.data.data.number == 200 || res.data.data.number == 5212){
					
				}else{
					if(url=='advancedEpc/cachekey'){
						
					}else{
						uni.showToast({
						 	     title: res.data.msg,
						 	 	 icon:'none',
						 	     duration: 3000,
						 	 });
					}
					
				}
			}
			
			// if(res.data.code==1){
			// 	 uni.showToast({
			// 	     title: res.data.msg,
			// 	 	 icon:'none',
			// 	     duration: 3000,
			// 	 });
			// 	//  uni.clearStorageSync()
				
			// }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
}