Vue 后台经后端静默打印标签
Vue 按钮调用你的后端,后端再调用打印接口。不要在 Vue 工程里放 API Key。与英文 Vue 页对应。调用 POST /v1/print-jobs,API Key 只放在服务端。
面向 仓库或门店使用的 Vue 管理后台
其他语言版本: English
说明
Vue 的点击事件只把记录 id 发给你的同源接口。PrintBase 的密钥留在那个接口的服务器上。浏览器直接调用会把密钥暴露给每一个打开后台的人。打印机由门店电脑上的 Agent 主动连出,不需要公网 IP,也不需要把 9100 端口映射到互联网。
工位下拉框的数据也由你的后端转发 GET /v1/printers,或者干脆只返回该用户可用的 printer_code。
这是英文 vue-admin-labels 的中文页。
创建任务使用 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 个任务。
接入步骤
- 01
密钥放在服务端
使用 pb_live_ 开头的 API Key,请求头 Authorization: Bearer。不要放进前端包或手机 App。
- 02
打印机旁安装 Agent
支持 Windows、macOS、Linux。GET /v1/printers 里的 code 就是 printer_code。
- 03
调用 POST /v1/print-jobs
content_type 只能是 pdf 或 raw。content 和 content_url 只能有一个。请求使用 POST /v1/print-jobs,请求头为 Authorization: Bearer,printer_code 来自 GET /v1/printers。
- 04
重试用同一个幂等键
换新键会再打一张。同一个键配不同 body 会返回 409。请求使用 POST /v1/print-jobs,请求头为 Authorization: Bearer,printer_code 来自 GET /v1/printers。
- 05
查询任务再告诉用户
GET /v1/print-jobs/{id}。付费套餐才有 print_job.completed 和 print_job.failed 的 Webhook。
Node.js
// Vue 后台调用的服务端接口
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': rowId,
},
body: JSON.stringify({
printer_code: Number(process.env.PRINTER_CODE),
content_type: 'raw',
content: Buffer.from(zpl,'utf8').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" }
这一套的细节
同源接口
浏览器不直接访问 api.printbase.cloud。Agent 只打印你提交的内容,不会替你向平台拉取单据。Agent 只打印你提交的内容,不会替你向平台拉取单据。
按钮防重复
服务端用记录 id 做幂等键,前端禁用只是体验。Agent 只打印你提交的内容,不会替你向平台拉取单据。Agent 只打印你提交的内容,不会替你向平台拉取单据。
常见问题
可以在 axios 里加 Bearer 吗?
不要加 PrintBase 的密钥。axios 只访问你的后端。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。
Nuxt 呢?(详见本页)
放在服务端路由。页面仍然不持有密钥。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。
本地 17891 端口呢?
公网后台的源站不在允许列表里。不要让页面去访问 127.0.0.1。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。
补打?(详见本页)(详见本页)
后端对补打使用新的 Idempotency-Key。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。
免费套餐:一台电脑每月 100 个任务,不需要信用卡。