feat: 请求增加requestid

This commit is contained in:
rd
2025-08-28 14:58:58 +08:00
parent cdda8ffba7
commit 0c14a0caec
4 changed files with 42 additions and 28 deletions

View File

@ -108,7 +108,7 @@ export function downloadByUrl(url: string, filename?: string) {
}
export function genRandomId() {
return `id_${Date.now()}_${Math.floor(Math.random() * 10000)}`;
return `id_${dayjs().unix()}_${Math.floor(Math.random() * 10000)}`;
}
export function formatFileSize(bytes: number): string {
@ -397,7 +397,7 @@ export function getImageMainColor(imageUrl: string): Promise<string> {
const avgColor = {
r: Math.round(maxGroup.sumR / maxGroup.count),
g: Math.round(maxGroup.sumG / maxGroup.count),
b: Math.round(maxGroup.sumB / maxGroup.count)
b: Math.round(maxGroup.sumB / maxGroup.count),
};
resolve(`rgb(${avgColor.r},${avgColor.g},${avgColor.b})`);
@ -427,28 +427,38 @@ function medianCut(data: Uint8ClampedArray, levels: number): any[] {
if (a < 128) continue;
colors.push({
r, g, b,
r,
g,
b,
count: 1,
sumR: r, sumG: g, sumB: b
sumR: r,
sumG: g,
sumB: b,
});
}
// 如果没有颜色数据,返回默认白色
if (colors.length === 0) {
return [{
count: 1,
sumR: 255, sumG: 255, sumB: 255
}];
return [
{
count: 1,
sumR: 255,
sumG: 255,
sumB: 255,
},
];
}
// 开始中位数切分
let colorGroups = [{
colors,
count: colors.length,
sumR: colors.reduce((sum, c) => sum + c.r, 0),
sumG: colors.reduce((sum, c) => sum + c.g, 0),
sumB: colors.reduce((sum, c) => sum + c.b, 0)
}];
let colorGroups = [
{
colors,
count: colors.length,
sumR: colors.reduce((sum, c) => sum + c.r, 0),
sumG: colors.reduce((sum, c) => sum + c.g, 0),
sumB: colors.reduce((sum, c) => sum + c.b, 0),
},
];
for (let i = 0; i < levels; i++) {
const newGroups = [];
@ -460,12 +470,12 @@ function medianCut(data: Uint8ClampedArray, levels: number): any[] {
}
// 找出颜色范围最大的通道
const rMin = Math.min(...group.colors.map(c => c.r));
const rMax = Math.max(...group.colors.map(c => c.r));
const gMin = Math.min(...group.colors.map(c => c.g));
const gMax = Math.max(...group.colors.map(c => c.g));
const bMin = Math.min(...group.colors.map(c => c.b));
const bMax = Math.max(...group.colors.map(c => c.b));
const rMin = Math.min(...group.colors.map((c) => c.r));
const rMax = Math.max(...group.colors.map((c) => c.r));
const gMin = Math.min(...group.colors.map((c) => c.g));
const gMax = Math.max(...group.colors.map((c) => c.g));
const bMin = Math.min(...group.colors.map((c) => c.b));
const bMax = Math.max(...group.colors.map((c) => c.b));
const rRange = rMax - rMin;
const gRange = gMax - gMin;
@ -500,7 +510,7 @@ function medianCut(data: Uint8ClampedArray, levels: number): any[] {
count: group1.length,
sumR: group1.reduce((sum, c) => sum + c.r, 0),
sumG: group1.reduce((sum, c) => sum + c.g, 0),
sumB: group1.reduce((sum, c) => sum + c.b, 0)
sumB: group1.reduce((sum, c) => sum + c.b, 0),
});
newGroups.push({
@ -508,7 +518,7 @@ function medianCut(data: Uint8ClampedArray, levels: number): any[] {
count: group2.length,
sumR: group2.reduce((sum, c) => sum + c.r, 0),
sumG: group2.reduce((sum, c) => sum + c.g, 0),
sumB: group2.reduce((sum, c) => sum + c.b, 0)
sumB: group2.reduce((sum, c) => sum + c.b, 0),
});
}
@ -516,4 +526,4 @@ function medianCut(data: Uint8ClampedArray, levels: number): any[] {
}
return colorGroups;
}
}