来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
112 lines
3.2 KiB
TypeScript
112 lines
3.2 KiB
TypeScript
import { useState } from 'react';
|
|
import {
|
|
createQuickCatalogCategory,
|
|
updateProductCatalogCategory,
|
|
} from '../utils/productCatalog';
|
|
import { getCategoryDisplayLabel, type CategoryRow } from '../utils/categories';
|
|
|
|
export interface QuickCategoryModalState {
|
|
showModal: boolean;
|
|
targetItemId: string | null;
|
|
categoryName: string;
|
|
error: string;
|
|
isCreating: boolean;
|
|
}
|
|
|
|
export interface UseQuickCategoryModalOptions {
|
|
categoryRows: CategoryRow[];
|
|
/** 当前经营/账号分公司;普通分公司账号创建分类必须传入 */
|
|
branchCode?: string | null;
|
|
/** 获取分类标签的函数,可在创建分类后正确回显 */
|
|
getCategoryLabel?: (code: string, name?: string) => string;
|
|
/** 分类创建成功后回调,用于刷新 */
|
|
onCategoryCreated: () => Promise<void>; // 通常是 refetch
|
|
}
|
|
|
|
export function useQuickCategoryModal(options: UseQuickCategoryModalOptions) {
|
|
const { categoryRows, branchCode, getCategoryLabel, onCategoryCreated } = options;
|
|
const [state, setState] = useState<QuickCategoryModalState>({
|
|
showModal: false,
|
|
targetItemId: null,
|
|
categoryName: '',
|
|
error: '',
|
|
isCreating: false,
|
|
});
|
|
|
|
const openModal = (itemId: string) => {
|
|
setState({
|
|
showModal: true,
|
|
targetItemId: itemId,
|
|
categoryName: '',
|
|
error: '',
|
|
isCreating: false,
|
|
});
|
|
};
|
|
|
|
const setCategoryName = (name: string) => {
|
|
setState((prev) => ({ ...prev, categoryName: name }));
|
|
};
|
|
|
|
const closeModal = () => {
|
|
setState((prev) => ({
|
|
...prev,
|
|
showModal: false,
|
|
categoryName: '',
|
|
error: '',
|
|
}));
|
|
};
|
|
|
|
/**
|
|
* 执行创建分类。
|
|
* @returns 创建成功后的 { categoryCode, categoryName } 或 null
|
|
*/
|
|
const createCategory = async (): Promise<{ categoryCode: string; categoryName: string } | null> => {
|
|
const name = state.categoryName.trim();
|
|
if (!name) {
|
|
setState((prev) => ({ ...prev, error: '请输入分类名称' }));
|
|
return null;
|
|
}
|
|
|
|
setState((prev) => ({ ...prev, isCreating: true, error: '' }));
|
|
try {
|
|
const { code: categoryCode, name: categoryName } = await createQuickCatalogCategory(
|
|
name,
|
|
categoryRows.length + 1,
|
|
branchCode
|
|
);
|
|
|
|
await onCategoryCreated();
|
|
|
|
const label = getCategoryLabel
|
|
? getCategoryLabel(categoryCode, categoryName)
|
|
: getCategoryDisplayLabel(categoryRows, categoryCode);
|
|
|
|
setState((prev) => ({ ...prev, showModal: false, categoryName: '', error: '' }));
|
|
return { categoryCode, categoryName: label || categoryName };
|
|
} catch (error: any) {
|
|
setState((prev) => ({
|
|
...prev,
|
|
error: error?.message || '创建分类失败',
|
|
}));
|
|
return null;
|
|
} finally {
|
|
setState((prev) => ({ ...prev, isCreating: false }));
|
|
}
|
|
};
|
|
|
|
return {
|
|
...state,
|
|
openModal,
|
|
setCategoryName,
|
|
closeModal,
|
|
createCategory,
|
|
};
|
|
}
|
|
|
|
/** 同步商品 skuCode → 分类映射到 product_catalog */
|
|
export function syncProductCategoryToCatalog(skuCode: string, categoryCode: string) {
|
|
updateProductCatalogCategory(skuCode, categoryCode).catch((error) => {
|
|
console.warn('同步商品分类到目录失败:', error);
|
|
});
|
|
}
|