feat(dealer/customer): 重构日期选择和处理逻辑
- 替换 DatePicker 组件为 Calendar 组件 - 优化日期格式化和解析逻辑,支持多种日期格式- 更新日期选择器的交互方式 - 调整相关组件和样式
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import {useEffect, useState, useRef} from "react";
|
||||
import {Loading, CellGroup, Cell, Input, Form, DatePicker, Popup} from '@nutui/nutui-react-taro'
|
||||
import {Edit, Calendar} from '@nutui/icons-react-taro'
|
||||
import {Loading, CellGroup, Cell, Input, Form, Calendar} from '@nutui/nutui-react-taro'
|
||||
import {Edit, Calendar as CalendarIcon} from '@nutui/icons-react-taro'
|
||||
import Taro from '@tarojs/taro'
|
||||
import {useRouter} from '@tarojs/taro'
|
||||
import {View,Text} from '@tarojs/components'
|
||||
@@ -41,22 +41,67 @@ const AddShopDealerApply = () => {
|
||||
}
|
||||
}
|
||||
|
||||
// 格式化日期为 YYYY-MM-DD
|
||||
const formatDate = (date: Date): string => {
|
||||
const year = date.getFullYear()
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0')
|
||||
const day = String(date.getDate()).padStart(2, '0')
|
||||
return `${year}-${month}-${day}`
|
||||
// 格式化日期为数据库格式 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 = (_: any, values: Date[]) => {
|
||||
const selectedDate = values[0]
|
||||
const formattedDate = formatDate(selectedDate)
|
||||
setApplyTime(formattedDate)
|
||||
const handleApplyTimeConfirm = (param: string[]) => {
|
||||
const selectedDate = param[3] // 选中的日期字符串 (YYYY-M-D)
|
||||
const formattedDate = formatDateForDatabase(selectedDate) // 转换为数据库格式
|
||||
setApplyTime(selectedDate) // 保存原始格式用于显示
|
||||
setShowApplyTimePicker(false)
|
||||
|
||||
// 更新表单数据
|
||||
// 更新表单数据(使用数据库格式)
|
||||
if (formRef.current) {
|
||||
formRef.current.setFieldsValue({
|
||||
applyTime: formattedDate
|
||||
@@ -65,13 +110,13 @@ const AddShopDealerApply = () => {
|
||||
}
|
||||
|
||||
// 处理合同日期选择
|
||||
const handleContractDateConfirm = (_: any, values: Date[]) => {
|
||||
const selectedDate = values[0]
|
||||
const formattedDate = formatDate(selectedDate)
|
||||
setContractDate(formattedDate)
|
||||
const handleContractDateConfirm = (param: string[]) => {
|
||||
const selectedDate = param[3] // 选中的日期字符串 (YYYY-M-D)
|
||||
const formattedDate = formatDateForDatabase(selectedDate) // 转换为数据库格式
|
||||
setContractDate(selectedDate) // 保存原始格式用于显示
|
||||
setShowContractDatePicker(false)
|
||||
|
||||
// 更新表单数据
|
||||
// 更新表单数据(使用数据库格式)
|
||||
if (formRef.current) {
|
||||
formRef.current.setFieldsValue({
|
||||
contractDate: formattedDate
|
||||
@@ -91,12 +136,12 @@ const AddShopDealerApply = () => {
|
||||
setIsEditMode(true);
|
||||
setExistingApply(dealerApply)
|
||||
|
||||
// 初始化日期数据
|
||||
// 初始化日期数据(从数据库格式转换为显示格式)
|
||||
if (dealerApply.applyTime) {
|
||||
setApplyTime(dealerApply.applyTime)
|
||||
setApplyTime(extractDateFromDatabase(dealerApply.applyTime))
|
||||
}
|
||||
if (dealerApply.contractTime) {
|
||||
setContractDate(dealerApply.contractTime)
|
||||
setContractDate(extractDateFromDatabase(dealerApply.contractTime))
|
||||
}
|
||||
|
||||
Taro.setNavigationBarTitle({title: '签约'})
|
||||
@@ -120,9 +165,9 @@ const AddShopDealerApply = () => {
|
||||
refereeId: 33534,
|
||||
applyStatus: 10,
|
||||
auditTime: undefined,
|
||||
// 确保日期数据正确提交
|
||||
applyTime: applyTime || values.applyTime,
|
||||
contractDate: contractDate || values.contractDate
|
||||
// 确保日期数据正确提交(使用数据库格式)
|
||||
applyTime: values.applyTime || (applyTime ? formatDateForDatabase(applyTime) : ''),
|
||||
contractDate: values.contractDate || (contractDate ? formatDateForDatabase(contractDate) : '')
|
||||
};
|
||||
|
||||
// 如果是编辑模式,添加现有申请的id
|
||||
@@ -215,7 +260,7 @@ const AddShopDealerApply = () => {
|
||||
}}
|
||||
>
|
||||
<View className="flex items-center">
|
||||
<Calendar size={16} color="#999" className="mr-2" />
|
||||
<CalendarIcon size={16} color="#999" className="mr-2" />
|
||||
<Text style={{ color: applyTime ? '#333' : '#999' }}>
|
||||
{applyTime || '请选择签约时间'}
|
||||
</Text>
|
||||
@@ -232,7 +277,7 @@ const AddShopDealerApply = () => {
|
||||
}}
|
||||
>
|
||||
<View className="flex items-center">
|
||||
<Calendar size={16} color="#999" className="mr-2" />
|
||||
<CalendarIcon size={16} color="#999" className="mr-2" />
|
||||
<Text style={{ color: contractDate ? '#333' : '#999' }}>
|
||||
{contractDate || '请选择合同生效起止时间'}
|
||||
</Text>
|
||||
@@ -246,32 +291,20 @@ const AddShopDealerApply = () => {
|
||||
</Form>
|
||||
|
||||
{/* 签约时间选择器 */}
|
||||
<Popup
|
||||
<Calendar
|
||||
visible={showApplyTimePicker}
|
||||
position="bottom"
|
||||
defaultValue={applyTime}
|
||||
onClose={() => setShowApplyTimePicker(false)}
|
||||
>
|
||||
<DatePicker
|
||||
title="选择签约时间"
|
||||
type="date"
|
||||
onConfirm={() => handleApplyTimeConfirm}
|
||||
onClose={() => setShowApplyTimePicker(false)}
|
||||
/>
|
||||
</Popup>
|
||||
onConfirm={handleApplyTimeConfirm}
|
||||
/>
|
||||
|
||||
{/* 合同日期选择器 */}
|
||||
<Popup
|
||||
<Calendar
|
||||
visible={showContractDatePicker}
|
||||
position="bottom"
|
||||
defaultValue={contractDate}
|
||||
onClose={() => setShowContractDatePicker(false)}
|
||||
>
|
||||
<DatePicker
|
||||
title="选择合同日期"
|
||||
type="date"
|
||||
onConfirm={() => handleContractDateConfirm}
|
||||
onClose={() => setShowContractDatePicker(false)}
|
||||
/>
|
||||
</Popup>
|
||||
onConfirm={handleContractDateConfirm}
|
||||
/>
|
||||
|
||||
{/* 审核状态显示(仅在编辑模式下显示) */}
|
||||
{isEditMode && (
|
||||
|
||||
Reference in New Issue
Block a user