前言
由于Next
主题修复了leancloud
的安全问题, 导致以前leancloud
失效, 所以重新介绍如何配置leancloud
效果如图
注册Leancloud并创建应用
首先,前往Leancloud官网leancloud.cn进行注册,并登陆。
-
点击创建应用
-
输入应用的名称(随便起都行), 选择开发版, 点击创建
-
创建成功后点击右上角的设置小齿轮
-
点击存储-创建Class-Class名称设置为
Counter
(必须为Counter
)-选择无限制-点击创建
-
创建后点击设置-点击应用Key-获取App ID和App Key
将获取到的App ID和App Key设置到
Next
主题配置文件_config.yml
配置文件已存在这个配置, 只要把id和key加上去就好了
leancloud_visitors:
enable: true
app_id: <<your app id>>
app_key: <<your app key>>
-
点击安全中心, 填写自己博客对应的域名(注意协议、域名和端口号需严格一致)
-
点击云引擎-点击部署-点击在线编辑
点击创建函数-选择Hook-AV.Cloud.选择beforeSave-类选择Counter-添加代码-点击保存
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!');
}
})
}
- 点击部署

- 待出现红框处的成功部署信息后,点击关闭
设置权限
- 打开
Next
主题配置文件_config.yml
, 将leancloud_visitors下的security设置为true(如没有则新增)
leancloud_visitors:
enable: true
app_id: <<your app id>> # 同前文的id
app_key: <<your app key>> # 同前文的key
# Dependencies: https://github.com/theme-next/hexo-leancloud-counter-security
security: true
# 由于Leancloud免费版的云引擎存在请求线程数和运行时间限制以及休眠机制,
# 很多时候访客数量加载会很慢。
# 如果设置betterPerformance为true,
# 则网页则会在提交请求之前直接显示访客人数为查询到的人数+1,以增加用户体验。
betterPerformance: false
- 打开站点配置文件
_config.yml
leancloud_counter_security:
enable_sync: true
app_id: <<your app id>> # 同前文的id
app_key: <<your app key>> # 同前文的key
username:
password:
- 打开命令行
cmd
并进入站点文件夹,键入以下命令以安装hexo-leancloud-counter-security
插件
复制
npm install hexo-leancloud-counter-security --save
- 接着键入命令
hexo lc-counter register <<username>> <<password>>
PS: 将<<username>>
和<<password>>
替换为你自己的用户名和密码(不必与leancloud的账号)相同。此用户名和密码将在hexo部署时使用。
- 打开站点配置文件
_config.yml
, 将<<username>>
和<<password>>
替换为你刚刚设置的用户名和密码
leancloud_counter_security:
enable_sync: true
app_id: <<your app id>> # 同前文的id
app_key: <<your app key>> # 同前文的key
username: <<username>> #如留空则将在部署时询问
password: <<password>> #建议留空以保证安全性,如留空则将在部署时询问
-
返回Leancloud控制台的应用内, 检查_User表中是否出现一条记录(图示以用户名为admin为例)
-
进入Counter表, 打开权限设置
至此配置完成