fit:工单修改和商品关联修改

This commit is contained in:
2025-12-10 20:17:13 +08:00
parent a09631551b
commit 0fe4ece1a9
5 changed files with 173 additions and 51 deletions
+134 -27
View File
@@ -545,7 +545,7 @@ const fetchTicketMessages = async (workId) => {
id: msg.id,
content: msg.content !== 'empty' ? msg.content : null,
images: images,
time: new Date().toLocaleString(), // API 没有返回时间,使用当前时间
time: msg.created_at || msg.updated_at || new Date().toLocaleString(),
isAdmin: isAdminMsg,
isSystem: false,
userId: msg.user?.userId,
@@ -730,42 +730,149 @@ const updateTicketStats = () => {
// 格式化消息时间
const formatMessageTime = (timeStr) => {
const date = new Date(timeStr)
return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
if (!timeStr) return ''
try {
const date = new Date(timeStr)
if (isNaN(date.getTime())) return ''
// 格式化为 HH:MM
const hours = String(date.getHours()).padStart(2, '0')
const minutes = String(date.getMinutes()).padStart(2, '0')
return `${hours}:${minutes}`
} catch (e) {
console.error('时间格式化失败:', e)
return ''
}
}
// 格式化列表项时间
const formatTime = (timeStr) => {
const date = new Date(timeStr)
const now = new Date()
const diff = now - date
// 今天内的消息只显示时间
if (diff < 24 * 60 * 60 * 1000 && date.getDate() === now.getDate()) {
return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
if (!timeStr) return ''; // 空值兜底
// 步骤1:解析中文时间字符串(核心适配点)
let date;
try {
// 先尝试原生解析(兼容ISO格式)
date = new Date(timeStr);
// 若原生解析失败(返回Invalid Date),解析中文格式
if (isNaN(date.getTime())) {
// 正则提取中文时间的年、月、日、时、分、秒
const cnTimeMatch = timeStr.match(
/(\d{4})年(\d{1,2})月(\d{1,2})日\s*(上午|下午)\s*(\d{1,2}):(\d{1,2}):(\d{1,2})/
);
if (cnTimeMatch) {
const [, year, month, day, period, hour, minute, second] = cnTimeMatch;
// 处理下午/上午的小时转换(12小时制转24小时制)
let hour24 = parseInt(hour, 10);
if (period === '下午' && hour24 !== 12) {
hour24 += 12;
}
if (period === '上午' && hour24 === 12) {
hour24 = 0; // 上午12点转为0点
}
// 构造日期(月份从0开始,需-1)
date = new Date(
parseInt(year, 10),
parseInt(month, 10) - 1,
parseInt(day, 10),
hour24,
parseInt(minute, 10),
parseInt(second, 10)
);
} else {
return '无效时间'; // 既不是ISO也不是中文格式
}
}
} catch (e) {
console.error('时间解析失败:', e);
return '无效时间';
}
const now = new Date();
const dateTime = date.getTime();
const nowTime = now.getTime();
const diff = nowTime - dateTime;
// 步骤2:判断“今天”(年/月/日完全一致)
const isToday = date.getFullYear() === now.getFullYear() &&
date.getMonth() === now.getMonth() &&
date.getDate() === now.getDate();
// 一周内的显示星期几
if (diff < 7 * 24 * 60 * 60 * 1000) {
const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
return weekdays[date.getDay()]
if (isToday) {
// 格式化今天的时间(24小时制,补零)
const hour = String(date.getHours()).padStart(2, '0');
const minute = String(date.getMinutes()).padStart(2, '0');
return `${hour}:${minute}`;
}
// 其他显示日期
return date.toLocaleDateString()
}
// 步骤3:判断“一周内”
const oneWeek = 7 * 24 * 60 * 60 * 1000;
if (diff < oneWeek) {
const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
return weekdays[date.getDay()];
}
// 步骤4:格式化其他日期(补零,统一格式:YYYY/MM/DD)
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}`;
};
// 格式化日期显示
const formatDate = (timeStr) => {
const date = new Date(timeStr)
const now = new Date()
if (date.toDateString() === now.toDateString()) {
return '今天'
console.log("原始时间字符串:", timeStr);
if (!timeStr) return ''; // 空值兜底
let date;
// 1. 先尝试原生解析(兼容ISO等标准格式)
date = new Date(timeStr);
// 2. 若原生解析失败,专门解析中文时间格式
if (isNaN(date.getTime())) {
// 正则匹配:xxxx年xx月xx日 上午/下午 xx:xx:xx
const cnTimeReg = /(\d{4})年(\d{1,2})月(\d{1,2})日\s*(上午|下午)\s*(\d{1,2}):(\d{1,2}):(\d{1,2})/;
const match = timeStr.match(cnTimeReg);
if (match) {
const [, year, month, day, period, hour, minute, second] = match;
// 处理12小时制转24小时制(关键适配)
let hour24 = parseInt(hour, 10);
if (period === '下午') {
hour24 = hour24 === 12 ? 12 : hour24 + 12; // 下午12点=12点,下午1-11点+12
} else { // 上午
hour24 = hour24 === 12 ? 0 : hour24; // 上午12点=0点,上午1-11点不变
}
// 手动构造合法的Date对象(月份从0开始,需-1)
date = new Date(
parseInt(year, 10),
parseInt(month, 10) - 1,
parseInt(day, 10),
hour24,
parseInt(minute, 10),
parseInt(second, 10)
);
} else {
return '无效时间'; // 非目标格式,返回兜底
}
}
return date.toLocaleDateString()
}
const now = new Date();
// 3. 对比“今天”(按日期维度,忽略时分秒)
const isToday = date.getFullYear() === now.getFullYear() &&
date.getMonth() === now.getMonth() &&
date.getDate() === now.getDate();
if (isToday) {
return '今天';
}
// 4. 格式化非今天的日期(统一格式,避免环境差异)
const formattedDate = `${date.getFullYear()}/${String(date.getMonth() + 1).padStart(2, '0')}/${String(date.getDate()).padStart(2, '0')}`;
return formattedDate;
};
// 监听显示工单变化,更新消息和滚动
watch(currentTicket, (newVal) => {
@@ -937,7 +1044,7 @@ const refreshTicketMessages = async (workId) => {
id: msg.id,
content: msg.content !== 'empty' ? msg.content : null,
images: images,
time: new Date().toLocaleString(), // API 没有返回时间,使用当前时间
time: msg.created_at || msg.updated_at || new Date().toLocaleString(),
isAdmin: isAdminMsg,
isSystem: false,
userId: msg.user?.userId,