110 lines
3.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 购买记录管理 Composable
*/
import { ref } from 'vue'
import { getIssueDrawLogs } from '@/api/appUser'
import { levelToAlpha } from '@/utils/activity'
/**
* 购买记录管理
*/
export function useRecords() {
const winRecords = ref([])
const loading = ref(false)
/**
* 获取购买记录
* @param {string} activityId - 活动ID
* @param {string} issueId - 期ID
*/
async function fetchWinRecords(activityId, issueId) {
if (!activityId || !issueId) return
loading.value = true
try {
const res = await getIssueDrawLogs(activityId, issueId)
const list = (res && res.list) || (Array.isArray(res) ? res : [])
// 直接使用原始记录列表,不进行聚合
// 映射字段以符合 RecordsList 组件的展示需求
winRecords.value = list.map(it => ({
id: it.id,
title: it.reward_name || it.title || it.name || '-', // 奖品名称
image: it.reward_image || it.image || '', // 奖品图片
count: 1, // 单个记录数量为1
// 用户信息
user_id: it.user_id,
user_name: it.user_name || '匿名用户',
avatar: cleanAvatar(it.avatar), // 清理 avatar 数据
// 时间信息
created_at: it.created_at,
// 其他元数据
is_winner: it.is_winner,
level: it.level,
level_name: getLevelName(it.level)
}))
} catch (e) {
console.error('fetchWinRecords error', e)
winRecords.value = []
} finally {
loading.value = false
}
}
function getLevelName(level) {
if (!level) return ''
const alpha = levelToAlpha(level)
return alpha + '赏'
}
/**
* 清理和验证 avatar 数据
* @param {string} avatar - 原始 avatar 数据(可能是 base64 或 URL
* @returns {string} - 清理后的 avatar 数据
*/
function cleanAvatar(avatar) {
if (!avatar) return ''
// 如果是 base64 格式,确保格式正确
const avatarStr = String(avatar).trim()
// 检查是否已经是 data:image 格式
if (avatarStr.startsWith('data:image/')) {
return avatarStr
}
// 如果是 http(s) URL直接返回
if (avatarStr.startsWith('http://') || avatarStr.startsWith('https://')) {
return avatarStr
}
// 如果是相对路径,直接返回
if (avatarStr.startsWith('/')) {
return avatarStr
}
// 其他情况,可能是不完整的 base64尝试修复
// 如果不包含 data:image 前缀,添加默认的 png 前缀
if (avatarStr.match(/^[A-Za-z0-9+/=]+$/)) {
// 看起来像 base64 编码
return `data:image/png;base64,${avatarStr}`
}
return avatarStr
}
function clearRecords() {
winRecords.value = []
}
return {
winRecords,
loading,
fetchWinRecords,
clearRecords
}
}