# ERP 打印标签对接说明（给应用端）

> 可直接交给应用端开发 / AI 实现。  
> 商品信息在字段 **`product_name`** 中，格式见下文。

---

## 1. 业务目标

1. ERP 销售单点击「打印标签」。
2. ERP 写入当前这一单的待打印数据（按账户 `aid` 隔离，只保留最新一条）。
3. 打印软件轮询绑定地址，拉到数据后打出 **一张** 标签。
4. 一张标签可含多个商品；每个商品一行。

---

## 2. 应用端必做

| 项 | 要求 |
|---|---|
| 绑定地址 | 从 ERP「系统配置 → API配置」复制完整 URL |
| 地址形态 | `{ERP域名}/PrintLabel/index?aid={账户aid}`，**必须带 aid** |
| 请求 | 仅 `GET`；无 Token；不带业务参数 |
| 轮询 | 建议 1～3 秒一次 |
| 有任务 | `state === "success"` 且 `info !== null` → 解析并打印一张 |
| 无任务 | `info === null` → 忽略 |
| 清空 | 服务端返回 `info` 后清空；同一条不会重复给 |

示例：

```http
GET http://pyerp.local/PrintLabel/index?aid=12
```

---

## 3. 拉取响应

### 有任务

```json
{
  "state": "success",
  "info": {
    "customer_name": "张三",
    "customer_phone": "13800138000",
    "customer_address": "某某路1号",
    "product_name": "足金,戒指,9.140\n足金,戒指,9.140",
    "ts": 1721568000
  }
}
```

### 无任务

```json
{
  "state": "success",
  "info": null
}
```

### 缺少 aid

```json
{
  "state": "error",
  "info": "缺少账户标识aid"
}
```

### 字段

| 字段 | 类型 | 说明 |
|---|---|---|
| `customer_name` | string | 客户名称 |
| `customer_phone` | string | 客户电话 |
| `customer_address` | string | 客户地址 |
| `product_name` | string | 商品明细文本（见第 4 节） |
| `ts` | number | 提交时间戳（可选） |

---

## 4. `product_name` 格式规则（核心）

### 4.1 总体

- 多个商品用换行 `\n` 分隔，**一行一个商品**。
- 单行固定 **3 段**，用英文逗号 `,` 分隔：

```text
材质,品类,克重
```

完整示例（两个商品）：

```text
足金,戒指,9.140
足金,戒指,9.140
```

对应 JSON 字符串：

```json
"足金,戒指,9.140\n足金,戒指,9.140"
```

### 4.2 单行三段含义

| 段序号（从 0） | 含义 | 示例 |
|---|---|---|
| 0 | 材质 | `足金` |
| 1 | 品类 | `戒指` |
| 2 | 克重 | `9.140` |

### 4.3 解析算法（应用端按此实现）

```text
function parseProducts(product_name):
  if product_name is null or product_name == "":
    return []

  text = product_name.replace("\r\n", "\n").replace("\r", "\n")
  lines = text.split("\n")
  products = []

  for line in lines:
    line = line.trim()
    if line == "":
      continue
    parts = line.split(",")
    caizhi = parts[0] if len(parts) > 0 else ""
    guige  = parts[1] if len(parts) > 1 else ""
    weight = parts[2] if len(parts) > 2 else ""
    if len(parts) > 3:
      weight = join(parts[2:], ",")   // 克重里万一含逗号的兜底

    products.append({
      caizhi: caizhi.trim(),
      guige: guige.trim(),
      weight: weight.trim()
    })

  return products
```

### 4.4 禁止事项

1. **不要**把整段 `product_name` 只按逗号拆成商品列表（商品分隔是换行，不是逗号）。
2. **不要**使用中文顿号 `、` 作为分隔符（协议是英文逗号 `,`）。
3. 单商品时通常没有 `\n`，整串仍按一行三段解析。

### 4.5 单商品示例

```json
"product_name": "足金,素圈,29.140"
```

解析结果仅 1 条：`{ caizhi: "足金", guige: "素圈", weight: "29.140" }`。

---

## 5. 打印规则

1. `info !== null` 时打印 **一张** 标签。
2. 客户区用：`customer_name` / `customer_phone` / `customer_address`。
3. 商品区：对 `parseProducts(info.product_name)` 的每一行展示材质、品类、克重。
4. 某段为空则显示空白，不要报错中断。
5. 同一 `info` 只打一次（服务端已清空）。

标签内容示意：

```text
客户：张三
电话：13800138000
地址：某某路1号

商品：
1. 足金  戒指  9.140
2. 足金  戒指  9.140
```

---

## 6. 轮询伪代码

```text
loop every 1..3 seconds:
  res = GET boundUrl   // 已含 ?aid=
  if res.state != "success":
    continue
  if res.info == null:
    continue

  products = parseProducts(res.info.product_name)
  printOneLabel(
    customer_name = res.info.customer_name,
    customer_phone = res.info.customer_phone,
    customer_address = res.info.customer_address,
    products = products
  )
```

---

## 7. 多账户绑定

1. 不同 ERP 账户 `aid` 不同，复制到的 API 地址不同。
2. 同一台电脑可绑多套打印配置，互不混用。
3. 换账户后需重新复制地址绑定。

---

## 8. ERP 写入（应用端无需实现）

ERP 登录后提交：

```http
GET /PrintLabel/index?customer_name=...&customer_phone=...&customer_address=...&product_name=足金,戒指,9.140%0A足金,戒指,9.140
```

`product_name` 由 ERP 按「材质,品类,克重」+ 换行规则生成。应用端只做第 2～6 节拉取与打印即可。
