class HTTP {
	constructor() {
		let ENV = wx.getAccountInfoSync().miniProgram.envVersion;
		if (ENV === 'release') { // 正式版
			// this.baseUrl = "https://lsa.cnyunl.com";
			this.baseUrl = "https://www.cnyunl.com";
		} else {
			// this.baseUrl = "https://www.cnyunl.com";
			this.baseUrl = "http://61.164.207.46:8200";
			// this.baseUrl = "https://cucu.cnyunl.com:8079";
		}
		console.log("接口地址:", this.baseUrl)
	}
	request({
		url,
		data = {},
		method = "POST",
		header = {
			'content-type': 'application/json'
		},
		showLoading = '加载中...'
	}) {
		return new Promise((resolve, reject) => {
			this._request(url, resolve, reject, data, method, header, showLoading);
		})
	}
	_request(url, resolve, reject, data, method, header, showLoading) {
		/* if (showLoading) wx.showLoading({
		    title: showLoading,
		    mask: true
		}) */
		wx.request({
			url: this.baseUrl + '/yos/rest/index' + url,
			data,
			method,
			header,
			timeout: 60000,
			success: (res) => {
				resolve(res.data);
			},
			fial: (err) => {
				reject(err);
			},
			complete: (res) => {
				// if (showLoading) wx.hideLoading()
				if (res.errMsg != 'request:ok') {
					wx.showToast({
						title: '网络异常，请重新进入',
						icon: "none"
					})
				} else if (res.data.msg == '登陆状态已过期，请重新登陆！') {
					wx.redirectTo({
						url: '/pages/login/phone',
					});
					wx.showToast({
						title: res.msg,
						icon: "none"
					})
				}
			}
		})
	}
}
export {
	HTTP
}