Skip to content

浏览量 API

详情 浏览量

https://yxw7ds3a1o.apifox.cn/292174489e0

实际使用案例

html
<script th:inline="javascript">
  var config = [[${config}]];
  var articleId = "[[${article.id}]]";

  var appId = config.appId;
  var sourceIds = [articleId];
  var sourceType = 40;

  var url = '/ugc/userSurfInfo/querySurfAndPraiseCount'

  var xhr = new XMLHttpRequest();
  xhr.open('POST', url, true);
  xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4 && xhr.status === 200) {
      try {
        var res = JSON.parse(xhr.responseText);
        if (res && res.data && res.data.length > 0) {
          renderReadCount(res.data[0].surfCount);
        }
      } catch (e) {
        // 解析失败,保持默认值
      }
    }
  };
  xhr.send(JSON.stringify({
    appId: appId,
    sourceIds: sourceIds,
    sourceType: sourceType
  }));

  function renderReadCount(count) {
    document.getElementById('hlx-read-count').innerText = "阅读量:" + count || 1;
  }
</script>

华龙网示例

html
<script>
  //浏览量接口
  var sid = document.querySelector('meta[name="sid"]');
  if (sid) {
    var sid = sid.getAttribute("content");
  }
  var viewData = {
    appId: Number(sid), // 实站点id
    sourceIds: [], // 原稿id数组
    sourceType: 40, //固定
    token: "",
  };
  if (window.location.href.indexOf("/web/article/") != -1) {
    var businessId = document.querySelector('meta[name="businessId"]');
    if (businessId) {
      var businessId = businessId.getAttribute("content");
    }
    viewData.sourceIds.push(businessId);
    getViewNum(viewData, "news");
  } else {
    $(".mobileContent_content_item").each(function () {
      viewData.sourceIds.push($(this).attr("newsId"));
    });
    getViewNum(viewData, "column");
  }

  function getViewNum(viewData, type) {
    $.ajax({
      type: "POST",
      url: "/ugc/userSurfInfo/querySurfAndPraiseCount",
      contentType: "application/json",
      data: JSON.stringify(viewData),
      success: function (res) {
        if (res && res.data && Array.isArray(res.data)) {
          if (type == "news") {
            $(".article_viewNum").text("阅读量:" + res.data[0].surfCount);
          } else if (type == "column") {
            $(".mobileContent_content_item").each(function (index) {
              if (index < res.data.length) {
                var viewNumElement = $(this).find(".article_viewNum");
                if (viewNumElement.length > 0) {
                  viewNumElement.text("阅读量:" + res.data[index].surfCount);
                }
              }
            });
          }
        }
      },
      error: function (err) {
        console.log(err);
      },
    });
  }
</script>

栏目 浏览量

https://s.apifox.cn/6897585a-1e0c-4bb2-bf8e-5cf410bdb36d