PrintBase
  • Product
  • Pricing
  • Docs
  • Resources
PrintBase/专题/平台打印接入

uni-app 如何自动打印小票

uni-app 客户端不要保存 API Key。订单由你的后端打印到门店小票机,支持各端共用同一套接口。调用 POST /v1/print-jobs,API Key 只放在服务端。

面向 用 uni-app 做多端下单、需要门店出票的团队

说明

uni-app 编译到小程序、H5 或 App 之后,前端运行环境都不应该持有打印密钥,也看不到门店 USB 打印机。各端只负责把已支付订单提交给你的后端。本产品没有 uni-app 插件。打印机由门店电脑上的 Agent 主动连出,不需要公网 IP,也不需要把 9100 端口映射到互联网。

后端按门店拼 ESC/POS,POST 到该门店的 printer_code。H5、微信和 App 可以共用这个接口。本地回环端口 127.0.0.1:17891 只接受 localhost 与 printbase.cloud 来源,不能当成 uni-app 页面的静默打印通道。

Idempotency-Key 使用订单主键。支付渠道的重复通知要复用同一 key,避免连打两张。

创建任务使用 POST /v1/print-jobs,请求头 Authorization: Bearer,正文包含 printer_code、content_type(pdf 或 raw)以及 content 或 content_url 二者之一。免费套餐是一台电脑每月 100 个任务。

创建任务使用 POST /v1/print-jobs,请求头 Authorization: Bearer,正文包含 printer_code、content_type(pdf 或 raw)以及 content 或 content_url 二者之一。免费套餐是一台电脑每月 100 个任务。

接入步骤

  1. 01

    前端只传订单号

    uni.request 打到你的业务 API。打印密钥不进客户端包。请求使用 POST /v1/print-jobs,请求头为 Authorization: Bearer,printer_code 来自 GET /v1/printers。

  2. 02

    后端按门店取打印机

    printer_code 存在门店表,不由前端随意指定,除非你另外做了权限校验。请求使用 POST /v1/print-jobs,请求头为 Authorization: Bearer,printer_code 来自 GET /v1/printers。

  3. 03

    生成 ESC/POS

    初始化、店名、菜品、金额、走纸、切纸。中文按打印机编码。请求使用 POST /v1/print-jobs,请求头为 Authorization: Bearer,printer_code 来自 GET /v1/printers。

  4. 04

    调用打印接口

    content_type raw,content 为 base64。请求使用 POST /v1/print-jobs,请求头为 Authorization: Bearer,printer_code 来自 GET /v1/printers。

  5. 05

    把状态返回给店员端

    店员使用的后台可以轮询任务。顾客确认页不必等待出纸完成。请求使用 POST /v1/print-jobs,请求头为 Authorization: Bearer,printer_code 来自 GET /v1/printers。

Node.js

// uni-app 的业务后端:门店小票
const res = await fetch('https://api.printbase.cloud/v1/print-jobs', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.PRINTBASE_API_KEY}`,
    'Content-Type': 'application/json',
    'Idempotency-Key': order.id,
  },
  body: JSON.stringify({
    printer_code: Number(process.env.PRINTER_CODE),
    content_type: 'raw',
    content: Buffer.from(receipt).toString('base64'),
    copies: 1,
  }),
});
if (!res.ok) throw new Error(`PrintBase ${res.status}: ${await res.text()}`);
const job = await res.json();
// { id: "job_abc", status: "queued" }

这一套的细节

多端同一后端

微信、H5、App 的差别停在下单接口。打印路径不随端改变,始终是服务端到 Agent。Agent 只打印你提交的内容,不会替你向平台拉取单据。Agent 只打印你提交的内容,不会替你向平台拉取单据。

怎样确认已经打出来

创建接口返回 id 和 status queued。之后是 queued、dispatched、printing、completed。失败原因包括 AGENT_OFFLINE、PRINTER_OFFLINE、DOWNLOAD_FAILED、PRINT_ERROR、INVALID_CONTENT。用 GET /v1/print-jobs/{id} 查询。Webhook 只在付费套餐提供,且只有 print_job.completed 和 print_job.failed。

常见问题

能不能在 App 里用原生插件直连蓝牙?

可以是另一条路。这条路解决的是云端订单打到门店已安装的打印机,不要求顾客手机靠近打印机。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

有 uni-app 插件市场的 PrintBase 插件吗?

没有。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

H5 在门店平板上打开,能打本机吗?

公网 H5 源站不能调用 127.0.0.1:17891。仍由后端调用云端 API,Agent 在平板或旁边的电脑上。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

如何避免重复出票?(详见本页)

支付回调和前端确认使用同一个订单号作为 Idempotency-Key。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

相关页面

  • 微信小程序如何自动打印小票

    小程序不能直接驱动店里的 USB 小票机。订单落到你的云函数或服务器后,再调用打印接口。没有官方小程序插件。调用 POST /v1/print-jobs,API Key 只放在服务端。

  • uni-app 后端如何打印小票

    uni-app 各端只提交订单。小票由后端打印。不要在 uni.request 里携带打印密钥。调用 POST /v1/print-jobs,API Key 只放在服务端。

  • 芯烨 XP-58IIH 远程打印 ESC/POS 小票

    XP-58IIH 是常见的 58 毫米 USB 小票机。用 ESC/POS raw 发送,中文 GBK,切纸前走纸。调用 POST /v1/print-jobs,API Key 只放在服务端。

  • 门店收银不买云打印机也能打小票

    收银电脑上已经有的小票机,装上 Agent 就能接收服务器发来的小票。不必换成带 SN 的云打印机。调用 POST /v1/print-jobs,API Key 只放在服务端。

免费套餐:一台电脑每月 100 个任务,不需要信用卡。

免费注册查看 API 文档
PrintBase

PrintBase connects your applications to physical printers anywhere - securely, reliably, and in real time.

Product
Receipt PrintingLabel PrintingDashboardDevicesWebhooks
Resources
DocsBlogStatusAPI ReferenceDownloadPrintBase vs PrintNodePrintNode PricingFAQ
Guides
All guidesIntegrationsPrintersCode samplesComparisonsUse casesTroubleshooting
Company
ContactSecurityPrivacy

© 2026 Aveniq Technologies OÜ. All rights reserved. PrintBase is a service of Aveniq Technologies OÜ.

PrivacyTermsRefunds