request.js 3.47 KB
import service from './service.js'
import BASE_URL from '../common/config.js'

const get = function(url, data) {
	return request(url, 'GET', data);
}
const getp = function(url, data) {
	
	return request(url, 'GET', data, {
		'Content-Type': 'application/json'
	});
}
const post = function(url, data) {
	return request(url, 'POST', data);
}
const put = function(url, data) {
	return request(url, 'put', data);
}

// const postAL = function(url, data) {
// 	return request(url, 'POST', data, {
// 		'accept: text/plain'
// 	});
// }

const postFormData = function(url, data) {
	return request(url, 'POST', data, {
		'Content-Type': 'application/x-www-form-urlencoded'
	});
}
const postPatchJson = function(url, data) {
	return request(url, 'POST', data, {
		'Content-Type': 'application/json-patch+json'
	});
}
const del = function(url, data) {
	return request(url, 'DELETE', data);
}

const post1 = function(url, data) {
	return requestGps(url, 'POST', data);
}
const get1 = function(url, data) {
	return requestGps(url, 'GET', data);
}

const request = function(url, method, data, headers) {
	let header = {};
	// let urlsNoAuth = ['/authentication/form'];
	// if (!urlsNoAuth.includes(url)) {
		// header['Authorization']=`${service.getToken()}`
		header['Authorization'] = service.getToken()
		// header['Authorization'] = "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjVkYTFkODY1LWE4ZmYtNGIxNC05YjdjLTNkNTJhOTkxNWQ1YiJ9.G1Y88EXFDPoU3bC6nbt1ADkdOPOAP1r5x4ElMzRaWUUz1fRFyM2kSGqpEl5MmhXcp5rj0kp-7BdxWFrX-w2nTQ"
	// }
	url = BASE_URL + url;
	if (headers) {
		header = Object.assign({}, header, headers)
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url,
			data,
			method,
			header,
			timeout: 50000,
			success: (res) => {
				console.log(res)
				if (res.statusCode === 200) {
					if (res.data.code == 600 || res.data.msg == '登录过期,请重新登录') {
						console.log('登录过期,跳转到登录界面');
						uni.reLaunch({
							url: '/pages/login/login'
						});
						uni.removeStorageSync('UserToken_KEY');
						return;
					}
					if (res.data.code < 0 && res.data.message) {
						uni.showToast({
							icon: 'none',
							title: res.data.data.message,
							duration: 2000
						});
						reject();
					} else if ( res.data.code === 500) {
						uni.showToast({
							icon: 'none',
							title: res.data.msg || '操作失败,请联系管理员',
							duration: 2000
						});
						// uni.removeStorageSync('UserToken_KEY');
						reject();
					} else if (res.data.code === 403 || res.data.code === 401) {
						uni.reLaunch({
							url: '/pages/login/login'
						});
						uni.removeStorageSync('UserToken_KEY');
						reject();
					} else {
						resolve(res.data);
					}
				} else if (res.statusCode === 403 || res.statusCode === 401) {
					console.log('7777');
					service.addToken('');
					uni.reLaunch({
						url: '/pages/login/login'
					});
					reject();
				} else {
					reject();
					// uni.showToast({
					// 	icon:'none',
					//     title: res.data.msg || '系统繁忙!',
					//     duration: 2000
					// });
				}

			},
			fail: (error) => {
				uni.hideLoading();
				// uni.showToast({
				// 	icon:'none',
				//     title: '网络错误!',
				//     duration: 2000
				// });
				console.log(error);
			}
		});
	});
}


export default {
	get,
	post,
	del,
	request,
	postFormData,
	put,
	postPatchJson,
	getp
}