feat(customer): 优化客户状态筛选和统计功能
- 新增状态映射函数,实现数字状态和字符串状态的互相转换 - 在 API 层面实现状态筛选,提高数据处理效率 - 重构客户数据获取逻辑,支持按状态筛选 - 优化状态统计功能,使用异步请求并行获取各状态数量 - 调整前端渲染逻辑,适应新的数据处理方式
This commit is contained in:
@@ -8,7 +8,9 @@ import {
|
||||
CustomerStatus,
|
||||
getStatusText,
|
||||
getStatusTagType,
|
||||
getStatusOptions
|
||||
getStatusOptions,
|
||||
mapApplyStatusToCustomerStatus,
|
||||
mapCustomerStatusToApplyStatus
|
||||
} from '@/utils/customerStatus';
|
||||
import FixedButton from "@/components/FixedButton";
|
||||
import navTo from "@/utils/common";
|
||||
@@ -28,52 +30,39 @@ const CustomerIndex = () => {
|
||||
// Tab配置
|
||||
const tabList = getStatusOptions();
|
||||
|
||||
|
||||
// 获取客户数据
|
||||
const fetchCustomerData = useCallback(async () => {
|
||||
const fetchCustomerData = useCallback(async (statusFilter?: CustomerStatus) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const res = await pageShopDealerApply({});
|
||||
// 构建API参数,根据状态筛选
|
||||
const params: any = {};
|
||||
const applyStatus = mapCustomerStatusToApplyStatus(statusFilter || activeTab);
|
||||
if (applyStatus !== undefined) {
|
||||
params.applyStatus = applyStatus;
|
||||
}
|
||||
|
||||
const res = await pageShopDealerApply(params);
|
||||
if (res?.list) {
|
||||
list.map(d => {
|
||||
if(d.applyStatus == 10){
|
||||
d.customerStatus = 'pending'
|
||||
}
|
||||
if(d.applyStatus == 20){
|
||||
d.customerStatus = 'signed'
|
||||
}
|
||||
if(d.applyStatus == 30){
|
||||
d.customerStatus = 'cancelled'
|
||||
}
|
||||
return {
|
||||
...d,
|
||||
customerStatus: d.applyStatus == 10 ? 'pending' : 'signed'
|
||||
}
|
||||
})
|
||||
setList(res.list);
|
||||
// 正确映射状态
|
||||
const mappedList = res.list.map(customer => ({
|
||||
...customer,
|
||||
customerStatus: mapApplyStatusToCustomerStatus(customer.applyStatus || 10)
|
||||
}));
|
||||
setList(mappedList);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取客户数据失败:', error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
}, [activeTab]);
|
||||
|
||||
|
||||
// 根据当前Tab和搜索条件筛选数据
|
||||
// 根据搜索条件筛选数据(状态筛选已在API层面处理)
|
||||
const getFilteredList = () => {
|
||||
console.log('TABS',activeTab)
|
||||
let filteredList = list;
|
||||
|
||||
// 按状态筛选
|
||||
if (activeTab !== 'all') {
|
||||
console.log(filteredList,'customerStatus')
|
||||
filteredList.map(d => {
|
||||
console.log(d.applyStatus)
|
||||
})
|
||||
|
||||
filteredList = filteredList.filter(customer => customer.customerStatus === activeTab);
|
||||
}
|
||||
|
||||
// 按搜索关键词筛选
|
||||
if (searchValue.trim()) {
|
||||
const keyword = searchValue.trim().toLowerCase();
|
||||
@@ -90,22 +79,36 @@ const CustomerIndex = () => {
|
||||
};
|
||||
|
||||
// 获取各状态的统计数量
|
||||
const getStatusCounts = () => {
|
||||
const counts = {
|
||||
all: list.length,
|
||||
pending: 0,
|
||||
signed: 0,
|
||||
cancelled: 0
|
||||
};
|
||||
const [statusCounts, setStatusCounts] = useState({
|
||||
all: 0,
|
||||
pending: 0,
|
||||
signed: 0,
|
||||
cancelled: 0
|
||||
});
|
||||
|
||||
list.forEach(customer => {
|
||||
if (customer.customerStatus && counts.hasOwnProperty(customer.customerStatus)) {
|
||||
counts[customer.customerStatus]++;
|
||||
}
|
||||
});
|
||||
// 获取所有状态的统计数量
|
||||
const fetchStatusCounts = useCallback(async () => {
|
||||
try {
|
||||
// 并行获取各状态的数量
|
||||
const [allRes, pendingRes, signedRes, cancelledRes] = await Promise.all([
|
||||
pageShopDealerApply({}), // 全部
|
||||
pageShopDealerApply({applyStatus: 10}), // 跟进中
|
||||
pageShopDealerApply({applyStatus: 20}), // 已签约
|
||||
pageShopDealerApply({applyStatus: 30}) // 已取消
|
||||
]);
|
||||
|
||||
return counts;
|
||||
};
|
||||
setStatusCounts({
|
||||
all: allRes?.count || 0,
|
||||
pending: pendingRes?.count || 0,
|
||||
signed: signedRes?.count || 0,
|
||||
cancelled: cancelledRes?.count || 0
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取状态统计失败:', error);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const getStatusCounts = () => statusCounts;
|
||||
|
||||
// 取消操作
|
||||
const handleCancel = (customer: ShopDealerApply) => {
|
||||
@@ -120,7 +123,13 @@ const CustomerIndex = () => {
|
||||
// 初始化数据
|
||||
useEffect(() => {
|
||||
fetchCustomerData();
|
||||
}, [fetchCustomerData]);
|
||||
fetchStatusCounts();
|
||||
}, [fetchCustomerData, fetchStatusCounts]);
|
||||
|
||||
// 当activeTab变化时重新获取数据
|
||||
useEffect(() => {
|
||||
fetchCustomerData(activeTab);
|
||||
}, [activeTab]);
|
||||
|
||||
// 渲染客户项
|
||||
const renderCustomerItem = (customer: CustomerUser) => (
|
||||
|
||||
Reference in New Issue
Block a user