
// let baseURL1 = "http://www.rangertech.com.cn";//测试
// let baseURL1 = 'http://10.81.8.103:8181'
let baseURL1 = 'http://10.81.8.233:8181'
// let baseURL1 = "http://localhost:8181";
//let baseURL1 = "http://289f65x458.qicp.vip";
//let baseURL1 = "https://rysbsj.shaphc.org:30443"; //钉钉
let datext='/covid'
const baseURL = baseURL1+datext;   //
// 请求接口
function getRequest(url,data){
    let token=uni.getStorageSync('token') || '';
	var promise = new Promise((resolve,reject) => {
			var postData = data;
			uni.request({
				url:baseURL + url,
				data:postData,
				method:"GET",
				dataType:'json',
				header:{
					 'Content-Type':'application/x-www-form-urlencoded;application/json',
					 'x-auth-token':token
				},
				success:function(res){
					if(res.statusCode === 200){
						if(res.data.code=='200'){
								if(res.data.data.token){
									uni.setStorage({
										key:'token',
										data:res.data.data.token
									})
								}
						}
						resolve(res.data);
					}else{
						resolve(res.data)
					}
				},
				error:function(e)
				{
					reject('网络出错');
				}
			});
	});
	return promise;
};
//post请求封装
function postRequest(url,data){
			let token=uni.getStorageSync('token');
	var promise = new Promise((resolve,reject) => {
		var postData = data;
		uni.request({
			url:baseURL + url,
			data:postData,
			method:'POST',
			dataType:'json',
			header:{
				'Content-Type':'application/json',
				'x-auth-token':token
			},
			success:function(res)
			{
				if(res.statusCode === 200 && res.data.resultCode == 0)
				{
					resolve(res.data);
				}else{
					resolve(res.data)
				}
			},
			error:function(e)
			{
				reject('网络出错');
			}
		})
	});
	return promise;
}
uni.addInterceptor('request', {
  invoke(args) {
    // request 触发前拼接 url 
	let token=uni.getStorageSync('token')
	if(token){
		args.header['x-auth-token']=token
	}
  },
  success(args) {
    // 请求成功后，修改code值为1
	// if(args.data.data.token){
	// 	uni.setStorage({
	// 		key:'token',
	// 		data:args.data.data.token
	// 	})
	// }
  }, 
  fail(err) {
    //console.log('interceptor-fail',err)
  }, 
  complete(res) {
    //console.log('interceptor-complete',res)
  }
})
module.exports = {
	postRequest,
	getRequest,
	baseURL,
	baseURL1
}
