You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.5 KiB
112 lines
3.5 KiB
import QQMapWX from "@/uni_modules/m-view/lib/qqmap-wx-jssdk.min"; |
|
|
|
export default { |
|
/** |
|
* 添加单位 |
|
* @param num |
|
* @param unit |
|
* @returns {string} |
|
*/ |
|
addUnit(num, unit = 'px') { |
|
return (/px$/.test(num)) ? num : num + unit; |
|
}, |
|
/** |
|
* 转换成px |
|
* 和upx2px区别是可带单位 |
|
* @param num |
|
* @returns {string} |
|
*/ |
|
getPx(num, isUnit = true) { |
|
return this.addUnit(( |
|
/(rpx|upx)$/.test(num)) |
|
? uni.upx2px(parseFloat(num)) |
|
: parseFloat(num), isUnit ? 'px' : ''); |
|
}, |
|
|
|
/** |
|
* 样式转换格式 |
|
* @param customStyle |
|
* @param target |
|
* @returns {{}|*|string|string} |
|
*/ |
|
addStyle(customStyle, target = 'object') { |
|
// 字符串转字符串,对象转对象情形,直接返回 |
|
if (this.empty(customStyle) || typeof (customStyle) === 'object' && target === 'object' || target === 'string' && |
|
typeof (customStyle) === 'string') { |
|
return customStyle |
|
} |
|
// 字符串转对象 |
|
if (target === 'object') { |
|
// 去除字符串样式中的两端空格(中间的空格不能去掉,比如padding: 20px 0如果去掉了就错了),空格是无用的 |
|
customStyle = this.trim(customStyle) |
|
// 根据";"将字符串转为数组形式 |
|
const styleArray = customStyle.split(';') |
|
const style = {} |
|
// 历遍数组,拼接成对象 |
|
for (let i = 0; i < styleArray.length; i++) { |
|
// 'font-size:20px;color:red;',如此最后字符串有";"的话,会导致styleArray最后一个元素为空字符串,这里需要过滤 |
|
if (styleArray[i]) { |
|
const item = styleArray[i].split(':') |
|
style[this.trim(item[0])] = this.trim(item[1]) |
|
} |
|
} |
|
return style |
|
} |
|
// 这里为对象转字符串形式 |
|
let string = '' |
|
for (const i in customStyle) { |
|
// 驼峰转为中划线的形式,否则css内联样式,无法识别驼峰样式属性名 |
|
const key = i.replace(/([A-Z])/g, '-$1').toLowerCase() |
|
string += `${key}:${customStyle[i]};` |
|
} |
|
// 去除两端空格 |
|
return this.trim(string) |
|
}, |
|
/** |
|
* 判断是否为空 |
|
*/ |
|
empty(value) { |
|
switch (typeof value) { |
|
case 'undefined': |
|
return true |
|
case 'string': |
|
if (value.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length == 0) return true |
|
break |
|
case 'boolean': |
|
if (!value) return true |
|
break |
|
case 'number': |
|
if (value === 0 || isNaN(value)) return true |
|
break |
|
case 'object': |
|
if (value === null || value.length === 0) return true |
|
for (const i in value) { |
|
return false |
|
} |
|
return true |
|
} |
|
return false |
|
}, |
|
/** |
|
* @description 去除空格 |
|
* @param str |
|
* @param pos |
|
*/ |
|
trim(str, pos = 'both') { |
|
str = String(str) |
|
if (pos == 'both') { |
|
return str.replace(/^\s+|\s+$/g, '') |
|
} |
|
if (pos == 'left') { |
|
return str.replace(/^\s*/, '') |
|
} |
|
if (pos == 'right') { |
|
return str.replace(/(\s*$)/g, '') |
|
} |
|
if (pos == 'all') { |
|
return str.replace(/\s+/g, '') |
|
} |
|
return str |
|
}, |
|
}; |
|
|
|
|