From 6f75dafa0dc06cb5a4e5997e993e9839be7601e9 Mon Sep 17 00:00:00 2001 From: admin Date: Mon, 26 May 2025 22:59:58 +0300 Subject: [PATCH] refactor: streamline printing API and update version check logic --- src/api/printingApi.ts | 8 ++-- src/screens/CommonPage/CommonPage.tsx | 8 ++-- src/utils/PrintingService.ts | 53 ++++----------------------- 3 files changed, 14 insertions(+), 55 deletions(-) diff --git a/src/api/printingApi.ts b/src/api/printingApi.ts index 5d232d8..08d5825 100644 --- a/src/api/printingApi.ts +++ b/src/api/printingApi.ts @@ -3,11 +3,9 @@ import apiClient from "./apiClient"; const router = '/printing'; const printingApi = { - getLabel: async (orderId: number): Promise => { - 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; \ No newline at end of file diff --git a/src/screens/CommonPage/CommonPage.tsx b/src/screens/CommonPage/CommonPage.tsx index 38a6a35..28de3e3 100644 --- a/src/screens/CommonPage/CommonPage.tsx +++ b/src/screens/CommonPage/CommonPage.tsx @@ -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; diff --git a/src/utils/PrintingService.ts b/src/utils/PrintingService.ts index f09e07d..43e3526 100644 --- a/src/utils/PrintingService.ts +++ b/src/utils/PrintingService.ts @@ -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 { 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((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 { + private async print(printer: string, type: string, bytes: { labels: string[] }): Promise { try { await this.printLabelAsync(bytes); @@ -110,7 +71,8 @@ class PrintingService { } } - public async printPdf(printer: string, pdfBytes: string): Promise { + public async printPdf(printer: string, pdfBytes: { labels: string[] }): Promise { + 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"; } }