84 lines
2.3 KiB
JavaScript
84 lines
2.3 KiB
JavaScript
import { ref, reactive } from 'vue'
|
|
import storage from '@/utils/storage'
|
|
import { mapFromDouyin, mapFromXhs } from '../utils/benchmarkUtils'
|
|
|
|
const TABLE_DATA_STORAGE_KEY = 'benchmark_table_data'
|
|
|
|
export function useBenchmarkData() {
|
|
const data = ref([])
|
|
const selectedRowKeys = ref([])
|
|
const expandedRowKeys = ref([])
|
|
|
|
async function saveTableDataToSession() {
|
|
try {
|
|
const persistData = (data.value || []).map((item) => {
|
|
const rest = { ...item }
|
|
delete rest._analyzing
|
|
return rest
|
|
})
|
|
await storage.setJSON(TABLE_DATA_STORAGE_KEY, persistData)
|
|
} catch (error) {
|
|
console.warn('保存表格数据到session失败:', error)
|
|
}
|
|
}
|
|
|
|
async function loadTableDataFromSession() {
|
|
try {
|
|
const savedData = await storage.getJSON(TABLE_DATA_STORAGE_KEY)
|
|
if (savedData?.length) {
|
|
data.value = savedData.map((item) => ({ ...item, _analyzing: false }))
|
|
console.log('从session加载了表格数据:', savedData.length, '条')
|
|
}
|
|
} catch (error) {
|
|
console.warn('从session加载表格数据失败:', error)
|
|
}
|
|
}
|
|
|
|
function processApiResponse(resp, platform) {
|
|
if (platform === '抖音') {
|
|
const awemeList = resp?.data?.aweme_list || []
|
|
console.log('抖音返回的原始数据:', awemeList[0])
|
|
data.value = mapFromDouyin(awemeList)
|
|
console.log('映射后的第一条数据:', data.value[0])
|
|
|
|
return {
|
|
maxCursor: resp?.data?.max_cursor || 0,
|
|
hasMore: resp?.data?.has_more || !(resp?.data?.aweme_list?.length < (resp?.data?.count || 20)),
|
|
count: data.value.length,
|
|
}
|
|
} else {
|
|
const notes = resp?.data?.notes || resp?.data?.data || []
|
|
data.value = mapFromXhs(notes)
|
|
|
|
return {
|
|
maxCursor: resp?.data?.max_cursor || 0,
|
|
hasMore: resp?.data?.has_more || false,
|
|
count: data.value.length,
|
|
}
|
|
}
|
|
}
|
|
|
|
function appendData(newItems) {
|
|
data.value = [...data.value, ...newItems]
|
|
}
|
|
|
|
async function clearData() {
|
|
data.value = []
|
|
selectedRowKeys.value = []
|
|
expandedRowKeys.value = []
|
|
await storage.remove(TABLE_DATA_STORAGE_KEY)
|
|
}
|
|
|
|
return {
|
|
data,
|
|
selectedRowKeys,
|
|
expandedRowKeys,
|
|
saveTableDataToSession,
|
|
loadTableDataFromSession,
|
|
processApiResponse,
|
|
appendData,
|
|
clearData,
|
|
}
|
|
}
|
|
|