431 lines
15 KiB
JavaScript
431 lines
15 KiB
JavaScript
"use strict";
|
||
const common_vendor = require("./vendor.js");
|
||
const getLocation = async () => {
|
||
return new Promise((resolve, reject) => {
|
||
common_vendor.index.getLocation({
|
||
isHighAccuracy: true,
|
||
altitude: true,
|
||
accuracy: "best",
|
||
success: (res) => {
|
||
resolve(res);
|
||
},
|
||
fail: (err) => {
|
||
reject(err);
|
||
}
|
||
});
|
||
});
|
||
};
|
||
const chooseImage = async () => {
|
||
return new Promise((resolve, reject) => {
|
||
common_vendor.index.chooseImage({
|
||
count: 1,
|
||
success: (res) => {
|
||
resolve(res);
|
||
},
|
||
fail: (err) => {
|
||
reject(err);
|
||
}
|
||
});
|
||
});
|
||
};
|
||
const formatDate = (date, format = "YYYY-MM-DD HH:mm:ss") => {
|
||
const year = date.getFullYear();
|
||
const month = String(date.getMonth() + 1).padStart(2, "0");
|
||
const day = String(date.getDate()).padStart(2, "0");
|
||
const hours = String(date.getHours()).padStart(2, "0");
|
||
const minutes = String(date.getMinutes()).padStart(2, "0");
|
||
const seconds = String(date.getSeconds()).padStart(2, "0");
|
||
return format.replace("YYYY", year).replace("MM", month).replace("DD", day).replace("HH", hours).replace("mm", minutes).replace("ss", seconds);
|
||
};
|
||
const logoCache = /* @__PURE__ */ new Map();
|
||
const getCachedLogo = async (logoUrl) => {
|
||
if (logoCache.has(logoUrl)) {
|
||
common_vendor.index.__f__("log", "at common/utils.js:70", "使用缓存的logo:", logoUrl);
|
||
return logoCache.get(logoUrl);
|
||
}
|
||
return new Promise((resolve, reject) => {
|
||
common_vendor.index.downloadFile({
|
||
url: logoUrl,
|
||
success: (downloadRes) => {
|
||
common_vendor.index.__f__("log", "at common/utils.js:79", "下载并缓存logo:", logoUrl);
|
||
logoCache.set(logoUrl, downloadRes.tempFilePath);
|
||
resolve(downloadRes.tempFilePath);
|
||
},
|
||
fail: (err) => {
|
||
common_vendor.index.__f__("error", "at common/utils.js:85", "下载logo失败:", err);
|
||
reject(err);
|
||
}
|
||
});
|
||
});
|
||
};
|
||
const getFileSize = async (filePath) => {
|
||
return new Promise((resolve, reject) => {
|
||
common_vendor.index.getFileInfo({
|
||
filePath,
|
||
success: (res) => {
|
||
resolve({
|
||
size: res.size,
|
||
// 文件大小(字节)
|
||
sizeKB: (res.size / 1024).toFixed(2),
|
||
// KB
|
||
sizeMB: (res.size / (1024 * 1024)).toFixed(2)
|
||
// MB
|
||
});
|
||
},
|
||
fail: (err) => {
|
||
common_vendor.index.__f__("error", "at common/utils.js:124", "获取文件大小失败:", err);
|
||
reject(err);
|
||
}
|
||
});
|
||
});
|
||
};
|
||
const handleWatermarkExport = async (tempFilePath, originalFileSize, width, height, resolve) => {
|
||
try {
|
||
const watermarkFileSize = await getFileSize(tempFilePath);
|
||
common_vendor.index.__f__("log", "at common/utils.js:142", "水印图片文件大小:", watermarkFileSize.sizeKB, "KB");
|
||
resolve({
|
||
filePath: tempFilePath,
|
||
originalSize: {
|
||
width,
|
||
height,
|
||
fileSize: originalFileSize
|
||
},
|
||
watermarkSize: {
|
||
width,
|
||
height,
|
||
fileSize: watermarkFileSize
|
||
}
|
||
});
|
||
} catch (err) {
|
||
common_vendor.index.__f__("error", "at common/utils.js:157", "获取水印图片文件大小失败:", err);
|
||
resolve({
|
||
filePath: tempFilePath,
|
||
originalSize: {
|
||
width,
|
||
height,
|
||
fileSize: originalFileSize
|
||
},
|
||
watermarkSize: {
|
||
width,
|
||
height,
|
||
fileSize: null
|
||
}
|
||
});
|
||
}
|
||
};
|
||
const calculateQuality = (originalSizeKB) => {
|
||
if (originalSizeKB < 100) {
|
||
return 0.9;
|
||
} else if (originalSizeKB < 500) {
|
||
return 0.8;
|
||
} else {
|
||
return 0.7;
|
||
}
|
||
};
|
||
const calculateOutputSize = (width, height, maxWidth = 1920) => {
|
||
if (width <= maxWidth) {
|
||
return {
|
||
width,
|
||
height
|
||
};
|
||
}
|
||
const ratio = maxWidth / width;
|
||
return {
|
||
width: Math.round(width * ratio),
|
||
height: Math.round(height * ratio)
|
||
};
|
||
};
|
||
const addWatermark = async (imagePath, watermarkInfo, logoUrl = null, logoOpacity = 1, quality = null, maxWidth = 1920) => {
|
||
return new Promise(async (resolve, reject) => {
|
||
try {
|
||
const originalFileSize = await getFileSize(imagePath);
|
||
common_vendor.index.__f__("log", "at common/utils.js:230", "原图文件大小:", originalFileSize.sizeKB, "KB");
|
||
const compressionQuality = quality !== null ? quality : calculateQuality(
|
||
originalFileSize.sizeKB
|
||
);
|
||
common_vendor.index.__f__("log", "at common/utils.js:235", "使用压缩质量:", compressionQuality);
|
||
common_vendor.index.getImageInfo({
|
||
src: imagePath,
|
||
success: (imageInfo) => {
|
||
const {
|
||
width,
|
||
height
|
||
} = imageInfo;
|
||
common_vendor.index.__f__("log", "at common/utils.js:245", "原图尺寸:", width, "x", height);
|
||
const outputSize = calculateOutputSize(width, height, maxWidth);
|
||
common_vendor.index.__f__("log", "at common/utils.js:249", "输出尺寸:", outputSize.width, "x", outputSize.height);
|
||
const canvas = common_vendor.index.createCanvasContext("watermarkCanvas");
|
||
canvas.clearRect(0, 0, outputSize.width, outputSize.height);
|
||
canvas.drawImage(imagePath, 0, 0, outputSize.width, outputSize.height);
|
||
const processText = (text, maxChars = 25, maxLines = 3) => {
|
||
if (text.length <= maxChars) {
|
||
return [text];
|
||
}
|
||
const lines = [];
|
||
let currentLine = "";
|
||
let lineCount = 0;
|
||
for (let i = 0; i < text.length && lineCount < maxLines; i++) {
|
||
currentLine += text[i];
|
||
if (currentLine.length >= maxChars || i === text.length - 1) {
|
||
lines.push(currentLine);
|
||
lineCount++;
|
||
currentLine = "";
|
||
if (lineCount === maxLines && i < text.length - 1) {
|
||
lines[lines.length - 1] = lines[lines.length - 1].slice(
|
||
0,
|
||
-3
|
||
) + "...";
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
return lines;
|
||
};
|
||
const watermarkText = [
|
||
`时间: ${watermarkInfo.time}`,
|
||
`经度: ${watermarkInfo.longitude}`,
|
||
`维度: ${watermarkInfo.latitude}`
|
||
];
|
||
var location = processText("位置: " + watermarkInfo.location);
|
||
watermarkText.push(...location);
|
||
var department = processText("部门: " + watermarkInfo.department);
|
||
watermarkText.push(...department);
|
||
var status = processText("状态: " + watermarkInfo.status);
|
||
watermarkText.push(...status);
|
||
var workers = processText("人员: " + watermarkInfo.workers.join(
|
||
", "
|
||
));
|
||
watermarkText.push(...workers);
|
||
var remarks = processText("内容: " + watermarkInfo.remarks);
|
||
watermarkText.push(...remarks);
|
||
const drawTextWatermark = () => {
|
||
const padding = 30;
|
||
const fontSize = 40;
|
||
const lineHeight = 50;
|
||
const startX = padding;
|
||
const startY = outputSize.height - padding - watermarkText.length * lineHeight;
|
||
canvas.setFillStyle("rgba(255, 255, 255, 1)");
|
||
canvas.setFontSize(fontSize);
|
||
canvas.setTextBaseline("top");
|
||
watermarkText.forEach((text, index) => {
|
||
canvas.fillText(text, startX, startY + index * lineHeight);
|
||
});
|
||
if (logoUrl) {
|
||
getCachedLogo(logoUrl).then((cachedLogoPath) => {
|
||
const logoSize = 200;
|
||
const logoPadding = 30;
|
||
const logoX = logoPadding;
|
||
const logoY = startY - logoSize - 10;
|
||
canvas.setGlobalAlpha(logoOpacity);
|
||
canvas.drawImage(
|
||
cachedLogoPath,
|
||
logoX,
|
||
logoY,
|
||
logoSize,
|
||
logoSize
|
||
);
|
||
canvas.setGlobalAlpha(1);
|
||
canvas.draw(true, () => {
|
||
common_vendor.index.__f__(
|
||
"log",
|
||
"at common/utils.js:357",
|
||
"Canvas绘制完成(包含logo)"
|
||
);
|
||
setTimeout(() => {
|
||
common_vendor.index.canvasToTempFilePath({
|
||
canvasId: "watermarkCanvas",
|
||
width: outputSize.width,
|
||
height: outputSize.height,
|
||
fileType: "jpg",
|
||
// 使用jpg格式,文件更小
|
||
quality: compressionQuality,
|
||
// 使用动态计算的压缩质量
|
||
success: (res) => {
|
||
console.log(
|
||
"导出成功:",
|
||
res.tempFilePath
|
||
);
|
||
console.log(
|
||
"水印图片尺寸:",
|
||
outputSize.width,
|
||
"x",
|
||
outputSize.height
|
||
);
|
||
handleWatermarkExport(
|
||
res.tempFilePath,
|
||
originalFileSize,
|
||
outputSize.width,
|
||
outputSize.height,
|
||
resolve
|
||
);
|
||
},
|
||
fail: (err) => {
|
||
console.error(
|
||
"导出图片失败:",
|
||
err
|
||
);
|
||
reject(
|
||
err
|
||
);
|
||
}
|
||
});
|
||
}, 100);
|
||
});
|
||
}).catch((err) => {
|
||
common_vendor.index.__f__("error", "at common/utils.js:415", "获取logo失败:", err);
|
||
canvas.draw(true, () => {
|
||
common_vendor.index.__f__(
|
||
"log",
|
||
"at common/utils.js:418",
|
||
"Canvas绘制完成(logo获取失败)"
|
||
);
|
||
setTimeout(() => {
|
||
common_vendor.index.canvasToTempFilePath({
|
||
canvasId: "watermarkCanvas",
|
||
width: outputSize.width,
|
||
height: outputSize.height,
|
||
fileType: "jpg",
|
||
// 使用jpg格式,文件更小
|
||
quality: compressionQuality,
|
||
// 使用动态计算的压缩质量
|
||
success: (res) => {
|
||
console.log(
|
||
"导出成功:",
|
||
res.tempFilePath
|
||
);
|
||
console.log(
|
||
"水印图片尺寸:",
|
||
outputSize.width,
|
||
"x",
|
||
outputSize.height
|
||
);
|
||
handleWatermarkExport(
|
||
res.tempFilePath,
|
||
originalFileSize,
|
||
outputSize.width,
|
||
outputSize.height,
|
||
resolve
|
||
);
|
||
},
|
||
fail: (err2) => {
|
||
console.error(
|
||
"导出图片失败:",
|
||
err2
|
||
);
|
||
reject(
|
||
err2
|
||
);
|
||
}
|
||
});
|
||
}, 100);
|
||
});
|
||
});
|
||
} else {
|
||
canvas.draw(true, () => {
|
||
common_vendor.index.__f__("log", "at common/utils.js:479", "Canvas绘制完成(无logo)");
|
||
setTimeout(() => {
|
||
common_vendor.index.canvasToTempFilePath({
|
||
canvasId: "watermarkCanvas",
|
||
width: outputSize.width,
|
||
height: outputSize.height,
|
||
fileType: "jpg",
|
||
// 使用jpg格式,文件更小
|
||
quality: compressionQuality,
|
||
// 使用动态计算的压缩质量
|
||
success: (res) => {
|
||
common_vendor.index.__f__(
|
||
"log",
|
||
"at common/utils.js:492",
|
||
"导出成功:",
|
||
res.tempFilePath
|
||
);
|
||
common_vendor.index.__f__(
|
||
"log",
|
||
"at common/utils.js:497",
|
||
"水印图片尺寸:",
|
||
outputSize.width,
|
||
"x",
|
||
outputSize.height
|
||
);
|
||
handleWatermarkExport(
|
||
res.tempFilePath,
|
||
originalFileSize,
|
||
outputSize.width,
|
||
outputSize.height,
|
||
resolve
|
||
);
|
||
},
|
||
fail: (err) => {
|
||
console.error(
|
||
"导出图片失败:",
|
||
err
|
||
);
|
||
reject(err);
|
||
}
|
||
});
|
||
}, 100);
|
||
});
|
||
}
|
||
};
|
||
drawTextWatermark();
|
||
},
|
||
fail: (err) => {
|
||
common_vendor.index.__f__("error", "at common/utils.js:533", "获取图片信息失败:", err);
|
||
reject(err);
|
||
}
|
||
});
|
||
} catch (error) {
|
||
common_vendor.index.__f__("error", "at common/utils.js:538", "处理图片失败:", error);
|
||
reject(error);
|
||
}
|
||
});
|
||
};
|
||
const saveImageToPhotosAlbum = (image) => {
|
||
return new Promise((resolve, reject) => {
|
||
common_vendor.index.saveImageToPhotosAlbum({
|
||
filePath: image,
|
||
success: () => {
|
||
common_vendor.index.__f__("log", "at common/utils.js:549", "图片保存到相册成功");
|
||
resolve(true);
|
||
},
|
||
fail: (err) => {
|
||
common_vendor.index.__f__("error", "at common/utils.js:553", "保存到相册失败:", err);
|
||
if (err.errMsg && err.errMsg.includes("auth deny")) {
|
||
common_vendor.index.showModal({
|
||
title: "提示",
|
||
content: "需要您授权保存图片到相册,请在设置中开启相册权限",
|
||
showCancel: false,
|
||
confirmText: "知道了"
|
||
});
|
||
}
|
||
resolve(false);
|
||
}
|
||
});
|
||
});
|
||
};
|
||
const imageToBase64 = (filePath) => {
|
||
return new Promise((resolve, reject) => {
|
||
common_vendor.index.getImageInfo({
|
||
src: filePath,
|
||
success: (info) => {
|
||
const type = info.type || "jpeg";
|
||
common_vendor.index.getFileSystemManager().readFile({
|
||
filePath,
|
||
encoding: "base64",
|
||
success: (res) => resolve(`data:image/${type};base64,${res.data}`),
|
||
fail: reject
|
||
});
|
||
},
|
||
fail: reject
|
||
});
|
||
});
|
||
};
|
||
exports.addWatermark = addWatermark;
|
||
exports.chooseImage = chooseImage;
|
||
exports.formatDate = formatDate;
|
||
exports.getCachedLogo = getCachedLogo;
|
||
exports.getLocation = getLocation;
|
||
exports.imageToBase64 = imageToBase64;
|
||
exports.saveImageToPhotosAlbum = saveImageToPhotosAlbum;
|
||
//# sourceMappingURL=../../.sourcemap/mp-weixin/common/utils.js.map
|