let app = getApp();

function request(method, url, data) {
	return new Promise((resolve) => {
		uni.request({
			method: method,
			url: url,
			data: data,
			header: {
				'Accept': "*/*",
				'content-type': 'application/json;charset=utf-8',
				'Authorization': app.globalData.token,
				'mchId': app.globalData.mchId,
				'regionId': app.globalData.cityId,
			}, //有的时候这里不一定是 token 还可能是 Authorization
			success(res) {
				resolve(res)
				if (res.statusCode === 401 || res.data.return_code == 102014 || res.data.return_code ==
					104001) {
					app.globalData.userInfo = '';
					app.globalData.token = '';
					uni.setStorage({
						key: "user",
						data: ''
					})
					uni.setStorage({
						key: "token",
						data: ''
					})
					// #ifdef H5
					uni.showModal({
						title: '前往登录',
						content: '当前登录信息失效,请前往登录',
						success: function(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/login/register'
								})
							}
						}
					})

					// uni.showToast({
					// 	title: '当前登录信息失效,请前往登录',
					// 	icon: 'none',
					// 	duration: 2000,
					// 	success() {
					// 		setTimeout(() => {
					// 			console.log('======'+window.location.href);
					// 			uni.navigateTo({
					// 				url: '/pages/login/register'
					// 			})

					// 		}, 1500);
					// 	}
					// })
					// #endif
					// #ifdef MP
					uni.showToast({
						title: '登录信息过期,请重新授权',
						icon: 'none',
						duration: 2000,
						success() {
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/userLogin/userLogin'
								})
							}, 1500);
						}
					})
					// #endif
				}
			},
			fail(err) {
				console.log('======='+JSON.stringify(err))
				uni.showToast({
					title: '网络异常',
					icon: 'none',
					duration: 1500,
				})
			}
		})
	})
}


function POST(method, url, data) {
	return request(method, url, data)
}


module.exports = {
	POST
}