Skip to content

Commit

Permalink
fix(client): show vistor count by post return val
Browse files Browse the repository at this point in the history
  • Loading branch information
lizheming committed Feb 14, 2021
1 parent 59cc1c5 commit b00dc64
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
3 changes: 1 addition & 2 deletions packages/client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ export default function Waline({

//visitor count
if(visitor) {
const visitorPromise = path ? Visitor.add({serverURL, path}) : Promise.resolve();
visitorPromise.then(() => Visitor.show({serverURL}));
path ? Visitor.add({serverURL, path}) : Visitor.show({serverURL});
}

//comment count
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/utils/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ export function postVisitCount({serverURL, path}) {
'Content-Type': 'application/json'
},
body: JSON.stringify({path})
});
}).then(resp => resp.json());
}
23 changes: 18 additions & 5 deletions packages/client/src/utils/visitor.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import { fetchVisitCount, postVisitCount } from "./fetch";

export default {
/**
* 增加阅读统计
*/
add({serverURL, path}) {
add({serverURL, path, countEl = '.leancloud_visitors,.leancloud-visitors'}) {
if(!serverURL || !path) {
return;
}

return postVisitCount({serverURL, path});
return postVisitCount({serverURL, path}).then(count => {
const countEls = [].filter.call(
document.querySelectorAll(countEl),
el => el.getAttribute('id')
);
if(!countEls.length) {
return;
}

countEls.forEach(el => {
let counterEl = el.querySelector('.leancloud-visitors-count');
if(!counterEl) {
counterEl = el;
}
counterEl.innerText = count;
});
});
},
show({serverURL, countEl = '.leancloud_visitors,.leancloud-visitors'}) {
const countEls = [].filter.call(
Expand Down

0 comments on commit b00dc64

Please sign in to comment.