UNPKG

ts-guoguo-mcp-server

Version:

裹裹寄件服务MCP Server - TypeScript版本,基于Model Context Protocol的寄件服务集成工具

84 lines 3.18 kB
import { HTTPClient } from "../utils/http-client.js"; import { logger } from "../utils/logger.js"; /** * 地址簿服务 */ export class AddressBookService { config; constructor(config) { this.config = config; } /** * 查询地址簿 */ async queryAddressBook(searchKey) { try { if (this.config.useMock) { return this.getMockAddressBook(searchKey); } const httpClient = new HTTPClient(this.config); const params = {}; if (searchKey) { params.search = searchKey; } const response = await httpClient.get("/mcp/api/v1/address-book", params); if (response.success) { const data = response.data || {}; return { status: "success", data: { total_count: data.totalCount || 0, addresses: (data.addresses || []).map((addr) => ({ name: addr.name, phone: addr.phone, address: addr.address, is_default: addr.default || false, })), }, }; } else { return { status: "error", error_code: response.errorCode || response.code || "API_ERROR", message: response.errorMsg || response.message || "API调用失败", }; } } catch (error) { logger.error(`地址簿查询失败: ${error}`); return { status: "error", error_code: "QUERY_ERROR", message: `查询失败: ${error instanceof Error ? error.message : String(error)}`, }; } } /** * 获取模拟地址簿数据 */ getMockAddressBook(searchKey) { logger.info("使用模拟数据进行地址簿查询"); const mockAddresses = [ { name: "张三", phone: "13800138001", address: "北京市朝阳区中关村大街1号", is_default: true }, { name: "李四", phone: "13800138002", address: "上海市浦东新区陆家嘴环路1000号" }, { name: "王五", phone: "13800138003", address: "广州市天河区珠江新城花城大道85号" }, { name: "赵六", phone: "13800138004", address: "深圳市南山区深南大道10000号" }, ]; let filteredAddresses = mockAddresses; if (searchKey) { const searchKeyLower = searchKey.toLowerCase(); filteredAddresses = mockAddresses.filter((addr) => addr.name.toLowerCase().includes(searchKeyLower) || addr.phone.includes(searchKey) || addr.address.toLowerCase().includes(searchKeyLower)); } return { status: "success", data: { total_count: filteredAddresses.length, addresses: filteredAddresses, }, }; } } //# sourceMappingURL=address-book.js.map