const install = (Vue, vm) => {
	
	// Vue.prototype.$u.http.setConfig({
		
	// 	 baseURL: 'https://yruibao.com/admin',
		
	// 	// baseUrl: 'http://192.168.10.104:8088',  
	// 	method: 'POST',      
	// 	// 设置为json,返回后会对数据进行一次JSON.parse()
	// 	dataType: 'json',  
	// 	showLoading: true, // 是否显示请求中的loading
	// 	loadingText: '...', // 请求loading中的文字提示
	// 	loadingTime: 800, // 在此时间内,请求还没回来的话,就显示加载中动画,单位ms
	// 	originalData: false, // 是否在拦截器中返回服务端的原始数据
	// 	loadingMask: true, // 展示loading的时候,是否给一个透明的蒙层,防止触摸穿透
	// 	// 配置请求头信息 
	// 	header: {   
	// 		'content-type': 'application/json;charset=UTF-8',
			
	// 	},
	// https://yxd.ccttiot.com/prod-api}); 
	Vue.prototype.$u.http.setConfig({
			// baseUrl: 'http://192.168.1.2:8089',
			baseUrl: 'https://testcha.chuangtewl.com/prod-api',
			loadingText: '努力加载中~',
			loadingTime: 800,
			// 设置自定义头部content-type
			header: {
				'content-type': 'application/json;charset=UTF-8',
			},
			// ......
		});

// 请求拦截部分,如配置,每次请求前都会执行
	Vue.prototype.$u.http.interceptor.request = (config) => {
		// 引用token
		// 方式一,存放在vuex的token,假设使用了uView封装的vuex方式
		// 见:https://uviewui.com/components/globalVariable.html
		// config.header.token = vm.token;
		
		// 方式二,如果没有使用uView封装的vuex方法,那么需要使用$store.state获取
		// config.header.token = vm.$store.state.token;
		
		// 方式三,如果token放在了globalData,通过getApp().globalData获取

		// 方式四,如果token放在了Storage本地存储中,拦截是每次请求都执行的
		// 所以哪怕您重新登录修改了Storage,下一次的请求将会是最新值
		const token = uni.getStorageSync('token');

		// const token = "	eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImE1ZmE2YzM0LWM0Y2YtNGEwMS05YjY2LTE1M2M4YWY1YzU1YSJ9.Wze_tAjY7cCfw2K6mtDpTSd-QFLRF4A49Fx7bq7g2tVLKL4v5bqqHfpG0VQy7_a_CUQch5RPBTBIKMVCDupIqg"

		// console.log("我是token", token)
		config.header.Authorization = token; 
		// config.header.Tenant-Id=1
		// #ifdef H5
			config.header.Authorization = 'Bearer '+token; 
		// #endif
		// 可以对某个url进行特别处理,此url参数为this.$u.get(url)中的url值
		if(config.url == '/user/login') config.header.noToken = true;
		// 最后需要将config进行return
		return config;
		
		// 如果return一个false值,则会取消本次请求
		// if(config.url == '/user/rest') return false; // 取消某次请求
	}
	
	// 响应拦截,如配置,每次请求结束都会执行本方法
		Vue.prototype.$u.http.interceptor.response = (res) => {
			// if(res.code == 10022 || res.code == 10023) {
			// 	// res为服务端返回值,可能有code,result等字段
			// 	// 这里对res.result进行返回,将会在this.$u.post(url).then(res => {})的then回调中的res的到
			// 	// 如果配置了originalData为true,请留意这里的返回值
			// 	uni.redirectTo({
			// 		url:"/pages/login/login",
			// 		fail:function(mes){
			// 			console.log(mes)
			// 		},
			// 		success:function(mes){
			// 			console.log(mes)
			// 		}
			// 	})
			// 	// return res.result;
			// }
			if(res.code == 401) {
				// res为服务端返回值,可能有code,result等字段
				// 这里对res.result进行返回,将会在this.$u.post(url).then(res => {})的then回调中的res的到
				// 如果配置了originalData为true,请留意这里的返回值
			
				// return res.result;
			
				// uni.login({
				// 	success: function(ret) {
				// 		console.log("main.js==>res", ret)
				// 		// vm.$u.post('/user/login',{"js_code": ret.code}).then(res=>{
				// 		// 		if (res.code == 10003) {
				// 		// 			// console.log("新用户登录")
				// 		// 			uni.setStorageSync('token', res.data);
									
				// 		// 		} else if (res.code == 200) {
				// 		// 			// console.log("老用户登录",res.data)
				// 		// 			uni.setStorageSync('token', res.data);
									
				// 		// 		}
				// 		// 	});
				// 	}
				// });
				wx.login({	
				    success(res) {
				        if (res.code) {
				            console.log('登录!', res)
				            let data = {
				                jsCode: res.code,  
				            }
							// vm.$u.post(`/loginByopenid?jsCode=${res.code}`,data).then(res=>{
				   //      		if (res.code == 200) {
							// 		console.log("老用户登录",res)
							// 		wx.setStorageSync('token', res.token)
							// 		// uni.reLaunch({
							// 		// 	url:'/pages/nearbystores/index'
							// 		// })
				   //      		} else{
				   //      			// uni.showModal({
				   //      			// 	    title: '温馨提示',  
				   //      			// 	    content: '您该操作需登录,是否前去登录?',  
				   //      			// 	    success: function (res) {  
				   //      			// 	        if (res.confirm) {
				   //      			// 				uni.navigateTo({
				   //      			// 					url:'/pages/login/index'
				   //      			// 				})
				   //      			// 	        } else if (res.cancel) {
				        				        
				   //      			// 	    }  
				   //      			// 	}  
				   //      			// })
				   //      		}
				   //      	})
				        }
				    },
				})
			}
			return res;
			//  else if(res.code == 201) {
			// 	// 假设201为token失效,这里跳转登录
			// 	vm.$u.toast('验证失败,请重新登录');
			// 	setTimeout(() => {
			// 		// 此为uView的方法,详见路由相关文档
			// 		vm.$u.route('/pages/user/login')
			// 	}, 1500)
			// 	return false;
			// } else {
			// 	// 如果返回false,则会调用Promise的reject回调,
			// 	// 并将进入this.$u.post(url).then().catch(res=>{})的catch回调中,res为服务端的返回值
			// 	return false;
			// }
		}
}

export default {
	install
}