40 lines
1.4 KiB
JavaScript
40 lines
1.4 KiB
JavaScript
|
|
/**
|
|||
|
|
* @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 添加单位,如果有rpx,upx,%,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()
|
|||
|
|
}
|