mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 05:49:20 +08:00
feat: 优化若干
This commit is contained in:
@@ -6,8 +6,8 @@ import type {
|
|||||||
CompanyOverviewVO,
|
CompanyOverviewVO,
|
||||||
} from '#/api/ecommerce/company-overview';
|
} from '#/api/ecommerce/company-overview';
|
||||||
|
|
||||||
import { onMounted, ref, watch } from 'vue';
|
import { onActivated, onMounted, ref, watch } from 'vue';
|
||||||
import { useRouter } from 'vue-router';
|
import { useRoute, useRouter } from 'vue-router';
|
||||||
|
|
||||||
import { Page } from '@vben/common-ui';
|
import { Page } from '@vben/common-ui';
|
||||||
import { EchartsUI, useEcharts } from '@vben/plugins/echarts';
|
import { EchartsUI, useEcharts } from '@vben/plugins/echarts';
|
||||||
@@ -22,6 +22,7 @@ defineOptions({ name: 'CompanyOverview' });
|
|||||||
|
|
||||||
// 路由
|
// 路由
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const route = useRoute();
|
||||||
|
|
||||||
// 响应式数据
|
// 响应式数据
|
||||||
const loading = ref(false);
|
const loading = ref(false);
|
||||||
@@ -41,6 +42,8 @@ const activeTimeRange = ref<
|
|||||||
|
|
||||||
// 自定义日期范围
|
// 自定义日期范围
|
||||||
const customDateRange = ref<[dayjs.Dayjs, dayjs.Dayjs] | undefined>(undefined);
|
const customDateRange = ref<[dayjs.Dayjs, dayjs.Dayjs] | undefined>(undefined);
|
||||||
|
const lastRequestKey = ref('');
|
||||||
|
const isSyncingRouteQuery = ref(false);
|
||||||
|
|
||||||
// 切换时间范围
|
// 切换时间范围
|
||||||
const switchTimeRange = (
|
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 barChartRef = ref<EchartsUIType>();
|
||||||
const scatterChartRef = 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 = () => {
|
const renderShopBarChart = () => {
|
||||||
if (!barChartRef.value || !overviewData.value?.shopSalesRatios) return;
|
if (!barChartRef.value || !overviewData.value?.shopSalesRatios) return;
|
||||||
@@ -322,15 +417,28 @@ watch(
|
|||||||
// 监听日期范围变化
|
// 监听日期范围变化
|
||||||
watch(
|
watch(
|
||||||
() => dateRange.value,
|
() => dateRange.value,
|
||||||
() => {
|
async () => {
|
||||||
loadOverviewData();
|
await syncDateRangeToRouteQuery();
|
||||||
|
await refreshOverviewByCurrentParams();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
watch(
|
||||||
|
() => [route.query.startDate, route.query.endDate],
|
||||||
|
async () => {
|
||||||
|
if (isSyncingRouteQuery.value) return;
|
||||||
|
await refreshOverviewByCurrentParams();
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
// 初始化
|
// 初始化
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
await loadShopCount();
|
await loadShopCount();
|
||||||
await loadOverviewData();
|
await refreshOverviewByCurrentParams(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
onActivated(async () => {
|
||||||
|
await refreshOverviewByCurrentParams(true);
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ import type { EchartsUIType } from '@vben/plugins/echarts';
|
|||||||
|
|
||||||
import type { ShopDTO, ShopVO } from '#/api/ecommerce/shop/types';
|
import type { ShopDTO, ShopVO } from '#/api/ecommerce/shop/types';
|
||||||
|
|
||||||
import { computed, onMounted, ref, watch } from 'vue';
|
import { computed, onActivated, onMounted, ref, watch } from 'vue';
|
||||||
import { useRoute } from 'vue-router';
|
import { useRoute, useRouter } from 'vue-router';
|
||||||
|
|
||||||
import { Page } from '@vben/common-ui';
|
import { Page } from '@vben/common-ui';
|
||||||
import { EchartsUI, useEcharts } from '@vben/plugins/echarts';
|
import { EchartsUI, useEcharts } from '@vben/plugins/echarts';
|
||||||
@@ -17,6 +17,7 @@ import { getShopOverviewStatisticsApi } from '#/api/ecommerce/shop';
|
|||||||
defineOptions({ name: 'ShopOverview' });
|
defineOptions({ name: 'ShopOverview' });
|
||||||
|
|
||||||
const route = useRoute();
|
const route = useRoute();
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
// 店铺ID(从路由参数获取)
|
// 店铺ID(从路由参数获取)
|
||||||
const shopId = computed(() => {
|
const shopId = computed(() => {
|
||||||
@@ -41,6 +42,8 @@ const activeTimeRange = ref<
|
|||||||
|
|
||||||
// 自定义日期范围
|
// 自定义日期范围
|
||||||
const customDateRange = ref<[dayjs.Dayjs, dayjs.Dayjs] | undefined>(undefined);
|
const customDateRange = ref<[dayjs.Dayjs, dayjs.Dayjs] | undefined>(undefined);
|
||||||
|
const lastRequestKey = ref('');
|
||||||
|
const isSyncingRouteQuery = ref(false);
|
||||||
|
|
||||||
// 切换时间范围
|
// 切换时间范围
|
||||||
const switchTimeRange = (
|
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 trendChartRef = ref<EchartsUIType>();
|
||||||
const { renderEcharts: renderTrendChart } = useEcharts(trendChartRef);
|
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 = () => {
|
const renderTrendChartData = () => {
|
||||||
if (!trendChartRef.value || !overviewData.value) return;
|
if (!trendChartRef.value || !overviewData.value) return;
|
||||||
@@ -250,68 +346,27 @@ watch(
|
|||||||
// 监听日期范围变化
|
// 监听日期范围变化
|
||||||
watch(
|
watch(
|
||||||
() => dateRange.value,
|
() => dateRange.value,
|
||||||
() => {
|
async () => {
|
||||||
loadOverviewData();
|
await syncDateRangeToRouteQuery();
|
||||||
|
await refreshOverviewByCurrentParams();
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
// 监听 shopId 变化
|
|
||||||
watch(
|
watch(
|
||||||
() => shopId.value,
|
() => [route.params.id, route.query.startDate, route.query.endDate],
|
||||||
() => {
|
async () => {
|
||||||
if (shopId.value) {
|
if (isSyncingRouteQuery.value) return;
|
||||||
loadOverviewData();
|
await refreshOverviewByCurrentParams();
|
||||||
}
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
// 初始化
|
// 初始化
|
||||||
onMounted(() => {
|
onMounted(async () => {
|
||||||
// 检查查询参数,如果有则设置时间范围
|
await refreshOverviewByCurrentParams(true);
|
||||||
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();
|
|
||||||
|
|
||||||
if (startDate.isValid() && endDate.isValid()) {
|
onActivated(async () => {
|
||||||
dateRange.value = [startDate, endDate];
|
await refreshOverviewByCurrentParams(true);
|
||||||
// 判断时间范围类型
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user