import  carhelp from '@/utils/mixin.js'

//记录请求次数和响应次数
let reqCount = 0,
	resCount = 0;
//var token=carhelp.getToken();
var baseUrl=process.car.BASE_URL;

if (process.env.NODE_ENV === "development") {
	//baseUrl="http://192.168.33.90:8086/jp-employment-server";
}
const request = (options) => {
	var token=carhelp.getToken(options);
	
	reqCount++;
	 
	return new Promise((resolve, reject) => {
		//如果特殊链接需要传入token
		if(options&&options.token){
			token=options.token;
		}
		uni.request({
			method: options.method?options.method:'get',
			url: baseUrl + options.url,
			data: options.data?options.data:{},
			
			header: options.header?{
				...options.header,
				'Authorization':token
			}:{
				'Content-Type': 'application/x-www-form-urlencoded',
				'X-Requested-With': 'XMLHttpRequest',
				'Authorization':token
			}
		}).then((response) => {
			//for(var t = Date.now(); Date.now() - t <= 2000;);
			
			resCount++
			//防止连续请求多个接口时loading闪现
			let [error, res] = response;
			
			if (res.data.code == 415&&res.data.message=='请先注册信息') {
				carhelp.signOut()
				uni.redirectTo({
					url: '/pages/login/index'
				})
				return;
			}
			
			if (res.data.code != 200) {
				reject(res.data.message)
			} else {
				if(!res.data.result){
					uni.showToast({
						title: res.data.message,
						position:'bottom'
					})
				}
				resolve(res.data);
			}
		}).catch(error => {
			resCount++
		
			let [err, res] = error;
			reject(err)
		})
	});
}
export default request
