index.js 2.03 KB
const popUp = (url) => {
	let time = undefined
	// 加载弹窗
	const showLoading = ({title='加载中...'}={}) => {
		let iconUrl = url || ''
		//#ifdef APP-PLUS
		plus.nativeUI.showWaiting(title, {
			color : '#646464',
			background : '#f7f7f7',
			round : '10px',
			loading : {
				display : 'block',
				icon : '/static/demo/demo.png',
				height:'30px',
				interval : '50ms'
			}
		});
		//#endif
		//#ifdef H5
		let div = document.createElement('div')
		div.className = 'loadingDiv'
		let loadingHtml = `
			<div class="loadingDiv-box">
				<div class="loadingDiv-box-window">
					<img src="${iconUrl}" style="display : none; mode="widthFix" alt="加载中..." class="loadingDiv-box-img" />
					<div class="loadingDiv-box-icon" style="background-image: url(${iconUrl});"></div>
				</div>
				<div class="loadingDiv-box-title">${title}</div>
			</div>`
		;
		div.innerHTML = loadingHtml
		document.body.appendChild(div);
		let imgData = document.querySelector('.loadingDiv-box-img');
		let scale = imgData.naturalHeight ? imgData.naturalWidth/imgData.naturalHeight : 0
		let img = document.querySelector('.loadingDiv-box-icon');
		let n = 0
		time = setInterval(() => {
			img.style.backgroundPosition = `${-n*30}px 0px`;
			n++;
			if(n >= scale){
				n = 0
			}
		}, 100)
		//#endif
		//#ifndef APP-PLUS || H5
		uni.showLoading({
		   title: title
		});
		//#endif
	}
	// 取消加载弹窗
	const hideLoading = () => {
		//#ifdef APP-PLUS
		plus.nativeUI.closeWaiting()
		//#endif
		//#ifdef H5
		let parent = document.querySelector('.loadingDiv');
		document.body.removeChild(parent)
		clearInterval(time)
		time = undefined
		//#endif
		//#ifndef APP-PLUS || H5
		uni.hideLoading();
		//#endif
	}
	// 普通弹窗提示
	const showToast = ({title='',duration=1500,icon='none'}) => {
		//#ifdef APP-PLUS
		plus.nativeUI.toast(title,{
			duration : duration
		})
		//#endif
		//#ifndef APP-PLUS
		uni.showToast({
			title : title,
			duration : duration,
			icon : icon
		});
		//#endif
	}
	return {
		showLoading,
		hideLoading,
		showToast
	}
}

export {
	popUp
}