// import store from '../store';

function request(url, data = {}, method = 'POST') {
	const token = this.$store.state.token
	return new Promise((resolve, reject) => {
		uni.request({
			url:  url,
			data,
			method,
			header: {
				Authorization: token ? ('Bearer ' + token) : ''
			},
			success: (res) => {
				if (res.data.code === 0) {
					resolve(res.data);
				} else if (res.data.code === 2) {
					uni.hideLoading();
					this.$logout(true)
				} else if (res.data.code === 4) {
					uni.hideLoading();
					this.$logout(true, data.msg)
				} else {
					uni.showToast({
						title: res.data.msg || '服务器错误！',
						icon: 'none',
						mask: true,
						duration: 3000
					});
					reject(res.data);
				}
			},
			fail: (error) => {
				reject(error);
			},
			// complete: () => {
			// 	// 关闭加载
			// 	uni.hideLoading();
			// }
		});
	});
}

export default request;
