|
@@ -1,111 +1,114 @@
|
|
|
export function formatDate(date) {
|
|
|
// 检查日期是否为空
|
|
|
if (!date) {
|
|
|
- return '';
|
|
|
+ return ''
|
|
|
}
|
|
|
-
|
|
|
- let d;
|
|
|
-
|
|
|
+
|
|
|
+ let d
|
|
|
+
|
|
|
// 处理各种日期格式
|
|
|
if (typeof date === 'string') {
|
|
|
// 处理中文年月日格式:YYYY年M月D日 或 YYYY年M月
|
|
|
if (date.includes('年')) {
|
|
|
- const yearMatch = date.match(/(\d+)年/);
|
|
|
- const monthMatch = date.match(/(\d+)月/);
|
|
|
- const dayMatch = date.match(/(\d+)日/);
|
|
|
-
|
|
|
+ const yearMatch = date.match(/(\d+)年/)
|
|
|
+ const monthMatch = date.match(/(\d+)月/)
|
|
|
+ const dayMatch = date.match(/(\d+)日/)
|
|
|
+
|
|
|
if (yearMatch && monthMatch) {
|
|
|
- const year = yearMatch[1];
|
|
|
- const month = String(parseInt(monthMatch[1])).padStart(2, '0');
|
|
|
+ const year = yearMatch[1]
|
|
|
+ const month = String(parseInt(monthMatch[1])).padStart(2, '0')
|
|
|
// 如果有日,使用日,否则默认为1日
|
|
|
- const day = dayMatch ? String(parseInt(dayMatch[1])).padStart(2, '0') : '01';
|
|
|
- d = new Date(`${year}-${month}-${day}`);
|
|
|
+ const day = dayMatch
|
|
|
+ ? String(parseInt(dayMatch[1])).padStart(2, '0')
|
|
|
+ : '01'
|
|
|
+ d = new Date(`${year}-${month}-${day}`)
|
|
|
} else {
|
|
|
- d = new Date(date);
|
|
|
+ d = new Date(date)
|
|
|
}
|
|
|
}
|
|
|
// 处理特殊格式:YYYY.M.D 或 YYYY.M
|
|
|
else if (date.includes('.')) {
|
|
|
- const parts = date.split('.');
|
|
|
+ const parts = date.split('.')
|
|
|
if (parts.length >= 2) {
|
|
|
// 将 YYYY.M.D 或 YYYY.M 转换为 YYYY-MM-DD 格式
|
|
|
- const year = parts[0];
|
|
|
- const month = String(parseInt(parts[1])).padStart(2, '0');
|
|
|
+ const year = parts[0]
|
|
|
+ const month = String(parseInt(parts[1])).padStart(2, '0')
|
|
|
// 如果有日,使用日,否则默认为1日
|
|
|
- const day = parts.length > 2 ? String(parseInt(parts[2])).padStart(2, '0') : '01';
|
|
|
- d = new Date(`${year}-${month}-${day}`);
|
|
|
+ const day =
|
|
|
+ parts.length > 2 ? String(parseInt(parts[2])).padStart(2, '0') : '01'
|
|
|
+ d = new Date(`${year}-${month}-${day}`)
|
|
|
} else {
|
|
|
- d = new Date(date);
|
|
|
+ d = new Date(date)
|
|
|
}
|
|
|
}
|
|
|
// 处理斜杠分隔格式:YYYY/M/D 或 D/M/YYYY
|
|
|
else if (date.includes('/')) {
|
|
|
- const parts = date.split('/');
|
|
|
+ const parts = date.split('/')
|
|
|
if (parts.length === 3) {
|
|
|
// 判断是 YYYY/MM/DD 还是 DD/MM/YYYY
|
|
|
if (parts[0].length === 4) {
|
|
|
// YYYY/MM/DD
|
|
|
- const year = parts[0];
|
|
|
- const month = String(parseInt(parts[1])).padStart(2, '0');
|
|
|
- const day = String(parseInt(parts[2])).padStart(2, '0');
|
|
|
- d = new Date(`${year}-${month}-${day}`);
|
|
|
+ const year = parts[0]
|
|
|
+ const month = String(parseInt(parts[1])).padStart(2, '0')
|
|
|
+ const day = String(parseInt(parts[2])).padStart(2, '0')
|
|
|
+ d = new Date(`${year}-${month}-${day}`)
|
|
|
} else if (parts[2].length === 4) {
|
|
|
// DD/MM/YYYY
|
|
|
- const year = parts[2];
|
|
|
- const month = String(parseInt(parts[1])).padStart(2, '0');
|
|
|
- const day = String(parseInt(parts[0])).padStart(2, '0');
|
|
|
- d = new Date(`${year}-${month}-${day}`);
|
|
|
+ const year = parts[2]
|
|
|
+ const month = String(parseInt(parts[1])).padStart(2, '0')
|
|
|
+ const day = String(parseInt(parts[0])).padStart(2, '0')
|
|
|
+ d = new Date(`${year}-${month}-${day}`)
|
|
|
} else {
|
|
|
- d = new Date(date);
|
|
|
+ d = new Date(date)
|
|
|
}
|
|
|
} else {
|
|
|
- d = new Date(date);
|
|
|
+ d = new Date(date)
|
|
|
}
|
|
|
}
|
|
|
// 处理短横线分隔格式:YYYY-M-D
|
|
|
else if (date.includes('-')) {
|
|
|
- const parts = date.split('-');
|
|
|
+ const parts = date.split('-')
|
|
|
if (parts.length === 3) {
|
|
|
- const year = parts[0];
|
|
|
- const month = String(parseInt(parts[1])).padStart(2, '0');
|
|
|
- const day = String(parseInt(parts[2])).padStart(2, '0');
|
|
|
- d = new Date(`${year}-${month}-${day}`);
|
|
|
+ const year = parts[0]
|
|
|
+ const month = String(parseInt(parts[1])).padStart(2, '0')
|
|
|
+ const day = String(parseInt(parts[2])).padStart(2, '0')
|
|
|
+ d = new Date(`${year}-${month}-${day}`)
|
|
|
} else {
|
|
|
- d = new Date(date);
|
|
|
+ d = new Date(date)
|
|
|
}
|
|
|
}
|
|
|
// 处理纯数字格式:YYYYMMDD
|
|
|
else if (/^\d{8}$/.test(date)) {
|
|
|
- const year = date.substring(0, 4);
|
|
|
- const month = date.substring(4, 6);
|
|
|
- const day = date.substring(6, 8);
|
|
|
- d = new Date(`${year}-${month}-${day}`);
|
|
|
+ const year = date.substring(0, 4)
|
|
|
+ const month = date.substring(4, 6)
|
|
|
+ const day = date.substring(6, 8)
|
|
|
+ d = new Date(`${year}-${month}-${day}`)
|
|
|
}
|
|
|
// 其他字符串格式
|
|
|
else {
|
|
|
- d = new Date(date);
|
|
|
+ d = new Date(date)
|
|
|
}
|
|
|
- }
|
|
|
+ }
|
|
|
// 处理时间戳(数字)
|
|
|
else if (typeof date === 'number') {
|
|
|
// 判断是否为13位时间戳(毫秒)或10位时间戳(秒)
|
|
|
- d = date > 10000000000 ? new Date(date) : new Date(date * 1000);
|
|
|
+ d = date > 10000000000 ? new Date(date) : new Date(date * 1000)
|
|
|
}
|
|
|
// 其他类型(如Date对象)
|
|
|
else {
|
|
|
- d = new Date(date);
|
|
|
+ d = new Date(date)
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 检查日期是否有效
|
|
|
if (isNaN(d.getTime())) {
|
|
|
- return ''; // 如果日期无效,返回空字符串
|
|
|
+ return '' // 如果日期无效,返回空字符串
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 获取年、月、日
|
|
|
- const year = d.getFullYear();
|
|
|
- const month = String(d.getMonth() + 1).padStart(2, '0'); // 月份从0开始,需要加1
|
|
|
- const day = String(d.getDate()).padStart(2, '0');
|
|
|
- return `${year}-${month}-${day}`;
|
|
|
+ const year = d.getFullYear()
|
|
|
+ const month = String(d.getMonth() + 1).padStart(2, '0') // 月份从0开始,需要加1
|
|
|
+ const day = String(d.getDate()).padStart(2, '0')
|
|
|
+ return `${year}-${month}-${day}`
|
|
|
}
|
|
|
|
|
|
export async function simulateCompleteUserAction(
|
|
@@ -240,48 +243,48 @@ export async function simulateUserInput(element, value) {
|
|
|
element.value = value
|
|
|
element.blur()
|
|
|
return
|
|
|
- }
|
|
|
- const simulateTyping = async (element, text, delay = 50) => {
|
|
|
- element.focus()
|
|
|
- for (let char of text) {
|
|
|
- await new Promise((resolve) => setTimeout(resolve, delay))
|
|
|
-
|
|
|
- // 按键按下
|
|
|
- const keydownEvent = new KeyboardEvent('keydown', {
|
|
|
- key: char,
|
|
|
- code: `Key${char.toUpperCase()}`,
|
|
|
- bubbles: true,
|
|
|
- cancelable: true
|
|
|
- })
|
|
|
- element.dispatchEvent(keydownEvent)
|
|
|
+ }
|
|
|
+ const simulateTyping = async (element, text, delay = 50) => {
|
|
|
+ element.focus()
|
|
|
+ for (let char of text) {
|
|
|
+ await new Promise((resolve) => setTimeout(resolve, delay))
|
|
|
|
|
|
- // 更新输入值
|
|
|
- element.value += char
|
|
|
+ // 按键按下
|
|
|
+ const keydownEvent = new KeyboardEvent('keydown', {
|
|
|
+ key: char,
|
|
|
+ code: `Key${char.toUpperCase()}`,
|
|
|
+ bubbles: true,
|
|
|
+ cancelable: true
|
|
|
+ })
|
|
|
+ element.dispatchEvent(keydownEvent)
|
|
|
|
|
|
- // 触发输入事件
|
|
|
- const inputEvent = new InputEvent('input', {
|
|
|
- bubbles: true,
|
|
|
- cancelable: true,
|
|
|
- data: char,
|
|
|
- inputType: 'insertText'
|
|
|
- })
|
|
|
- element.dispatchEvent(inputEvent)
|
|
|
+ // 更新输入值
|
|
|
+ element.value += char
|
|
|
|
|
|
- // 按键弹起
|
|
|
- const keyupEvent = new KeyboardEvent('keyup', {
|
|
|
- key: char,
|
|
|
- code: `Key${char.toUpperCase()}`,
|
|
|
- bubbles: true,
|
|
|
- cancelable: true
|
|
|
- })
|
|
|
- element.dispatchEvent(keyupEvent)
|
|
|
- }
|
|
|
+ // 触发输入事件
|
|
|
+ const inputEvent = new InputEvent('input', {
|
|
|
+ bubbles: true,
|
|
|
+ cancelable: true,
|
|
|
+ data: char,
|
|
|
+ inputType: 'insertText'
|
|
|
+ })
|
|
|
+ element.dispatchEvent(inputEvent)
|
|
|
|
|
|
- // 触发change事件
|
|
|
- element.dispatchEvent(new Event('change', { bubbles: true }))
|
|
|
+ // 按键弹起
|
|
|
+ const keyupEvent = new KeyboardEvent('keyup', {
|
|
|
+ key: char,
|
|
|
+ code: `Key${char.toUpperCase()}`,
|
|
|
+ bubbles: true,
|
|
|
+ cancelable: true
|
|
|
+ })
|
|
|
+ element.dispatchEvent(keyupEvent)
|
|
|
}
|
|
|
+
|
|
|
+ // 触发change事件
|
|
|
+ element.dispatchEvent(new Event('change', { bubbles: true }))
|
|
|
+ }
|
|
|
element.value = value
|
|
|
- // simulateTyping(element,value,50)
|
|
|
+ // simulateTyping(element,value,50)
|
|
|
// 创建并触发 input 事件
|
|
|
const inputEvent = new Event('input', { bubbles: true })
|
|
|
element.dispatchEvent(inputEvent)
|