Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复 leancloud 原 CDN 失效,导致无法获取文章阅读数量。 #2267

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -529,11 +529,12 @@ rating:
# ---------------------------------------------------------------

# Show number of visitors to each article.
# You can visit https://leancloud.cn get AppID and AppKey.
# You can visit https://leancloud.cn get AppID,AppKey and ServerURL.
leancloud_visitors:
enable: false
app_id: #<app_id>
app_key: #<app_key>
server_url: #<server_url>

# Another tool to show number of visitors to each article.
# visit https://console.firebase.google.com/u/0/ to get apiKey and projectId
Expand Down
52 changes: 25 additions & 27 deletions layout/_third-party/analytics/lean-analytics.swig
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
{% if theme.leancloud_visitors.enable %}

{# custom analytics part create by xiamo #}
<script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
<script>AV.initialize("{{theme.leancloud_visitors.app_id}}", "{{theme.leancloud_visitors.app_key}}");</script>

<script src="//code.bdstatic.com/npm/[email protected]/dist/av-min.js"></script>
<script>AV.init({
appId: "{{theme.leancloud_visitors.app_id}}",
appKey: "{{theme.leancloud_visitors.app_key}}",
serverURL: "{{theme.leancloud_visitors.server_url}}"
});</script>
<script>
function showTime(Counter) {
var query = new AV.Query(Counter);
Expand All @@ -15,14 +20,13 @@

query.containedIn('url', entries);
query.find()
.done(function (results) {
.then(function (results) {
var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

if (results.length === 0) {
$visitors.find(COUNT_CONTAINER_REF).text(0);
return;
}

for (var i = 0; i < results.length; i++) {
var item = results[i];
var url = item.get('url');
Expand All @@ -40,8 +44,8 @@
}
}
})
.fail(function (object, error) {
console.log("Error: " + error.code + " " + error.message);
.catch(function (object, error) {
console.log("Error: " + error, object);
});
}

Expand All @@ -52,21 +56,18 @@
var query = new AV.Query(Counter);

query.equalTo("url", url);
query.find({
success: function(results) {
if (results.length > 0) {
query.find().then(function(results) {
console.log(results);
if (results.length > 0) {
var counter = results[0];
counter.fetchWhenSave(true);
counter.increment("time");
counter.save(null, {
success: function(counter) {
counter.save().then(function(counter) {
var $element = $(document.getElementById(url));
$element.find('.leancloud-visitors-count').text(counter.get('time'));
},
error: function(counter, error) {
})
.catch(function(counter, error) {
console.log('Failed to save Visitor num, with error message: ' + error.message);
}
});
})
} else {
var newcounter = new Counter();
/* Set ACL */
Expand All @@ -78,21 +79,18 @@
newcounter.set("title", title);
newcounter.set("url", url);
newcounter.set("time", 1);
newcounter.save(null, {
success: function(newcounter) {
newcounter.save().then(function(newcounter) {
var $element = $(document.getElementById(url));
$element.find('.leancloud-visitors-count').text(newcounter.get('time'));
},
error: function(newcounter, error) {
})
.catch(function(error) {
console.log('Failed to create');
}
});
})
}
},
error: function(error) {
console.log('Error:' + error.code + " " + error.message);
}
});
})
.catch(function(error) {
console.log('Error:' + error);
})
}

$(function() {
Expand Down