# API

#### 签名算法 <a href="#qian-ming-suan-fa" id="qian-ming-suan-fa"></a>

```php
    /**
     * @param array $data
     * @param string $secret
     * @return string
     */
    public static function generateSignature(array $data, string $secret): string
    {
        unset($data['sign']);
        ksort($data);
        foreach ($data as $key => $val) {
            if ($val === '' || is_array($val)) {
                unset($data[$key]);
            }
        }
        return md5(urldecode(http_build_query($data) . "&key=" . (string)$secret));
    }
```

#### 公共请求头 <a href="#gong-gong-qing-qiu-tou" id="gong-gong-qing-qiu-tou"></a>

| 参数名称          | 必选 | 类型     | 说明                                                                        |
| ------------- | -- | ------ | ------------------------------------------------------------------------- |
| Api-Id        | 是  | string | 会员ID，在会员中心，我的资料中可以获得                                                      |
| Api-Signature | 是  | string | 数据签名，将POST所有参数，通过上方的签名算法进行签名，然后可以得到此签名字符串，其中`$secret`数，就是API密钥，也是在我的资料中获得 |

#### 获取全部货源列表 <a href="#huo-qu-quan-bu-huo-yuan-lie-biao" id="huo-qu-quan-bu-huo-yuan-lie-biao"></a>

`POST /plugin/open-api/items`

#### 单独获取某个商货源信息 <a href="#dan-du-huo-qu-mou-ge-shang-huo-yuan-xin-xi" id="dan-du-huo-qu-mou-ge-shang-huo-yuan-xin-xi"></a>

`POST /plugin/open-api/item`

* Body参数

| 参数名称 | 必选 | 类型  | 说明   |
| ---- | -- | --- | ---- |
| id   | 是  | int | 货源ID |

#### 获取SKU库存数量 <a href="#huo-qu-sku-ku-cun-shu-liang" id="huo-qu-sku-ku-cun-shu-liang"></a>

`POST /plugin/open-api/sku/stock`

* Body参数

| 参数名称    | 必选 | 类型  | 说明         |
| ------- | -- | --- | ---------- |
| sku\_id | 是  | int | 货源下的SKU ID |

#### 获取SKU当前状态，判断当前SKU是否可以进行购买 <a href="#huo-qu-sku-dang-qian-zhuang-tai-pan-duan-dang-qian-sku-shi-fou-ke-yi-jin-xing-gou-mai" id="huo-qu-sku-dang-qian-zhuang-tai-pan-duan-dang-qian-sku-shi-fou-ke-yi-jin-xing-gou-mai"></a>

`POST /plugin/open-api/sku/state`

* Body参数

| 参数名称     | 必选 | 类型  | 说明         |
| -------- | -- | --- | ---------- |
| sku\_id  | 是  | int | 货源下的SKU ID |
| quantity | 是  | int | 需购买的数量     |

#### 下单（进货） <a href="#xia-dan-jin-huo" id="xia-dan-jin-huo"></a>

`POST /plugin/open-api/trade`

* Body参数

| 参数名称       | 必选 | 类型     | 说明              |
| ---------- | -- | ------ | --------------- |
| sku\_id    | 是  | int    | 货源下的SKU ID      |
| quantity   | 是  | int    | 需购买的数量          |
| trade\_no  | 是  | string | 24位数字订单号，必须是24位 |
| async\_url | 否  | string | 异步通知地址          |

值得注意的是，拉取货源时，需要在意控件，控件的`name`，就是参数名称，一起提交到此接口即可

#### 订单查询 <a href="#ding-dan-cha-xun" id="ding-dan-cha-xun"></a>

`POST /plugin/open-api/query`

* Body参数

| 参数名称      | 必选 | 类型     | 说明              |
| --------- | -- | ------ | --------------- |
| trade\_no | 是  | string | 24位数字订单号，必须是24位 |

#### 异步通知 <a href="#yi-bu-tong-zhi" id="yi-bu-tong-zhi"></a>

> 通过下单接口传递的`async_url`异步通知地址，当订单状态发生变化，会通知此地址

**请求头**

| 参数名称          | 类型     | 说明                           |
| ------------- | ------ | ---------------------------- |
| Api-Signature | string | 在进行异步通知时，会将签名参数携带到`header`头中 |

**POST数据**

| 参数名称            | 类型     | 说明                                |
| --------------- | ------ | --------------------------------- |
| status          | int    | 订单状态：状态：0=等待发货，1=已发货，2=发货失败，3=已退款 |
| contents        | string | 发货内容                              |
| item\_trade\_no | string | 三方订单号，也就是你自己平台的订单号                |
| trade\_no       | string | 系统订单号，上游平台订单号                     |
| amount          | string | 金额                                |
| trade\_time     | string | 交易时间                              |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.tcmenu.cc/wiki/api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
