import  carhelp from '@/apis/utils/mixin.js'
import requestSon from './requestSon.js';


const request = (options) => {
	//记录请求次数和响应次数
	var tdate=new Date( +new Date() + 8 * 3600 * 1000 ).toJSON().substr(0,15).replace("T"," ")
	var token_tdate=carhelp.get("token_tdate");//每10分钟存储一次token
	
	var token=carhelp.getToken(options);
	
	 if(token_tdate&&token_tdate==tdate){
	 	return requestSon(options);
	 }
	 var openId=carhelp.getOpenId()
	 
	var  urlindex="/wechat/findUserInfo"
	var k=options.url.indexOf(urlindex)==-1;
	
	 if(!openId&&k){
		 return
	 }
	if(!k){
		return requestSon(options);
	}
	
	return new Promise((resolve, reject) => {
		 var loginurl="/mobile/employeeUser/findByOpenId"
		 
		uni.request({
			method:'get',
			url: process.car.BASE_URL + loginurl,
			data: {
				openId:openId,
				//data.type="5"
				code:"reading"
				
			},
			
			header: {
				'Content-Type': 'application/x-www-form-urlencoded',
				'X-Requested-With': 'XMLHttpRequest',
				//'Authorization':token
			}
		}).then((response) => {
 			let [error, res] = response;
			
			
			if (res.data.code == 200&&res.data.result) {
				
				var token = res ? res.data.data.token : '';
				carhelp.setPersonInfo(res.data.data.regUser );
				
				carhelp.setToken(token);
				carhelp.setPersonInfoPlus(res.data.data);
				
				options.token=token
				
				var isson= requestSon(options)
				
				resolve(isson);
				 
			} else  if(res.data.code == 415||(res.data.code == 200&&!res.data.result)||res.data.code == 400){
				
				//var url=window.location.href.split("#")[1]
			
				console.log("没有免登陆 ----------------"+options.url)
				//var url="/pages/login/index?message=请登录&back="+ url+"&phone="+options.data.phone
				var thisurl='/pages/login/login';
				
				if(options&&options.data&&options.data.jpcode){
					thisurl+="?jpcode="+options.data.jpcode
				}
				carhelp.signOut()
				uni.redirectTo({
					url: thisurl
				})
				
				
				
			 
				
			}else{
		
				reject(res.data.message)
			}
		}).catch(error => {
			
			let [err, res] = error;
			reject(err)
		})
	});
}
export default request
