/** * 将小数点后面的字体变小 * */ export const splitPrice = (price = 0, px = 10) => { let arr = (price || 0).toString().split("."); return `${arr[0]}.${arr[1]||"00"}`; } // 判断市场常见的几种刘海屏机型 export const modelmes = () => { // 判断市场常见的几种刘海屏机型 const systemInfo = uni.getSystemInfoSync(); let modelmes = systemInfo.model; if (modelmes.indexOf('iPhone X') >= 0 || modelmes.indexOf('iPhone XR') >= 0 || modelmes.indexOf('iPhone XS') >= 0 || modelmes.indexOf('iPhone 12') >= 0 || modelmes.indexOf('iPhone 11') >= 0 || modelmes.indexOf('iPhone11') >= 0 || modelmes.indexOf('iPhone12') >= 0 || modelmes.indexOf('iPhoneXR') >= 0 || modelmes.indexOf('iPhoneX') >= 0 ) { return true } else { return false } } /** * 将rgba转为十六进制 * */ export const hexify = (color) => { if (!color) return '' var values = color .replace(/rgba?\(/, '') .replace(/\)/, '') .replace(/[\s+]/g, '') .split(','); var a = parseFloat(values[3] || 1), r = Math.floor(a * parseInt(values[0]) + (1 - a) * 255), g = Math.floor(a * parseInt(values[1]) + (1 - a) * 255), b = Math.floor(a * parseInt(values[2]) + (1 - a) * 255); return "#" + ("0" + r.toString(16)).slice(-2) + ("0" + g.toString(16)).slice(-2) + ("0" + b.toString(16)).slice(-2); } /** * 将十六进制颜色转为rgba *使用时只需传入十六进制字符串,“n”表示透明度 * */ export const colorRgba = (str, n) => { console.log(n) // 十六进制颜色值的正则表达式 let reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/ let sColor = str.toLowerCase() if (str === 'rgb(95,171,42)') { sColor = '#91E055'.toLowerCase() } if (str === 'rgb(145,224,85)') { sColor = '#5FAB2A'.toLowerCase() } // 十六进制颜色转换为RGB格式 if (sColor && reg.test(sColor)) { if (sColor.length === 4) { let sColorNew = '#' for (let i = 1; i < 4; i += 1) { // 例如:#eee,#fff等 sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1)) } sColor = sColorNew } // 处理六位颜色值 let sColorChange = [] for (let i = 1; i < 7; i += 2) { sColorChange.push(parseInt('0x' + sColor.slice(i, i + 2))) } return 'rgba(' + sColorChange.join(',') + ',' + n + ')' } else { return sColor } } // 获取【加载更多】组件状态 export function loadStatus(page, pageSize, total) { if (total / pageSize > page) { return 'loadmore'; } else { return 'nomore'; } } /*加法函数,用来得到精确的加法结果 */ export const accAdd = (num1, num2) => { var p1 = 0; var p2 = 0; if (num1.toString().split('.').length > 1) { p1 = num1.toString().split('.')[1].length; } if (num2.toString().split('.').length > 1) { p2 = num2.toString().split(".")[1].length; } var p = p1 > p2 ? p1 : p2; var n1 = num1 * Math.pow(10, p); var n2 = num2 * Math.pow(10, p); var result = (n1 + n2) / Math.pow(10, p); return parseFloat(result.toPrecision(12)); } /*减法函数,用来得到精确的减法结果 */ export const Subtr = (arg1, arg2) => { var r1, r2, m, n; try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 } try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 } m = Math.pow(10, Math.max(r1, r2)); //last modify by deeka //动态控制精度长度 n = (r1 >= r2) ? r1 : r2; var result = ((arg1 * m - arg2 * m) / m).toFixed(n); return result } // 本月第一天 export function showMonthFirstDay() { var Nowdate = new Date(); var MonthFirstDay = new Date(Nowdate.getFullYear(), Nowdate.getMonth(), 1); const M = Number(MonthFirstDay.getMonth()) + 1; return ( MonthFirstDay.getFullYear() + "-" + M + "-" + MonthFirstDay.getDate() + " 00:00:00" ); } /** * @description 时间格式化 时间戳转换为时间 * @param date * @param fmt * @return {*} */ export function formatDate(date, fmt) { if (!date) { return '' } // if (typeof date !== 'string') { // return '' // } date *= (date.toString().length === 10 ? 1000 : 1) let _date = new Date(date) let _fmt = fmt || 'yyyy-MM-dd hh:mm:ss' let o = { 'M+': _date.getMonth() + 1, 'd+': _date.getDate(), 'h+': _date.getHours(), 'm+': _date.getMinutes(), 's+': _date.getSeconds() } if (/(y+)/.test(_fmt)) { _fmt = _fmt.replace(RegExp.$1, (_date.getFullYear() + '').substr(4 - RegExp.$1.length)) } for (let k in o) { if (new RegExp('(' + k + ')').test(_fmt)) { _fmt = _fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]) .length))) } } return _fmt } /** * @description 时间格式化 时间字符串转换为时间戳 * @param date * @param len 默认时间戳长度为10位 * @return {*} */ export function dateToStamp(str, len = 10) { // new Date(yyyy-mm-dd hh:mm:ss) 这里ios会返回null,把‘-’划分替换为‘/’划分 let date = new Date(str.replace(/-/g, '/')) return parseInt(date.getTime() / (len === 13 ? 1 : 1000)) } /** * 格式化数字,数字每隔三位加个逗号 * params: num 需要处理的数字 * params: len 需要保留的小数点位数 */ export function formattedNumber(num, len = 2) { let result = "", counter = 0; let formatNum; if (num < 0) { formatNum = formatNub(Math.abs(num) || 0, len); } else { formatNum = formatNub(num || 0, len); } let stringNum = formatNum.toString().split("."); for (let i = stringNum[0].length - 1; i >= 0; i--) { counter++; result = stringNum[0].charAt(i) + result; if (!(counter % 3) && i != 0) { result = "," + result; } } result = num < 0 ? "-" + result : result; if (stringNum[1]) { return "¥" + result + "." + stringNum[1]; } else { return "¥" + result; } }; /** * 不四舍五入保留n位小数 * params: val 需要处理的数字 * params: len 需要保留的小数点位数 */ export function formatNub(num, n = 2) { if (typeof num != "number" && !Number(num)) { return "0.00"; } num = Number(num).toString(); let result = ""; let zeroResult = function(n) { let zero = ""; for (let i = 0; i < n; i++) { zero += "0"; } return zero; }; if (num % 1 == 0) { //整数 result = num + "." + zeroResult(n); } else { //小数 let num1 = num.split("."); if (num1[1].length < n) { result = num1[0] + "." + num1[1] + zeroResult(n - num1[1].length); } else { result = num1[0] + "." + num1[1].substring(0, n); } } return result; }; //省市区截取 export function getArea(str) { let area = {} let index11 = 0 let index1 = str.indexOf("省") if (index1 == -1) { index11 = str.indexOf("自治区") if (index11 != -1) { area.provinceName = str.substring(0, index11 + 3) } else { area.provinceName = str.substring(0, 0) } } else { area.provinceName = str.substring(0, index1 + 1) } let index2 = str.indexOf("市") if (index11 == -1) { area.cityName = str.substring(index11 + 1, index2 + 1) } else { if (index11 == 0) { area.cityName = str.substring(index1 + 1, index2 + 1) } else { area.cityName = str.substring(index11 + 3, index2 + 1) } } let index3 = str.lastIndexOf("区") if (index3 == -1) { index3 = str.indexOf("县") if (index3 == -1) { index3 = str.lastIndexOf("市") area.districtName = str.substring(index2 + 1, index3 + 1) } else { area.districtName = str.substring(index2 + 1, index3 + 1) } } else { area.districtName = str.substring(index2 + 1, index3 + 1) } area.address = str.substring(index3 + 1) return area; } /** * 将手机号以3-4-4格式展示 * */ export const mobileFormat = (mobile) => { return mobile.replace(/(\d{3})(\d{4})(\d{4})/, '$1 $2 $3'); } /** * 将手机号空格还原 * */ export const mobileRe = (mobile) => { return mobile.replace(/\s/g, '') }