后期修改完善,上线版本
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
<div v-if="mode === 'Daily'" class="daily-picker">
|
||||
<HorizontalDateList>
|
||||
<button
|
||||
style="width: 36px"
|
||||
class="bt-d"
|
||||
v-for="day in dailyDays"
|
||||
:key="day.dateStr"
|
||||
:class="{ 'selected': day.dateStr === selectedDate }"
|
||||
@ -19,7 +19,7 @@
|
||||
<div v-else-if="mode === 'Weekly'" class="weekly-picker">
|
||||
<HorizontalDateList>
|
||||
<button
|
||||
style="margin: 0 30px; width: 105px"
|
||||
class="bt-w"
|
||||
v-for="week in weeklyRanges"
|
||||
:key="week.start"
|
||||
:class="{ 'selected': week.start === selectedDate[0] && week.end === selectedDate[1] }"
|
||||
@ -32,7 +32,7 @@
|
||||
<div v-else class="monthly-picker">
|
||||
<HorizontalDateList>
|
||||
<button
|
||||
style="margin: 0 9px; width: 55px"
|
||||
class="bt-m"
|
||||
v-for="(monthAbbr, index) in monthAbbrs"
|
||||
:key="index"
|
||||
:class="{ 'selected': (index + 1) === selectedMonth }"
|
||||
@ -47,6 +47,17 @@
|
||||
<script>
|
||||
import HorizontalDateList from '@/components/HorizontalDateList.vue';
|
||||
|
||||
// 从 sessionStorage 获取缓存数据的辅助函数
|
||||
function getCachedData() {
|
||||
try {
|
||||
const cachedData = sessionStorage.getItem('launches_search_cache');
|
||||
return cachedData ? JSON.parse(cachedData) : null;
|
||||
} catch (e) {
|
||||
// console.error('获取缓存数据失败', e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
name: 'OptionDates',
|
||||
components: {
|
||||
@ -72,9 +83,34 @@ export default {
|
||||
}
|
||||
},
|
||||
data() {
|
||||
const cachedData = getCachedData();
|
||||
let initialSelectedDate = this.mode === 'Daily' ? '' : ['', ''];
|
||||
let initialSelectedMonth = this.month;
|
||||
|
||||
// 改进:优先从缓存中获取月份
|
||||
if (cachedData && cachedData.currentMonth) {
|
||||
initialSelectedMonth = cachedData.currentMonth;
|
||||
}
|
||||
|
||||
// 如果有缓存数据,优先使用缓存数据
|
||||
if (cachedData && cachedData.lastSelectedValue) {
|
||||
try {
|
||||
const lastSelectedValue = JSON.parse(cachedData.lastSelectedValue);
|
||||
if (lastSelectedValue) {
|
||||
if (this.mode === 'Daily' && typeof lastSelectedValue === 'string') {
|
||||
initialSelectedDate = lastSelectedValue;
|
||||
} else if (this.mode !== 'Daily' && Array.isArray(lastSelectedValue)) {
|
||||
initialSelectedDate = lastSelectedValue;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('解析缓存的lastSelectedValue失败', e);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
selectedDate: this.mode === 'Daily' ? '' : ['', ''], // 选中的日期/日期范围
|
||||
selectedMonth: this.month // 月模式下选中的月份
|
||||
selectedDate: initialSelectedDate,
|
||||
selectedMonth: initialSelectedMonth,
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
@ -102,10 +138,60 @@ export default {
|
||||
}
|
||||
},
|
||||
created() {
|
||||
// 组件创建时自动选中当前日期或周
|
||||
this.autoSelectCurrentDate();
|
||||
// 先从props获取月份,避免初始为null
|
||||
this.selectedMonth = this.month;
|
||||
|
||||
// 立即尝试获取缓存数据
|
||||
this.updateFromCache();
|
||||
|
||||
// 添加一个小延迟再次尝试,确保父组件的loadSearchCache执行完毕
|
||||
setTimeout(() => {
|
||||
this.updateFromCache();
|
||||
// 强制更新UI,确保选中状态正确显示
|
||||
this.$forceUpdate();
|
||||
}, 100);
|
||||
},
|
||||
methods: {
|
||||
updateFromCache() {
|
||||
const cachedData = getCachedData();
|
||||
|
||||
if (cachedData) {
|
||||
// 优先从缓存获取currentMonth
|
||||
if (cachedData.currentMonth) {
|
||||
this.selectedMonth = cachedData.currentMonth;
|
||||
}
|
||||
|
||||
// 处理lastSelectedValue
|
||||
if (cachedData.lastSelectedValue) {
|
||||
try {
|
||||
const lastSelectedValue = JSON.parse(cachedData.lastSelectedValue);
|
||||
if (lastSelectedValue) {
|
||||
if (this.mode === 'Monthly') {
|
||||
// 月模式下,确保selectedMonth和lastSelectedValue一致
|
||||
let monthToUse = this.selectedMonth;
|
||||
// 如果缓存中有currentMonth,优先使用
|
||||
if (cachedData.currentMonth) {
|
||||
monthToUse = cachedData.currentMonth;
|
||||
this.selectedMonth = monthToUse;
|
||||
}
|
||||
// 生成与selectedMonth对应的日期范围
|
||||
const [start, end] = this.getMonthRange(this.year, monthToUse);
|
||||
// 使用$nextTick确保DOM更新后再发出事件
|
||||
this.$nextTick(() => {
|
||||
this.$emit('select', [start, end]);
|
||||
this.$emit('month-change', monthToUse);
|
||||
});
|
||||
} else if ((this.mode === 'Daily' && typeof lastSelectedValue === 'string') ||
|
||||
(this.mode !== 'Daily' && Array.isArray(lastSelectedValue))) {
|
||||
this.$emit('select', lastSelectedValue);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('使用缓存的lastSelectedValue失败', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
// 自动选中当前日期或周
|
||||
autoSelectCurrentDate(shouldEmit = true) {
|
||||
const now = new Date();
|
||||
@ -162,8 +248,15 @@ export default {
|
||||
|
||||
// 处理月模式的选择
|
||||
handleMonthSelect(month) {
|
||||
// 确保状态立即更新
|
||||
this.selectedMonth = month;
|
||||
// 强制更新UI
|
||||
this.$forceUpdate();
|
||||
|
||||
const [start, end] = this.getMonthRange(this.year, month);
|
||||
// 先发出month-change事件,确保父组件的currentMonth先更新
|
||||
this.$emit('month-change', month);
|
||||
// 然后发出select事件更新lastSelectedValue,包含正确的时间格式
|
||||
this.$emit('select', [start, end]);
|
||||
},
|
||||
|
||||
@ -184,7 +277,15 @@ export default {
|
||||
getMonthRange(year, month) {
|
||||
const start = new Date(year, month - 1, 1);
|
||||
const end = new Date(year, month, 0);
|
||||
return [this.formatDate(start), this.formatDate(end)];
|
||||
const formatDateWithTime = (date, isStart = true) => {
|
||||
const year = date.getFullYear();
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(date.getDate()).padStart(2, '0');
|
||||
const time = isStart ? '00:00:00' : '23:59:59';
|
||||
return `${year}-${month}-${day} ${time}`;
|
||||
};
|
||||
return [formatDateWithTime(start), formatDateWithTime(end, false)];
|
||||
|
||||
},
|
||||
|
||||
// 辅助:获取某年的所有完整周区间(假设周从周日开始,到周六结束)
|
||||
@ -253,23 +354,27 @@ export default {
|
||||
month() {
|
||||
if (this.mode === 'Daily') {
|
||||
this.selectedDate = '';
|
||||
this.autoSelectCurrentDate();
|
||||
}
|
||||
this.autoSelectCurrentDate();
|
||||
}
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.date-picker-core {
|
||||
padding: 10px;
|
||||
.bt-d {
|
||||
width: 36px; margin: 0 10px;
|
||||
}
|
||||
.bt-w {
|
||||
margin: 0 30px; width: 105px;
|
||||
}
|
||||
.bt-m {
|
||||
margin: 0 20px; width: 55px;
|
||||
}
|
||||
|
||||
.picker-container {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 8px;
|
||||
padding: 12px;
|
||||
border-radius: 4px;
|
||||
width: 100%;
|
||||
box-sizing: border-box;
|
||||
|
||||
Reference in New Issue
Block a user