111 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			111 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
import config from '../config/common' // 配置文件
 | 
						||
import handle401 from './handle401'
 | 
						||
// import MD5 from 'md5'
 | 
						||
 | 
						||
 | 
						||
export default (params) => {
 | 
						||
	let pages = getCurrentPages()
 | 
						||
	let currentPage = pages[pages.length - 1]
 | 
						||
	const currentUrl = `/${currentPage.route}`
 | 
						||
	console.log(currentUrl, 'currentUrl')
 | 
						||
	let routes = currentPage.route
 | 
						||
	let options = currentPage.options
 | 
						||
	let opurl = routes + '?'
 | 
						||
	for (let key in options) {
 | 
						||
		opurl += key + '=' + options[key] + '&'
 | 
						||
	}
 | 
						||
	opurl = opurl.substr(0, opurl.length - 1)
 | 
						||
	console.log(opurl, '1231312321')
 | 
						||
 | 
						||
	let url = params.url;
 | 
						||
	let method = params.method || "get";
 | 
						||
	let data = params.data || {};
 | 
						||
	let header = {
 | 
						||
		// 'Blade-Auth': uni.getStorageSync('token') || '',
 | 
						||
		'Content-Type': 'application/json;charset=UTF-8',
 | 
						||
		// 'Authorization': 'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
 | 
						||
		'Tenant': uni.getStorageSync('tenantId') || '0001', // avue配置相关
 | 
						||
		...params.header
 | 
						||
	}
 | 
						||
	if (method == "post") {
 | 
						||
		header = {
 | 
						||
			'Content-Type': 'application/json',
 | 
						||
			'Tenant': uni.getStorageSync('tenantId') || '0001',
 | 
						||
			...params.header
 | 
						||
		};
 | 
						||
	}
 | 
						||
	return new Promise((resolve, reject) => {
 | 
						||
		uni.request({
 | 
						||
			url: config.serviceHost(url),
 | 
						||
			method: method,
 | 
						||
			header: header,
 | 
						||
			data: data,
 | 
						||
			// timeout,
 | 
						||
			success(response) {
 | 
						||
				const res = response
 | 
						||
				// 根据返回的状态码做出对应的操作
 | 
						||
				//获取成功
 | 
						||
				// console.log(res.statusCode);
 | 
						||
				if (res.statusCode == 200) {
 | 
						||
					resolve(res.data);
 | 
						||
				} else {
 | 
						||
					uni.clearStorageSync()
 | 
						||
					switch (res.statusCode) {
 | 
						||
						case 401:
 | 
						||
							// uni.showModal({
 | 
						||
							// 	title: "提示",
 | 
						||
							// 	content: "请登录",
 | 
						||
							// 	showCancel: false,
 | 
						||
							// 	success() {
 | 
						||
							// 		setTimeout(() => {
 | 
						||
							// 			uni.navigateTo({
 | 
						||
							// 				url: "/pages/login/index",
 | 
						||
							// 			})
 | 
						||
							// 		}, 1000);
 | 
						||
							// 	},
 | 
						||
							// });
 | 
						||
							uni.showToast({
 | 
						||
								title: '请登录...',
 | 
						||
								duration: 2000,
 | 
						||
							})
 | 
						||
							break;
 | 
						||
						case 404:
 | 
						||
							uni.showToast({
 | 
						||
								title: '请求地址不存在...',
 | 
						||
								duration: 2000,
 | 
						||
							})
 | 
						||
							break;
 | 
						||
						default:
 | 
						||
							uni.showToast({
 | 
						||
								title: '请重试...',
 | 
						||
								duration: 2000,
 | 
						||
							})
 | 
						||
							break;
 | 
						||
					}
 | 
						||
				}
 | 
						||
			},
 | 
						||
			fail(err) {
 | 
						||
				console.log(err)
 | 
						||
				if (err.errMsg.indexOf('request:fail') !== -1) {
 | 
						||
					uni.showToast({
 | 
						||
						title: '网络异常',
 | 
						||
						icon: "error",
 | 
						||
						duration: 2000
 | 
						||
					})
 | 
						||
				} else {
 | 
						||
					uni.showToast({
 | 
						||
						title: '未知异常',
 | 
						||
						duration: 2000
 | 
						||
					})
 | 
						||
				}
 | 
						||
				reject(err);
 | 
						||
 | 
						||
			},
 | 
						||
			complete() {
 | 
						||
				// 不管成功还是失败都会执行
 | 
						||
				uni.hideLoading();
 | 
						||
				uni.hideToast();
 | 
						||
			}
 | 
						||
		});
 | 
						||
	}).catch(() => {});
 | 
						||
} |