diff --git a/docs/.vitepress/config/head.ts b/docs/.vitepress/config/head.ts index 586de3597..d8718e1a8 100644 --- a/docs/.vitepress/config/head.ts +++ b/docs/.vitepress/config/head.ts @@ -28,10 +28,16 @@ export const head: HeadConfig[] = [ })();`], // 页面访问量统计 ['script', {}, `var pageUrl = location.href; + (function() { + xhr = new XMLHttpRequest(); + xhr.open('GET', 'https://api.charles7c.top/blog/pv?pageUrl=' + pageUrl); + xhr.send(); + })();`] + /*['script', {}, `var pageUrl = location.href; (function() { var pv = document.createElement("script"); pv.src = "https://api.charles7c.top/blog/pv?pageUrl=" + pageUrl; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(pv, s); - })();`], + })();`]*/ ] \ No newline at end of file diff --git a/docs/.vitepress/config/theme.ts b/docs/.vitepress/config/theme.ts index 944b3a2e6..353f1d6b3 100644 --- a/docs/.vitepress/config/theme.ts +++ b/docs/.vitepress/config/theme.ts @@ -47,7 +47,7 @@ export const themeConfig: DefaultTheme.Config = { articleMetadataConfig: { author: '查尔斯', // 文章全局默认作者名称 authorLink: '/about/me', // 点击作者名时默认跳转的链接 - showPv: true // 是否显示文章阅读数, 需要在 docs/.vitepress/theme/api/config.js 及 interface.js 配置好相应 API 接口 + showViewCount: true // 是否显示文章阅读数, 需要在 docs/.vitepress/theme/api/config.js 及 interface.js 配置好相应 API 接口 }, // 自定义扩展: 文章版权配置 copyrightConfig: { diff --git a/docs/.vitepress/theme/components/ArticleMetadata.vue b/docs/.vitepress/theme/components/ArticleMetadata.vue index 04b1fb30a..d7cc3f765 100644 --- a/docs/.vitepress/theme/components/ArticleMetadata.vue +++ b/docs/.vitepress/theme/components/ArticleMetadata.vue @@ -14,11 +14,11 @@ -
+
阅读数 - 0 +
@@ -71,19 +71,20 @@ const { theme, page } = useData() const data = reactive({ author: props.article?.author ?? theme.value.articleMetadataConfig.author, authorLink: props.article?.authorLink ?? theme.value.articleMetadataConfig.authorLink, - showPv: theme.value.articleMetadataConfig?.showPv ?? false, + showViewCount: theme.value.articleMetadataConfig?.showViewCount ?? false, + viewCount: 0, date: new Date(props.article.date), categories: props.article?.categories ?? [], tags: props.article?.tags ?? [], showCategory: props.showCategory }) -const { author, authorLink, showPv, date, toDate, categories, tags, showCategory } = toRefs(data) +const { author, authorLink, showViewCount, viewCount, date, toDate, categories, tags, showCategory } = toRefs(data) -if (data.showPv) { +if (data.showViewCount) { // 记录并获取文章阅读数(使用文章标题 + 发布时间生成 MD5 值,作为文章的唯一标识) onMounted(() => { - $api.getPv(md5(props.article.title + props.article.date), location.href, function(data) { - document.getElementById("pv").innerText = data + $api.getPv(md5(props.article.title + props.article.date), location.href, function(viewCountData) { + data.viewCount = viewCountData }) }) }