来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
156 lines
5.3 KiB
TypeScript
156 lines
5.3 KiB
TypeScript
export const GENERAL_IMAGE_LIMITS = {
|
|
maxFileSizeBytes: 5 * 1024 * 1024,
|
|
acceptedTypes: ['image/jpeg', 'image/png', 'image/webp'],
|
|
acceptedExtensions: ['jpg', 'jpeg', 'png', 'webp'],
|
|
};
|
|
|
|
export function isGeneralImageFile(file: File): boolean {
|
|
if (file.type && GENERAL_IMAGE_LIMITS.acceptedTypes.includes(file.type)) return true;
|
|
const ext = file.name.split('.').pop()?.toLowerCase();
|
|
return Boolean(ext && GENERAL_IMAGE_LIMITS.acceptedExtensions.includes(ext));
|
|
}
|
|
|
|
import { pickFileFromDataTransfer } from './fileDrop';
|
|
|
|
export function getImageFileFromDataTransfer(dataTransfer: DataTransfer | null): File | null {
|
|
return pickFileFromDataTransfer(dataTransfer, isGeneralImageFile);
|
|
}
|
|
|
|
export function dataTransferHasImageFiles(dataTransfer: DataTransfer | null): boolean {
|
|
if (!dataTransfer) return false;
|
|
return Array.from(dataTransfer.types || []).some(
|
|
(type) =>
|
|
type === 'Files' ||
|
|
type === 'application/x-moz-file' ||
|
|
type === 'public.file-url' ||
|
|
type.startsWith('image/')
|
|
);
|
|
}
|
|
|
|
export const ORDER_RECOGNITION_IMAGE_LIMITS = {
|
|
maxFileSizeBytes: 2 * 1024 * 1024,
|
|
maxDimension: 1600,
|
|
quality: 0.82,
|
|
acceptedTypes: ['image/jpeg', 'image/png', 'image/webp'],
|
|
};
|
|
|
|
export function validateGeneralImageFile(file: File): string | null {
|
|
if (!isGeneralImageFile(file)) {
|
|
return '仅支持 JPG、PNG、WEBP 图片';
|
|
}
|
|
if (file.size > GENERAL_IMAGE_LIMITS.maxFileSizeBytes) {
|
|
return '图片不能超过 5MB';
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function loadImageFromDataUrl(dataUrl: string): Promise<HTMLImageElement> {
|
|
return new Promise((resolve, reject) => {
|
|
const image = new Image();
|
|
image.onload = () => resolve(image);
|
|
image.onerror = () => reject(new Error('图片读取失败,请重试'));
|
|
image.src = dataUrl;
|
|
});
|
|
}
|
|
|
|
function readFileAsDataUrl(file: File): Promise<string> {
|
|
return new Promise((resolve, reject) => {
|
|
const reader = new FileReader();
|
|
reader.onloadend = () => resolve(String(reader.result || ''));
|
|
reader.onerror = () => reject(new Error('图片读取失败,请重试'));
|
|
reader.readAsDataURL(file);
|
|
});
|
|
}
|
|
|
|
const INSPECTION_PHOTO_JPEG_QUALITY = 0.92;
|
|
|
|
export async function loadOrientedCanvasFromFile(file: File): Promise<HTMLCanvasElement> {
|
|
if (typeof createImageBitmap === 'function') {
|
|
try {
|
|
const bitmap = await createImageBitmap(file, { imageOrientation: 'from-image' });
|
|
try {
|
|
const canvas = document.createElement('canvas');
|
|
canvas.width = bitmap.width;
|
|
canvas.height = bitmap.height;
|
|
const context = canvas.getContext('2d');
|
|
if (!context) throw new Error('浏览器不支持图片处理');
|
|
context.drawImage(bitmap, 0, 0);
|
|
return canvas;
|
|
} finally {
|
|
bitmap.close();
|
|
}
|
|
} catch {
|
|
// fall back to Image() below
|
|
}
|
|
}
|
|
|
|
const originalDataUrl = await readFileAsDataUrl(file);
|
|
const image = await loadImageFromDataUrl(originalDataUrl);
|
|
const canvas = document.createElement('canvas');
|
|
canvas.width = image.width;
|
|
canvas.height = image.height;
|
|
const context = canvas.getContext('2d');
|
|
if (!context) {
|
|
throw new Error('浏览器不支持图片处理');
|
|
}
|
|
context.drawImage(image, 0, 0);
|
|
return canvas;
|
|
}
|
|
|
|
export async function renderInspectionPhotoWithWatermark(
|
|
file: File,
|
|
meta: { orderCode?: string; branchName?: string }
|
|
): Promise<string> {
|
|
const canvas = await loadOrientedCanvasFromFile(file);
|
|
const context = canvas.getContext('2d');
|
|
if (!context) {
|
|
throw new Error('浏览器不支持图片处理');
|
|
}
|
|
|
|
const timestamp = new Date().toLocaleString();
|
|
const orderCode = String(meta.orderCode || '').trim();
|
|
const branchName = String(meta.branchName || '分公司').trim() || '分公司';
|
|
context.font = 'bold 24px Arial';
|
|
context.fillStyle = 'rgba(255, 255, 255, 0.85)';
|
|
context.fillText(`验收拍照 ${timestamp}`, 20, canvas.height - 40);
|
|
if (orderCode) {
|
|
context.fillText(`订单: ${orderCode}`, 20, canvas.height - 15);
|
|
}
|
|
context.fillText(branchName, 20, canvas.height - 65);
|
|
|
|
return canvas.toDataURL('image/jpeg', INSPECTION_PHOTO_JPEG_QUALITY);
|
|
}
|
|
|
|
export async function compressOrderRecognitionImage(file: File): Promise<string> {
|
|
if (!isGeneralImageFile(file)) {
|
|
throw new Error('仅支持 JPG、PNG、WEBP 图片');
|
|
}
|
|
|
|
const originalDataUrl = await readFileAsDataUrl(file);
|
|
const image = await loadImageFromDataUrl(originalDataUrl);
|
|
const scale = Math.min(
|
|
1,
|
|
ORDER_RECOGNITION_IMAGE_LIMITS.maxDimension / Math.max(image.width, image.height)
|
|
);
|
|
|
|
const width = Math.max(1, Math.round(image.width * scale));
|
|
const height = Math.max(1, Math.round(image.height * scale));
|
|
const canvas = document.createElement('canvas');
|
|
canvas.width = width;
|
|
canvas.height = height;
|
|
const context = canvas.getContext('2d');
|
|
if (!context) {
|
|
throw new Error('浏览器不支持图片压缩,请更换浏览器或上传更小图片');
|
|
}
|
|
|
|
context.drawImage(image, 0, 0, width, height);
|
|
const compressed = canvas.toDataURL('image/jpeg', ORDER_RECOGNITION_IMAGE_LIMITS.quality);
|
|
const estimatedBytes = Math.ceil((compressed.split(',')[1]?.length || 0) * 0.75);
|
|
|
|
if (estimatedBytes > ORDER_RECOGNITION_IMAGE_LIMITS.maxFileSizeBytes) {
|
|
throw new Error('图片压缩后仍超过2MB,请重新拍照或裁剪后上传');
|
|
}
|
|
|
|
return compressed;
|
|
}
|