uview-plus组件库全面升级更新,订单结算判断支付方式是否可用代码调整,公众号关注二维码修改
This commit is contained in:
@@ -32,6 +32,20 @@ export function getPx(value, unit = false) {
|
||||
return unit ? `${parseInt(value)}px` : parseInt(value)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 用于统一rpx2px方法,因uni-app现有API未统一。
|
||||
* @param {number} value 用户传递值的rpx值
|
||||
* @returns {number}
|
||||
*/
|
||||
export function rpx2px(value) {
|
||||
// #ifdef APP
|
||||
return uni.upx2px(value)
|
||||
// #endif
|
||||
// #ifndef APP
|
||||
return uni.rpx2px(value)
|
||||
// #endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 进行延时,以达到可以简写代码的目的 比如: await uni.$u.sleep(20)将会阻塞20ms
|
||||
* @param {number} value 堵塞时间 单位ms 毫秒
|
||||
@@ -361,6 +375,10 @@ export function timeFormat(dateTime = null, formatStr = 'yyyy-mm-dd') {
|
||||
else if (typeof dateTime === 'string' && /^\d+$/.test(dateTime.trim())) {
|
||||
date = new Date(Number(dateTime))
|
||||
}
|
||||
// 检查是否为UTC格式的时间字符串 (2024-12-18T02:25:31.432Z)
|
||||
else if (typeof dateTime === 'string' && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/.test(dateTime)) {
|
||||
date = new Date(dateTime)
|
||||
}
|
||||
// 其他都认为符合 RFC 2822 规范
|
||||
else {
|
||||
// 处理平台性差异,在Safari/Webkit中,new Date仅支持/作为分割符的字符串时间
|
||||
@@ -730,6 +748,93 @@ export function getValueByPath(obj, path) {
|
||||
}, obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成同色系浅色背景色
|
||||
* @param {string} textColor - 支持 #RGB、#RRGGBB、rgb()、rgba() 格式
|
||||
* @param {number} [lightness=85] - 目标亮度百分比(默认85%)
|
||||
* @returns {string} 十六进制颜色值
|
||||
*/
|
||||
export function genLightColor(textColor, lightness = 95) {
|
||||
// 手动解析颜色值(避免使用document)
|
||||
const rgb = parseColorWithoutDOM(textColor);
|
||||
|
||||
// RGB转HSL色域
|
||||
const hsl = rgbToHsl(rgb.r, rgb.g, rgb.b);
|
||||
|
||||
// 生成浅色背景
|
||||
const bgHsl = {
|
||||
h: hsl.h,
|
||||
s: hsl.s,
|
||||
l: Math.min(lightness, 95)
|
||||
};
|
||||
|
||||
return hslToHex(bgHsl.h, bgHsl.s, bgHsl.l);
|
||||
}
|
||||
|
||||
/* 手动解析颜色字符串(兼容uni-app环境) */
|
||||
function parseColorWithoutDOM(colorStr) {
|
||||
// 统一转小写处理
|
||||
const str = colorStr.toLowerCase().trim();
|
||||
|
||||
// 处理十六进制格式
|
||||
if (str.startsWith('#')) {
|
||||
const hex = str.replace('#', '');
|
||||
const fullHex = hex.length === 3 ?
|
||||
hex.split('').map(c => c + c).join('') : hex;
|
||||
|
||||
return {
|
||||
r: parseInt(fullHex.substring(0,2), 16),
|
||||
g: parseInt(fullHex.substring(2,4), 16),
|
||||
b: parseInt(fullHex.substring(4,6), 16)
|
||||
};
|
||||
}
|
||||
|
||||
// 处理rgb/rgba格式
|
||||
const rgbMatch = str.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/);
|
||||
if (rgbMatch) {
|
||||
return {
|
||||
r: +rgbMatch[1],
|
||||
g: +rgbMatch[2],
|
||||
b: +rgbMatch[3]
|
||||
};
|
||||
}
|
||||
|
||||
throw new Error('Invalid color format');
|
||||
}
|
||||
|
||||
// 辅助函数:RGB 转 HSL(色相、饱和度、亮度)
|
||||
function rgbToHsl(r, g, b) {
|
||||
r /= 255, g /= 255, b /= 255;
|
||||
const max = Math.max(r, g, b), min = Math.min(r, g, b);
|
||||
let h, s, l = (max + min) / 2;
|
||||
|
||||
if (max === min) {
|
||||
h = s = 0; // achromatic
|
||||
} else {
|
||||
const d = max - min;
|
||||
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
||||
switch (max) {
|
||||
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
|
||||
case g: h = (b - r) / d + 2; break;
|
||||
case b: h = (r - g) / d + 4; break;
|
||||
}
|
||||
h = (h * 60).toFixed(1);
|
||||
}
|
||||
return { h: +h, s: +(s * 100).toFixed(1), l: +(l * 100).toFixed(1) };
|
||||
}
|
||||
|
||||
// 辅助函数:HSL 转十六进制
|
||||
function hslToHex(h, s, l) {
|
||||
l /= 100;
|
||||
const a = s * Math.min(l, 1 - l) / 100;
|
||||
const f = n => {
|
||||
const k = (n + h / 30) % 12;
|
||||
const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
|
||||
return Math.round(255 * color).toString(16).padStart(2, '0');
|
||||
};
|
||||
return `#${f(0)}${f(8)}${f(4)}`;
|
||||
}
|
||||
|
||||
export default {
|
||||
range,
|
||||
getPx,
|
||||
@@ -762,5 +867,6 @@ export default {
|
||||
page,
|
||||
pages,
|
||||
getValueByPath,
|
||||
// setConfig
|
||||
genLightColor,
|
||||
rpx2px
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user