优化:优化 API 服务调用
This commit is contained in:
@@ -27,17 +27,33 @@ export const head: HeadConfig[] = [
|
|||||||
s.parentNode.insertBefore(hm, s);
|
s.parentNode.insertBefore(hm, s);
|
||||||
})();`],
|
})();`],
|
||||||
// 页面访问量统计
|
// 页面访问量统计
|
||||||
['script', {}, `var pageUrl = location.href;
|
['script', {}, `
|
||||||
(function() {
|
window.addEventListener('load', function() {
|
||||||
|
let oldHref = document.location.href, bodyDOM = document.querySelector('body');
|
||||||
|
const observer = new MutationObserver(function(mutations) {
|
||||||
|
if (oldHref != document.location.href) {
|
||||||
|
oldHref = document.location.href;
|
||||||
|
getPv()
|
||||||
|
window.requestAnimationFrame(function() {
|
||||||
|
let tmp = document.querySelector('body');
|
||||||
|
if(tmp != bodyDOM) {
|
||||||
|
bodyDOM = tmp;
|
||||||
|
observer.observe(bodyDOM, config);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const config = {
|
||||||
|
childList: true,
|
||||||
|
subtree: true
|
||||||
|
};
|
||||||
|
observer.observe(bodyDOM, config);
|
||||||
|
getPv()
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
function getPv() {
|
||||||
xhr = new XMLHttpRequest();
|
xhr = new XMLHttpRequest();
|
||||||
xhr.open('GET', 'https://api.charles7c.top/blog/pv?pageUrl=' + pageUrl + "&t=" + new Date().getTime());
|
xhr.open('GET', 'https://api.charles7c.top/blog/pv?pageUrl=' + location.href);
|
||||||
xhr.send();
|
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);
|
|
||||||
})();`]*/
|
|
||||||
]
|
]
|
@@ -1,6 +1,6 @@
|
|||||||
import { request } from './config'
|
import { request } from './config'
|
||||||
|
|
||||||
export const getPv = (id, pageUrl, call) => {
|
export const getArticleViewCount = (id, pageUrl, call) => {
|
||||||
request.get(`/article/view/${id}?pageUrl=${pageUrl}`, {}).then(result => {
|
request.get(`/article/view/${id}?pageUrl=${pageUrl}`, {}).then(result => {
|
||||||
call(process(result))
|
call(process(result))
|
||||||
})
|
})
|
||||||
@@ -14,4 +14,4 @@ function process(result) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default { getPv }
|
export default { getArticleViewCount }
|
@@ -83,7 +83,7 @@ const { author, authorLink, showViewCount, viewCount, date, toDate, categories,
|
|||||||
if (data.showViewCount) {
|
if (data.showViewCount) {
|
||||||
// 记录并获取文章阅读数(使用文章标题 + 发布时间生成 MD5 值,作为文章的唯一标识)
|
// 记录并获取文章阅读数(使用文章标题 + 发布时间生成 MD5 值,作为文章的唯一标识)
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
$api.getPv(md5(props.article.title + props.article.date), location.href, function(viewCountData) {
|
$api.getArticleViewCount(md5(props.article.title + props.article.date), location.href, function(viewCountData) {
|
||||||
data.viewCount = viewCountData
|
data.viewCount = viewCountData
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user