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

@@ -108,23 +108,40 @@ const AddShopDealerApply = () => {
}
// 提交表单
// 计算保护期过期时间7天后
const calculateExpirationTime = (): string => {
const now = new Date();
const expirationDate = new Date(now);
expirationDate.setDate(now.getDate() + 7); // 7天后
// 格式化为数据库需要的格式YYYY-MM-DD HH:mm:ss
const year = expirationDate.getFullYear();
const month = String(expirationDate.getMonth() + 1).padStart(2, '0');
const day = String(expirationDate.getDate()).padStart(2, '0');
const hours = String(expirationDate.getHours()).padStart(2, '0');
const minutes = String(expirationDate.getMinutes()).padStart(2, '0');
const seconds = String(expirationDate.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};
const submitSucceed = async (values: any) => {
try {
// 检查客户是否已存在
const res = await pageShopDealerApply({dealerName: values.dealerName, type: 4});
const res = await pageShopDealerApply({dealerName: values.dealerName, type: 4, applyStatus: 10});
if (res && res.count > 0) {
const existingCustomer = res.list[0];
// 检查是否在7天保护期内
if (existingCustomer.applyTime) {
if (!isEditMode && existingCustomer.applyTime) {
// 将申请时间字符串转换为时间戳进行比较
const applyTimeStamp = new Date(existingCustomer.applyTime).getTime();
const currentTimeStamp = new Date().getTime();
const sevenDaysInMs = 7 * 24 * 60 * 60 * 1000; // 7天的毫秒数
// 如果在7天保护期内不允许重复添加
if (!isEditMode && currentTimeStamp - applyTimeStamp < sevenDaysInMs) {
if (currentTimeStamp - applyTimeStamp < sevenDaysInMs) {
const remainingDays = Math.ceil((sevenDaysInMs - (currentTimeStamp - applyTimeStamp)) / (24 * 60 * 60 * 1000));
Taro.showToast({
@@ -156,17 +173,13 @@ const AddShopDealerApply = () => {
}
// 用户确认后继续执行添加逻辑
}
} else {
// 没有申请时间,按已存在处理
Taro.showToast({
title: '该客户已存在',
icon: 'none'
});
return false;
}
}
// 计算过期时间
const expirationTime = isEditMode ? existingApply?.expirationTime : calculateExpirationTime();
// 准备提交的数据
const submitData = {
...values,
@@ -174,13 +187,22 @@ const AddShopDealerApply = () => {
realName: values.realName || user?.nickname,
mobile: user?.phone,
refereeId: 33534,
applyStatus: 10,
applyStatus: isEditMode ? 20 : 10,
auditTime: undefined,
// 设置保护期过期时间7天后
expirationTime: expirationTime,
// 确保日期数据正确提交(使用数据库格式)
applyTime: values.applyTime || (applyTime ? formatDateForDatabase(applyTime) : ''),
contractTime: values.contractTime || (contractTime ? formatDateForDatabase(contractTime) : '')
};
// 调试信息
console.log('=== 提交数据调试 ===');
console.log('是否编辑模式:', isEditMode);
console.log('计算的过期时间:', expirationTime);
console.log('提交的数据:', submitData);
console.log('==================');
// 如果是编辑模式添加现有申请的id
if (isEditMode && existingApply?.applyId) {
submitData.applyId = existingApply.applyId;

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>
);