add first custom component

This commit is contained in:
Владимир Фёдоров 2025-07-01 01:03:43 +07:00
parent 695b5b149f
commit bd71eee8fd
5 changed files with 129 additions and 96 deletions

View File

@ -1,10 +1,9 @@
<script setup lang="ts"> <script setup lang="ts">
import { onMounted, ref } from 'vue'; import { onMounted, ref } from 'vue';
import router from '@/router'; import router from '@/router';
import VueQrcode from '@chenfengyuan/vue-qrcode';
import type { Game, Teams } from './models'; import type { Game, Teams } from './models';
import { qrOptions } from './qr';
import { apiGetTeams, apiAddTeam, apiGetGame, apiStartGame, apiStopGame, apiGaveApplication, apiDownloadQrCodesFile } from './client'; import { apiGetTeams, apiAddTeam, apiGetGame, apiStartGame, apiStopGame, apiGaveApplication, apiDownloadQrCodesFile } from './client';
import TeamQRCode from '../components/TeamQRCode.vue'
const qrurl = ref("-") const qrurl = ref("-")
const qrteam = ref("-") const qrteam = ref("-")
@ -20,12 +19,28 @@ async function addTeam() {
teamName.value = "" teamName.value = ""
} }
function startGame() { async function startGame() {
apiStartGame(gameState) gameState.value = "Загрузка..."
await apiStartGame()
} }
function stopGame() { async function stopGame() {
apiStopGame(gameState) gameState.value = "Загрузка..."
await apiStopGame()
}
async function getGame() {
game.value = await apiGetGame()
if (game.value.state === "NEW") {
gameState.value = "Игра ещё не началась"
}
if (game.value.state === "RUN") {
gameState.value = "Игра идет с " + game.value?.startAt.substring(11)
}
if (game.value.state === "STOP") {
gameState.value = "Игра остановлена " + game.value?.startAt.substring(11) + " - " + game.value?.endAt.substring(11)
}
} }
let intervalId = 0 let intervalId = 0
@ -34,7 +49,7 @@ onMounted(async () => {
intervalId = setInterval(async () => { intervalId = setInterval(async () => {
teams.value = await apiGetTeams() teams.value = await apiGetTeams()
apiGetGame(game, gameState) await getGame()
}, 2000); }, 2000);
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {
@ -49,12 +64,7 @@ onMounted(async () => {
Вечерний детектив - {{ gameState }} Вечерний детектив - {{ gameState }}
</div> </div>
<div class="qr"> <TeamQRCode :data="qrurl" :title="qrteam" />
<VueQrcode :value="qrurl" :options="qrOptions" tag="svg" class="qr-code" />
<div>
{{ qrteam }}
</div>
</div>
<div class="form-block buttons-block"> <div class="form-block buttons-block">
<a v-on:click="startGame" class="button-menu">Начать</a> <a v-on:click="startGame" class="button-menu">Начать</a>
@ -241,14 +251,6 @@ a {
} }
} }
.qr {
position: absolute;
top: 80px;
right: 30px;
text-align: center;
width: 120px;
}
.button-container { .button-container {
margin-bottom: 30px; margin-bottom: 30px;
} }

View File

@ -0,0 +1,28 @@
<script setup lang="ts">
import { qrOptions } from './qr';
import VueQrcode from '@chenfengyuan/vue-qrcode';
defineProps({
data: String,
title: String
});
</script>
<template>
<div class="qr">
<VueQrcode :value="data" :options="qrOptions" tag="svg" />
<div>
{{ title }}
</div>
</div>
</template>
<style scoped>
.qr {
position: absolute;
top: 80px;
right: 30px;
text-align: center;
width: 120px;
}
</style>

View File

@ -1,5 +1,3 @@
import type { Ref } from 'vue';
import { getApiUrl } from './net';
import type { Game, Teams } from './models'; import type { Game, Teams } from './models';
import { downloadData } from './qr'; import { downloadData } from './qr';
@ -32,60 +30,60 @@ export const apiAddTeam = async (teamName: string) => {
if (!response.ok) { if (!response.ok) {
throw new Error(`http error status: ${response.status}`) throw new Error(`http error status: ${response.status}`)
} }
return await response.json()
} catch (error) { } catch (error) {
console.error('[apiAddTeam] error:', error) console.error('[apiAddTeam] error:', error)
throw error throw error
} }
} }
export const apiGetGame = (game: Ref<Game | undefined>, gameState: Ref<string>) => { export const apiGetGame = async (): Promise<Game> => {
fetch( try {
const response = await fetch(
getApiUrl("/game") getApiUrl("/game")
) )
.then(response => response.json()) if (!response.ok) {
.then(data => { throw new Error(`http error status: ${response.status}`)
game.value = data
if (data.state === "NEW") {
gameState.value = "Игра ещё не началась"
} }
if (data.state === "RUN") { return await response.json()
gameState.value = "Игра идет с " + game.value?.startAt.substring(11) } catch (error) {
console.error('[apiGetGame] error:', error)
throw error
} }
if (data.state === "STOP") {
gameState.value = "Игра остановлена " + game.value?.startAt.substring(11) + " - " + game.value?.endAt.substring(11)
}
})
.catch(error => {
console.error('Ошибка:', error)
});
} }
export const apiStartGame = (gameState: Ref<string>) => { export const apiStartGame = async () => {
gameState.value = "Загрузка..." try {
fetch( const response = await fetch(
getApiUrl("/game/start"), getApiUrl("/game/start"),
{ method: "POST" } { method: "POST" }
) )
.catch(error => { if (!response.ok) {
console.error('Ошибка:', error) throw new Error(`http error status: ${response.status}`)
}); }
} catch (error) {
console.error('[apiStartGame] error:', error)
throw error
}
} }
export const apiStopGame = (gameState: Ref<string>) => { export const apiStopGame = async () => {
gameState.value = "Загрузка..." try {
fetch( const response = await fetch(
getApiUrl("/game/stop"), getApiUrl("/game/stop"),
{ method: "POST" } { method: "POST" }
) )
.catch(error => { if (!response.ok) {
console.error('Ошибка:', error) throw new Error(`http error status: ${response.status}`)
}); }
} catch (error) {
console.error('[apiStopGame] error:', error)
throw error
}
} }
export const apiGaveApplication = async (teamId: number, id: number) => {
export const apiGaveApplication = (teamId: number, id: number) => { try {
fetch( const response = await fetch(
getApiUrl("/teams/" + teamId + "/applications"), getApiUrl("/teams/" + teamId + "/applications"),
{ {
method: "POST", method: "POST",
@ -94,21 +92,31 @@ export const apiGaveApplication = (teamId: number, id: number) => {
}) })
} }
) )
.then(() => { }) if (!response.ok) {
.catch(error => { throw new Error(`http error status: ${response.status}`)
console.error('Ошибка:', error) }
}); } catch (error) {
console.error('[apiGaveApplication] error:', error)
throw error
}
} }
export const apiDownloadQrCodesFile = () => { export const apiDownloadQrCodesFile = async () => {
fetch( try {
const response = await fetch(
getApiUrl("/teams/pdf") getApiUrl("/teams/pdf")
) )
.then(response => response.json()) if (!response.ok) {
.then(data => { throw new Error(`http error status: ${response.status}`)
downloadData(data.result) }
}) const data = await response.json();
.catch(error => { downloadData(data.result)
console.error('Ошибка:', error) } catch (error) {
}); console.error('[apiDownloadQrCodesFile] error:', error)
throw error
}
}
function getApiUrl(path: string) {
return "http://" + window.location.host.split(":")[0] + ":8090" + path
} }

