Files
psp_panel/src/app/core/services/native-bridge.service.ts
T

158 lines
4.6 KiB
TypeScript
Raw Normal View History

import { inject, Injectable } from '@angular/core';
import { Maybe } from '../models';
import { ToastService } from './toast.service';
interface INativeBridgeHost {
pay?: (amount: number, id: Maybe<string>) => unknown;
print?: (payload: INativePrintRequest) => unknown;
isEnabled?: () => boolean;
getUUID?: () => Maybe<string>;
}
export interface INativePayRequest {
amount: number;
id: Maybe<string>;
}
export interface INativePrintRequest {
title: string;
items: {
label: string;
value: string;
}[];
}
export interface INativeBridgeResult<T = unknown> {
success: boolean;
data?: T;
error?: string;
}
2026-05-10 09:44:30 +03:30
export interface INativeBridgeDeviceInfo {
deviceName: string;
androidId: string;
}
@Injectable({ providedIn: 'root' })
export class NativeBridgeService {
private readonly toastService = inject(ToastService);
private get host(): INativeBridgeHost | undefined {
const globalWindow = window as unknown as {
NativeBridge?: INativeBridgeHost;
};
return globalWindow.NativeBridge;
}
isEnabled(): boolean {
// @ts-ignore
return !!window.NativeBridge;
// if (window.NativeBridge) {
// }
// if (!this.host) return false;
// const hostEnabled = this.host?.isEnabled;
// if (typeof hostEnabled === 'function') {
// try {
// return !!hostEnabled();
// } catch {
// return false;
// }
// }
// return !!environment.enableNativeBridge && !!this.host;
}
canPay(): boolean {
return this.isEnabled() && typeof this.host?.pay === 'function';
}
canPrint(): boolean {
return this.isEnabled() && typeof this.host?.print === 'function';
}
pay(request: INativePayRequest): any {
if (request.amount <= 10_000) {
const errorMessage = 'برای مقادیر زیر ۱۰۰ هزار ریال، پرداخت ممکن نیست.';
this.toastService.warn({ text: errorMessage, life: 3000 });
return {
success: false,
error: errorMessage,
};
}
this.toastService.info({ text: 'در حال پردازش پرداخت...' });
try {
// @ts-ignore
window.NativeBridge.pay(request.amount, request.id || '');
return { success: true };
} catch (error) {
this.toastService.info({ text: (error as Error).message });
return { success: false, error: (error as Error).message };
}
// const fn = window.NativeBridge.pay(123, 'test');
// if (typeof fn !== 'function') {
// this.toastService.error({ text: 'متاسفانه پرداخت امکان‌پذیر نیست.', life: 3000 });
// return { success: false, error: 'متاسفانه پرداخت امکان‌پذیر نیست.' };
// }
// try {
// fn(123, 'test');
// // fn(request.amount, request.id || '');
// return { success: true };
// // return { success: true, data: parsed ?? raw };
// } catch (error) {
// this.toastService.info({ text: (error as Error).message, });
// return { success: false, error: (error as Error).message };
// }
}
print(request: INativePrintRequest[]): INativeBridgeResult {
return this.invokePrint(request);
}
2026-05-10 09:44:30 +03:30
async getDeviceInfo(): Promise<INativeBridgeResult<INativeBridgeDeviceInfo>> {
if (!this.isEnabled()) {
return { success: false, error: 'متاسفانه ارتباط با دستگاه برقرار نیست.' };
}
try {
// @ts-ignore
const deviceInfo = await window.NativeBridge.deviceInfo();
return { success: true, data: JSON.parse(deviceInfo) };
} catch (error) {
return { success: false, error: (error as Error).message };
}
}
private invokePrint(payload: INativePrintRequest[]): INativeBridgeResult {
if (!this.isEnabled() || !this.canPrint()) {
this.toastService.warn({ text: 'متاسفانه ارتباط با چاپگر برقرار نیست.' });
return { success: false, error: 'متاسفانه ارتباط با چاپگر برقرار نیست.' };
}
try {
2026-05-12 20:24:55 +03:30
this.toastService.info({ text: 'در حال چاپ ...' });
// @ts-ignore
2026-05-12 20:24:55 +03:30
window.NativeBridge.print(JSON.stringify(payload));
return { success: true };
} catch (error) {
this.toastService.warn({ text: 'متاسفانه ارتباط با چاپگر برقرار نیست.' });
return { success: false, error: (error as Error).message };
}
}
getNativeDeviceId(): Maybe<string> {
return typeof this.host?.getUUID === 'function' ? this.host.getUUID!() : null;
}
private tryParse(value: unknown): unknown {
if (typeof value !== 'string') return value;
try {
return JSON.parse(value);
} catch {
return value;
}
}
}