import './libs/jquery'; import type {App} from 'vue'; import type {Router} from 'vue-router'; import {showLoadingToast, showFailToast, showSuccessToast, closeToast} from 'vant'; import {printerClient} from './libs/printerClient'; import {fetchAndRender} from './libs/fetchAndRender'; /** 移动端 install 配置 */ export interface MobileInstallOptions { /** vue-router 实例(用于注册打印设置路由) */ router: Router; /** 打印服务(WebSocket)地址,默认 'ws://localhost:17891' */ printerHost?: string; /** 打印设置路由路径,默认 '/printer-setting' */ routePath?: string; } /** * 移动端打印入口(vant/x5 webview)。 * * 与桌面 install 的区别:不注册设计器/预览页/hiprintVue,只 * 1. 建立打印服务 WS 连接(printerClient 单例) * 2. 注册一个打印设置路由(vant 版页面:连接状态/选打印机/默认开关) * 3. 注入 $serverPrint 方法 * * 用法: * import {mobileInstall} from 'yh-hiprint/mobile'; * mobileInstall(app, {router, printerHost: 'ws://...'}); * // 任意位置 router.push('/printer-setting') 进入设置页 * // 注入的 $serverPrint(code, params, data) 直接打印 */ export function mobileInstall(app: App, options: MobileInstallOptions): void { const {router, printerHost, routePath = '/printer-setting'} = options; // 1. 建立打印服务连接(printerClient 单例,与设置页共享) if (typeof window !== 'undefined') { printerClient.connect(printerHost || 'ws://localhost:17891'); } // 2. 注册打印设置路由 router.addRoute({ path: routePath, name: 'PrinterSetting', component: () => import('./mobile/PrinterSetting.vue'), }); // 3. 注入 $serverPrint app.provide('$serverPrint', mobileServerPrint); app.config.globalProperties.$serverPrint = mobileServerPrint; } /** * 移动端服务端打印。 * * 获取模板 + 渲染 HTML → 通过 printerClient 单例发送到打印服务静默打印。 * 打印机选择遵循「使用默认打印机」开关:开启时不弹框直接用选中打印机;关闭时同样不弹框 * (移动端无选择弹框,用户需先到设置页选好打印机)。 * * 兼容历史签名:位置参数 (code, params),data 可通过 params.data 传递 * * @returns true 成功 / false 失败(失败会 toast 提示) */ export async function mobileServerPrint(code: string, params: any = {}): Promise { const toast = showLoadingToast({message: '正在获取打印模板', forbidClick: true, duration: 0}); try { // 1. 兼容历史调用:从 params 取出 data(支持 params.data 嵌套传递) const data = params.data; const fetchParams = {...params}; delete fetchParams.data; // 2. 渲染 HTML(不传 onAlert,失败时 throw 由 catch 统一提示) const result = await fetchAndRender({code, params: fetchParams, data}); if (!result || !result.html) { closeToast(); showFailToast('打印内容渲染失败'); return false; } if (!result.hasData) { closeToast(); showFailToast('数据源没有数据,打印将取消'); return false; } // 2. 校验连接 if (printerClient.status.value !== 'connected') { closeToast(); showFailToast('打印服务未连接,请检查打印客户端是否运行'); return false; } // 3. 取选中打印机 const selectedPrinter = printerClient.selectedPrinter.value; if (!selectedPrinter) { closeToast(); showFailToast('未选择打印机,请到打印设置中选择'); return false; } // 4. 发送打印任务 toast.message = '正在发送打印任务'; const printRes = await printerClient.printHtml({ html: result.html, printerName: selectedPrinter, copies: 1, title: result.name, pageSize: result.pageSize, }); closeToast(); if (printRes?.success) { showSuccessToast('打印任务已发送'); return true; } showFailToast('打印失败: ' + (printRes?.message || '未知错误')); return false; } catch (error) { closeToast(); showFailToast((error as Error)?.message || '打印出错'); return false; } } export default mobileInstall;