1 注册leanCloud会员

点击官网:https://leancloud.cn/注册会员,注册成功之后,按下面的提示点击:
注册成功之后
然后创建一个class,命名为:Counter必须是这个名字
创建class

然后找到你自己的APP-IDAPP-KEY:
app-id and app-key

2 配置主题配置文件

在主题配置文件中,加入下面的代码:

1
2
3
4
5
6
7
8
9
10
11
# Show number of visitors to each article.
# You can visit https://leancloud.cn get AppID and AppKey.
leancloud_visitors:
enable: true
app_id: # your app-id
app_key: # your app-key
# Dependencies: https://github.com/theme-next/hexo-leancloud-counter-security
# # If you don't care about security in lc counter and just want to use it directly
# # (without hexo-leancloud-counter-security plugin), set the `security` to `false`.
# security: true
# betterPerformance: true

3 配置全局配置文件

打开全局配置文件,路径如下:your_blog/_config.yml,添加下面的代码:

1
2
3
4
5
6
leancloud_counter_security:
enable_sync: true
app_id: # your app-id
app_key: # your app-key
username: # your username
password: # your password

3.1 配置用户名和密码

首先安全插件:

1
npm install hexo-leancloud-counter-security --save

安装成功之后,注册用户名:

1
hexo lc-counter r "your username" "your password"

如果出现下面的错误:
Error: Cannot find module 'babel-runtime/regenerator'

可以试试下面的解决方案:

1
npm i babel-runtime --save

4 leanCloud部署

部署
创建函数
部署完之后,将下面的代码复制进去。

1
2
3
4
5
6
7
8
9
10
11
12
var AV = require('leanengine');

AV.Cloud.beforeUpdate('Counter', async function(request) {
var query = new AV.Query("Counter");
if (request.object.updatedKeys.indexOf('time') !== -1) {
return query.get(request.object.id).then(function (obj) {
if (obj.get("time") + 1 !== request.object.get("time")) {
throw new AV.Cloud.Error('Invalid update!');
}
})
}
});

5 创建hot page

进入到你的hexo博客目录,使用hexo new page hot创建一个hot主页,打开hot/index.md文件,将下面的代码复制进去:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<div id="hot"></div>
<script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
<script>AV.initialize("2lPeEraOnOk7GF6ou1WWs6BP-gzGzoHsz", "nXeW1bmcRE4TDrorjmdqj0ML");</script>
<script type="text/javascript">
var time=0
var title=""
var url=""
var query = new AV.Query('Counter');
query.notEqualTo('id',0);
query.descending('time');
query.limit(1000);
query.find().then(function (todo) {
for (var i=0;i<1000;i++){
var result=todo[i].attributes;
time=result.time;
title=result.title;
url=result.url;
var content="<p>"+"<font color='#1C1C1C'>"+"【文章热度:"+time+"℃】"+"</font>"+"<a href='"+"https://jums.club" + url + "'>"+title+"</a>"+"</p>";
document.getElementById("hot").innerHTML+=content
}
}, function (error) {
console.log("error");
});
</script>

5 添加菜单

打开主题的配置文件:找到menu,添加:

1
2
menu:
hot /hot

原文:https://blog.qust.cc/archives/48665.html

写在最后

欢迎大家关注鄙人的公众号【麦田里的守望者zhg】,让我们一起成长,谢谢。
微信公众号