@itriton/uniapp
Version:
基于uni-app的UI组件库
108 lines (100 loc) • 2.41 kB
JavaScript
/**
* rpx转px
* @param {number} upx
*/
export const rpx2px = (upx) => {
return upx / 750 * uni.getSystemInfoSync().windowWidth
}
/**
* px转rpx
* @param {number} px
*/
export const px2rpx = (px) => {
return (px * 750) / uni.getSystemInfoSync().windowWidth
}
/**
* 判断是否微信环境
* @returns {boolean} 返回Boolean
*/
export const isWechat = () => {
const ua = window.navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == 'micromessenger') return true;
else return false;
}
/**
* @description 进行延时,以达到可以简写代码的目的 比如: await uni.$u.sleep(20)将会阻塞20ms
* @param {number} value 堵塞时间 单位ms 毫秒
* @returns {Promise} 返回promise
*/
export const sleep = (value = 30) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve()
}, value)
})
}
/**
* 判断是否为空
* @param {any} val 参数
*/
export const isEmpty = (val) => {
return !(!!val ? typeof val === 'object' ? Array.isArray(val) ? !!val.length : !!Object.keys(val).length : true : false);
}
/**
* 替换符号
* @param {string} value 要替换的参数
* @param {Array} symbolList 要替换的符号列表
*/
export const replaceSymbol = (value, symbolList) => {
let result = value
symbolList.forEach(item => {
result = result.replace(item, '')
})
return result
}
/**
* 显示toast消息
* @param {Object} options 选项对象
* @param {string} options.title 消息标题
* @param {string} [options.icon='none'] 图标类型,默认为'none'
* @param {boolean} [options.mask=true] 是否显示透明蒙层,默认为true
* @param {number} [options.duration=2000] 显示时长(毫秒),默认为2000
* @returns {Promise<any>} 返回一个Promise,当toast显示成功时解析,显示失败时拒绝
*/
export const showToast = ({
title,
icon = 'none',
mask = true,
duration = 2000,
}) => {
return new Promise((resolve, reject) => {
uni.showToast({
title,
icon,
mask,
duration,
success: (res) => {
resolve(res)
},
fail: (error) => {
reject(error)
}
})
})
}
/**
* 隐藏消息框
*/
export const hideToast = () => {
uni.hideToast()
}
export default {
rpx2px,
px2rpx,
sleep,
isEmpty,
isWechat,
replaceSymbol,
showToast,
hideToast,
}