View File

@ -1,5 +0,0 @@
export function getApiUrl(path: string) {
const url = "http://" + window.location.host.split(":")[0] + ":8090" + path
console.log(url)
return url
}

View File

@ -23,7 +23,7 @@ export const downloadData = (data: string) => {
downloadFile(b, 'teams_qr_code.pdf', 'application/pdf;teams_qr_code.pdf') downloadFile(b, 'teams_qr_code.pdf', 'application/pdf;teams_qr_code.pdf')
} }
export const base64ToBytes = (base64: string): Uint8Array => { const base64ToBytes = (base64: string): Uint8Array => {
const binaryString = atob(base64); const binaryString = atob(base64);
const bytes = new Uint8Array(binaryString.length); const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) { for (let i = 0; i < binaryString.length; i++) {
@ -32,7 +32,7 @@ export const base64ToBytes = (base64: string): Uint8Array => {
return bytes; return bytes;
} }
export const downloadFile = (bytes: Uint8Array, fileName: string, mimeType: string) => { const downloadFile = (bytes: Uint8Array, fileName: string, mimeType: string) => {
const blob = new Blob([bytes], { type: mimeType }); const blob = new Blob([bytes], { type: mimeType });
const url = URL.createObjectURL(blob); const url = URL.createObjectURL(blob);
const link = document.createElement('a'); const link = document.createElement('a');