update pay function

This commit is contained in:
2025-11-20 12:55:28 +08:00
parent 082e644534
commit 80676dd622
11 changed files with 1395 additions and 2166 deletions

View File

@@ -0,0 +1,192 @@
/**
* 商品分类树数据服务
* 对接化工商品数据分类树API
* API文档: category_tree_openapi.json
*/
import { getApiBase } from '@utils/apiConfig';
// 类型定义
export interface TreeMetric {
metric_id: string;
metric_name: string;
source: 'SMM' | 'Mysteel';
frequency: string;
unit: string;
description?: string;
}
export interface TreeNode {
name: string;
path: string;
level: number;
children?: TreeNode[];
metrics?: TreeMetric[];
}
export interface CategoryTreeResponse {
source: 'SMM' | 'Mysteel';
total_metrics: number;
tree: TreeNode[];
}
export interface ErrorResponse {
detail: string;
}
/**
* 获取完整分类树
* @param source 数据源类型 ('SMM' | 'Mysteel')
* @returns 完整的分类树数据
*/
export const fetchCategoryTree = async (
source: 'SMM' | 'Mysteel'
): Promise<CategoryTreeResponse> => {
try {
const response = await fetch(`/category-api/api/category-tree?source=${source}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (!response.ok) {
const errorData: ErrorResponse = await response.json();
throw new Error(errorData.detail || `HTTP ${response.status}`);
}
const data: CategoryTreeResponse = await response.json();
return data;
} catch (error) {
console.error('fetchCategoryTree error:', error);
throw error;
}
};
/**
* 获取特定节点及其子树
* @param path 节点完整路径(用 | 分隔)
* @param source 数据源类型 ('SMM' | 'Mysteel')
* @returns 节点数据及其子树
*/
export const fetchCategoryNode = async (
path: string,
source: 'SMM' | 'Mysteel'
): Promise<TreeNode> => {
try {
const encodedPath = encodeURIComponent(path);
const response = await fetch(
`/category-api/api/category-tree/node?path=${encodedPath}&source=${source}`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
}
);
if (!response.ok) {
const errorData: ErrorResponse = await response.json();
throw new Error(errorData.detail || `HTTP ${response.status}`);
}
const data: TreeNode = await response.json();
return data;
} catch (error) {
console.error('fetchCategoryNode error:', error);
throw error;
}
};
/**
* 搜索指标
* @param query 搜索关键词
* @param source 数据源类型 ('SMM' | 'Mysteel')
* @returns 匹配的指标列表
*/
export const searchMetrics = async (
query: string,
source: 'SMM' | 'Mysteel'
): Promise<TreeMetric[]> => {
try {
// 注意:这个接口可能需要后端额外实现
// 如果后端没有提供搜索接口,可以在前端基于完整树进行过滤
const response = await fetch(
`/category-api/api/metrics/search?query=${encodeURIComponent(query)}&source=${source}`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
}
);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const data: TreeMetric[] = await response.json();
return data;
} catch (error) {
console.error('searchMetrics error:', error);
throw error;
}
};
/**
* 从树中提取所有指标(用于前端搜索)
* @param nodes 树节点数组
* @returns 所有指标的扁平化数组
*/
export const extractAllMetrics = (nodes: TreeNode[]): TreeMetric[] => {
const metrics: TreeMetric[] = [];
const traverse = (node: TreeNode) => {
if (node.metrics && node.metrics.length > 0) {
metrics.push(...node.metrics);
}
if (node.children && node.children.length > 0) {
node.children.forEach(traverse);
}
};
nodes.forEach(traverse);
return metrics;
};
/**
* 在树中查找节点
* @param nodes 树节点数组
* @param path 节点路径
* @returns 找到的节点或 null
*/
export const findNodeByPath = (nodes: TreeNode[], path: string): TreeNode | null => {
for (const node of nodes) {
if (node.path === path) {
return node;
}
if (node.children) {
const found = findNodeByPath(node.children, path);
if (found) {
return found;
}
}
}
return null;
};
/**
* 获取节点的所有父节点路径
* @param path 节点路径(用 | 分隔)
* @returns 父节点路径数组
*/
export const getParentPaths = (path: string): string[] => {
const parts = path.split('|');
const parentPaths: string[] = [];
for (let i = 1; i < parts.length; i++) {
parentPaths.push(parts.slice(0, i).join('|'));
}
return parentPaths;
};