//设置域名
const dev_baseUrl = 'https://test.hz-hanghui.com:18890/patrol-management'; //测试
const pro_baseUrl = 'https://hhsz.hz-hanghui.com:8088/patrol-management'; //正式

//是否是正式环境
const env = true
// const baseUrl = env ? pro_baseUrl : dev_baseUrl;
let app = getApp();

// https请求
const request = e => {
	let token = uni.getStorageSync('token');
	return new Promise((resolve, reject) => {
		uni.request({
			url: env ? (pro_baseUrl + e.url) : (dev_baseUrl + e.url),
			method: e.method,
			data: e.data,
			header: {
				'Content-Type': e.type,
				'patrol-token': token ? token : null,
			},
			success: res => {
				if (res.data.code == 200) {
					resolve(res.data)
					uni.hideLoading(); //关闭loading
				} else if (res.data.code == 401) {
					uni.showToast({
						icon: 'none',
						title: '登录过期,请重新登录',
						duration: 2000,
						mask: true,
						success: () => {
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/login/index',
								})
							}, 800)
						}
					})
					uni.hideLoading(); //关闭loading
				} else {
					setTimeout(function() {
						uni.showToast({
							icon: 'none',
							title: res.data.msg,
							duration: 3000
						})
					}, 500);

					reject(res.data)
					uni.hideLoading(); //关闭loading
				};
			},
			fail: error => {
				wx.showToast({
					icon: 'none',
					title: error.errMsg,
					duration: 3000,
				});
				uni.hideLoading(); //关闭loading
				// reject(error);
			},
			complete: () => {
				uni.hideLoading(); //关闭loading
			}
		})
	})
}

// 链接另一个小程序
// 是否是正式环境
const wx_rlxf = true

//设置域名 
const dev_baseUrl_wxrlxf = 'http://192.168.11.172:9111/yx-fyzd'; //测试
const pro_baseUrl_wxrlxf = 'https://tx.hz-hanghui.com:8088/yx-fyzd'; //正式
const baseUrl = wx_rlxf ? pro_baseUrl_wxrlxf : dev_baseUrl_wxrlxf;

//https请求
const request_wxrlxf = e => {
	return new Promise(function(resolve, reject) {
		wx.request({
			url: wx_rlxf ? (pro_baseUrl_wxrlxf + e.url) : (dev_baseUrl_wxrlxf + e.url),
			data: e.data,
			method: e.method,
			header: {
				'content-type': e.type
			},
			success: res => {
				// app.globalData.requestTit = 'res：' + res
				if (res.statusCode == 200) {
					resolve(res.data)
				} else {
					reject(res.data)
				};
			},
			fail: error => {
				// app.globalData.requestTit = 'error：' + JSON.stringify(error) + 'data：' + JSON
				// 	.stringify(e)
				reject(error.errMsg)
			}
		})
	})
}
export {
	request,
	wx_rlxf,
	dev_baseUrl_wxrlxf,
	pro_baseUrl_wxrlxf,
	request_wxrlxf,
}