mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:59:19 +08:00
feat: 优化若干
This commit is contained in:
@@ -6,8 +6,8 @@ import type {
|
||||
CompanyOverviewVO,
|
||||
} from '#/api/ecommerce/company-overview';
|
||||
|
||||
import { onMounted, ref, watch } from 'vue';
|
||||
import { useRouter } from 'vue-router';
|
||||
import { onActivated, onMounted, ref, watch } from 'vue';
|
||||
import { useRoute, useRouter } from 'vue-router';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import { EchartsUI, useEcharts } from '@vben/plugins/echarts';
|
||||
@@ -22,6 +22,7 @@ defineOptions({ name: 'CompanyOverview' });
|
||||
|
||||
// 路由
|
||||
const router = useRouter();
|
||||
const route = useRoute();
|
||||
|
||||
// 响应式数据
|
||||
const loading = ref(false);
|
||||
@@ -41,6 +42,8 @@ const activeTimeRange = ref<
|
||||
|
||||
// 自定义日期范围
|
||||
const customDateRange = ref<[dayjs.Dayjs, dayjs.Dayjs] | undefined>(undefined);
|
||||
const lastRequestKey = ref('');
|
||||
const isSyncingRouteQuery = ref(false);
|
||||
|
||||
// 切换时间范围
|
||||
const switchTimeRange = (
|
||||
@@ -97,6 +100,86 @@ const handleCustomDateRangeChange = (
|
||||
}
|
||||
};
|
||||
|
||||
const setTimeRangeTypeByDates = (startDate: dayjs.Dayjs, endDate: dayjs.Dayjs) => {
|
||||
const today = dayjs();
|
||||
const daysDiff = endDate.diff(startDate, 'day');
|
||||
const isEndDateToday = endDate.isSame(today, 'day');
|
||||
const isStartDate7DaysAgo = startDate.isSame(dayjs().subtract(7, 'day'), 'day');
|
||||
const isStartDate30DaysAgo = startDate.isSame(
|
||||
dayjs().subtract(30, 'day'),
|
||||
'day',
|
||||
);
|
||||
|
||||
if (daysDiff === 0 && startDate.isSame(today, 'day')) {
|
||||
activeTimeRange.value = 'today';
|
||||
customDateRange.value = undefined;
|
||||
return;
|
||||
}
|
||||
|
||||
if (daysDiff === 0 && startDate.isSame(dayjs().subtract(1, 'day'), 'day')) {
|
||||
activeTimeRange.value = 'yesterday';
|
||||
customDateRange.value = undefined;
|
||||
return;
|
||||
}
|
||||
|
||||
if (isEndDateToday && isStartDate7DaysAgo) {
|
||||
activeTimeRange.value = '7days';
|
||||
customDateRange.value = undefined;
|
||||
return;
|
||||
}
|
||||
|
||||
if (isEndDateToday && isStartDate30DaysAgo) {
|
||||
activeTimeRange.value = '30days';
|
||||
customDateRange.value = undefined;
|
||||
return;
|
||||
}
|
||||
|
||||
activeTimeRange.value = 'custom';
|
||||
customDateRange.value = [startDate, endDate];
|
||||
};
|
||||
|
||||
const applyDateRangeFromRouteQuery = () => {
|
||||
const { endDate, startDate } = route.query;
|
||||
if (!startDate || !endDate) return false;
|
||||
|
||||
const parsedStartDate = dayjs(startDate as string);
|
||||
const parsedEndDate = dayjs(endDate as string);
|
||||
if (!parsedStartDate.isValid() || !parsedEndDate.isValid()) return false;
|
||||
|
||||
const isSameRange =
|
||||
dateRange.value[0].isSame(parsedStartDate, 'day') &&
|
||||
dateRange.value[1].isSame(parsedEndDate, 'day');
|
||||
|
||||
if (!isSameRange) {
|
||||
dateRange.value = [parsedStartDate, parsedEndDate];
|
||||
}
|
||||
setTimeRangeTypeByDates(parsedStartDate, parsedEndDate);
|
||||
return true;
|
||||
};
|
||||
|
||||
const syncDateRangeToRouteQuery = async () => {
|
||||
const startDate = dateRange.value[0].format('YYYY-MM-DD');
|
||||
const endDate = dateRange.value[1].format('YYYY-MM-DD');
|
||||
|
||||
if (route.query.startDate === startDate && route.query.endDate === endDate) {
|
||||
return;
|
||||
}
|
||||
|
||||
isSyncingRouteQuery.value = true;
|
||||
try {
|
||||
await router.replace({
|
||||
path: route.path,
|
||||
query: {
|
||||
...route.query,
|
||||
endDate,
|
||||
startDate,
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
isSyncingRouteQuery.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// 图表引用
|
||||
const barChartRef = ref<EchartsUIType>();
|
||||
const scatterChartRef = ref<EchartsUIType>();
|
||||
@@ -165,6 +248,18 @@ const loadOverviewData = async () => {
|
||||
}
|
||||
};
|
||||
|
||||
const refreshOverviewByCurrentParams = async (force = false) => {
|
||||
applyDateRangeFromRouteQuery();
|
||||
const requestKey = `${dateRange.value[0].format('YYYY-MM-DD')}_${dateRange.value[1].format('YYYY-MM-DD')}`;
|
||||
|
||||
if (!force && lastRequestKey.value === requestKey) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastRequestKey.value = requestKey;
|
||||
await loadOverviewData();
|
||||
};
|
||||
|
||||
// 渲染店铺销售占比排行图表
|
||||
const renderShopBarChart = () => {
|
||||
if (!barChartRef.value || !overviewData.value?.shopSalesRatios) return;
|
||||
@@ -322,15 +417,28 @@ watch(
|
||||
// 监听日期范围变化
|
||||
watch(
|
||||
() => dateRange.value,
|
||||
() => {
|
||||
loadOverviewData();
|
||||
async () => {
|
||||
await syncDateRangeToRouteQuery();
|
||||
await refreshOverviewByCurrentParams();
|
||||
},
|
||||
);
|
||||
|
||||
watch(
|
||||
() => [route.query.startDate, route.query.endDate],
|
||||
async () => {
|
||||
if (isSyncingRouteQuery.value) return;
|
||||
await refreshOverviewByCurrentParams();
|
||||
},
|
||||
);
|
||||
|
||||
// 初始化
|
||||
onMounted(async () => {
|
||||
await loadShopCount();
|
||||
await loadOverviewData();
|
||||
await refreshOverviewByCurrentParams(true);
|
||||
});
|
||||
|
||||
onActivated(async () => {
|
||||
await refreshOverviewByCurrentParams(true);
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
@@ -3,8 +3,8 @@ import type { EchartsUIType } from '@vben/plugins/echarts';
|
||||
|
||||
import type { ShopDTO, ShopVO } from '#/api/ecommerce/shop/types';
|
||||
|
||||
import { computed, onMounted, ref, watch } from 'vue';
|
||||
import { useRoute } from 'vue-router';
|
||||
import { computed, onActivated, onMounted, ref, watch } from 'vue';
|
||||
import { useRoute, useRouter } from 'vue-router';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import { EchartsUI, useEcharts } from '@vben/plugins/echarts';
|
||||
@@ -17,6 +17,7 @@ import { getShopOverviewStatisticsApi } from '#/api/ecommerce/shop';
|
||||
defineOptions({ name: 'ShopOverview' });
|
||||
|
||||
const route = useRoute();
|
||||
const router = useRouter();
|
||||
|
||||
// 店铺ID(从路由参数获取)
|
||||
const shopId = computed(() => {
|
||||
@@ -41,6 +42,8 @@ const activeTimeRange = ref<
|
||||
|
||||
// 自定义日期范围
|
||||
const customDateRange = ref<[dayjs.Dayjs, dayjs.Dayjs] | undefined>(undefined);
|
||||
const lastRequestKey = ref('');
|
||||
const isSyncingRouteQuery = ref(false);
|
||||
|
||||
// 切换时间范围
|
||||
const switchTimeRange = (
|
||||
@@ -96,6 +99,86 @@ const handleCustomDateRangeChange = (
|
||||
}
|
||||
};
|
||||
|
||||
const setTimeRangeTypeByDates = (startDate: dayjs.Dayjs, endDate: dayjs.Dayjs) => {
|
||||
const today = dayjs();
|
||||
const daysDiff = endDate.diff(startDate, 'day');
|
||||
const isEndDateToday = endDate.isSame(today, 'day');
|
||||
const isStartDate7DaysAgo = startDate.isSame(dayjs().subtract(7, 'day'), 'day');
|
||||
const isStartDate30DaysAgo = startDate.isSame(
|
||||
dayjs().subtract(30, 'day'),
|
||||
'day',
|
||||
);
|
||||
|
||||
if (daysDiff === 0 && startDate.isSame(today, 'day')) {
|
||||
activeTimeRange.value = 'today';
|
||||
customDateRange.value = undefined;
|
||||
return;
|
||||
}
|
||||
|
||||
if (daysDiff === 0 && startDate.isSame(dayjs().subtract(1, 'day'), 'day')) {
|
||||
activeTimeRange.value = 'yesterday';
|
||||
customDateRange.value = undefined;
|
||||
return;
|
||||
}
|
||||
|
||||
if (isEndDateToday && isStartDate7DaysAgo) {
|
||||
activeTimeRange.value = '7days';
|
||||
customDateRange.value = undefined;
|
||||
return;
|
||||
}
|
||||
|
||||
if (isEndDateToday && isStartDate30DaysAgo) {
|
||||
activeTimeRange.value = '30days';
|
||||
customDateRange.value = undefined;
|
||||
return;
|
||||
}
|
||||
|
||||
activeTimeRange.value = 'custom';
|
||||
customDateRange.value = [startDate, endDate];
|
||||
};
|
||||
|
||||
const applyDateRangeFromRouteQuery = () => {
|
||||
const { endDate, startDate } = route.query;
|
||||
if (!startDate || !endDate) return false;
|
||||
|
||||
const parsedStartDate = dayjs(startDate as string);
|
||||
const parsedEndDate = dayjs(endDate as string);
|
||||
if (!parsedStartDate.isValid() || !parsedEndDate.isValid()) return false;
|
||||
|
||||
const isSameRange =
|
||||
dateRange.value[0].isSame(parsedStartDate, 'day') &&
|
||||
dateRange.value[1].isSame(parsedEndDate, 'day');
|
||||
|
||||
if (!isSameRange) {
|
||||
dateRange.value = [parsedStartDate, parsedEndDate];
|
||||
}
|
||||
setTimeRangeTypeByDates(parsedStartDate, parsedEndDate);
|
||||
return true;
|
||||
};
|
||||
|
||||
const syncDateRangeToRouteQuery = async () => {
|
||||
const startDate = dateRange.value[0].format('YYYY-MM-DD');
|
||||
const endDate = dateRange.value[1].format('YYYY-MM-DD');
|
||||
|
||||
if (route.query.startDate === startDate && route.query.endDate === endDate) {
|
||||
return;
|
||||
}
|
||||
|
||||
isSyncingRouteQuery.value = true;
|
||||
try {
|
||||
await router.replace({
|
||||
path: route.path,
|
||||
query: {
|
||||
...route.query,
|
||||
endDate,
|
||||
startDate,
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
isSyncingRouteQuery.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// 图表引用
|
||||
const trendChartRef = ref<EchartsUIType>();
|
||||
const { renderEcharts: renderTrendChart } = useEcharts(trendChartRef);
|
||||
@@ -139,6 +222,19 @@ const loadOverviewData = async () => {
|
||||
}
|
||||
};
|
||||
|
||||
const refreshOverviewByCurrentParams = async (force = false) => {
|
||||
applyDateRangeFromRouteQuery();
|
||||
if (!shopId.value) return;
|
||||
|
||||
const requestKey = `${shopId.value}_${dateRange.value[0].format('YYYY-MM-DD')}_${dateRange.value[1].format('YYYY-MM-DD')}`;
|
||||
if (!force && lastRequestKey.value === requestKey) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastRequestKey.value = requestKey;
|
||||
await loadOverviewData();
|
||||
};
|
||||
|
||||
// 渲染趋势图表
|
||||
const renderTrendChartData = () => {
|
||||
if (!trendChartRef.value || !overviewData.value) return;
|
||||
@@ -250,68 +346,27 @@ watch(
|
||||
// 监听日期范围变化
|
||||
watch(
|
||||
() => dateRange.value,
|
||||
() => {
|
||||
loadOverviewData();
|
||||
async () => {
|
||||
await syncDateRangeToRouteQuery();
|
||||
await refreshOverviewByCurrentParams();
|
||||
},
|
||||
);
|
||||
|
||||
// 监听 shopId 变化
|
||||
watch(
|
||||
() => shopId.value,
|
||||
() => {
|
||||
if (shopId.value) {
|
||||
loadOverviewData();
|
||||
}
|
||||
() => [route.params.id, route.query.startDate, route.query.endDate],
|
||||
async () => {
|
||||
if (isSyncingRouteQuery.value) return;
|
||||
await refreshOverviewByCurrentParams();
|
||||
},
|
||||
);
|
||||
|
||||
// 初始化
|
||||
onMounted(() => {
|
||||
// 检查查询参数,如果有则设置时间范围
|
||||
const query = route.query;
|
||||
if (query.startDate && query.endDate) {
|
||||
const startDate = dayjs(query.startDate as string);
|
||||
const endDate = dayjs(query.endDate as string);
|
||||
const today = dayjs();
|
||||
onMounted(async () => {
|
||||
await refreshOverviewByCurrentParams(true);
|
||||
});
|
||||
|
||||
if (startDate.isValid() && endDate.isValid()) {
|
||||
dateRange.value = [startDate, endDate];
|
||||
// 判断时间范围类型
|
||||
const daysDiff = endDate.diff(startDate, 'day');
|
||||
const isEndDateToday = endDate.isSame(today, 'day');
|
||||
const isStartDate7DaysAgo = startDate.isSame(
|
||||
dayjs().subtract(7, 'day'),
|
||||
'day',
|
||||
);
|
||||
const isStartDate30DaysAgo = startDate.isSame(
|
||||
dayjs().subtract(30, 'day'),
|
||||
'day',
|
||||
);
|
||||
|
||||
if (daysDiff === 0 && startDate.isSame(today, 'day')) {
|
||||
activeTimeRange.value = 'today';
|
||||
} else if (
|
||||
daysDiff === 0 &&
|
||||
startDate.isSame(dayjs().subtract(1, 'day'), 'day')
|
||||
) {
|
||||
activeTimeRange.value = 'yesterday';
|
||||
} else if (isEndDateToday && isStartDate7DaysAgo) {
|
||||
// 近7天:从7天前到今天
|
||||
activeTimeRange.value = '7days';
|
||||
} else if (isEndDateToday && isStartDate30DaysAgo) {
|
||||
// 近30天:从30天前到今天
|
||||
activeTimeRange.value = '30days';
|
||||
} else {
|
||||
activeTimeRange.value = 'custom';
|
||||
customDateRange.value = [startDate, endDate];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 设置完时间范围后再加载数据
|
||||
if (shopId.value) {
|
||||
loadOverviewData();
|
||||
}
|
||||
onActivated(async () => {
|
||||
await refreshOverviewByCurrentParams(true);
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user