refactor: streamline printing API and update version check logic

This commit is contained in:
2025-05-26 22:59:58 +03:00
parent 64a54dabb8
commit 6f75dafa0d
3 changed files with 14 additions and 55 deletions

View File

@@ -3,11 +3,9 @@ import apiClient from "./apiClient";
const router = '/printing';
const printingApi = {
getLabel: async (orderId: number): Promise<string> => {
let response = await apiClient.get(`${router}/getLabel?orderId=${orderId}`, {responseType: 'arraybuffer'});
const binaryString = String.fromCharCode(...new Uint8Array(response.data));
return btoa(binaryString);
getLabel: async (orderId: number): Promise<{ labels: string[] }> => {
let response = await apiClient.get(`${router}/getLabel?orderId=${orderId}&format=zpl`);
return response.data
},
}
export default printingApi;

View File

@@ -1,4 +1,4 @@
import {Linking, StyleSheet, View} from "react-native";
import {StyleSheet, View} from "react-native";
import LoginScreen from "../LoginScreen/LoginScreen";
import MainScreen from "../MainScreen/MainScreen";
import React, {useEffect} from "react";
@@ -18,7 +18,6 @@ import ordersApi from "../../api/ordersApi";
import ImageZoomModal from "../../components/Modals/ImageZoomModal/ImageZoomModal";
import SortingModal from "../../components/Modals/SortingModal/SortingModal";
import {ASSEMBLY_STATE} from "../../types/assembly";
import Constants from "expo-constants";
import * as FileSystem from 'expo-file-system';
import applicationApi from "../../api/applicationApi";
import {startActivityAsync} from "expo-intent-launcher";
@@ -33,6 +32,8 @@ import {AxiosHeaders} from "axios";
import apiClient from "../../api/apiClient";
import AnimationsView from "../../components/Animations/AnimationsView";
import {ScanningContextProvider} from "../../providers/ScanProvider";
import * as Application from 'expo-application';
function CommonPage() {
@@ -64,9 +65,8 @@ function CommonPage() {
loadAssembly();
})
}
const checkUpdates = async () => {
const currentVersion = Constants.manifest2?.extra?.expoClient?.version;
const currentVersion = Application.nativeApplicationVersion;
applicationApi.getVersion('assemblr').then(({latest_version}) => {
if (currentVersion == latest_version) return;

View File

@@ -1,5 +1,3 @@
import {Zpl} from "react-native-zpl-code";
import {convertPdfToBitmaps} from "../connectors/PdfToBitmap";
import TcpSocket from "react-native-tcp-socket";
import * as SecureStore from 'expo-secure-store';
@@ -20,14 +18,15 @@ class PrintingService {
this.port = port;
}
private async printLabelAsync(pdf: string) {
private async printLabelAsync(pdf: { labels: string[] }): Promise<void> {
const printerIp = await SecureStore.getItemAsync("printerIp");
if (!printerIp) {
throw new Error("Printer IP not found in secure store");
}
// Fetch the PDF, convert to bitmaps, generate ZPL codes
const images = await convertPdfToBitmaps(pdf);
const codes = await this.generateZplCodes(images);
// const images = await convertPdfToBitmaps(pdf);
// const codes = await this.generateZplCodes(images);
const codes = pdf.labels;
return new Promise<void>((resolve, reject) => {
// Create and connect the socket
@@ -60,46 +59,8 @@ class PrintingService {
});
}
private async generateZplCodes(images: string[]) {
const zplCodes = [];
for (const image of images) {
const zplBuilder = new Zpl.Builder();
zplBuilder.setup({
size: {
heightDots: 320, // 40mm at 203 DPI
widthDots: 464, // 58mm at 203 DPI
},
labelHome: {
x: 0,
y: 0,
},
labelTop: 0,
labeShift: 0,
orientation: "NORMAL",
media: {
type: "MARK_SENSING",
dots: 0,
},
});
zplBuilder.image({
uri: `data:image/png;base64,${image}`,
x: 0,
y: 0,
width: 464,
height: 320,
dither: true,
});
const zplCode = await zplBuilder.build();
zplCodes.push(zplCode);
}
return zplCodes;
}
private async print(printer: string, type: string, bytes: string): Promise<boolean> {
private async print(printer: string, type: string, bytes: { labels: string[] }): Promise<boolean> {
try {
await this.printLabelAsync(bytes);
@@ -110,7 +71,8 @@ class PrintingService {
}
}
public async printPdf(printer: string, pdfBytes: string): Promise<boolean> {
public async printPdf(printer: string, pdfBytes: { labels: string[] }): Promise<boolean> {
return this.print(printer, "pdf", pdfBytes);
}
@@ -118,7 +80,6 @@ class PrintingService {
public getPrinter(baseMarketplace: number): string {
if (baseMarketplace == 2) return 'ozon';
return "wildberries";
// return "ozon";
}
}