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

- 在客户列表和添加页面增加保护期相关功能
- 优化客户数据获取和展示,添加保护天数计算
- 调整添加客户的逻辑,增加对保护期的判断
- 更新环境配置,使用本地API地址
This commit is contained in:
2025-09-12 13:43:44 +08:00
parent 86516a8334
commit 8b20d6c7c2
3 changed files with 212 additions and 109 deletions

View File

@@ -1,9 +1,9 @@
import {useEffect, useState, useRef} from "react";
import {Loading, CellGroup, Cell, Input, Form, Calendar} from '@nutui/nutui-react-taro'
import {Edit} from '@nutui/icons-react-taro'
import {Edit, Calendar as CalendarIcon} from '@nutui/icons-react-taro'
import Taro from '@tarojs/taro'
import {useRouter} from '@tarojs/taro'
import {View} from '@tarojs/components'
import {View, Text} from '@tarojs/components'
import FixedButton from "@/components/FixedButton";
import {useUser} from "@/hooks/useUser";
import {ShopDealerApply} from "@/api/shop/shopDealerApply/model";
@@ -13,7 +13,7 @@ import {
} from "@/api/shop/shopDealerApply";
import {
formatDateForDatabase,
extractDateForCalendar
extractDateForCalendar, formatDateForDisplay
} from "@/utils/dateUtils";
const AddShopDealerApply = () => {
@@ -45,6 +45,7 @@ const AddShopDealerApply = () => {
}
}
console.log(getApplyStatusText)
// 处理签约时间选择
const handleApplyTimeConfirm = (param: string) => {
@@ -107,60 +108,107 @@ const AddShopDealerApply = () => {
}
// 提交表单
const submitSucceed = (values: any) => {
const submitSucceed = async (values: any) => {
try {
// 检查客户是否已存在
const res = await pageShopDealerApply({dealerName: values.dealerName, type: 4});
pageShopDealerApply({dealerName: values.dealerName, type: 4}).then((res) => {
if (res && res?.count > 0) {
Taro.showToast({
title: '该客户已存在',
icon: 'error'
});
return false;
}
try {
// 准备提交的数据
const submitData = {
...values,
type: 4,
realName: values.realName || user?.nickname,
mobile: user?.phone,
refereeId: 33534,
applyStatus: 10,
auditTime: undefined,
// 确保日期数据正确提交(使用数据库格式)
applyTime: values.applyTime || (applyTime ? formatDateForDatabase(applyTime) : ''),
contractTime: values.contractTime || (contractTime ? formatDateForDatabase(contractTime) : '')
};
if (res && res.count > 0) {
const existingCustomer = res.list[0];
// 如果是编辑模式添加现有申请的id
if (isEditMode && existingApply?.applyId) {
submitData.applyId = existingApply.applyId;
}
// 检查是否在7天保护期内
if (existingCustomer.applyTime) {
// 将申请时间字符串转换为时间戳进行比较
const applyTimeStamp = new Date(existingCustomer.applyTime).getTime();
const currentTimeStamp = new Date().getTime();
const sevenDaysInMs = 7 * 24 * 60 * 60 * 1000; // 7天的毫秒数
// 执行新增或更新操作
if (isEditMode) {
updateShopDealerApply(submitData);
// 如果在7天保护期内不允许重复添加
if (!isEditMode && currentTimeStamp - applyTimeStamp < sevenDaysInMs) {
const remainingDays = Math.ceil((sevenDaysInMs - (currentTimeStamp - applyTimeStamp)) / (24 * 60 * 60 * 1000));
Taro.showToast({
title: `该客户还在保护期,还需等待${remainingDays}天后才能重新添加`,
icon: 'none',
duration: 3000
});
return false;
} else {
// 超过7天保护期可以重新添加显示确认对话框
const modalResult = await new Promise<boolean>((resolve) => {
Taro.showModal({
title: '提示',
content: '该客户已超过7天保护期是否重新添加跟进',
showCancel: true,
cancelText: '取消',
confirmText: '确定',
success: (modalRes) => {
resolve(modalRes.confirm);
},
fail: () => {
resolve(false);
}
});
});
if (!modalResult) {
return false; // 用户取消,不继续执行
}
// 用户确认后继续执行添加逻辑
}
} else {
addShopDealerApply(submitData);
// 没有申请时间,按已存在处理
Taro.showToast({
title: '该客户已存在',
icon: 'none'
});
return false;
}
Taro.showToast({
title: `${isEditMode ? '提交' : '提交'}成功`,
icon: 'success'
});
setTimeout(() => {
Taro.navigateBack();
}, 1000);
} catch (error) {
console.error('验证邀请人失败:', error);
return Taro.showToast({
title: '邀请人ID不存在',
icon: 'error'
});
}
})
// 准备提交的数据
const submitData = {
...values,
type: 4,
realName: values.realName || user?.nickname,
mobile: user?.phone,
refereeId: 33534,
applyStatus: 10,
auditTime: undefined,
// 确保日期数据正确提交(使用数据库格式)
applyTime: values.applyTime || (applyTime ? formatDateForDatabase(applyTime) : ''),
contractTime: values.contractTime || (contractTime ? formatDateForDatabase(contractTime) : '')
};
// 如果是编辑模式添加现有申请的id
if (isEditMode && existingApply?.applyId) {
submitData.applyId = existingApply.applyId;
}
// 执行新增或更新操作
if (isEditMode) {
await updateShopDealerApply(submitData);
} else {
await addShopDealerApply(submitData);
}
Taro.showToast({
title: `${isEditMode ? '更新' : '提交'}成功`,
icon: 'success'
});
setTimeout(() => {
Taro.navigateBack();
}, 1000);
} catch (error) {
console.error('提交失败:', error);
Taro.showToast({
title: '提交失败,请重试',
icon: 'error'
});
}
}
// 处理固定按钮点击事件
@@ -210,46 +258,42 @@ const AddShopDealerApply = () => {
<Form.Item name="dealerCode" label="户号" initialValue={FormData?.dealerCode} required>
<Input placeholder="请填写户号" disabled={isEditMode}/>
</Form.Item>
{/*<Form.Item name="money" label="签约价格" initialValue={FormData?.money} required>*/}
{/* <Input placeholder="(元/兆瓦时)" disabled={false}/>*/}
{/*</Form.Item>*/}
{/*<Form.Item name="applyTime" label="签约时间" initialValue={FormData?.applyTime} required>*/}
{/* <View*/}
{/* className="flex items-center justify-between py-2"*/}
{/* onClick={() => !isEditMode && setShowApplyTimePicker(true)}*/}
{/* style={{*/}
{/* cursor: isEditMode ? 'not-allowed' : 'pointer',*/}
{/* opacity: isEditMode ? 0.6 : 1*/}
{/* }}*/}
{/* >*/}
{/* <View className="flex items-center">*/}
{/* <CalendarIcon size={16} color="#999" className="mr-2" />*/}
{/* <Text style={{ color: applyTime ? '#333' : '#999' }}>*/}
{/* {applyTime ? formatDateForDisplay(applyTime) : '请选择签约时间'}*/}
{/* </Text>*/}
{/* </View>*/}
{/* </View>*/}
{/*</Form.Item>*/}
{/*<Form.Item name="contractTime" label="合同日期" initialValue={FormData?.contractTime} required>*/}
{/* <View*/}
{/* className="flex items-center justify-between py-2"*/}
{/* onClick={() => !isEditMode && setShowContractTimePicker(true)}*/}
{/* style={{*/}
{/* cursor: isEditMode ? 'not-allowed' : 'pointer',*/}
{/* opacity: isEditMode ? 0.6 : 1*/}
{/* }}*/}
{/* >*/}
{/* <View className="flex items-center">*/}
{/* <CalendarIcon size={16} color="#999" className="mr-2" />*/}
{/* <Text style={{ color: contractTime ? '#333' : '#999' }}>*/}
{/* {contractTime ? formatDateForDisplay(contractTime) : '请选择合同生效起止时间'}*/}
{/* </Text>*/}
{/* </View>*/}
{/* </View>*/}
{/*</Form.Item>*/}
{/*<Form.Item name="refereeId" label="邀请人ID" initialValue={FormData?.refereeId} required>*/}
{/* <Input placeholder="邀请人ID"/>*/}
{/*</Form.Item>*/}
{isEditMode && (
<>
<Form.Item name="money" label="签约价格" initialValue={FormData?.money} required>
<Input placeholder="(元/兆瓦时)" disabled={false}/>
</Form.Item>
<Form.Item name="applyTime" label="签约时间" initialValue={FormData?.applyTime}>
<View
className="flex items-center justify-between py-2"
onClick={() => setShowApplyTimePicker(true)}
>
<View className="flex items-center">
<CalendarIcon size={16} color="#999" className="mr-2"/>
<Text style={{color: applyTime ? '#333' : '#999'}}>
{applyTime ? formatDateForDisplay(applyTime) : '请选择签约时间'}
</Text>
</View>
</View>
</Form.Item>
<Form.Item name="contractTime" label="合同日期" initialValue={FormData?.contractTime}>
<View
className="flex items-center justify-between py-2"
onClick={() => setShowContractTimePicker(true)}
>
<View className="flex items-center">
<CalendarIcon size={16} color="#999" className="mr-2"/>
<Text style={{color: contractTime ? '#333' : '#999'}}>
{contractTime ? formatDateForDisplay(contractTime) : '请选择合同生效起止时间'}
</Text>
</View>
</View>
</Form.Item>
{/*<Form.Item name="refereeId" label="邀请人ID" initialValue={FormData?.refereeId} required>*/}
{/* <Input placeholder="邀请人ID"/>*/}
{/*</Form.Item>*/}
</>
)}
</CellGroup>
</Form>
@@ -272,17 +316,17 @@ const AddShopDealerApply = () => {
{/* 审核状态显示(仅在编辑模式下显示) */}
{isEditMode && (
<CellGroup>
<Cell
title={'审核状态'}
extra={
<span style={{
color: FormData?.applyStatus === 20 ? '#52c41a' :
FormData?.applyStatus === 30 ? '#ff4d4f' : '#faad14'
}}>
{getApplyStatusText(FormData?.applyStatus)}
</span>
}
/>
{/*<Cell*/}
{/* title={'审核状态'}*/}
{/* extra={*/}
{/* <span style={{*/}
{/* color: FormData?.applyStatus === 20 ? '#52c41a' :*/}
{/* FormData?.applyStatus === 30 ? '#ff4d4f' : '#faad14'*/}
{/* }}>*/}
{/* {getApplyStatusText(FormData?.applyStatus)}*/}
{/* </span>*/}
{/* }*/}
{/*/>*/}
{FormData?.applyStatus === 20 && (
<Cell title={'签约时间'} extra={FormData?.auditTime || '无'}/>
)}
@@ -297,8 +341,7 @@ const AddShopDealerApply = () => {
{(!isEditMode || FormData?.applyStatus === 10) && (
<FixedButton
icon={<Edit/>}
text={isEditMode ? '保存修改' : '确定签约'}
disabled={FormData?.applyStatus === 10}
text={'确定签约'}
onClick={handleFixedButtonClick}
/>
)}

View File

@@ -16,9 +16,10 @@ import FixedButton from "@/components/FixedButton";
import navTo from "@/utils/common";
import {pageShopDealerApply, updateShopDealerApply} from "@/api/shop/shopDealerApply";
// 扩展User类型添加客户状态
// 扩展User类型添加客户状态和保护天数
interface CustomerUser extends UserType {
customerStatus?: CustomerStatus;
protectDays?: number; // 剩余保护天数
}
const CustomerIndex = () => {
@@ -32,6 +33,43 @@ const CustomerIndex = () => {
// Tab配置
const tabList = getStatusOptions();
// 计算剩余保护天数
const calculateProtectDays = (applyTime: string): number => {
if (!applyTime) return 0;
try {
// 确保日期格式正确解析
const applyDate = new Date(applyTime.replace(/-/g, '/'));
const currentDate = new Date();
const protectionPeriod = 7; // 保护期7天
// 调试信息
console.log('申请时间:', applyTime);
console.log('解析后的申请日期:', applyDate);
console.log('当前日期:', currentDate);
// 计算保护期结束时间
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);
return 0;
}
};
// 获取客户数据
const fetchCustomerData = useCallback(async (statusFilter?: CustomerStatus, resetPage = false, targetPage?: number) => {
@@ -52,10 +90,11 @@ const CustomerIndex = () => {
const res = await pageShopDealerApply(params);
if (res?.list && res.list.length > 0) {
// 正确映射状态
// 正确映射状态并计算保护天数
const mappedList = res.list.map(customer => ({
...customer,
customerStatus: mapApplyStatusToCustomerStatus(customer.applyStatus || 10)
customerStatus: mapApplyStatusToCustomerStatus(customer.applyStatus || 10),
protectDays: calculateProtectDays(customer.applyTime || customer.createTime || '')
}));
// 如果是重置页面或第一页,直接设置新数据;否则追加数据
@@ -214,11 +253,32 @@ const CustomerIndex = () => {
<Text className="text-xs text-gray-500">
{customer.createTime}
</Text>
{/* 保护天数显示 */}
{customer.applyStatus === 10 && (
<View className="flex items-center mt-1">
<Text className="text-xs text-gray-500 mr-2"></Text>
{customer.protectDays && customer.protectDays > 0 ? (
<Text className={`text-xs px-2 py-1 rounded ${
customer.protectDays <= 2
? 'bg-red-100 text-red-600'
: customer.protectDays <= 4
? 'bg-orange-100 text-orange-600'
: 'bg-green-100 text-green-600'
}`}>
{customer.protectDays}
</Text>
) : (
<Text className="text-xs px-2 py-1 rounded bg-gray-100 text-gray-500">
</Text>
)}
</View>
)}
</View>
</View>
{/* 跟进中状态显示操作按钮 */}
{customer.applyStatus === 10 && (
{(customer.applyStatus === 10 && customer.userId == Taro.getStorageSync('UserId')) && (
<Space className="flex justify-end">
<Button
size="small"