xiangqinxiaochengxu/miniprogram/pages/my-matches/my-matches.js
2026-04-17 21:28:56 +08:00

92 lines
3.1 KiB
JavaScript

const request = require('../../utils/request')
const MATCH_STORAGE_KEY = 'my_matches_records'
Page({
data: {
matches: []
},
onShow() {
this.loadMatches()
},
normalizeMatch(item) {
const otherUser = item.other_user || {}
const hobbies = Array.isArray(otherUser.hobbies) ? otherUser.hobbies : []
const matchStatus = item.match_status || (item.fail_reason ? 'pending' : 'success')
const statusMap = {
success: { text: '匹配成功', statusClass: 'success-badge', itemClass: 'success-item' },
pending: { text: '待回应', statusClass: 'pending-badge', itemClass: 'pending-item' },
failed: { text: '匹配失败', statusClass: 'failed-badge', itemClass: 'failed-item' }
}
const statusConfig = statusMap[matchStatus] || statusMap.pending
return {
...item,
match_status: matchStatus,
match_status_text: item.match_status_text || statusConfig.text,
match_status_class: statusConfig.statusClass,
match_item_class: statusConfig.itemClass,
other_user: {
...otherUser,
nicknameText: otherUser.nickname ? otherUser.nickname : '匿名用户',
cityText: otherUser.city ? otherUser.city : '城市未知',
educationText: otherUser.education ? otherUser.education : '学历未知',
hobbiesText: hobbies.length ? hobbies.join('、') : '未填写爱好',
hobbies_text: hobbies.length ? hobbies.join('、') : ''
}
}
},
getStorageMatches() {
return wx.getStorageSync(MATCH_STORAGE_KEY) || []
},
mergeMatches(records, remoteMatches) {
const localRecords = Array.isArray(records) ? records : []
const remoteRecords = Array.isArray(remoteMatches) ? remoteMatches : []
const merged = [...remoteRecords]
localRecords.forEach((record) => {
const index = merged.findIndex((item) => item.match_id === record.match_id)
if (index >= 0) {
merged[index] = { ...merged[index], ...record }
} else {
merged.unshift(record)
}
})
return merged
},
async loadMatches() {
try {
const remoteMatches = await request({ url: '/matches/my' })
const storedMatches = this.getStorageMatches()
const mergedMatches = this.mergeMatches(storedMatches, remoteMatches)
const normalizedMatches = mergedMatches.length ? mergedMatches.map((item) => this.normalizeMatch(item)) : []
this.setData({ matches: normalizedMatches })
if (!remoteMatches || !remoteMatches.length) {
wx.setStorageSync(MATCH_STORAGE_KEY, mergedMatches)
}
} catch (err) {
console.error('Load my matches failed:', err)
const storedMatches = this.getStorageMatches()
this.setData({ matches: storedMatches.map((item) => this.normalizeMatch(item)) })
}
},
viewDetail(e) {
const matchId = e.currentTarget.dataset.matchId
const status = e.currentTarget.dataset.status
if (!matchId) {
return
}
if (status === 'pending') {
wx.showToast({ title: '待回应记录暂不可查看详情', icon: 'none' })
return
}
wx.navigateTo({
url: `/pages/match-detail/match-detail?match_id=${matchId}`
})
}
})