feat(dealer): 优化客户保护期逻辑并添加删除功能

- 新增计算保护期过期时间的方法
- 修改提交逻辑,增加过期时间字段
- 优化保护天数计算逻辑,优先使用过期时间
- 添加客户删除功能
- 调整客户列表显示逻辑
This commit is contained in:
2025-09-14 23:39:05 +08:00
parent 8b20d6c7c2
commit 924188568c
4 changed files with 117 additions and 40 deletions

View File

@@ -14,7 +14,7 @@ import {
} from '@/utils/customerStatus';
import FixedButton from "@/components/FixedButton";
import navTo from "@/utils/common";
import {pageShopDealerApply, updateShopDealerApply} from "@/api/shop/shopDealerApply";
import {pageShopDealerApply, removeShopDealerApply, updateShopDealerApply} from "@/api/shop/shopDealerApply";
// 扩展User类型添加客户状态和保护天数
interface CustomerUser extends UserType {
@@ -33,36 +33,62 @@ const CustomerIndex = () => {
// Tab配置
const tabList = getStatusOptions();
// 计算剩余保护天数
const calculateProtectDays = (applyTime: string): number => {
if (!applyTime) return 0;
// 计算剩余保护天数(基于过期时间)
const calculateProtectDays = (expirationTime?: string, applyTime?: string): number => {
try {
// 确保日期格式正确解析
const applyDate = new Date(applyTime.replace(/-/g, '/'));
const currentDate = new Date();
// 优先使用过期时间字段
if (expirationTime) {
const expDate = new Date(expirationTime.replace(' ', 'T'));
const now = new Date();
// 计算剩余毫秒数
const remainingMs = expDate.getTime() - now.getTime();
// 转换为天数,向上取整
const remainingDays = Math.ceil(remainingMs / (1000 * 60 * 60 * 24));
console.log('=== 基于过期时间计算 ===');
console.log('过期时间:', expirationTime);
console.log('当前时间:', now.toLocaleString());
console.log('剩余天数:', remainingDays);
console.log('======================');
return Math.max(0, remainingDays);
}
// 如果没有过期时间,回退到基于申请时间计算
if (!applyTime) return 0;
const protectionPeriod = 7; // 保护期7天
// 调试信息
// 解析申请时间
let applyDate: Date;
if (applyTime.includes('T')) {
applyDate = new Date(applyTime);
} else {
applyDate = new Date(applyTime.replace(' ', 'T'));
}
// 获取当前时间
const now = new Date();
// 只比较日期部分,忽略时间
const applyDateOnly = new Date(applyDate.getFullYear(), applyDate.getMonth(), applyDate.getDate());
const currentDateOnly = new Date(now.getFullYear(), now.getMonth(), now.getDate());
// 计算已经过去的天数
const timeDiff = currentDateOnly.getTime() - applyDateOnly.getTime();
const daysPassed = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
// 计算剩余保护天数
const remainingDays = protectionPeriod - daysPassed;
console.log('=== 基于申请时间计算 ===');
console.log('申请时间:', applyTime);
console.log('解析后的申请日期:', applyDate);
console.log('当前日期:', currentDate);
console.log('已过去天数:', daysPassed);
console.log('剩余保护天数:', remainingDays);
console.log('======================');
// 计算保护期结束时间
const protectionEndDate = new Date(applyDate);
protectionEndDate.setDate(protectionEndDate.getDate() + protectionPeriod);
console.log('保护期结束时间:', protectionEndDate);
// 计算剩余毫秒数
const remainingMs = protectionEndDate.getTime() - currentDate.getTime();
// 转换为天数,向上取整
const remainingDays = Math.ceil(remainingMs / (1000 * 60 * 60 * 24));
console.log('剩余天数:', remainingDays);
// 如果剩余天数小于0返回0
return Math.max(0, remainingDays);
} catch (error) {
console.error('日期计算错误:', error);
@@ -94,7 +120,7 @@ const CustomerIndex = () => {
const mappedList = res.list.map(customer => ({
...customer,
customerStatus: mapApplyStatusToCustomerStatus(customer.applyStatus || 10),
protectDays: calculateProtectDays(customer.applyTime || customer.createTime || '')
protectDays: calculateProtectDays(customer.expirationTime, customer.applyTime || customer.createTime || '')
}));
// 如果是重置页面或第一页,直接设置新数据;否则追加数据
@@ -203,6 +229,22 @@ const CustomerIndex = () => {
})
};
// 删除
const handleDelete = (customer: ShopDealerApply) => {
removeShopDealerApply(customer.applyId).then(() => {
Taro.showToast({
title: '删除成功',
icon: 'success'
});
// 刷新当前tab的数据
setList([]);
setPage(1);
setHasMore(true);
fetchCustomerData(activeTab, true).then();
fetchStatusCounts().then();
})
}
// 初始化数据
useEffect(() => {
fetchCustomerData(activeTab, true).then();
@@ -235,7 +277,7 @@ const CustomerIndex = () => {
<View className="flex-1">
<View className="flex items-center justify-between mb-1">
<Text className="font-semibold text-gray-800 mr-2">
{customer.realName || customer.dealerName || customer.dealerCode}
{customer.dealerName}
</Text>
{customer.customerStatus && (
<Tag type={getStatusTagType(customer.customerStatus)}>
@@ -296,6 +338,17 @@ const CustomerIndex = () => {
</Button>
</Space>
)}
{(customer.applyStatus === 30 && customer.userId == Taro.getStorageSync('UserId')) && (
<Space className="flex justify-end">
<Button
size="small"
onClick={() => handleDelete(customer)}
style={{backgroundColor: '#ff4d4f', color: 'white'}}
>
</Button>
</Space>
)}
</View>
);