• 默认效果
        • 点击点赞或反对链接,页面会刷新,点赞数或反对数增加。
      • 优化方案
        • 使用AJAX请求API接口,避免页面刷新。
        • 点赞接口http://IP/api.php/do/likes/id/*
        • 反对接口http://IP/api.php/do/oppose/id/*
        • 示例代码
          $(document).ready(function (e) {
            var url = '/api.php/do/likes/id/' + {content:id};
            $.ajax({
              type: 'POST',
              url: url,
              dataType: 'json',
              data: {
                appid: '{pboot:appid}',
                timestamp: '{pboot:timestamp}',
                signature: '{pboot:signature}'
              },
              success: function (response, status) {
                if (response.code) {
                  // 获取数据成功
                  // 对页面已显示的 {content:likes} +1
                  alert(response.data);
                } else {
                  // 返回错误数据
                  alert(response.data);
                }
              },
              error: function (xhr, status, error) {
                // 返回数据异常
                alert('返回数据异常!');
              }
            });
          });
        • 限制24小时内只能点赞一次
          • 修改 apps/api/controller/DoController.php 文件,大约在24行。
          • 代码示例
            public function likes() {
              if (! ! $id = request('id', 'int')) {
                if (!cookie('likes_' . $id)) {
                  $this->model->addLikes($id);
                  cookie('likes_' . $id, true, 86400, null, null, null, null);
                  json(1, '点赞成功');
                } else {
                  json(0, '24小时内只能点赞一次哦!');
                }
              } else {
                json(0, '点赞失败');
              }
            }