Files
shoot-miniprograms/src/pages/practise-one.vue
2025-07-11 00:47:34 +08:00

130 lines
3.8 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script setup>
import { ref, onMounted, onUnmounted } from "vue";
import Container from "@/components/Container.vue";
import ShootProgress from "@/components/ShootProgress.vue";
import BowTarget from "@/components/BowTarget.vue";
import ScorePanel2 from "@/components/ScorePanel2.vue";
import ScoreResult from "@/components/ScoreResult.vue";
import SButton from "@/components/SButton.vue";
import Avatar from "@/components/Avatar.vue";
import BowPower from "@/components/BowPower.vue";
import TestDistance from "@/components/TestDistance.vue";
import { createPractiseAPI, getHomeData } from "@/apis";
import { generateCanvasImage } from "@/util";
import { MESSAGETYPES, roundsName } from "@/constants";
import useStore from "@/store";
import { storeToRefs } from "pinia";
const store = useStore();
const { user } = storeToRefs(store);
const { updateUser } = store;
const start = ref(false);
const scores = ref([]);
const total = 12;
const currentRound = ref(0);
const practiseResult = ref({});
const power = ref(0);
const practiseId = ref("");
const onReady = async () => {
const result = await createPractiseAPI(total);
if (result) practiseId.value = result.id;
currentRound.value = 0;
scores.value = [];
start.value = true;
};
async function onReceiveMessage(messages = []) {
messages.forEach((msg) => {
if (msg.constructor === MESSAGETYPES.ShootSyncMeArrowID) {
scores.value.push(msg.target);
currentRound.value += 1;
if (currentRound.value === 4) {
currentRound.value = 1;
}
power.value = msg.target.battery;
}
if (msg.constructor === MESSAGETYPES.ShootSyncMePracticeID) {
if (practiseId.value && practiseId.value === msg.practice.id) {
start.value = false;
practiseResult.value = {
...msg.practice,
arrows: JSON.parse(msg.practice.arrows),
};
generateCanvasImage("shareCanvas", 2, user.value, practiseResult.value);
}
}
});
}
async function onComplete() {
if (
practiseResult.value.arrows &&
practiseResult.value.arrows.length === total
) {
uni.navigateBack();
} else {
practiseResult.value = {};
start.value = false;
scores.value = [];
currentRound.value = 0;
}
}
onMounted(() => {
uni.$on("socket-inbox", onReceiveMessage);
});
onUnmounted(() => {
uni.$off("socket-inbox", onReceiveMessage);
});
</script>
<template>
<Container :bgType="1" title="个人单组练习">
<view>
<TestDistance v-if="!start && !practiseResult.arrows" />
<block v-if="start || practiseResult.arrows">
<ShootProgress
:tips="`${
!start || scores.length === 12
? ''
: `请开始射箭第${
roundsName[Math.ceil((scores.length + 1) / 3)]
}轮`
}`"
:start="start"
:total="120"
/>
<view class="user-row">
<Avatar :src="user.avatar" :size="35" />
<BowPower :power="power" />
</view>
<BowTarget
:start="start"
:totalRound="start ? total / 4 : 0"
:currentRound="currentRound"
:scores="scores"
/>
<ScorePanel2 :scores="scores.map((s) => s.ring)" />
<ScoreResult
v-if="practiseResult.arrows"
:rowCount="6"
:total="total"
:onClose="onComplete"
:result="practiseResult"
:tipSrc="`../static/${
practiseResult.arrows.length < total ? 'un' : ''
}finish-tip.png`"
/>
<canvas class="share-canvas" canvas-id="shareCanvas"></canvas>
</block>
</view>
<view :style="{ marginBottom: '20px' }">
<SButton v-if="!start" :onClick="onReady">准备好了直接开始</SButton>
</view>
</Container>
</template>
<style scoped>
</style>