处理分享后的兼容问题
This commit is contained in:
221
src/util.js
221
src/util.js
@@ -454,3 +454,224 @@ export const calcRing = (bowtargetId, x, y, diameter, arrowRadius = 5) => {
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
|
||||
export const generateShareCardImage = (canvasId, date, actual, total) => {
|
||||
try {
|
||||
const ctx = uni.createCanvasContext(canvasId);
|
||||
const imgUrl =
|
||||
"https://static.shelingxingqiu.com/attachment/2025-11-04/ddzpm4tyh5vunyacsr.png";
|
||||
const drawWidth = 375;
|
||||
const drawHeight = 300;
|
||||
// 兼容第三个参数传入 "actual/total" 的情况
|
||||
let a = actual;
|
||||
let t = total;
|
||||
if (
|
||||
(t === undefined || t === null) &&
|
||||
typeof a === "string" &&
|
||||
a.includes("/")
|
||||
) {
|
||||
const parts = a.split("/");
|
||||
a = parts[0];
|
||||
t = parts[1] || "";
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.getImageInfo({
|
||||
src: imgUrl,
|
||||
success: (res) => {
|
||||
const path = res.path || res.tempFilePath || imgUrl;
|
||||
if (typeof ctx.clearRect === "function") {
|
||||
ctx.clearRect(0, 0, drawWidth, drawHeight);
|
||||
}
|
||||
ctx.drawImage(path, 0, 0, drawWidth, drawHeight);
|
||||
// 绘制左上角日期(白色,22px)
|
||||
renderText(ctx, String(date || ""), 18, "#FFFFFF", 6, 20, "left");
|
||||
// 居中绘制第三个参数为圆角黑底标签
|
||||
const rectW = 200;
|
||||
const rectH = 78;
|
||||
const radius = 39;
|
||||
const rectX = (drawWidth - rectW) / 2;
|
||||
const rectY = (drawHeight - rectH) / 2;
|
||||
ctx.save();
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(rectX + radius, rectY);
|
||||
ctx.lineTo(rectX + rectW - radius, rectY);
|
||||
ctx.quadraticCurveTo(
|
||||
rectX + rectW,
|
||||
rectY,
|
||||
rectX + rectW,
|
||||
rectY + radius
|
||||
);
|
||||
ctx.lineTo(rectX + rectW, rectY + rectH - radius);
|
||||
ctx.quadraticCurveTo(
|
||||
rectX + rectW,
|
||||
rectY + rectH,
|
||||
rectX + rectW - radius,
|
||||
rectY + rectH
|
||||
);
|
||||
ctx.lineTo(rectX + radius, rectY + rectH);
|
||||
ctx.quadraticCurveTo(
|
||||
rectX,
|
||||
rectY + rectH,
|
||||
rectX,
|
||||
rectY + rectH - radius
|
||||
);
|
||||
ctx.lineTo(rectX, rectY + radius);
|
||||
ctx.quadraticCurveTo(rectX, rectY, rectX + radius, rectY);
|
||||
ctx.closePath();
|
||||
ctx.setFillStyle("rgba(0,0,0,0.5)");
|
||||
ctx.fill();
|
||||
ctx.restore();
|
||||
|
||||
// 居中排版:左侧显示 actual/,右侧显示 total,再追加“环”
|
||||
ctx.save();
|
||||
if (typeof ctx.setTextBaseline === "function")
|
||||
ctx.setTextBaseline("middle");
|
||||
const centerX = rectX + rectW / 2;
|
||||
const centerY = rectY + rectH / 2;
|
||||
// 左半部:右对齐 actual/
|
||||
renderText(ctx, `${a}/`, 40, "#FFFFFF", centerX, centerY, "right");
|
||||
// 右半部:左对齐 total
|
||||
renderText(
|
||||
ctx,
|
||||
`${t || ""}`,
|
||||
30,
|
||||
"#FFFFFF",
|
||||
centerX,
|
||||
centerY,
|
||||
"left"
|
||||
);
|
||||
// 追加单位“环”:放在 total 文本之后 16px
|
||||
const totalWidth = ctx.measureText(`${t || ""}`).width || 0;
|
||||
renderText(
|
||||
ctx,
|
||||
"环",
|
||||
20,
|
||||
"#FFFFFF",
|
||||
centerX + totalWidth + 5,
|
||||
centerY,
|
||||
"left"
|
||||
);
|
||||
ctx.restore();
|
||||
ctx.draw(false, () => {
|
||||
try {
|
||||
uni.canvasToTempFilePath({
|
||||
canvasId,
|
||||
width: drawWidth,
|
||||
height: drawHeight,
|
||||
destWidth: drawWidth,
|
||||
destHeight: drawHeight,
|
||||
fileType: "png",
|
||||
quality: 1,
|
||||
success: (r) => {
|
||||
const path = r.tempFilePath || r.apFilePath || r.filePath;
|
||||
resolve(path);
|
||||
},
|
||||
fail: (err) => reject(err),
|
||||
});
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
},
|
||||
fail: () => {
|
||||
try {
|
||||
ctx.drawImage(imgUrl, 0, 0, drawWidth, drawHeight);
|
||||
// 绘制左上角日期(白色,22px)
|
||||
renderText(ctx, String(date || ""), 22, "#FFFFFF", 10, 22, "left");
|
||||
// 居中绘制第三个参数为圆角黑底标签
|
||||
const rectW = 200;
|
||||
const rectH = 78;
|
||||
const radius = 39;
|
||||
const rectX = (drawWidth - rectW) / 2;
|
||||
const rectY = (drawHeight - rectH) / 2;
|
||||
ctx.save();
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(rectX + radius, rectY);
|
||||
ctx.lineTo(rectX + rectW - radius, rectY);
|
||||
ctx.quadraticCurveTo(
|
||||
rectX + rectW,
|
||||
rectY,
|
||||
rectX + rectW,
|
||||
rectY + radius
|
||||
);
|
||||
ctx.lineTo(rectX + rectW, rectY + rectH - radius);
|
||||
ctx.quadraticCurveTo(
|
||||
rectX + rectW,
|
||||
rectY + rectH,
|
||||
rectX + rectW - radius,
|
||||
rectY + rectH
|
||||
);
|
||||
ctx.lineTo(rectX + radius, rectY + rectH);
|
||||
ctx.quadraticCurveTo(
|
||||
rectX,
|
||||
rectY + rectH,
|
||||
rectX,
|
||||
rectY + rectH - radius
|
||||
);
|
||||
ctx.lineTo(rectX, rectY + radius);
|
||||
ctx.quadraticCurveTo(rectX, rectY, rectX + radius, rectY);
|
||||
ctx.closePath();
|
||||
ctx.setFillStyle("rgba(0,0,0,0.5)");
|
||||
ctx.fill();
|
||||
ctx.restore();
|
||||
// 居中排版:左侧显示 actual/,右侧显示 total,再追加“环”
|
||||
ctx.save();
|
||||
if (typeof ctx.setTextBaseline === "function")
|
||||
ctx.setTextBaseline("middle");
|
||||
const centerX = rectX + rectW / 2;
|
||||
const centerY = rectY + rectH / 2;
|
||||
// 左半部:右对齐 actual/
|
||||
renderText(ctx, `${a}/`, 40, "#FFFFFF", centerX, centerY, "right");
|
||||
// 右半部:左对齐 total
|
||||
renderText(
|
||||
ctx,
|
||||
`${t || ""}`,
|
||||
35,
|
||||
"#FFFFFF",
|
||||
centerX,
|
||||
centerY,
|
||||
"left"
|
||||
);
|
||||
// 追加单位“环”:放在 total 文本之后 16px
|
||||
const totalWidth = ctx.measureText(`${t || ""}`).width || 0;
|
||||
renderText(
|
||||
ctx,
|
||||
"环",
|
||||
20,
|
||||
"#FFFFFF",
|
||||
centerX + totalWidth + 5,
|
||||
centerY,
|
||||
"left"
|
||||
);
|
||||
ctx.restore();
|
||||
ctx.draw(false, () => {
|
||||
try {
|
||||
uni.canvasToTempFilePath({
|
||||
canvasId,
|
||||
width: drawWidth,
|
||||
height: drawHeight,
|
||||
destWidth: drawWidth,
|
||||
destHeight: drawHeight,
|
||||
fileType: "png",
|
||||
quality: 1,
|
||||
success: (r) => {
|
||||
const path = r.tempFilePath || r.apFilePath || r.filePath;
|
||||
resolve(path);
|
||||
},
|
||||
fail: (err) => reject(err),
|
||||
});
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
},
|
||||
});
|
||||
});
|
||||
} catch (e) {
|
||||
console.error("generateShareCardImage 绘制失败:", e);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user