Napisałem aplikację w Vue.js. Zamierzam dodać dla… · The Chat Archive
Explore / Programming Napisałem aplikację w Vue.js. Zamierzam dodać dla PrinterPosnet funkcjonalność… Save Share Download ▾ ReportNapisałem aplikację w Vue.js. Zamierzam dodać dla PrinterPosnet funkcjonalność wyświetlania jej statusu. Napisz pełną implementację plików, które są niezbędne do dodania tej funkcjonalności. Wzoruj się przy tym na wyświetlaniu statusu zaimplementowanym dla Printer. Nie zmieniaj przy tym istniejących funkcjonalności i widoków już zaimplementowanych dla PrinterPosnet poza zmianami niezbędnymi do dodania nowej funkcjonalności.
/// printer-posnet-row.vue
code Copy code
<v-col cols="auto" class="pt-2">
<edit-async-delete-buttons :readonly="readonly" :delete-action="onDelete" @select="select" />
</v-col>
/// printer-posnet.ts
import { GenericNamedGuidEntity } from "@/views/commerce/common/named-item";
export default class PrinterPosnet extends GenericNamedGuidEntity {
printerId: string | null = null;
ipAddress: string | null = null;
targetId: number;
organizationUnitId: number;
isActive: boolean = false;
public static fromDto(dto: any): PrinterPosnet {
const entity = new PrinterPosnet();
entity.readDto(dto);
return entity;
}
public static empty(): PrinterPosnet {
const entity = new PrinterPosnet();
return entity;
}
protected readDto(dto: any) {
super.readDto(dto);
this.printerId = dto.printerId;
this.ipAddress = dto.ipAddress;
this.targetId = dto.target;
this.organizationUnitId = dto.organizationUnitId;
this.isActive = dto.isActive;
}
get displayName() {
return ${this.name} (${this.id});
}
get dto(): any {
const baseDto: any = {
name: this.name,
isActive: this.isActive,
printerId: this.printerId,
ipAddress: this.ipAddress,
organizationUnitId: this.organizationUnitId,
target: this.targetId
};
code Copy code
if (this.id !== null && this.id !== undefined) {
baseDto.id = this.id;
}
return baseDto;
}
}
/// printer-posnet-view.vue
code Copy code
<paged-data-iterator v-model="pagedState.query.pagedModel" :paged-items="pagedState">
<template #default="{ item, idx }">
<printer-posnet-row
:key="item.id"
:value="item"
:readonly="pagedState.readonly"
@select="() => pagedState.select(item.id)"
@update="() => pagedState.onSearchUpdate()" />
</template>
</paged-data-iterator>
Show full message · 926 lines Assistant2024-10-10 17:37 UTC Copy response
Copy first prompt
Details
Provider OpenAI
Model provenance As recorded by source dataset
Turns 1 (2 messages)
Language en
Conversation date Oct 10, 2024
Added to archive Nov 16, 2026
ID 01m7h41x6fsrqekddrv1drp303Source & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: 4ca427fdc1f8b47e74f16c078097fadb
Zhao et al., WildChat: 1M ChatGPT Interaction Logs in the Wild (ICLR 2024). Contains information from WildChat-4.8M, made available under the ODC Attribution License.
License: ODC-BY-1.0
/// printer-device-state.ts
export default class PrinterDeviceState {
name: string;
id: number;
public static instance = {
ready: new PrinterDeviceState(0, "Gotowa"),
inMenu: new PrinterDeviceState(1, "Otwarte menu"),
waitForKey: new PrinterDeviceState(2, "Oczekiwanie na przycisk"),
waitForUserInput: new PrinterDeviceState(3, "Oczekwianie na użytkownia")
};
public static readonly All: PrinterDeviceState[] = Object.values(PrinterDeviceState.instance);
constructor(id: number, name: string) {
this.name = name;
this.id = id;
}
static getById(id: number | null): PrinterDeviceState {
return PrinterDeviceState.All.find(v => v.id === id) ?? PrinterDeviceState.instance.ready;
}
}
/// printer-job-status-response.ts
import { GenericEntity } from "@/views/commerce/common/named-item";
import PrinterJobStatus from "@/views/commerce/printers/printer-job-status";
export default class PrinterJobStatusResponse extends GenericEntity {
status: PrinterJobStatus = PrinterJobStatus.None;
public static fromDto(dto: any): PrinterJobStatusResponse {
const entity = new PrinterJobStatusResponse();
entity.readDto(dto);
return entity;
}
protected readDto(dto: any) {
super.readDto(dto);
this.status = PrinterJobStatus.getById(dto.status);
}
get dto(): any {
return {
status: this.status.id
};
}
}
/// printer-job-status.ts
export default class PrinterJobStatus {
name: string;
color: string;
id: number;
public static None = new PrinterJobStatus("Nieznany", "error--text", 0);
public static Started = new PrinterJobStatus(
"W trakcie wydruku",
"primary--text text--darken-3",
1
);
public static Completed = new PrinterJobStatus("Wydruk zakończony", "success--text", 2);
public static Expired = new PrinterJobStatus("Brak odpowiedzi", "error--text", 3);
public static Error = new PrinterJobStatus("Błąd drukarki", "error--text", 4);
public static readonly All: PrinterJobStatus[] = [
PrinterJobStatus.None,
PrinterJobStatus.Started,
PrinterJobStatus.Completed,
PrinterJobStatus.Expired,
PrinterJobStatus.Error
];
constructor(name: string, color: string, id: number) {
this.name = name;
this.id = id;
}
static getById(id: number | null): PrinterJobStatus {
return PrinterJobStatus.All.find(v => v.id === id) ?? PrinterJobStatus.None;
}
}
/// printer-staus-card.vue
const TerminalStatusCard = defineComponent({
components: {
SubmitButton,
PrinterStatusContent,
TerminalHelloDataContent,
LoadingContainer,
TitledCard
},
props: {
value: {
type: Object as PropType,
required: true
}
},
setup(props, context) {
const printerStatus = ref(PrinterStatus.empty() as PrinterStatus);
const isLoaded = ref(false);
code Copy code
onMounted(async () => {
try {
await load();
} finally {
isLoaded.value = true;
}
});
const restart = async () => {
const response = await axios.post("/api/services/app/Printers/RestartProxy", {
id: props.value.id
});
printerStatus.value = Printer.fromDto(response.data.result).status;
};
const load = async () => {
const response = await axios.get("/api/services/app/Printers/GetPrinter", {
params: { id: props.value.id }
});
printerStatus.value = Printer.fromDto(response.data.result).status;
};
const close = () => context.emit("close", true);
return { close, isLoaded, printerStatus, restart };
export default TerminalStatusCard;
/// printer-status-content.vue
const PrinterStatusContent = defineComponent({
components: { InlineIcon, TooltipIcon, LoadingContainer, TitledCard },
props: {
value: {
type: Object as PropType,
required: true
}
},
setup(props) {
const responseTimeFormatted = computed(() => {
const raw = props.value.responseTime;
if (!raw) return null;
return Math.round(raw / 10) / 100;
});
return { responseTimeFormatted };
}
});
export default PrinterStatusContent;
import { NoVatString } from "@/views/commerce/vat-settings/vat-level-entry";
import PrinterDeviceState from "@/views/commerce/printers/printer-device-state";
export class PrinterVatLevel {
name: string;
value: number | null;
get formattedValue() {
return this.value !== null ? ${this.value}% : NoVatString;
}
public static fromDto(dto: any): PrinterVatLevel {
const entity = new PrinterVatLevel();
entity.readDto(dto);
return entity;
}
protected readDto(dto: any) {
this.name = dto.name;
this.value = dto.value;
}
}
export class PrinterCounters {
rd: string | null;
hn: string | null;
bn: string | null;
fn: string | null;
nu: string | null;
bt: string | null;
bc: string | null;
fc: string | null;
public static fromDto(dto: any): PrinterCounters {
const entity = new PrinterCounters();
entity.readDto(dto);
return entity;
}
get list() {
const items = [] as Array<{ name: string; val: string }>;
if (this.rd) items.push({ name: "rd", val: this.rd });
if (this.hn) items.push({ name: "hn", val: this.hn });
if (this.bn) items.push({ name: "bn", val: this.bn });
if (this.fn) items.push({ name: "fn", val: this.fn });
if (this.nu) items.push({ name: "nu", val: this.nu });
if (this.bc) items.push({ name: "bc", val: this.bc });
if (this.bt) items.push({ name: "bt", val: this.bt });
if (this.fc) items.push({ name: "fc", val: this.fc });
return items;
}
public static empty(): PrinterCounters {
return new PrinterCounters();
}
protected readDto(dto: any) {
this.rd = dto.rd;
this.hn = dto.hn;
this.bn = dto.bn;
this.fn = dto.fn;
this.nu = dto.nu;
this.bt = dto.bt;
this.bc = dto.bc;
this.fc = dto.fc;
}
}
export default class PrinterStatus {
public static readonly DataStatusUnknown = 0;
public static readonly DataStatusValid = 1;
public static readonly DataStatusError = 2;
vatLevels: PrinterVatLevel[] = [];
areVatLevelsOk = false;
isLowPaper = false;
deviceState: PrinterDeviceState = PrinterDeviceState.instance.ready;
counters: PrinterCounters = PrinterCounters.empty();
serialNumber: string | null = null;
dataStatus = PrinterStatus.DataStatusError;
lastRestartTime: string | null = null;
responseTime: number | null = null;
public static fromDto(dto: any): PrinterStatus {
const entity = new PrinterStatus();
entity.readDto(dto);
return entity;
}
get hasValidStatus() {
return this.dataStatus === PrinterStatus.DataStatusValid;
}
get isUnknown() {
return this.dataStatus === PrinterStatus.DataStatusUnknown;
}
get isNoConnection() {
return this.dataStatus === PrinterStatus.DataStatusError;
}
get error() {
if (this.dataStatus === PrinterStatus.DataStatusError) return "Brak komunikacji z drukarką";
if (this.dataStatus === PrinterStatus.DataStatusUnknown) return "Status nieznany";
code Copy code
const issues = [] as string[];
if (!this.areVatLevelsOk)
issues.push("Stawki VAT zgodne z wartościami skonfigurowanymi w portalu");
if (this.isLowPaper) issues.push("Niski poziom papieru");
if (this.responseTime && this.responseTime > 4000)
issues.push(`Długi czas odpowiedzi (${this.responseTime} ms)`);
return issues.join(", ");
public static empty(): PrinterStatus {
return new PrinterStatus();
}
protected readDto(dto: any) {
this.vatLevels = dto.vatLevels ? dto.vatLevels.map(PrinterVatLevel.fromDto) : [];
this.counters = dto.counters ? PrinterCounters.fromDto(dto.counters) : PrinterCounters.empty();
this.areVatLevelsOk = dto.areVatLevelsOk;
this.deviceState = PrinterDeviceState.getById(dto.deviceState);
this.isLowPaper = dto.isLowPaper;
this.dataStatus = dto.dataStatus;
this.serialNumber = dto.serialNumber;
this.responseTime = dto.responseTime;
this.lastRestartTime = dto.lastRestartTime;
}
}
import { GenericEntity } from "@/views/commerce/common/named-item";
import PrinterStatus from "@/views/commerce/printers/printer-status";
import PrinterTarget from "@/views/commerce/printers/printer-target";
import type { SelectPanelOption } from "@/components/action-buttons/select-panel-option";
export default class Printer extends GenericEntity implements SelectPanelOption {
id: string;
orgUnitId: number;
name: string | null;
target: PrinterTarget = PrinterTarget.instance.reception;
status: PrinterStatus = PrinterStatus.empty();
public static fromDto(dto: any): Printer {
const entity = new Printer();
entity.readDto(dto);
return entity;
}
protected readDto(dto: any) {
super.readDto(dto);
this.id = dto.id;
this.orgUnitId = dto.orgUnitId;
this.name = dto.name;
this.target = PrinterTarget.getById(dto.target);
this.status = dto.status ? PrinterStatus.fromDto(dto.status) : PrinterStatus.empty();
}
get displayName() {
if (this.name) return ${this.name} (${this.id});
return this.id;
}
get dto(): any {
return {
id: this.id
};
}
}
const TerminalsView = defineComponent({
components: {
TooltipIcon,
V2Btn
},
props: {
value: {
type: Object as PropType,
required: true
}
},
setup(props, context) {
const appState = inject(AppState.Key) as AppState;
const stateMappings = ref(StateMappings.create(appState));
code Copy code
const icon = computed(() => {
if (props.value.status.isUnknown) return "mdi-map-marker-question-outline";
if (props.value.status.isNoConnection) return "mdi-close-circle";
if (props.value.status.error) return "mdi-alert";
return "mdi-check";
});
const color = computed(() => {
if (props.value.status.isUnknown) return "primary darken-2";
if (props.value.status.isNoConnection) return "error";
if (props.value.status.error) return "orange";
return "success";
});
return {
stateMappings,
icon,
color,
select: () => context.emit("select")
};
export default TerminalsView;
code Copy code
<template v-for="(item, idx) in items">
<printers-view-row :key="idx" :value="item" @select="() => (selectedItem = item)" />
<v-divider v-if="idx < items.length - 1" :key="idx" class="my-2" />
</template>
const TerminalsView = defineComponent({
components: {
PrintersViewRow,
SubmitButton,
TooltipIcon,
LoadingBar,
OrganizationUnitSelector,
PrinterStatusCard,
code Copy code
LoadingContainer
setup(props, context) {
const router = ref(MedicaRouter.forContext(context));
const appState = inject(AppState.Key) as AppState;
const stateMappings = ref(StateMappings.create(appState));
const items = ref([] as Printer[]);
const isLoaded = ref(false);
const isSearchLoading = ref(false);
const query = ref(new PrintersQuery());
const selectedItem = ref(null as Terminal | null);
code Copy code
onMounted(async () => {
router.value.getParams(query.value);
await loadData();
});
const loadData = async () => {
isLoaded.value = false;
await executeSearch();
isLoaded.value = true;
};
const setQueryParams = async () => {
await router.value.setParams(query.value.serializableQuery);
};
const prevPage = ref(1);
watch(
query,
async (_1, _2) => {
query.value.handleUpdate(prevPage, onSearchUpdate);
if (isLoaded.value) await setQueryParams();
},
{
deep: true
}
);
const executeSearch = async () => {
const response = await axios.post("/api/services/app/Printers/Search", query.value.dto);
items.value = response.data.result.map(Printer.fromDto);
};
const refresh = async () => {
const response = await axios.post(
"/api/services/app/Printers/CheckAll",
{},
{ timeout: 240000 }
);
items.value = response.data.result.map(Printer.fromDto);
};
const searchDebounce = debounce(async () => {
await executeSearch();
isSearchLoading.value = false;
}, 500);
const onSearchUpdate = () => {
isSearchLoading.value = true;
searchDebounce();
};
const allOrgUnits = computed(() => appState.orgUnits);
const onCardClose = (needUpdate: boolean) => {
selectedItem.value = null;
if (needUpdate) onSearchUpdate();
};
return {
items,
query,
refresh,
allOrgUnits,
isLoaded,
isSearchLoading,
onCardClose,
stateMappings,
selectedItem
};
export default TerminalsView;
Aby dodać funkcjonalność wyświetlania statusu dla PrinterPosnet podobnie jak dla Printer, należy utworzyć kilka nowych plików oraz wprowadzić minimalne zmiany w istniejących komponentach. Poniżej przedstawiam pełną implementację niezbędnych plików oraz wymagane modyfikacje:
1. Definicje Statusów dla PrinterPosnet
printer-posnet-device-state.ts
Ten plik definiuje stany urządzenia dla PrinterPosnet, analogicznie do PrinterDeviceState.
typescript Copy code
// src/views/commerce/printers-posnet/printer-posnet-device-state.ts
export default class PrinterPosnetDeviceState {
name: string;
id: number;
public static instance = {
ready: new PrinterPosnetDeviceState(0, "Gotowa"),
inMenu: new PrinterPosnetDeviceState(1, "Otwarte menu"),
waitForKey: new PrinterPosnetDeviceState(2, "Oczekiwanie na przycisk"),
waitForUserInput: new PrinterPosnetDeviceState(3, "Oczekiwanie na użytkownika")
};
public static readonly All: PrinterPosnetDeviceState[] = Object.values(PrinterPosnetDeviceState.instance);
constructor(id: number, name: string) {
this.name = name;
this.id = id;
}
static getById(id: number | null): PrinterPosnetDeviceState {
return PrinterPosnetDeviceState.All.find(v => v.id === id) ?? PrinterPosnetDeviceState.instance.ready;
}
}
printer-posnet-job-status.ts
Definicja statusów zadań drukarki PrinterPosnet.
typescript Copy code
// src/views/commerce/printers-posnet/printer-posnet-job-status.ts
export default class PrinterPosnetJobStatus {
name: string;
color: string;
id: number;
public static None = new PrinterPosnetJobStatus("Nieznany", "error--text", 0);
public static Started = new PrinterPosnetJobStatus("W trakcie wydruku", "primary--text text--darken-3", 1);
public static Completed = new PrinterPosnetJobStatus("Wydruk zakończony", "success--text", 2);
public static Expired = new PrinterPosnetJobStatus("Brak odpowiedzi", "error--text", 3);
public static Error = new PrinterPosnetJobStatus("Błąd drukarki", "error--text", 4);
public static readonly All: PrinterPosnetJobStatus[] = [
PrinterPosnetJobStatus.None,
PrinterPosnetJobStatus.Started,
PrinterPosnetJobStatus.Completed,
PrinterPosnetJobStatus.Expired,
PrinterPosnetJobStatus.Error
];
constructor(name: string, color: string, id: number) {
this.name = name;
this.color = color;
this.id = id;
}
static getById(id: number | null): PrinterPosnetJobStatus {
return PrinterPosnetJobStatus.All.find(v => v.id === id) ?? PrinterPosnetJobStatus.None;
}
}
printer-posnet-status.ts
Model statusu PrinterPosnet.
2. Aktualizacja Modelu PrinterPosnet
Dodaj właściwość status do klasy PrinterPosnet.
printer-posnet.ts
typescript Copy code
// src/views/commerce/printers-posnet/printer-posnet.ts
import { GenericNamedGuidEntity } from "@/views/commerce/common/named-item";
import PrinterPosnetStatus from "@/views/commerce/printers-posnet/printer-posnet-status";
export default class PrinterPosnet extends GenericNamedGuidEntity {
printerId: string | null = null;
ipAddress: string | null = null;
targetId: number;
organizationUnitId: number;
isActive: boolean = false;
status: PrinterPosnetStatus = PrinterPosnetStatus.empty(); // Dodana właściwość
public static fromDto(dto: any): PrinterPosnet {
const entity = new PrinterPosnet();
entity.readDto(dto);
return entity;
}
public static empty(): PrinterPosnet {
const entity = new PrinterPosnet();
return entity;
}
protected readDto(dto: any) {
super.readDto(dto);
this.printerId = dto.printerId;
this.ipAddress = dto.ipAddress;
this.targetId = dto.target;
this.organizationUnitId = dto.organizationUnitId;
this.isActive = dto.isActive;
this.status = dto.status ? PrinterPosnetStatus.fromDto(dto.status) : PrinterPosnetStatus.empty(); // Inicjalizacja statusu
}
get displayName() {
return `${this.name} (${this.id})`;
}
get dto(): any {
const baseDto: any = {
name: this.name,
isActive: this.isActive,
printerId: this.printerId,
ipAddress: this.ipAddress,
organizationUnitId: this.organizationUnitId,
target: this.targetId,
status: this.status.dto // Dodanie statusu do DTO
};
if (this.id !== null && this.id !== undefined) {
baseDto.id = this.id;
}
return baseDto;
}
}
3. Komponenty Statusu dla PrinterPosnet
printer-posnet-status-content.vue
Komponent do wyświetlania szczegółowych informacji o statusie PrinterPosnet.
vue Copy code
<!-- src/views/commerce/printers-posnet/printer-posnet-status-content.vue -->
<template>
<div>
<v-row justify="space-between">
<v-col cols="auto" class="text-body-2" :class="{ 'warn--text': !!value.deviceState.id }">
Status: <strong>{{ value.deviceState.name }}</strong>
<div v-if="responseTimeFormatted">
Czas odpowiedzi: {{ responseTimeFormatted }} s.
</div>
</v-col>
<v-col v-if="value.isLowPaper" cols="auto">
<span class="error--text text-body-2">
<inline-icon small color="error">mdi-alert</inline-icon>
Niski poziom papieru
</span>
</v-col>
</v-row>
<v-row>
<v-col cols="5">
<titled-card title="Stawki VAT">
<template #actions>
<tooltip-icon
v-if="value.areVatLevelsOk"
color="success"
icon="mdi-check"
tooltip="Stawki VAT zgodne z wartościami skonfigurowanymi w systemie" />
<tooltip-icon
v-else
color="error"
icon="mdi-alert"
tooltip="Stawki VAT nie są zgodne z wartościami skonfigurowanymi w systemie" />
</template>
<div v-for="(item, idx) in value.vatLevels" :key="idx">
<span class="text--bold text-body-2">PTU {{ item.name.toUpperCase() }}:</span>
<span class="ml-1 text-body-2">{{ item.formattedValue }}</span>
</div>
</titled-card>
</v-col>
<v-col>
<titled-card title="Liczniki">
<div v-for="(item, idx) in value.counters.list" :key="idx">
<span class="text--bold text-body-2">{{ item.name }}:</span>
<span class="ml-1 text-body-2">{{ item.val }}</span>
</div>
</titled-card>
</v-col>
</v-row>
</div>
</template>
<script lang="ts">
import type { PropType } from "@vue/composition-api";
import { computed, defineComponent } from "@vue/composition-api";
import TitledCard from "@/components/simple/titled-card.vue";
import LoadingContainer from "@/components/loading/loading-container.vue";
import PrinterPosnetStatus from "@/views/commerce/printers-posnet/printer-posnet-status";
import TooltipIcon from "@/components/simple/tooltip-icon.vue";
import InlineIcon from "@/components/simple/inline-icon.vue";
const PrinterPosnetStatusContent = defineComponent({
components: { InlineIcon, TooltipIcon, LoadingContainer, TitledCard },
props: {
value: {
type: Object as PropType<PrinterPosnetStatus>,
required: true
}
},
setup(props) {
const responseTimeFormatted = computed(() => {
const raw = props.value.responseTime;
if (!raw) return null;
return Math.round(raw / 1000); // Przeliczenie na sekundy
});
return { responseTimeFormatted };
}
});
export default PrinterPosnetStatusContent;
</script>
printer-posnet-status-card.vue
Komponent karty wyświetlającej status PrinterPosnet z możliwością restartu.
printer-posnet-status-content.vue (Opcjonalne Pipe)
Upewnij się, że masz zdefiniowany pipe formatTime używany w printer-posnet-status-card.vue. Jeśli nie, dodaj go:
typescript Copy code
// src/lib/filters.ts
import Vue from "vue";
Vue.filter("formatTime", (value: string) => {
if (!value) return "";
const date = new Date(value);
return date.toLocaleString();
});
Zarejestruj filtry globalnie, jeśli jeszcze tego nie zrobiłeś:
typescript Copy code
// src/main.ts
import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import axios from "@/lib/axios";
import "@/lib/filters"; // Import filtrów
new Vue({
router,
store,
render: h => h(App)
}).$mount("#app");
4. Aktualizacja Komponentów Wyświetlających PrinterPosnet
printer-posnet-row.vue
Dodaj ikonę statusu oraz możliwość otwarcia karty statusu.
printer-posnet-view.vue
Dodaj możliwość otwariania karty statusu PrinterPosnet.
Aktualizacja printer-posnet-query.ts
Upewnij się, że PrinterPosnetQuery obsługuje dodatkowe potrzebne parametry, jeśli są wymagane.
typescript Copy code
// src/views/commerce/printers-posnet/printer-posnet-query.ts
import { GenericPagedQuery } from "@/components/paging/generic-paged-query";
export class PrinterPosnetQuery extends GenericPagedQuery {
organizationUnitId: number | null = null;
activeOnly: boolean = false;
searchPhrase: string = "";
target: number | null = null;
get dto() {
return {
...super.dto,
organizationUnitId: this.organizationUnitId,
activeOnly: this.activeOnly,
searchPhrase: this.searchPhrase,
target: this.target
};
}
}
5. Nowe Komponenty Inicjalizacji Statusu
printer-posnet-status-content.vue
Zapewnione już powyżej, ale upewnij się, że wszystkie potrzebne komponenty są zaimportowane i używane poprawnie.
printer-posnet-status-card.vue
Zapewnione powyżej.
6. API Integracja (Axios)
Zakładamy, że API posiada endpointy do pobierania statusu PrinterPosnet oraz do restartowania drukarki. Jeśli tak nie jest, należy je zaimplementować na serwerze.
Przykładowe Wywołania API
typescript Copy code
// src/lib/axios.ts
import axios from "axios";
const instance = axios.create({
baseURL: process.env.VUE_APP_API_BASE_URL || "",
// inne ustawienia, takie jak nagłówki domyślne
});
export default instance;
7. Podsumowanie
W powyższej implementacji dodaliśmy:
Modele Statusu : Definicje klas PrinterPosnetDeviceState, PrinterPosnetJobStatus, oraz PrinterPosnetStatus w pliku printer-posnet-status.ts.
Aktualizacja Modelu PrinterPosnet : Dodaliśmy właściwość status do klasy PrinterPosnet w printer-posnet.ts.
Komponenty Vue :
printer-posnet-status-content.vue: Wyświetlanie szczegółów statusu.
printer-posnet-status-card.vue: Karta zawierająca printer-posnet-status-content oraz akcje takie jak restart.
Aktualizacja printer-posnet-row.vue: Dodanie ikony statusu.
Aktualizacja printer-posnet-view.vue: Dodanie możliwości otwarcia karty statusu.
API Integracja : Dodałeś wywołania API do pobierania i restartowania statusu PrinterPosnet.
Upewnij się, że wszystkie nowe pliki są poprawnie zaimportowane i że ścieżki do nich są zgodne z twoją strukturą projektu. Po wdrożeniu powyższych zmian, użytkownicy będą mogli wyświetlać i zarządzać statusem PrinterPosnet w aplikacji.
Show full message · 806 lines Copy first response
Not yet included in a published release.
// src/views/commerce/printers-posnet/printer-posnet-status.ts
import PrinterPosnetDeviceState from "@/views/commerce/printers-posnet/printer-posnet-device-state";
import PrinterPosnetJobStatus from "@/views/commerce/printers-posnet/printer-posnet-job-status";
export class PrinterPosnetVatLevel {
name: string;
value: number | null;
get formattedValue() {
return this.value !== null ? `${this.value}%` : "Brak VAT";
}
public static fromDto(dto: any): PrinterPosnetVatLevel {
const entity = new PrinterPosnetVatLevel();
entity.readDto(dto);
return entity;
}
protected readDto(dto: any) {
this.name = dto.name;
this.value = dto.value;
}
}
export class PrinterPosnetCounters {
counterA: string | null;
counterB: string | null;
// Dodaj inne liczniki w zależności od potrzeb
public static fromDto(dto: any): PrinterPosnetCounters {
const entity = new PrinterPosnetCounters();
entity.readDto(dto);
return entity;
}
get list() {
const items = [] as Array<{ name: string; val: string }>;
if (this.counterA) items.push({ name: "Counter A", val: this.counterA });
if (this.counterB) items.push({ name: "Counter B", val: this.counterB });
// Dodaj inne liczniki w zależności od potrzeb
return items;
}
public static empty(): PrinterPosnetCounters {
return new PrinterPosnetCounters();
}
protected readDto(dto: any) {
this.counterA = dto.counterA;
this.counterB = dto.counterB;
// Dodaj inne liczniki w zależności od potrzeb
}
}
export default class PrinterPosnetStatus {
vatLevels: PrinterPosnetVatLevel[] = [];
areVatLevelsOk = false;
isLowPaper = false;
deviceState: PrinterPosnetDeviceState = PrinterPosnetDeviceState.instance.ready;
counters: PrinterPosnetCounters = PrinterPosnetCounters.empty();
dataStatus = 2; // 0: Unknown, 1: Valid, 2: Error
lastRestartTime: string | null = null;
responseTime: number | null = null;
public static readonly DataStatusUnknown = 0;
public static readonly DataStatusValid = 1;
public static readonly DataStatusError = 2;
public static fromDto(dto: any): PrinterPosnetStatus {
const entity = new PrinterPosnetStatus();
entity.readDto(dto);
return entity;
}
get hasValidStatus() {
return this.dataStatus === PrinterPosnetStatus.DataStatusValid;
}
get isUnknown() {
return this.dataStatus === PrinterPosnetStatus.DataStatusUnknown;
}
get isError() {
return this.dataStatus === PrinterPosnetStatus.DataStatusError;
}
get error() {
if (this.dataStatus === PrinterPosnetStatus.DataStatusError) return "Brak komunikacji z drukarką";
if (this.dataStatus === PrinterPosnetStatus.DataStatusUnknown) return "Status nieznany";
const issues = [] as string[];
if (!this.areVatLevelsOk)
issues.push("Stawki VAT nie są zgodne z wartościami skonfigurowanymi w systemie");
if (this.isLowPaper) issues.push("Niski poziom papieru");
if (this.responseTime && this.responseTime > 4000)
issues.push(`Długi czas odpowiedzi (${this.responseTime} ms)`);
return issues.join(", ");
}
public static empty(): PrinterPosnetStatus {
return new PrinterPosnetStatus();
}
protected readDto(dto: any) {
this.vatLevels = dto.vatLevels ? dto.vatLevels.map(PrinterPosnetVatLevel.fromDto) : [];
this.counters = dto.counters ? PrinterPosnetCounters.fromDto(dto.counters) : PrinterPosnetCounters.empty();
this.areVatLevelsOk = dto.areVatLevelsOk;
this.deviceState = PrinterPosnetDeviceState.getById(dto.deviceState);
this.isLowPaper = dto.isLowPaper;
this.dataStatus = dto.dataStatus;
this.responseTime = dto.responseTime;
this.lastRestartTime = dto.lastRestartTime;
}
}
<!-- src/views/commerce/printers-posnet/printer-posnet-status-card.vue -->
<template>
<titled-card
toolbar
:title="value.displayName"
close-button
high
color="primary darken-2"
@close="close">
<loading-container :is-loaded="isLoaded" style="min-width: 500px">
<printer-posnet-status-content v-if="printerStatus.hasValidStatus" :value="printerStatus" />
<template v-else>
<div style="width: 100%; text-align: center">
<v-icon x-large color="error">
mdi-alert
</v-icon>
</div>
<div style="width: 100%; text-align: center" class="error--text text-body-2">
Błąd komunikacji z drukarką
</div>
</template>
<v-row dense justify="space-between" align="end">
<v-col cols="auto">
<submit-button :action="restart">
Restart
</submit-button>
</v-col>
<v-col v-if="printerStatus.lastRestartTime" cols="auto" class="text-caption">
Ostatni restart: {{ printerStatus.lastRestartTime | formatTime }}
</v-col>
</v-row>
</loading-container>
</titled-card>
</template>
<script lang="ts">
import type { PropType } from "@vue/composition-api";
import { defineComponent, onMounted, ref } from "@vue/composition-api";
import TitledCard from "@/components/simple/titled-card.vue";
import LoadingContainer from "@/components/loading/loading-container.vue";
import axios from "@/lib/axios";
import PrinterPosnetStatusContent from "@/views/commerce/printers-posnet/printer-posnet-status-content.vue";
import PrinterPosnet from "@/views/commerce/printers-posnet/printer-posnet";
import PrinterPosnetStatus from "@/views/commerce/printers-posnet/printer-posnet-status";
import SubmitButton from "@/components/action-buttons/submit-button.vue";
const PrinterPosnetStatusCard = defineComponent({
components: {
SubmitButton,
PrinterPosnetStatusContent,
LoadingContainer,
TitledCard
},
props: {
value: {
type: Object as PropType<PrinterPosnet>,
required: true
},
readonly: {
type: Boolean,
required: false,
default: false
}
},
emits: ["close", "update"],
setup(props, context) {
const printerStatus = ref(PrinterPosnetStatus.empty() as PrinterPosnetStatus);
const isLoaded = ref(false);
onMounted(async () => {
try {
await load();
} finally {
isLoaded.value = true;
}
});
const restart = async () => {
try {
await axios.post("/api/services/app/PrinterPosnet/Restart", {
id: props.value.id
});
await load(); // Po restarcie, ponownie załaduj status
} catch (error) {
console.error("Błąd podczas restartu drukarki:", error);
}
};
const load = async () => {
try {
const response = await axios.get("/api/services/app/PrinterPosnet/GetStatus", {
params: { id: props.value.id }
});
printerStatus.value = PrinterPosnetStatus.fromDto(response.data.result);
} catch (error) {
console.error("Błąd podczas ładowania statusu drukarki:", error);
printerStatus.value.dataStatus = PrinterPosnetStatus.DataStatusError;
}
};
const close = () => context.emit("close", true);
return { close, isLoaded, printerStatus, restart };
}
});
export default PrinterPosnetStatusCard;
</script>
<!-- src/views/commerce/printers-posnet/printer-posnet-row.vue -->
<template>
<v-row align="center">
<v-col>
<v-row dense>
<v-col cols="auto">
<span class="text-body-2 text--bold" v-text="value.displayName"></span>
</v-col>
<v-col cols="auto">
<span :class="{ 'text-body-2': true }">
<v-icon small class="mr-1">mdi-map-marker</v-icon>
{{ stateMappings.orgUnitName(value.organizationUnitId) }}
</span>
</v-col>
</v-row>
</v-col>
<v-col cols="auto">
<div class="text-body-2" style="min-width: 100px">{{ targetName }}</div>
</v-col>
<v-col cols="1" class="px-2">
<span class="text-body-2"><strong>{{ value.name }}</strong></span>
<div class="text-caption">
IP: {{ value.ipAddress }}
</div>
</v-col>
<v-col cols="auto" class="text-center">
<tooltip-icon
:icon="statusIcon"
:color="statusColor"
:tooltip="value.status.error"
:show-tooltip="!!value.status.error" />
</v-col>
<v-col cols="auto" class="px-2">
<edit-async-delete-buttons :readonly="readonly" :delete-action="onDelete" @select="select" />
</v-col>
</v-row>
</template>
<script lang="ts">
import type { PropType } from "@vue/composition-api";
import { computed, defineComponent, inject, ref } from "@vue/composition-api";
import axios from "@/lib/axios";
import AppState from "@/views/main-wrap/app-state";
import StateMappings from "@/views/main-wrap/state-mappings";
import SubmitButton from "@/components/action-buttons/submit-button.vue";
import TooltipIcon from "@/components/simple/tooltip-icon.vue";
import InlineIcon from "@/components/simple/inline-icon.vue";
import TextUtils from "@/lib/text-utils";
import type PrinterPosnet from "@/views/commerce/printers-posnet/printer-posnet";
import EditAsyncDeleteButtons from "@/components/action-buttons/edit-async-delete-buttons.vue";
import PrinterTarget from "./../printers/printer-target";
const PrinterPosnetRow = defineComponent({
components: {
EditAsyncDeleteButtons,
InlineIcon,
TooltipIcon,
SubmitButton
},
props: {
value: {
type: Object as PropType<PrinterPosnet>,
required: true
},
readonly: {
type: Boolean,
required: false,
default: false
}
},
emits: ["update", "select"],
setup(props, context) {
const appState = inject(AppState.Key) as AppState;
const stateMappings = ref(StateMappings.create(appState));
const targetName = ref(PrinterTarget.getById(props.value.targetId)?.name);
const onDelete = async () => {
await axios.delete(`/api/services/app/PrinterPosnet/${props.value.id}`);
context.emit("update");
};
// Obliczanie ikony i koloru na podstawie statusu
const statusIcon = computed(() => {
if (props.value.status.isError) return "mdi-alert";
if (props.value.status.isUnknown) return "mdi-help-circle";
if (props.value.status.hasValidStatus) return "mdi-check-circle";
return "mdi-alert-circle-outline";
});
const statusColor = computed(() => {
if (props.value.status.isError) return "error";
if (props.value.status.isUnknown) return "warning";
if (props.value.status.hasValidStatus) return "success";
return "grey";
});
return {
stateMappings,
targetName,
onDelete,
trim: TextUtils.trim,
select: () => context.emit("select"),
statusIcon,
statusColor
};
}
});
export default PrinterPosnetRow;
</script>
<!-- src/views/commerce/printers-posnet/printer-posnet-view.vue -->
<template>
<loading-container :is-loaded="pagedState.isLoaded">
<v-overlay v-if="pagedState.isCardVisible" z-index="10" :dark="false">
<printer-posnet-status-card
:value="pagedState.selectedItem"
@close="v => pagedState.onCardClose(v)" />
</v-overlay>
<v-row dense align="center">
<v-col cols="6">
<organization-unit-selector v-model="pagedState.query.organizationUnitId" />
</v-col>
<v-col cols="6">
<show-active-only-switch v-model="pagedState.query.activeOnly" />
</v-col>
<v-col cols="6">
<search-input v-model="pagedState.query.searchPhrase" />
</v-col>
<v-col cols="6">
<printer-posnet-target-selector v-model="pagedState.query.target" />
</v-col>
</v-row>
<paged-data-iterator v-model="pagedState.query.pagedModel" :paged-items="pagedState">
<template #default="{ item, idx }">
<printer-posnet-row
:key="item.id"
:value="item"
:readonly="pagedState.readonly"
@select="() => pagedState.selectItem(item)" <!-- Zmiana wywołania -->
@update="() => pagedState.onSearchUpdate()" />
</template>
</paged-data-iterator>
</loading-container>
</template>
<script lang="ts">
import { defineComponent } from "@vue/composition-api";
import LoadingContainer from "@/components/loading/loading-container.vue";
import SearchInput from "@/components/simple/search-input.vue";
import PrinterPosnet from "@/views/commerce/printers-posnet/printer-posnet";
import PrinterPosnetCard from "@/views/commerce/printers-posnet/printer-posnet-card.vue";
import PrinterPosnetRow from "@/views/commerce/printers-posnet/printer-posnet-row.vue";
import PagedDataIterator from "@/components/paging/paged-data-iterator.vue";
import { usePagedSearch } from "@/components/paging/paged-view-helpers";
import { PermissionNames } from "@/lib/permissions";
import type { PagedSearchOptions } from "@/components/paging/paged-search-options";
import { PrinterPosnetQuery } from "@/views/commerce/printers-posnet/printer-posnet-query";
import PrinterPosnetTargetSelector from "@/views/commerce/printers-posnet/printer-posnet-target-selector.vue";
import OrganizationUnitSelector from "@/views/commerce/common/selectors/organization-unit-selector.vue";
const PrinterPosnetsView = defineComponent({
components: {
PagedDataIterator,
PrinterPosnetRow,
PrinterPosnetCard,
PrinterPosnetTargetSelector,
OrganizationUnitSelector,
SearchInput,
LoadingContainer
},
props: {},
setup(props, context) {
const options: PagedSearchOptions<PrinterPosnetQuery> = {
addItemButton: true,
editPermission: PermissionNames.Printers_Manage,
fixedQuerySetup: q => { }
};
const pagedState = usePagedSearch(
PrinterPosnet.fromDto,
PrinterPosnetQuery,
"/api/services/app/PrinterPosnet/Search",
context,
options
);
// Zmiana metody select z emit na nowe emitowanie selectItem
const selectItem = (item: PrinterPosnet) => {
pagedState.selectedItem = item;
pagedState.isCardVisible = true;
};
pagedState.selectItem = selectItem;
return {
pagedState
};
}
});
export default PrinterPosnetsView;
</script>