我的博客自2021年以来就没能正常记录和显示阅读量,此前知道是 leancloud CDN失效了导致的,但一直没能修改,最近有时间就改了,遂写篇文章记录一下。

解决方案

  1. 编辑文件 /layout/_third-party/analytics/leancloud-analytics.swig,内容如下:
{% if theme.leancloud_visitors.enable %}

  <script src="//code.bdstatic.com/npm/leancloud-storage@4.12.0/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);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find().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');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .catch(function (object, error) {
          console.log("Error: " + error, object);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

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

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>

&#123;% endif %&#125;
  1. 编辑主题配置文件 _config.yml,新增或修改以下配置:
leancloud_visitors:
  enable: true
  app_id: <Your leancloud appId>
  app_key: <Your leancloud appKey>
  # url 以 'http://' 或 'https://' 开头
  server_url: <自定义域名或leancloud的访问地址>
  1. 重新发布:
hexo clean
hexo g

具体请看Github PR:https://github.com/iissnan/hexo-theme-next/pull/2267

文章目录