const navTo = function(url, data) {
	url += (url.indexOf('?') < 0 ? '?' : '&') + param(data)
	uni.navigateTo({
		url,
		success: (res) => {
			// 通过eventChannel向被打开页面传送数据
			res.eventChannel.emit('acceptDataFromOpenerPage', {
				data: data
			})
		}
	})
}

const navBack = function() {
	uni.navigateBack({
		delta: 1
	});
}

const navTabbar = function(url) {
	uni.switchTab({
		url
	});
}

const redirectTo = function(url) {
	uni.redirectTo({
		url
	});
}

const reLaunch = function(url) {
	uni.reLaunch({
		url
	});
}

const switchTab = function(url) {
	uni.switchTab({
		url
	});
}

function param(data) {
	let url = ''
	for (let k in data) {
		let value = data[k] !== undefined ? data[k] : ''
		url += '&' + k + '=' + encodeURIComponent(value)
	}
	return url ? url.substring(1) : ''
}

export {
	navTo,
	navTabbar,
	navBack,
	redirectTo,
	reLaunch,
	switchTab
}