template.html 5.15 KB
<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  <meta name="format-detection" content="telephone=no" />
  <meta name="format-detection" content="email=no" />
  <meta name="format-detection" content="address=no" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  <meta name="screen-orientation" content="portrait" />
  <meta name="x5-orientation" content="portrait" />
  <meta name="x5-page-mode" content="app" />
  <meta name="x5-fullscreen" content="true" />
  <meta name="full-screen" content="yes" />
  <meta http-equiv="x-dns-prefetch-control" content="on" />
  <style type="text/css">
    * {
      padding: 0;
      font-weight: normal;
    }

    .content {
      margin: 0 15px;
    }

    img {
      width: 100%;
    }

    #title {
      font-weight: bold;
      font-size: 1.1rem;
      text-align: center;
      margin-top: 10px;
    }

    .common {
      padding-top: 10px;
      color: #999;
    }

    .bottom {
      text-align: center;
      margin-bottom: 1rem;
    }

    .btn {
      display: inline-block;
      border: 1px solid #999;
      border-radius: 0.5rem;
      text-align: center;
    }

    p {
      margin-top: 15px;
    }

    span {
      display: inline-block;
      text-align: justify;
      line-height: 1.5rem !important;
      font-family: "黑体" !important;
      font-size: 1rem !important;
    }

    .btn img {
      width: 1rem;
      padding: 0.1rem 1.25rem;
      padding-top: 0.2rem;
      position: relative;
    }

    #inner-html img {
      margin: 15px 0;
    }
  </style>
</head>

<body>
<div class="content">
  <div>
    <div id="title"></div>
    <div class="common">
      <span id="person-num"></span>
      <span id="date"></span>
    </div>
  </div>
  <div id="inner-html"></div>
  <div class="bottom">
    <div onclick="switchClickFn()" class="btn">
      <img id="disable" src="https://oss.tanleizhen.com/static/newnotgood.png" />
      <img id="enable" src="https://oss.tanleizhen.com/static/newgood.png" style="display: none;" />
    </div>
  </div>
</div>
</body>

<script>
  var clickStatus = false;
  var pNum = 0;
  // 原生ajax实现
  function ajax(option) {
    var xhr = new XMLHttpRequest();
    if (option.method === 'GET') {
      option.url += '?';

      var strUrl = '';
      for (var key in option.data) {
        strUrl += key + '=' + option.data[key] + '&';
      }
      option.url += strUrl.substring(0, strUrl.length - 1);
      xhr.open(option.method, option.url);
      xhr.send()
    }
    if (option.method === 'POST') {
      // xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
      xhr.open(option.method, option.url);
      xhr.send(option.data);
    }

    xhr.onreadystatechange = function() {
      if (xhr.readyState == 4 && xhr.status == 200) {
        option.success(JSON.parse(xhr.responseText));
      }
    }
  }

  // 请求接口数据
  ajax({
    url: '../web/news/detail',
    method: 'GET',
    data: {
      newsId: window.location.search.split('?')[1].split('&')[0],
      clientType: '1',
      version: '1.2.4'
    },
    success: function(resp) {
      clickStatus = resp.data.hasClicked == '1' ? true : false;
      var defaultImg = document.getElementById(clickStatus ? 'enable' : 'disable');
      defaultImg.style.display = "inline-block";

      // 设置title
      var title = document.getElementById("title");
      title.innerHTML = resp.data.newsTitle;

      // 设置点赞人数
      var personNum = document.getElementById("person-num");
      pNum = parseInt(resp.data.randomNum);
      personNum.innerHTML = resp.data.randomNum + '人赞过&nbsp;&nbsp;&nbsp;';

      // 设置时间
      var date = document.getElementById("date");
      date.innerHTML = resp.data.releaseTime;

      // 展示富文本字符串
      var innerContent = document.getElementById("inner-html");
      innerContent.innerHTML = resp.data.content;
    }
  })

  // switch点击事件
  function switchClickFn() {
    ajax({
      url: '../web/likeList/' + (clickStatus ? 'add' : 'delete'),
      method: 'GET',
      data: {
        newsId: window.location.search.split('?')[1].split('&')[0],
        token: window.location.search.split('?')[1].split('&')[1],
        clientType: '1',
        version: '1.2.4'
      },
      success: function(resp) {
        var disable = document.getElementById('disable');
        var enable = document.getElementById('enable');
        var personNum = document.getElementById("person-num");
        if (!clickStatus) {
          clickStatus = true;
          disable.style.display = "none";
          enable.style.display = "inline-block";
          pNum += 1;
          personNum.innerHTML = pNum + '人赞过&nbsp;&nbsp;&nbsp;';
        } else {
          clickStatus = false;
          enable.style.display = "none";
          disable.style.display = "inline-block";
          pNum -= 1;
          personNum.innerHTML = pNum + '人赞过&nbsp;&nbsp;&nbsp;';
        }
      }
    })
  }
</script>

</html>