Skip to content

Commit 3b57176

Browse files
committed
feat: 🚀 访问量添加 iteration 配置项
1 parent 8c31b0e commit 3b57176

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed

vitepress-theme-teek/src/components/ArticleAnalyze/src/index.vue

+12-3
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,25 @@ const docAnalysisConfig = computed<DocAnalysis>(() => {
8585
});
8686
8787
const statisticsConfig = computed<NonNullable<DocAnalysis["statistics"]>>(() => {
88-
const { provider = "", pageView = true, pageIteration = 2000 } = unref(docAnalysisConfig).statistics || {};
88+
const {
89+
provider = "",
90+
pageView = true,
91+
iteration = false,
92+
pageIteration = 2000,
93+
} = unref(docAnalysisConfig).statistics || {};
8994
90-
return { provider, pageView, pageIteration };
95+
return { provider, pageView, iteration, pageIteration };
9196
});
9297
// 是否使用访问量功能
9398
const usePageView = computed(() => unref(statisticsConfig).provider && unref(statisticsConfig).pageView);
9499
95100
const statisticsInfo: UseBuSunZi = { pagePv: ref(0), isGet: ref(false) };
96101
// 通过不蒜子获取访问量
97-
const { pagePv, isGet, request } = useBuSunZi(true, unref(statisticsConfig).pageIteration);
102+
const { pagePv, isGet, request } = useBuSunZi(
103+
true,
104+
unref(statisticsConfig).iteration,
105+
unref(statisticsConfig).pageIteration
106+
);
98107
statisticsInfo.pagePv = pagePv;
99108
statisticsInfo.isGet = isGet;
100109

vitepress-theme-teek/src/components/HomeDocAnalysisCard/src/index.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ const statisticsInfo: UseBuSunZi = {
7474
siteUv: ref(0),
7575
isGet: ref(false),
7676
};
77-
const { provider = "", siteView = true, siteIteration = 2000 }: DocAnalysis["statistics"] = statistics;
77+
const { provider = "", siteView = true, iteration, siteIteration = 2000 }: DocAnalysis["statistics"] = statistics;
7878
const useSiteView = provider === "busuanzi" && siteView;
7979
8080
if (useSiteView) {
8181
// 通过不蒜子获取访问量和访客数
82-
const { sitePv, siteUv, isGet, request } = useBuSunZi(true, siteIteration);
82+
const { sitePv, siteUv, isGet, request } = useBuSunZi(true, iteration, siteIteration);
8383
statisticsInfo.sitePv = sitePv;
8484
statisticsInfo.siteUv = siteUv;
8585
statisticsInfo.isGet = isGet;

vitepress-theme-teek/src/config/types.ts

+6
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,12 @@ export interface DocAnalysis {
762762
* @default true
763763
*/
764764
pageView?: boolean;
765+
/**
766+
* 是否开启重试机制,当获取访问量失败会尝试重新获取
767+
*
768+
* @default false
769+
*/
770+
iteration?: boolean;
765771
/**
766772
* 如果首页获取访问量失败,则每隔多少时间后获取一次访问量,直到获取成功或获取 5 次后
767773
*

vitepress-theme-teek/src/hooks/useBuSunZi.ts

+17-14
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export interface UseBuSunZi {
3838
isGet?: Ref<boolean | null>;
3939
}
4040

41-
export const useBuSunZi = (initRequest = false, iterationTime = 2000) => {
41+
export const useBuSunZi = (initRequest = false, iteration = false, iterationTime = 2000) => {
4242
const sitePv = ref(9999);
4343
const siteUv = ref(9999);
4444
const pagePv = ref(9999);
@@ -60,21 +60,24 @@ export const useBuSunZi = (initRequest = false, iterationTime = 2000) => {
6060

6161
// 第一次调用
6262
if (initRequest) request();
63-
let intervalId: NodeJS.Timeout;
64-
let i = 0;
6563

66-
// 如果第一次调用获取失败,每 3s 后重新调用,直至尝试 5 次或调用成功
67-
intervalId = setInterval(() => {
68-
if (!unref(isGet)) {
69-
i += iterationTime;
70-
if (i > iterationTime * 5) clearInterval(intervalId);
71-
request();
72-
} else clearInterval(intervalId);
73-
}, iterationTime);
64+
if (iteration) {
65+
let intervalId: NodeJS.Timeout;
66+
let i = 0;
7467

75-
onBeforeUnmount(() => {
76-
if (intervalId) clearInterval(intervalId);
77-
});
68+
// 如果第一次调用获取失败,每 3s 后重新调用,直至尝试 5 次或调用成功
69+
intervalId = setInterval(() => {
70+
if (!unref(isGet)) {
71+
i += iterationTime;
72+
if (i > iterationTime * 5) clearInterval(intervalId);
73+
request();
74+
} else clearInterval(intervalId);
75+
}, iterationTime);
76+
77+
onBeforeUnmount(() => {
78+
if (intervalId) clearInterval(intervalId);
79+
});
80+
}
7881

7982
return { sitePv, siteUv, pagePv, isGet, request };
8083
};

0 commit comments

Comments
 (0)