add client
This commit is contained in:
parent
614ec9e059
commit
e8158eb746
@ -6,7 +6,7 @@ import BeltBlock from './BeltBlock.vue';
|
|||||||
import MetalPlate from './MetalPlate.vue';
|
import MetalPlate from './MetalPlate.vue';
|
||||||
import GameHeader from './GameHeader.vue';
|
import GameHeader from './GameHeader.vue';
|
||||||
import type { Action, Door, Team } from './models';
|
import type { Action, Door, Team } from './models';
|
||||||
import { apiGetTeam, encodeUTF8ToBase64, getApiUrl } from './client';
|
import { apiGetGame, apiGetTeam, apiLetsgo } from './client';
|
||||||
import { UnauthorizedError } from './UnauthorizedError';
|
import { UnauthorizedError } from './UnauthorizedError';
|
||||||
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
@ -84,21 +84,8 @@ function addAction() {
|
|||||||
place.value = ""
|
place.value = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
function letsgo(place: string) {
|
async function letsgo(place: string) {
|
||||||
console.log("letsgo to " + place)
|
await apiLetsgo(login.value, password.value, place)
|
||||||
fetch(
|
|
||||||
getApiUrl("/team/actions"),
|
|
||||||
{
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"X-Id": encodeUTF8ToBase64(login.value),
|
|
||||||
"X-Password": password.value
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
"place": place
|
|
||||||
})
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const scrollToBottom = async (behavior: ScrollBehavior = 'smooth'): Promise<void> => {
|
const scrollToBottom = async (behavior: ScrollBehavior = 'smooth'): Promise<void> => {
|
||||||
@ -111,13 +98,9 @@ const scrollToBottom = async (behavior: ScrollBehavior = 'smooth'): Promise<void
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function getGame() {
|
async function getGame() {
|
||||||
qrurl.value = location.href
|
qrurl.value = location.href
|
||||||
fetch(
|
const data = await apiGetGame(login.value, password.value)
|
||||||
getApiUrl("/game")
|
|
||||||
)
|
|
||||||
.then(response => response.json())
|
|
||||||
.then(data => {
|
|
||||||
gameState.value = data.state
|
gameState.value = data.state
|
||||||
if (data.state === "NEW") {
|
if (data.state === "NEW") {
|
||||||
gameStateText.value = "Игра ещё не началась"
|
gameStateText.value = "Игра ещё не началась"
|
||||||
@ -128,10 +111,6 @@ function getGame() {
|
|||||||
if (data.state === "STOP") {
|
if (data.state === "STOP") {
|
||||||
gameStateText.value = "Игра остановлена"
|
gameStateText.value = "Игра остановлена"
|
||||||
}
|
}
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
console.error('Ошибка:', error)
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Автоматическая прокрутка при изменении items
|
// Автоматическая прокрутка при изменении items
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import type { Team } from './models'
|
import type { Game, Team } from './models'
|
||||||
import { UnauthorizedError } from './UnauthorizedError'
|
import { UnauthorizedError } from './UnauthorizedError'
|
||||||
|
|
||||||
export const apiGetTeam = async (login: string, password: string): Promise<Team> => {
|
export const apiGetTeam = async (login: string, password: string): Promise<Team> => {
|
||||||
@ -23,6 +23,53 @@ export const apiGetTeam = async (login: string, password: string): Promise<Team>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const apiLetsgo = async (login: string, password: string, place: string) => {
|
||||||
|
try {
|
||||||
|
const response = await fetch(getApiUrl("/team/actions"), {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"X-Id": encodeUTF8ToBase64(login),
|
||||||
|
"X-Password": password
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
"place": place
|
||||||
|
})
|
||||||
|
})
|
||||||
|
if (response.status === 401) {
|
||||||
|
throw new UnauthorizedError('Ошибка авторизации')
|
||||||
|
}
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`http error status: ${response.status}`)
|
||||||
|
}
|
||||||
|
return await response.json()
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[apiLetsgo] error:', error)
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const apiGetGame = async (login: string, password: string): Promise<Game> => {
|
||||||
|
try {
|
||||||
|
const response = await fetch(getApiUrl("/game"), {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'X-Id': encodeUTF8ToBase64(login),
|
||||||
|
'X-Password': password,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if (response.status === 401) {
|
||||||
|
throw new UnauthorizedError('Ошибка авторизации')
|
||||||
|
}
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`http error status: ${response.status}`)
|
||||||
|
}
|
||||||
|
return await response.json()
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[apiGetGame] error:', error)
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function getApiUrl(path: string) {
|
export function getApiUrl(path: string) {
|
||||||
const url = 'http://' + window.location.host.split(':')[0] + ':8090' + path
|
const url = 'http://' + window.location.host.split(':')[0] + ':8090' + path
|
||||||
return url
|
return url
|
||||||
|
|||||||
@ -1,3 +1,13 @@
|
|||||||
|
// Игра
|
||||||
|
export type Game = {
|
||||||
|
// Статус игры (NEW, RUN, STOP)
|
||||||
|
state: string
|
||||||
|
// Время начала игры
|
||||||
|
startAt: string
|
||||||
|
// Время окончания игры
|
||||||
|
endAt: string
|
||||||
|
}
|
||||||
|
|
||||||
// Команда
|
// Команда
|
||||||
export type Team = {
|
export type Team = {
|
||||||
// Название
|
// Название
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user