购物车

This commit is contained in:
李志鹏
2026-04-23 14:28:26 +08:00
parent 7ca69021c4
commit 829f164833
5 changed files with 164 additions and 74 deletions

View File

@@ -53,9 +53,9 @@ export function generateUUID(): string {
if (typeof crypto !== 'undefined' && crypto.randomUUID) {
return crypto.randomUUID()
}
// 备用方案手动生成UUID v4
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = Math.random() * 16 | 0
const v = c === 'x' ? r : (r & 0x3 | 0x8)
return v.toString(16)
@@ -70,39 +70,37 @@ export {
/** 时间格式化-自定义格式
* @param value 时间对象|时间戳|时间字符串
* @param format 格式化字符串,默认值为 'yyyy-MM-dd HH:mm:ss'
* @param format 格式化字符串,默认值为 'YYYY-MM-DD HH:mm:ss'
* @returns 格式化后的时间字符串
*/
export function FormatDate(value: Date | number | string, format: string = 'yyyy-MM-dd HH:mm:ss') {
const date = new Date(value);
const yyyy = String(date.getFullYear());
const yy = String(date.getFullYear()).slice(-2);
const MM = String(date.getMonth() + 1).padStart(2, '0');
const M = String(date.getMonth() + 1);
const dd = String(date.getDate()).padStart(2, '0');
const d = String(date.getDate());
const HH = String(date.getHours()).padStart(2, '0');
const H = String(date.getHours());
const mm = String(date.getMinutes()).padStart(2, '0');
const m = String(date.getMinutes());
const ss = String(date.getSeconds()).padStart(2, '0');
const s = String(date.getSeconds());
const str = format.replaceAll('yyyy', yyyy)
.replaceAll('yy', yy)
.replaceAll('MM', MM)
.replaceAll('M', M)
.replaceAll('dd', dd)
.replaceAll('d', d)
.replaceAll('HH', HH)
.replaceAll('H', H)
.replaceAll('mm', mm)
.replaceAll('m', m)
.replaceAll('ss', ss)
.replaceAll('s', s);
return str;
export function FormatDate(value: Date | number | string, format: string = 'YYYY-MM-DD HH:mm:ss') {
const d = new Date(value);
if (!d || isNaN(d.getTime())) return 'Invalid Date';
const pad = (n) => String(n).padStart(2, '0');
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
const tokens = {
YYYY: d.getFullYear(),
YY: String(d.getFullYear()).slice(-2),
MM: pad(d.getMonth() + 1),
M: d.getMonth() + 1,
SM: months[d.getMonth()],
DD: pad(d.getDate()),
D: d.getDate(),
HH: pad(d.getHours()),
H: d.getHours(),
hh: pad(d.getHours() % 12 || 12),
h: d.getHours() % 12 || 12,
mm: pad(d.getMinutes()),
m: d.getMinutes(),
ss: pad(d.getSeconds()),
s: d.getSeconds(),
A: d.getHours() < 12 ? 'AM' : 'PM',
a: d.getHours() < 12 ? 'am' : 'pm'
}
const reg = new RegExp(Object.keys(tokens).join('|'), 'g')
return format.replace(reg, match => tokens[match]);
}
/**
* 下载图片
* @param list 图片列表
@@ -163,7 +161,7 @@ export function encryptPassword(password: string): string {
* @param url 图片URL
* @returns 无
*/
export async function shareImageToWhatsapp (url: string){
export async function shareImageToWhatsapp(url: string) {
// 把图片 URL 转为 Blob
const blob = await fetch(url).then((res) => res.blob())
@@ -195,3 +193,19 @@ export function CountDown(time: number) {
const ss = String(time % 60).padStart(2, '0');
return `${mm}:${ss}`;
}
/**
* 字节转换为可读格式
* @param {number} bytes - 字节数
* @param {number} decimals - 保留小数位数默认2位
* @returns {string} 格式化后的字符串
*/
export function FormatBytes(bytes, decimals = 2) {
if (bytes === 0) return '0 B';
if (!bytes || isNaN(bytes)) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
const value = bytes / Math.pow(k, i);
return `${Number(value.toFixed(decimals))} ${sizes[i]}`;
}