From c5a1dd5183f862a85a1859c0b7f16f6b5f495a13 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Mon, 28 Nov 2022 00:13:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=88=91=E7=9A=84=E5=BD=92=E6=A1=A3=E3=80=81=E6=88=91=E7=9A=84?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=E9=A1=B5=E9=9D=A2=E6=9F=A5=E8=AF=A2=E6=96=87?= =?UTF-8?q?=E7=AB=A0=E9=98=85=E8=AF=BB=E6=95=B0=E4=B8=8D=E5=9B=9E=E6=98=BE?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=88=E5=B9=B6=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BA=86=E8=AE=B0=E5=BD=95=E9=A1=B5=E9=9D=A2=E6=B5=8F=E8=A7=88?= =?UTF-8?q?=E9=87=8F=20API=20=E7=9A=84=E8=B0=83=E7=94=A8=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/.vitepress/config/head.ts | 8 +++++++- docs/.vitepress/config/theme.ts | 2 +- .../theme/components/ArticleMetadata.vue | 15 ++++++++------- 3 files changed, 16 insertions(+), 9 deletions(-) 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 }) }) }