77 lines
2.0 KiB
TypeScript
77 lines
2.0 KiB
TypeScript
import { mockChatHistory } from '../../utils/mock-data'
|
||
import { sortByTimestamp } from '../../utils/sort'
|
||
|
||
/** 带展示标签的对话历史项 */
|
||
interface ChatHistoryDisplay {
|
||
id: string
|
||
title: string
|
||
lastMessage: string
|
||
timestamp: string
|
||
customerName?: string
|
||
/** 格式化后的时间标签 */
|
||
timeLabel: string
|
||
}
|
||
|
||
Page({
|
||
data: {
|
||
/** 页面状态:loading / empty / normal */
|
||
pageState: 'loading' as 'loading' | 'empty' | 'normal',
|
||
/** 对话历史列表 */
|
||
list: [] as ChatHistoryDisplay[],
|
||
},
|
||
|
||
onLoad() {
|
||
this.loadData()
|
||
},
|
||
|
||
/** 加载数据 */
|
||
loadData() {
|
||
this.setData({ pageState: 'loading' })
|
||
|
||
setTimeout(() => {
|
||
// TODO: 替换为真实 API 调用
|
||
const sorted = sortByTimestamp(mockChatHistory)
|
||
const list: ChatHistoryDisplay[] = sorted.map((item) => ({
|
||
...item,
|
||
timeLabel: this.formatTime(item.timestamp),
|
||
}))
|
||
|
||
this.setData({
|
||
list,
|
||
pageState: list.length === 0 ? 'empty' : 'normal',
|
||
})
|
||
}, 400)
|
||
},
|
||
|
||
/** 格式化时间为相对标签 */
|
||
formatTime(timestamp: string): string {
|
||
const now = new Date()
|
||
const target = new Date(timestamp)
|
||
const diffMs = now.getTime() - target.getTime()
|
||
const diffMin = Math.floor(diffMs / 60000)
|
||
const diffHour = Math.floor(diffMs / 3600000)
|
||
const diffDay = Math.floor(diffMs / 86400000)
|
||
|
||
if (diffMin < 1) return '刚刚'
|
||
if (diffMin < 60) return `${diffMin}分钟前`
|
||
if (diffHour < 24) return `${diffHour}小时前`
|
||
if (diffDay < 7) return `${diffDay}天前`
|
||
|
||
const month = target.getMonth() + 1
|
||
const day = target.getDate()
|
||
return `${month}月${day}日`
|
||
},
|
||
|
||
/** 点击对话记录 → 跳转 chat 页面 */
|
||
onItemTap(e: WechatMiniprogram.TouchEvent) {
|
||
const id = e.currentTarget.dataset.id
|
||
wx.navigateTo({ url: '/pages/chat/chat?historyId=' + id })
|
||
},
|
||
|
||
/** 下拉刷新 */
|
||
onPullDownRefresh() {
|
||
this.loadData()
|
||
setTimeout(() => wx.stopPullDownRefresh(), 600)
|
||
},
|
||
})
|