refactor(dealer): 重构日期处理逻辑并提取到工具库- 将日期处理相关函数提取到单独的工具库文件中- 重命名相关变量和函数以提高可读性
-优化日期格式化和解析逻辑- 更新组件中日期相关代码以使用新的工具函数
This commit is contained in:
@@ -11,6 +11,11 @@ import {
|
||||
addShopDealerApply, getShopDealerApply,
|
||||
updateShopDealerApply
|
||||
} from "@/api/shop/shopDealerApply";
|
||||
import {
|
||||
formatDateForDatabase,
|
||||
extractDateForCalendar,
|
||||
formatDateForDisplay
|
||||
} from "@/utils/dateUtils";
|
||||
|
||||
const AddShopDealerApply = () => {
|
||||
const {user} = useUser()
|
||||
@@ -23,9 +28,9 @@ const AddShopDealerApply = () => {
|
||||
|
||||
// 日期选择器状态
|
||||
const [showApplyTimePicker, setShowApplyTimePicker] = useState<boolean>(false)
|
||||
const [showContractDatePicker, setShowContractDatePicker] = useState<boolean>(false)
|
||||
const [showContractTimePicker, setShowContractTimePicker] = useState<boolean>(false)
|
||||
const [applyTime, setApplyTime] = useState<string>('')
|
||||
const [contractDate, setContractDate] = useState<string>('')
|
||||
const [contractTime, setContractTime] = useState<string>('')
|
||||
|
||||
// 获取审核状态文字
|
||||
const getApplyStatusText = (status?: number) => {
|
||||
@@ -41,61 +46,10 @@ const AddShopDealerApply = () => {
|
||||
}
|
||||
}
|
||||
|
||||
// 格式化日期为数据库格式 YYYY-MM-DD HH:mm:ss
|
||||
const formatDateForDatabase = (dateStr: string): string => {
|
||||
if (!dateStr) return ''
|
||||
|
||||
let parts: string[] = []
|
||||
|
||||
// 处理不同的日期格式
|
||||
if (dateStr.includes('/')) {
|
||||
// 处理 YYYY/MM/DD 或 YYYY/M/D 格式
|
||||
parts = dateStr.split('/')
|
||||
} else if (dateStr.includes('-')) {
|
||||
// 处理 YYYY-MM-DD 或 YYYY-M-D 格式
|
||||
parts = dateStr.split('-')
|
||||
} else {
|
||||
return dateStr
|
||||
}
|
||||
|
||||
if (parts.length !== 3) return dateStr
|
||||
|
||||
const year = parts[0]
|
||||
const month = parts[1].padStart(2, '0')
|
||||
const day = parts[2].padStart(2, '0')
|
||||
|
||||
return `${year}-${month}-${day} 00:00:00`
|
||||
}
|
||||
|
||||
// 从数据库格式提取日期部分用于显示和Calendar组件
|
||||
const extractDateFromDatabase = (dateTimeStr: string): string => {
|
||||
if (!dateTimeStr) return ''
|
||||
|
||||
// 处理不同的输入格式
|
||||
let dateStr = ''
|
||||
if (dateTimeStr.includes(' ')) {
|
||||
// 从 "YYYY-MM-DD HH:mm:ss" 格式中提取日期部分
|
||||
dateStr = dateTimeStr.split(' ')[0]
|
||||
} else {
|
||||
dateStr = dateTimeStr
|
||||
}
|
||||
|
||||
// 转换为Calendar组件需要的格式 (YYYY-M-D)
|
||||
if (dateStr.includes('-')) {
|
||||
const parts = dateStr.split('-')
|
||||
if (parts.length === 3) {
|
||||
const year = parts[0]
|
||||
const month = parseInt(parts[1]).toString() // 去掉前导0
|
||||
const day = parseInt(parts[2]).toString() // 去掉前导0
|
||||
return `${year}-${month}-${day}`
|
||||
}
|
||||
}
|
||||
|
||||
return dateStr
|
||||
}
|
||||
|
||||
// 处理签约时间选择
|
||||
const handleApplyTimeConfirm = (param: string[]) => {
|
||||
const handleApplyTimeConfirm = (param: string) => {
|
||||
const selectedDate = param[3] // 选中的日期字符串 (YYYY-M-D)
|
||||
const formattedDate = formatDateForDatabase(selectedDate) // 转换为数据库格式
|
||||
setApplyTime(selectedDate) // 保存原始格式用于显示
|
||||
@@ -110,16 +64,16 @@ const AddShopDealerApply = () => {
|
||||
}
|
||||
|
||||
// 处理合同日期选择
|
||||
const handleContractDateConfirm = (param: string[]) => {
|
||||
const handleContractTimeConfirm = (param: string) => {
|
||||
const selectedDate = param[3] // 选中的日期字符串 (YYYY-M-D)
|
||||
const formattedDate = formatDateForDatabase(selectedDate) // 转换为数据库格式
|
||||
setContractDate(selectedDate) // 保存原始格式用于显示
|
||||
setShowContractDatePicker(false)
|
||||
setContractTime(selectedDate) // 保存原始格式用于显示
|
||||
setShowContractTimePicker(false)
|
||||
|
||||
// 更新表单数据(使用数据库格式)
|
||||
if (formRef.current) {
|
||||
formRef.current.setFieldsValue({
|
||||
contractDate: formattedDate
|
||||
contractTime: formattedDate
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -136,12 +90,12 @@ const AddShopDealerApply = () => {
|
||||
setIsEditMode(true);
|
||||
setExistingApply(dealerApply)
|
||||
|
||||
// 初始化日期数据(从数据库格式转换为显示格式)
|
||||
// 初始化日期数据(从数据库格式转换为Calendar组件格式)
|
||||
if (dealerApply.applyTime) {
|
||||
setApplyTime(extractDateFromDatabase(dealerApply.applyTime))
|
||||
setApplyTime(extractDateForCalendar(dealerApply.applyTime))
|
||||
}
|
||||
if (dealerApply.contractTime) {
|
||||
setContractDate(extractDateFromDatabase(dealerApply.contractTime))
|
||||
setContractTime(extractDateForCalendar(dealerApply.contractTime))
|
||||
}
|
||||
|
||||
Taro.setNavigationBarTitle({title: '签约'})
|
||||
@@ -167,7 +121,7 @@ const AddShopDealerApply = () => {
|
||||
auditTime: undefined,
|
||||
// 确保日期数据正确提交(使用数据库格式)
|
||||
applyTime: values.applyTime || (applyTime ? formatDateForDatabase(applyTime) : ''),
|
||||
contractDate: values.contractDate || (contractDate ? formatDateForDatabase(contractDate) : '')
|
||||
contractTime: values.contractTime || (contractTime ? formatDateForDatabase(contractTime) : '')
|
||||
};
|
||||
|
||||
// 如果是编辑模式,添加现有申请的id
|
||||
@@ -262,15 +216,15 @@ const AddShopDealerApply = () => {
|
||||
<View className="flex items-center">
|
||||
<CalendarIcon size={16} color="#999" className="mr-2" />
|
||||
<Text style={{ color: applyTime ? '#333' : '#999' }}>
|
||||
{applyTime || '请选择签约时间'}
|
||||
{applyTime ? formatDateForDisplay(applyTime) : '请选择签约时间'}
|
||||
</Text>
|
||||
</View>
|
||||
</View>
|
||||
</Form.Item>
|
||||
<Form.Item name="contractDate" label="合同日期" initialValue={FormData?.contractTime} required>
|
||||
<Form.Item name="contractTime" label="合同日期" initialValue={FormData?.contractTime} required>
|
||||
<View
|
||||
className="flex items-center justify-between py-2"
|
||||
onClick={() => !isEditMode && setShowContractDatePicker(true)}
|
||||
onClick={() => !isEditMode && setShowContractTimePicker(true)}
|
||||
style={{
|
||||
cursor: isEditMode ? 'not-allowed' : 'pointer',
|
||||
opacity: isEditMode ? 0.6 : 1
|
||||
@@ -278,8 +232,8 @@ const AddShopDealerApply = () => {
|
||||
>
|
||||
<View className="flex items-center">
|
||||
<CalendarIcon size={16} color="#999" className="mr-2" />
|
||||
<Text style={{ color: contractDate ? '#333' : '#999' }}>
|
||||
{contractDate || '请选择合同生效起止时间'}
|
||||
<Text style={{ color: contractTime ? '#333' : '#999' }}>
|
||||
{contractTime ? formatDateForDisplay(contractTime) : '请选择合同生效起止时间'}
|
||||
</Text>
|
||||
</View>
|
||||
</View>
|
||||
@@ -300,10 +254,10 @@ const AddShopDealerApply = () => {
|
||||
|
||||
{/* 合同日期选择器 */}
|
||||
<Calendar
|
||||
visible={showContractDatePicker}
|
||||
defaultValue={contractDate}
|
||||
onClose={() => setShowContractDatePicker(false)}
|
||||
onConfirm={handleContractDateConfirm}
|
||||
visible={showContractTimePicker}
|
||||
defaultValue={contractTime}
|
||||
onClose={() => setShowContractTimePicker(false)}
|
||||
onConfirm={handleContractTimeConfirm}
|
||||
/>
|
||||
|
||||
{/* 审核状态显示(仅在编辑模式下显示) */}
|
||||
|
||||
Reference in New Issue
Block a user