feat: 数据时间按当前日期提示范围,运行时再计算
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, h, onMounted, ref } from "vue";
|
||||
import type { VNode } from "vue";
|
||||
import { NButton, NIcon, NInput, NInputNumber, NModal, NSelect, NSwitch, NTimePicker, NTooltip, useDialog } from "naive-ui";
|
||||
import type { SelectRenderLabel, SelectRenderTag } from "naive-ui";
|
||||
import { CreateOutline, InformationCircleOutline, PlayOutline, TrashOutline } from "@vicons/ionicons5";
|
||||
@@ -71,6 +72,55 @@ const timeOptions = [
|
||||
];
|
||||
const weekdays = ["一", "二", "三", "四", "五", "六", "日"].map((label, index) => ({ label: `周${label}`, value: index + 1 }));
|
||||
const monthDays = Array.from({ length: 31 }, (_, index) => ({ label: `${index + 1} 日`, value: index + 1 }));
|
||||
function previewWindow(mode: string, count: number, today = new Date()) {
|
||||
const day = new Date(today.getFullYear(), today.getMonth(), today.getDate());
|
||||
const fixed = mode === "prev_month" || mode === "prev_week" || mode === "prev_day";
|
||||
const amount = fixed ? 1 : Math.min(366, Math.max(1, Number(count) || 1));
|
||||
if (mode === "prev_month" || mode === "prev_months") {
|
||||
const first = new Date(day.getFullYear(), day.getMonth(), 1);
|
||||
return {
|
||||
start: new Date(first.getFullYear(), first.getMonth() - amount, 1),
|
||||
end: new Date(first.getFullYear(), first.getMonth(), 0),
|
||||
};
|
||||
}
|
||||
if (mode === "prev_week" || mode === "prev_weeks") {
|
||||
const weekday = (day.getDay() + 6) % 7;
|
||||
const monday = new Date(day);
|
||||
monday.setDate(day.getDate() - weekday);
|
||||
const end = new Date(monday);
|
||||
end.setDate(monday.getDate() - 1);
|
||||
const start = new Date(monday);
|
||||
start.setDate(monday.getDate() - 7 * amount);
|
||||
return { start, end };
|
||||
}
|
||||
const end = new Date(day);
|
||||
end.setDate(day.getDate() - 1);
|
||||
const start = new Date(day);
|
||||
start.setDate(day.getDate() - amount);
|
||||
return { start, end };
|
||||
}
|
||||
|
||||
function formatDay(value: Date) {
|
||||
const pad = (part: number) => String(part).padStart(2, "0");
|
||||
return `${value.getFullYear()}-${pad(value.getMonth() + 1)}-${pad(value.getDate())}`;
|
||||
}
|
||||
|
||||
function rangeText(mode: string, count = timeCount.value) {
|
||||
const { start, end } = previewWindow(mode, count);
|
||||
const startText = formatDay(start);
|
||||
const endText = formatDay(end);
|
||||
return startText === endText ? startText : `${startText} 至 ${endText}`;
|
||||
}
|
||||
|
||||
const timeHint = computed(() => `现在看来是 ${rangeText(timeMode.value)},运行时按当天重新计算`);
|
||||
const renderTimeOption = ({ node, option }: { node: VNode; option: { label?: string; value?: string | number } }) => h(
|
||||
"div",
|
||||
node.props ?? {},
|
||||
[
|
||||
h("span", String(option.label ?? "")),
|
||||
h("span", { style: "display:block;margin-top:2px;color:var(--muted);font-size:12px;font-weight:400;line-height:1.3" }, rangeText(String(option.value ?? ""))),
|
||||
],
|
||||
);
|
||||
const needsCount = computed(() => ["prev_weeks", "prev_months", "prev_days"].includes(timeMode.value));
|
||||
const countLabel = computed(() => (timeMode.value === "prev_weeks" ? "周数" : timeMode.value === "prev_months" ? "月数" : "天数"));
|
||||
const tableOptions = computed(() => groups.value.map((group) => ({
|
||||
@@ -388,7 +438,13 @@ onMounted(load);
|
||||
<n-button class="picked-link" text type="primary" :disabled="!chosen.length" @click="details = true">已选 {{ chosen.length }}</n-button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-row"><label>数据时间</label><n-select v-model:value="timeMode" :options="timeOptions" /></div>
|
||||
<div class="form-row">
|
||||
<label>数据时间</label>
|
||||
<div class="time-field">
|
||||
<n-select v-model:value="timeMode" :options="timeOptions" :render-option="renderTimeOption" />
|
||||
<p class="time-hint">{{ timeHint }}</p>
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="needsCount" class="form-row"><label>{{ countLabel }}</label><n-input-number v-model:value="timeCount" :min="1" :max="366" :show-button="false" /></div>
|
||||
<div class="form-row"><label>城市</label><n-input v-model:value="city" placeholder="不填则不按城市过滤" /></div>
|
||||
<div class="form-row">
|
||||
@@ -469,6 +525,8 @@ onMounted(load);
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.time-field { display: grid; gap: 6px; min-width: 0; }
|
||||
.time-hint { margin: 0; color: var(--muted); font-size: 12px; line-height: 1.4; }
|
||||
.schedule-time { display: grid; grid-template-columns: minmax(0, 1fr) 128px; gap: 8px; }
|
||||
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
|
||||
.picked-list { display: grid; gap: 8px; }
|
||||
|
||||
Reference in New Issue
Block a user