This commit is contained in:
2023-08-18 01:41:17 +08:00
31 changed files with 2540 additions and 118 deletions

View File

@@ -0,0 +1,39 @@
/**
* @description 用于获取用户传递值的px值 如果用户传递了"xxpx"或者"xxrpx",取出其数值部分,如果是"xxxrpx"还需要用过uni.upx2px进行转换
* @param {number|string} value 用户传递值的px值
* @param {boolean} unit
* @returns {number|string}
*/
export const getPx = (value, unit = false)=> {
if (testNumber(value)) {
return unit ? `${value}px` : Number(value)
}
// 如果带有rpx先取出其数值部分再转为px值
if (/(rpx|upx)$/.test(value)) {
return unit ? `${uni.upx2px(parseInt(value))}px` : Number(uni.upx2px(parseInt(value)))
}
return unit ? `${parseInt(value)}px` : parseInt(value)
}
/**
* @description 添加单位如果有rpxupx%px等单位结尾或者值为auto直接返回否则加上px单位结尾
* @param {string|number} value 需要添加单位的值
* @param {string} unit 添加的单位名 比如px
*/
export const addUnit =(value = 'auto', unit = 'px')=> {
value = String(value)
// 用uView内置验证规则中的number判断是否为数值
return testNumber(value) ? `${value}${unit}` : value
}
/**
* 验证十进制数字
*/
function testNumber(value) {
return /^[\+-]?(\d+\.?\d*|\.\d+|\d\.\d+e\+\d+)$/.test(value)
}
/**
* @description 获取系统信息同步接口
* @link 获取系统信息同步接口 https://uniapp.dcloud.io/api/system/info?id=getsysteminfosync
*/
export const sys=()=> {
return uni.getSystemInfoSync()
}