来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
175 lines
5.4 KiB
TypeScript
175 lines
5.4 KiB
TypeScript
import { buildApiUrl } from '../lib/apiConfig';
|
|
import { readAuthSessionRaw } from '../lib/authSession';
|
|
import { getOperatingBranchHeader } from './operatingBranch';
|
|
import { invalidateCategoryRowsCache } from './categories';
|
|
|
|
function getAuthHeaders(): Record<string, string> {
|
|
const headers: Record<string, string> = {
|
|
'Content-Type': 'application/json',
|
|
...getOperatingBranchHeader(),
|
|
};
|
|
try {
|
|
const raw = readAuthSessionRaw();
|
|
if (raw) {
|
|
const session = JSON.parse(raw);
|
|
if (session?.access_token) {
|
|
headers.Authorization = `Bearer ${session.access_token}`;
|
|
}
|
|
}
|
|
} catch {
|
|
// ignore
|
|
}
|
|
return headers;
|
|
}
|
|
|
|
export type TaxCodeSource =
|
|
| 'rule_exact'
|
|
| 'rule_keyword'
|
|
| 'rule_category'
|
|
| 'official_search'
|
|
| 'official_api'
|
|
| 'ai';
|
|
|
|
export type TaxCodeCandidate = {
|
|
tax_code: string;
|
|
tax_name: string;
|
|
short_name: string;
|
|
score: number;
|
|
source: TaxCodeSource;
|
|
reason: string;
|
|
};
|
|
|
|
export type TaxCodeSuggestion = {
|
|
matched: boolean;
|
|
tax_code: string | null;
|
|
tax_name: string | null;
|
|
short_name: string | null;
|
|
source: TaxCodeSource | null;
|
|
confidence: number;
|
|
reason: string | null;
|
|
catalog_version: string | null;
|
|
needs_confirmation: boolean;
|
|
candidates: TaxCodeCandidate[];
|
|
};
|
|
|
|
export type TaxCatalogSearchItem = {
|
|
tax_code: string;
|
|
tax_name: string;
|
|
short_name: string;
|
|
score: number;
|
|
};
|
|
|
|
const TAX_SOURCE_LABELS: Record<TaxCodeSource, string> = {
|
|
rule_exact: '规则引擎',
|
|
rule_keyword: '规则引擎',
|
|
rule_category: '规则引擎',
|
|
official_search: '官方库',
|
|
official_api: '官方库',
|
|
ai: 'AI',
|
|
};
|
|
|
|
export function getTaxCodeSourceLabel(source: TaxCodeSource | null | undefined) {
|
|
if (!source) return '未匹配';
|
|
return TAX_SOURCE_LABELS[source] || source;
|
|
}
|
|
|
|
/** 分层确认:规则引擎 + 实时官方库 + AI */
|
|
export async function suggestTaxCodeApi(input: {
|
|
skuName?: string;
|
|
categoryName?: string;
|
|
spec?: string;
|
|
useAi?: boolean;
|
|
}): Promise<TaxCodeSuggestion> {
|
|
const query = new URLSearchParams();
|
|
if (input.skuName?.trim()) query.set('sku_name', input.skuName.trim());
|
|
if (input.categoryName?.trim()) query.set('category_name', input.categoryName.trim());
|
|
if (input.spec?.trim()) query.set('spec', input.spec.trim());
|
|
if (input.useAi === false) query.set('use_ai', 'false');
|
|
|
|
const response = await fetch(buildApiUrl(`/catalog/tax-code/suggest?${query.toString()}`), {
|
|
headers: getAuthHeaders(),
|
|
});
|
|
const payload = await response.json().catch(() => ({}));
|
|
if (!response.ok) {
|
|
throw new Error(payload?.error?.message || '查询税收编码失败');
|
|
}
|
|
return payload.data as TaxCodeSuggestion;
|
|
}
|
|
|
|
/** 实时检索官方税收分类库 */
|
|
export async function searchTaxCatalogApi(q: string, limit = 15): Promise<TaxCatalogSearchItem[]> {
|
|
const query = new URLSearchParams();
|
|
query.set('q', q.trim());
|
|
query.set('limit', String(limit));
|
|
|
|
const response = await fetch(buildApiUrl(`/catalog/tax-code/search?${query.toString()}`), {
|
|
headers: getAuthHeaders(),
|
|
});
|
|
const payload = await response.json().catch(() => ({}));
|
|
if (!response.ok) {
|
|
throw new Error(payload?.error?.message || '检索官方库失败');
|
|
}
|
|
return (payload.data?.results || []) as TaxCatalogSearchItem[];
|
|
}
|
|
|
|
/** 事务删除分类(含解除 order_items / skus 等外键引用) */
|
|
export async function deleteCatalogCategoriesApi(categoryCodes: string[]): Promise<void> {
|
|
const response = await fetch(buildApiUrl('/catalog/categories/delete'), {
|
|
method: 'POST',
|
|
headers: getAuthHeaders(),
|
|
body: JSON.stringify({ codes: categoryCodes }),
|
|
});
|
|
const payload = await response.json().catch(() => ({}));
|
|
if (!response.ok) {
|
|
throw new Error(payload?.error?.message || '删除分类失败');
|
|
}
|
|
invalidateCategoryRowsCache();
|
|
}
|
|
|
|
/** 当前分公司下无 catalog 的孤儿 skus(列表合并展示用) */
|
|
export async function fetchOrphanCatalogProductsApi(): Promise<
|
|
Array<{
|
|
sku_code: string;
|
|
sku_name: string;
|
|
category_code?: string | null;
|
|
unit?: string | null;
|
|
spec?: string | null;
|
|
barcode?: string | null;
|
|
image_url?: string | null;
|
|
default_loss_threshold?: number | null;
|
|
is_prepackaged?: boolean | null;
|
|
brand?: string | null;
|
|
tax_code?: string | null;
|
|
tax_rate?: number | null;
|
|
origin?: string | null;
|
|
unit_weight?: number | null;
|
|
status?: number | null;
|
|
created_at?: string | null;
|
|
branch_code?: string | null;
|
|
}>
|
|
> {
|
|
const response = await fetch(buildApiUrl('/catalog/products/orphans'), {
|
|
method: 'GET',
|
|
headers: getAuthHeaders(),
|
|
});
|
|
const payload = await response.json().catch(() => ({}));
|
|
if (!response.ok) {
|
|
throw new Error(payload?.error?.message || '查询孤儿商品失败');
|
|
}
|
|
return Array.isArray(payload?.data?.items) ? payload.data.items : [];
|
|
}
|
|
|
|
/** 真删除商品(无业务引用时);有引用则 409;仅 skus 残留亦可删 */
|
|
export async function deleteCatalogProductsApi(productCodes: string[]): Promise<{ deleted: number }> {
|
|
const response = await fetch(buildApiUrl('/catalog/products/delete'), {
|
|
method: 'POST',
|
|
headers: getAuthHeaders(),
|
|
body: JSON.stringify({ codes: productCodes }),
|
|
});
|
|
const payload = await response.json().catch(() => ({}));
|
|
if (!response.ok) {
|
|
throw new Error(payload?.error?.message || '删除商品失败');
|
|
}
|
|
return { deleted: Number(payload?.data?.deleted) || productCodes.length };
|
|
}
|