fix: align report date defaults with recent periods

This commit is contained in:
Yvan
2026-11-05 14:26:36 +08:00
parent 31584c73bf
commit 22ba319f7f
16 changed files with 181 additions and 70 deletions
+3 -2
View File
@@ -25,12 +25,12 @@ import { computed, onMounted, reactive, ref } from 'vue'
import { listRankings } from '@/api/ranking'
import { channelCount, channelMoney } from './channel-display'
import CompactRankingTable from './components/CompactRankingTable.vue'
import { normalizeReportDates } from './report-dates'
defineOptions({ name: 'RankingAnalysis' })
const platforms = { lx: '龙翔', hw: '汇玩', yq: '游趣' }, dimensions = { channel: '渠道', business: '商务', game: '游戏' }
const sources = ref(Object.keys(platforms)), dimension = ref('channel')
const ranges = [{ value: 'today', label: '今日' }, { value: 'yesterday', label: '昨日' }, { value: '7d', label: '近7天' }, { value: '30d', label: '近30天' }]
const ymd = value => `${value.getFullYear()}-${String(value.getMonth() + 1).padStart(2, '0')}-${String(value.getDate()).padStart(2, '0')}`
const dateRange = range => { const e = new Date(), b = new Date(); if (range === 'yesterday') { e.setDate(e.getDate() - 1); b.setDate(b.getDate() - 1) } else if (range === '7d') b.setDate(b.getDate() - 6); else if (range === '30d') b.setDate(b.getDate() - 29); return [ymd(b), ymd(e)] }
const dateRange = range => { const end = new Date(); if (range === 'yesterday') end.setDate(end.getDate() - 1); return normalizeReportDates(undefined, range === '7d' ? 7 : range === '30d' ? 30 : 1, end) }
const memory = reactive(Object.fromEntries(Object.keys(dimensions).map(key => [key, { range: '7d', dates: dateRange('7d') }])))
const dates = computed(() => memory[dimension.value].dates)
const result = ref(null), available = ref(true), loading = ref(false), error = ref('')
@@ -49,6 +49,7 @@ const displayRows = panel => {
}
const metric = (panel, value) => value == null ? '—' : panel.money ? channelMoney(value) : panel.rate ? `${value}%` : channelCount(value)
const load = async () => {
memory[dimension.value].dates = normalizeReportDates(dates.value, 7)
const current = ++version; loading.value = true; result.value = null; error.value = ''; available.value = true
try {
// 旧排行榜空选平台表示全部,保留此语义;其他报表页面有各自的空选规则。
+4 -10
View File
@@ -36,19 +36,11 @@ import PlatformTag from './components/PlatformTag.vue'
import MetricValue from './components/MetricValue.vue'
import { metricTips, remoteSort, sortDescription } from './table-display'
import { analysisGameSelection } from './analysis-game-selection'
import { formatReportDate as ymd, normalizeReportDates, reportDateShortcuts as shortcuts } from './report-dates'
defineOptions({ name: 'ServerAnalysis' })
const platforms = { lx: '龙翔', hw: '汇玩', yq: '游趣' }
const day = () => { const n = new Date(); return new Date(n.getFullYear(), n.getMonth(), n.getDate()) }
const ymd = value => `${value.getFullYear()}-${String(value.getMonth() + 1).padStart(2, '0')}-${String(value.getDate()).padStart(2, '0')}`
const end = day(), begin = day(); begin.setDate(begin.getDate() - 29)
const dates = ref([ymd(begin), ymd(end)])
const shortcuts = [
{ text: '今日', value: () => [day(), day()] },
{ text: '昨日', value: () => { const d = day(); d.setDate(d.getDate() - 1); return [d, new Date(d)] } },
{ text: '本周', value: () => { const e = day(), b = day(); b.setDate(b.getDate() - (b.getDay() || 7) + 1); return [b, e] } },
{ text: '本月', value: () => { const e = day(); return [new Date(e.getFullYear(), e.getMonth(), 1), e] } }
]
const dates = ref(normalizeReportDates())
const sources = ref(Object.keys(platforms)), games = ref([]), gameOptions = ref([]), serverOptions = ref([])
const selectedGame = computed({ get: () => games.value[0] || '', set: value => { games.value = value ? [value] : [] } })
const query = reactive({ pageNum: 1, pageSize: 10, serverNum: '', sortField: 'totalPay', sortOrder: 'desc' })
@@ -71,6 +63,8 @@ const summaries = ({ columns: tableColumns }) => tableColumns.map((column, index
const clear = () => { ++version; rows.value = []; total.value = 0; summary.value = null; result.value = null; loading.value = false; error.value = ''; available.value = true }
const params = () => ({ ...query, sourceDb: sources.value.join(','), gameName: games.value.join(','), beginTime: dates.value[0], endTime: dates.value[1] })
const load = async () => {
dates.value = normalizeReportDates(dates.value)
if (dates.value[0] > dates.value[1]) { ElMessage.warning('统计开始时间不能晚于结束时间'); return }
if (!sources.value.length || !games.value.length) { clear(); return }
const current = ++version; loading.value = true; error.value = ''
try {
+4 -10
View File
@@ -54,6 +54,7 @@ import { createGameOptionsLoader } from '../game-options-loader'
import { remoteSort, sortDescription } from '../table-display'
import PlatformTag from './PlatformTag.vue'
import MetricValue from './MetricValue.vue'
import { formatReportDate as ymd, normalizeReportDates, reportDateShortcuts as shortcuts } from '../report-dates'
const fetchGameOptions = createGameOptionsLoader(listLedgerGames)
const props = defineProps({ kind: { type: String, required: true } })
const spend = computed(() => props.kind === 'spend')
@@ -63,16 +64,7 @@ const canExport = computed(() => Number(user.userInfo.authorityId) === 888 || !!
const platforms = { lx: '龙翔', hw: '汇玩', yq: '游趣' }
const payWays = { 1: '绑币', 2: '平台币', 3: '支付宝', 4: '微信', 5: '金猪', 6: '苹果支付', 11: '盒子币', 12: '龙翔币', 14: '抖音小游戏' }
const statuses = { 1: '成功', 2: '处理中', 0: '失败' }
const today = () => { const now = new Date(); return new Date(now.getFullYear(), now.getMonth(), now.getDate()) }
const ymd = date => `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
const end = today(), begin = today(); begin.setDate(begin.getDate() - 29)
const dates = ref([ymd(begin), ymd(end)])
const shortcuts = [
{ text: '今日', value: () => [today(), today()] },
{ text: '昨日', value: () => { const d = today(); d.setDate(d.getDate() - 1); return [d, new Date(d)] } },
{ text: '本周', value: () => { const e = today(), b = today(); b.setDate(b.getDate() - (b.getDay() || 7) + 1); return [b, e] } },
{ text: '本月', value: () => { const e = today(); return [new Date(e.getFullYear(), e.getMonth(), 1), e] } }
]
const dates = ref(normalizeReportDates())
const sources = ref(Object.keys(platforms)), gameNames = ref([]), gameOptions = ref([]), playerID = ref('')
const gamesLoading = ref(false), exporting = ref(false), loading = ref(false), available = ref(true)
const error = ref('')
@@ -82,6 +74,8 @@ const currentSort = computed(() => sortDescription(query, { id: 'ID', payTime: '
const sortChanged = event => { Object.assign(query, remoteSort(event, defaultSortField)); return search() }
let listVersion = 0, gameVersion = 0
const buildQuery = () => {
dates.value = normalizeReportDates(dates.value)
if (dates.value[0] > dates.value[1]) { ElMessage.warning('开始日期不能晚于结束日期'); return null }
const player = playerID.value.trim()
if (player && !/^\d+$/.test(player)) { ElMessage.warning('玩家ID应为整数'); return null }
const result = { ...query, sourceDb: sources.value.join(','), gameName: gameNames.value.join(','), userId: player || undefined, busierAccount: query.busierAccount.trim(), promoteAccount: query.promoteAccount.trim(), gamePlayerName: query.gamePlayerName.trim(), payOrderNumber: query.payOrderNumber.trim() }
@@ -47,18 +47,12 @@ import MetricValue from './MetricValue.vue'
import ChannelProfileDrawer from './ChannelProfileDrawer.vue'
import { metricTips, remoteSort, sortDescription } from '../table-display'
import { analysisGameSelection } from '../analysis-game-selection'
import { normalizeReportDates, reportDateShortcuts as shortcuts } from '../report-dates'
const fetchGameOptions = createGameOptionsLoader(listLedgerGames)
const props = defineProps({ kind: { type: String, required: true } })
const isChannel = computed(() => props.kind === 'channel')
const platforms = { lx: '龙翔', hw: '汇玩', yq: '游趣' }
const day = () => { const n = new Date(); return new Date(n.getFullYear(), n.getMonth(), n.getDate()) }
const ymd = value => `${value.getFullYear()}-${String(value.getMonth() + 1).padStart(2, '0')}-${String(value.getDate()).padStart(2, '0')}`
const dates = ref(isChannel.value ? ['', ''] : [ymd(day()), ymd(day())])
const shortcuts = [
{ text: '今日', value: () => [day(), day()] }, { text: '昨日', value: () => { const d = day(); d.setDate(d.getDate() - 1); return [d, new Date(d)] } },
{ text: '本周', value: () => { const e = day(), b = day(); b.setDate(b.getDate() - (b.getDay() || 7) + 1); return [b, e] } },
{ text: '本月', value: () => { const e = day(); return [new Date(e.getFullYear(), e.getMonth(), 1), e] } }
]
const dates = ref(normalizeReportDates())
const startShortcuts = shortcuts.map(item => ({ text: item.text, value: () => item.value()[0] }))
const endShortcuts = shortcuts.map(item => ({ text: item.text, value: () => item.value()[1] }))
const sources = ref(Object.keys(platforms)), games = ref([]), gameOptions = ref([]), gameLoading = ref(false)
@@ -80,7 +74,8 @@ const profileOpen = ref(false), profileChannel = ref(null)
const openChannel = row => { profileChannel.value = { id: row.promoteId, sourceDb: row.sourceDb, account: row.promoteName }; profileOpen.value = true }
const clear = () => { rows.value = []; total.value = 0; summary.value = null; result.value = null; loadedQuery.value = null }
const load = async () => {
if (dates.value[0] && dates.value[1] && dates.value[0] > dates.value[1]) { ElMessage.warning('统计开始时间不能晚于结束时间'); return }
dates.value = normalizeReportDates(dates.value)
if (dates.value[0] > dates.value[1]) { ElMessage.warning('统计开始时间不能晚于结束时间'); return }
const current = ++version
if (!sources.value.length) { clear(); loading.value = false; available.value = true; return }
loading.value = true; error.value = ''
+23
View File
@@ -0,0 +1,23 @@
export const formatReportDate = date => `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
const today = () => {
const now = new Date()
return new Date(now.getFullYear(), now.getMonth(), now.getDate())
}
// Match the API defaults and fill only missing bounds. Explicit historical
// periods remain selectable; clearing dates cannot trigger an unbounded query.
export function normalizeReportDates(dates, days = 30, now = new Date()) {
const end = dates?.[1] || formatReportDate(now)
const start = new Date(`${end}T00:00:00`)
start.setDate(start.getDate() - days + 1)
return [dates?.[0] || formatReportDate(start), end]
}
export const reportDateShortcuts = [
{ text: '近30天', value: () => { const end = today(), start = today(); start.setDate(start.getDate() - 29); return [start, end] } },
{ text: '今日', value: () => [today(), today()] },
{ text: '昨日', value: () => { const day = today(); day.setDate(day.getDate() - 1); return [day, new Date(day)] } },
{ text: '本周', value: () => { const end = today(), start = today(); start.setDate(start.getDate() - (start.getDay() || 7) + 1); return [start, end] } },
{ text: '本月', value: () => { const end = today(); return [new Date(end.getFullYear(), end.getMonth(), 1), end] } }
]
+32
View File
@@ -0,0 +1,32 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { normalizeReportDates, reportDateShortcuts, formatReportDate } from './report-dates.js'
test('report defaults include today and span 30 calendar days across month, year and leap-day boundaries', () => {
for (const [today, start] of [['2026-11-05', '2026-10-07'], ['2026-01-05', '2025-12-07'], ['2024-03-01', '2024-02-01']]) {
const now = new Date(`${today}T13:45:00`)
for (const empty of [undefined, null, [], ['', ''], [null, null]]) {
assert.deepEqual(normalizeReportDates(empty, 30, now), [start, today])
}
}
})
test('partial date inputs preserve the selected bound, and explicit history is never clamped', () => {
const now = new Date(2026, 10, 5, 13)
assert.deepEqual(normalizeReportDates(['', '2024-03-01'], 30, now), ['2024-02-01', '2024-03-01'])
assert.deepEqual(normalizeReportDates(['2026-01-01', ''], 30, now), ['2026-01-01', '2026-11-05'])
const history = ['2023-01-01', '2023-12-31']
assert.deepEqual(normalizeReportDates(history, 30, now), history)
assert.deepEqual(history, ['2023-01-01', '2023-12-31'])
})
test('rankings keep their seven-day default and reversed ranges remain available for validation', () => {
const now = new Date(2026, 10, 5)
assert.deepEqual(normalizeReportDates(undefined, 7, now), ['2026-10-30', '2026-11-05'])
assert.deepEqual(normalizeReportDates(['2026-11-05', '2026-11-01'], 30, now), ['2026-11-05', '2026-11-01'])
})
test('the recent-30-days shortcut restores the default query interval', () => {
const [start, end] = reportDateShortcuts.find(item => item.text === '近30天').value()
assert.deepEqual([formatReportDate(start), formatReportDate(end)], normalizeReportDates(undefined, 30, end))
})