74 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			74 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
const install = (Vue, vm) => {
 | 
						||
	Vue.prototype.$u.http.setConfig({
 | 
						||
			baseUrl: 'http://192.168.2.88:3100/dev-api',
 | 
						||
			// baseUrl: 'https://znb.ccttiot.com',
 | 
						||
			loadingText: '努力加载中~',
 | 
						||
			loadingTime: 800,
 | 
						||
			// 设置自定义头部content-type
 | 
						||
			header: {
 | 
						||
				'content-type': 'application/json;charset=UTF-8',
 | 
						||
				
 | 
						||
			},
 | 
						||
			// ......
 | 
						||
		});
 | 
						||
 | 
						||
// 请求拦截部分,如配置,每次请求前都会执行
 | 
						||
	Vue.prototype.$u.http.interceptor.request = (config) => {
 | 
						||
		const token = uni.getStorageSync('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 == 401) {
 | 
						||
				uni.login({
 | 
						||
					success: function(ret) {
 | 
						||
						console.log("main.js==>res", ret)
 | 
						||
					}
 | 
						||
				});
 | 
						||
				wx.login({
 | 
						||
				    success(res) {
 | 
						||
				        if (res.code) {
 | 
						||
				            console.log('登录!', res);
 | 
						||
				            let data = {
 | 
						||
				                wxOpenId: res.code,
 | 
						||
				                
 | 
						||
				            };
 | 
						||
							vm.$u.post('/app/auth/wxLogin',data).then(res=>{
 | 
						||
				        		if (res.code == 10003) {
 | 
						||
				        			uni.navigateTo({
 | 
						||
				        				url:'/pages/login/login'
 | 
						||
				        			})
 | 
						||
				        			
 | 
						||
				        		} else if (res.code == 200) {
 | 
						||
				        			// console.log("老用户登录",res.data)
 | 
						||
				        			uni.switchTab({
 | 
						||
				        				url:'/pages/index/index'
 | 
						||
				        			})
 | 
						||
				        			
 | 
						||
				        		}
 | 
						||
				        	});
 | 
						||
				        }
 | 
						||
				    },
 | 
						||
				  
 | 
						||
				});
 | 
						||
				
 | 
						||
			}
 | 
						||
			return res;
 | 
						||
		}
 | 
						||
}
 | 
						||
 | 
						||
export default {
 | 
						||
	install
 | 
						||
} 
 |