const MAX_DIMENSION = 1600; const COMPRESS_QUALITY = 80; function compressOrderImage(filePath) { return new Promise((resolve) => { wx.getImageInfo({ src: filePath, success: (info) => { const maxSide = Math.max(info.width, info.height); const scale = maxSide > MAX_DIMENSION ? MAX_DIMENSION / maxSide : 1; const compressedWidth = scale < 1 ? Math.max(1, Math.round(info.width * scale)) : undefined; const options = { src: filePath, quality: COMPRESS_QUALITY, success: (res) => resolve(res.tempFilePath || filePath), fail: () => resolve(filePath), }; if (compressedWidth) { options.compressedWidth = compressedWidth; } wx.compressImage(options); }, fail: () => resolve(filePath), }); }); } function readFileAsBase64(filePath) { return new Promise((resolve, reject) => { wx.getFileSystemManager().readFile({ filePath, encoding: 'base64', success: (res) => resolve(res.data), fail: reject, }); }); } async function prepareOrderImageBase64(filePath) { const compressedPath = await compressOrderImage(filePath); return readFileAsBase64(compressedPath); } module.exports = { compressOrderImage, readFileAsBase64, prepareOrderImageBase64, };