145 lines
5.5 KiB
PHP
145 lines
5.5 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
require __DIR__ . '/vendor/autoload.php';
|
||
|
||
use GuzzleHttp\Client;
|
||
use Mcp\Server;
|
||
use Mcp\Server\Transport\StdioTransport;
|
||
|
||
$token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwd2QiOiI2ODI5N2FjYzUwZDczYWQzZmNmMTNlM2Y4MWYxMWQyZSIsImlzcyI6InRwb2ludC5hZ3JpbWVkaWEuY24iLCJhdWQiOiJ0cG9pbnQuYWdyaW1lZGlhLmNuIiwiaWF0IjoxNzgwODk3Mjk3LCJuYmYiOjE3ODA4OTcyOTcsImV4cCI6MTc4MzQwMjg5NywianRpIjp7ImlkIjo0LCJ0eXBlIjoiYWRtaW4ifX0.G2yB38bYkf0b37kpAbQViDuXY4QHeRjm99WNXjSh4L0';
|
||
|
||
$server = Server::builder()
|
||
->setServerInfo('Merchant MCP', '1.0.0')
|
||
->addTool(
|
||
handler: function (
|
||
?string $date = null,
|
||
?int $status = null,
|
||
?string $keyword = null,
|
||
?string $fieldKey = null,
|
||
?string $payType = null,
|
||
?string $type = null,
|
||
?int $btcId = null,
|
||
) use ($token): array {
|
||
$client = new Client([
|
||
'timeout' => 10,
|
||
]);
|
||
|
||
// page、limit 由 MCP 内部固定,不开放给 AI 配置
|
||
$query = [
|
||
'status' => $status ?? -1,
|
||
'field_key' => $fieldKey ?? 'all',
|
||
'page' => 1,
|
||
'limit' => 100,
|
||
'time' => $date ?? date('Y/m/d') . '-' . date('Y/m/d'),
|
||
];
|
||
|
||
// 仅在传入非空值时追加可选筛选条件
|
||
// 注意:API 搜索关键词参数是 real_name,不是 data
|
||
if ($keyword !== null && $keyword !== '') {
|
||
$query['real_name'] = $keyword;
|
||
}
|
||
if ($payType !== null && $payType !== '') {
|
||
$query['pay_type'] = $payType;
|
||
}
|
||
if ($type !== null && $type !== '') {
|
||
$query['type'] = $type;
|
||
}
|
||
if ($btcId !== null && $btcId > 0) {
|
||
$query['btc_id'] = $btcId;
|
||
}
|
||
|
||
$response = $client->get(
|
||
'https://tpoint.agrimedia.cn/adminapi/offline/list',
|
||
[
|
||
'headers' => [
|
||
'accept' => 'application/json',
|
||
'authori-zation' => 'Bearer ' . $token,
|
||
],
|
||
'query' => $query,
|
||
]
|
||
);
|
||
|
||
$data = json_decode(
|
||
(string) $response->getBody(),
|
||
true
|
||
);
|
||
|
||
$orders = $data['data']['list'] ?? [];
|
||
|
||
$activeMerchants = [];
|
||
$successOrderCount = 0;
|
||
$totalOrderAmount = 0.0;
|
||
$totalRealPay = 0.0;
|
||
|
||
foreach ($orders as $order) {
|
||
$merchantId = (int) ($order['merchant_id'] ?? 0);
|
||
|
||
if ($merchantId > 0) {
|
||
$activeMerchants[$merchantId] = true;
|
||
}
|
||
|
||
/*
|
||
* 这里暂时按 status = 1 作为成功订单。
|
||
* 后面再确认你项目的真实状态含义。
|
||
*/
|
||
if ((int) ($order['status'] ?? -1) === 1) {
|
||
$successOrderCount++;
|
||
}
|
||
|
||
$totalOrderAmount += (float) ($order['order_price'] ?? 0);
|
||
$totalRealPay += (float) ($order['real_pay'] ?? 0);
|
||
}
|
||
|
||
return [
|
||
'orderCount' => count($orders),
|
||
'activeMerchantCount' => count($activeMerchants),
|
||
'successOrderCount' => $successOrderCount,
|
||
'totalOrderAmount' => round($totalOrderAmount, 2),
|
||
'totalRealPay' => round($totalRealPay, 2),
|
||
// 调试信息:实际发送给 API 的查询参数
|
||
'_debug_query' => $query,
|
||
];
|
||
},
|
||
name: 'get_order_summary',
|
||
description: '获取订单总数、活跃商户数、成功订单数和金额统计。可按日期、状态、关键词、搜索字段、支付方式、订单类型、商户分类ID筛选。',
|
||
inputSchema: [
|
||
'type' => 'object',
|
||
'properties' => [
|
||
'date' => [
|
||
'type' => 'string',
|
||
'description' => '查询日期,格式如 2026/06/23-2026/06/23(起止日期用 - 连接)。不传默认当天。',
|
||
],
|
||
'status' => [
|
||
'type' => 'integer',
|
||
'description' => '订单状态,-1 表示全部。不传默认 -1。',
|
||
],
|
||
'fieldKey' => [
|
||
'type' => 'string',
|
||
'description' => '搜索字段,对应接口的 field_key 参数。常用值:order_sn(按订单号搜索)、all(全部字段)。不传默认 all。注意:当 keyword 有值时,建议同时指定 fieldKey 以确保搜索生效。',
|
||
],
|
||
'keyword' => [
|
||
'type' => 'string',
|
||
'description' => '搜索关键词,对应接口的 data 参数。需配合 fieldKey 一起使用。',
|
||
],
|
||
'payType' => [
|
||
'type' => 'string',
|
||
'description' => '支付方式,对应接口的 pay_type 参数。',
|
||
],
|
||
'type' => [
|
||
'type' => 'string',
|
||
'description' => '订单类型,对应接口的 type 参数。',
|
||
],
|
||
'btcId' => [
|
||
'type' => 'integer',
|
||
'description' => '商户分类ID,对应接口的 btc_id 参数。',
|
||
],
|
||
],
|
||
],
|
||
)
|
||
->build();
|
||
|
||
$transport = new StdioTransport();
|
||
|
||
$server->run($transport); |