完成不同练习生成不同截图

This commit is contained in:
kron
2025-06-21 12:59:59 +08:00
parent c1148d6e51
commit a9f4f22622
13 changed files with 177 additions and 63 deletions

View File

@@ -1,58 +1,55 @@
<script setup>
import { onMounted } from "vue";
import { ref } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import Container from "@/components/Container.vue";
import { renderText, renderRankTitle, renderLine } from "@/util.js";
import {
renderText,
renderRankTitle,
renderLine,
renderScores,
drawRoundImage,
} from "@/util.js";
import { getPractiseAPI } from "@/apis";
import useStore from "@/store";
import { storeToRefs } from "pinia";
const store = useStore();
const { user } = storeToRefs(store);
const saveImage = () => {
uni.canvasToTempFilePath({
canvasId: "shareCanvas",
success: (res) => {
const tempFilePath = res.tempFilePath;
// 保存图片到相册
uni.saveImageToPhotosAlbum({
filePath: tempFilePath,
success: () => {
uni.showToast({ title: "保存成功" });
},
fail: () => {
uni.showToast({ title: "保存失败", icon: "error" });
},
});
},
});
};
onMounted(() => {
onLoad(async (options) => {
const id = options.id || 462;
const data = await getPractiseAPI(id);
if (!data.arrows.length) return;
var ctx = uni.createCanvasContext("shareCanvas");
const width = 302;
const width = 303;
const height = 535;
ctx.drawImage("../static/share-bg.png", 0, 0, 302, 534);
ctx.drawImage("../static/avatar.png", 20, 20, 35, 35);
drawRoundImage(ctx, user.value.avatar, 20, 20, 35, 35, 20);
ctx.drawImage("../static/avatar-frame.png", 15, 15, 45, 45);
renderText(ctx, "用户名称", 14, "#fff", 70, 35);
renderRankTitle(ctx, "钻石1级");
ctx.drawImage(
"../static/first-try-title.png",
(width - 200) / 2,
155,
200,
53
);
renderText(ctx, "正式开启弓箭手之路", 22, "#fff", 53, 230);
renderText(ctx, user.value.nickName, 14, "#fff", 77, 35, "center");
renderRankTitle(ctx, user.value.lvlName, "center");
let titleImage = "../static/first-try-title.png";
let subTitle = "正式开启弓箭手之路";
if (options.type > 1) {
subTitle = `今日弓箭练习打卡 ${data.createdAt
.split(" ")[0]
.replaceAll("-", ".")}`;
}
if (options.type == 2) {
titleImage = "../static/practise-one-title.png";
} else if (options.type == 3) {
titleImage = "../static/practise-two-title.png";
}
ctx.drawImage(titleImage, (width - 160) / 2, 160, 160, 40);
const subTitleWidth = ctx.measureText(subTitle).width;
renderText(ctx, subTitle, 18, "#fff", width / 2 - subTitleWidth + 10, 220);
renderText(ctx, "共", 16, "#fff", 124, 300);
renderText(ctx, "50", 16, "#fed847", 144, 300);
const totalRing = data.arrows.reduce((last, next) => last + next.ring, 0);
renderText(ctx, totalRing, 16, "#fed847", totalRing < 100 ? 144 : 142, 300);
renderText(ctx, "环", 16, "#fff", 166, 300);
renderLine(ctx, 80);
renderLine(ctx, 192);
for (let i = 0; i < 6; i++) {
ctx.drawImage("../static/score-bg.png", 16 + i * 46, 320, 40, 40);
renderText(ctx, "9", 25, "#fed847", 29 + i * 46, 349);
}
for (let i = 0; i < 6; i++) {
ctx.drawImage("../static/score-bg.png", 16 + i * 46, 366, 40, 40);
renderText(ctx, "9", 25, "#fed847", 29 + i * 46, 395);
}
renderScores(ctx, data.arrows);
ctx.drawImage(
"../static/device-icon.png",
width * 0.06,
@@ -80,16 +77,38 @@ onMounted(() => {
ctx.drawImage("../static/qr-code.png", width * 0.75, height * 0.86, 60, 60);
ctx.draw();
});
const saveImage = () => {
uni.canvasToTempFilePath({
canvasId: "shareCanvas",
success: (res) => {
const tempFilePath = res.tempFilePath;
// 保存图片到相册
uni.saveImageToPhotosAlbum({
filePath: tempFilePath,
success: () => {
uni.showToast({ title: "保存成功" });
},
fail: () => {
uni.showToast({ title: "保存失败", icon: "error" });
},
});
},
});
};
</script>
<template>
<Container>
<view class="content">
<canvas
style="width: 302px; height: 534px"
canvas-id="shareCanvas"
id="firstCanvas"
></canvas>
<view :style="{ overflow: 'hidden', borderRadius: '10px' }">
<canvas
style="width: 302px; height: 534px"
canvas-id="shareCanvas"
id="firstCanvas"
></canvas>
</view>
<view class="footer">
<view>
<image src="../static/wechat-outline.png" mode="widthFix" />