Topic.php 30.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
<?php
namespace app\mobile\controller;

use think\Validate;
use think\Db;
use app\common\controller\Api;
use app\mobile\model\TopicCategory;
use app\common\model\Attachment;
use app\mobile\model\TopicKeyword;
use app\mobile\model\TopicGood;
use app\mobile\model\TopicAppraise;
use app\mobile\model\TopicAppraiseGood;

/**
 * 论坛接口
 * @ApiWeigh (92)
 */
class Topic extends Api
{
	protected $noNeedLogin = ['index','info'];
    protected $noNeedRight = ['*'];
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('app\mobile\model\Topic');
    }

    /**
     * @ApiWeigh (99)
     * @ApiTitle    (发帖-话题分类)
     * @ApiSummary  (发帖-话题分类)
     * @ApiMethod   (POST)
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1601341356",
        "data": [{
            "id": 2,//分类ID
            "name": "测试分类2" //分类名称
        }]
    })
     */
    public function topicCategory()
    {
        $list = TopicCategory::order('createtime desc')
            ->field('id,name')
            ->select();
        $this->success('成功',$list);
    }


    /**
     * @ApiWeigh (97)
     * @ApiTitle    (发帖)
     * @ApiSummary  (发帖)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="topic_category_id", type="inter", required=false, description="话题分类ID")
     * @ApiParams   (name="title", type="string", required=false, description="标题")
     * @ApiParams   (name="cover", type="string", required=false, description="封面")
     * @ApiParams   (name="content", type="string", required=false, description="话题内容")
     * @ApiParams   (name="attachments", type="string", required=false, description="json数据:[{'name':'原始文件名','url':'文件地址''},{'name':'原始文件名','url':'文件地址'}]")
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599201246",
        "data": null
    })
     */
    public function topicAdd()
    {
        $topic_category_id = $this->request->param('topic_category_id');
        $title = $this->request->param('title');
        $cover = $this->request->param('cover');
        $content = $this->request->param('content','','htmlspecialchars_decode');
        $attachments = $this->request->param('attachments');
        empty($topic_category_id) && $this->error('请选择话题分类');
        empty($title) && $this->error('请输入标题');
        empty($content) && $this->error('请填写话题内容');
        Db::startTrans();
        try {
            $this->model->allowField(true)->save([
                'user_id' => $this->auth->id,
                'topic_category_id' => $topic_category_id,
                'title' => $title,
                'cover' => $cover,
                'content' => $content
            ]);
            if(!empty($attachments)){
                $attachment_list = json_decode(htmlspecialchars_decode($attachments),true);
                // 保存订单商品信息
                $this->model->attachment()->saveAll($attachment_list);
            }
            Db::commit();
        } catch (\think\exception\PDOException $e) {
            Db::rollback();
            $this->error($e->getMessage());
        } catch (\think\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('发帖成功');
    }

    /**
     * @ApiWeigh (95)
     * @ApiTitle    (论坛-首页)
     * @ApiSummary  (论坛-首页)
     * @ApiMethod   (POST)
     * @ApiParams   (name="topic_category_id", type="inter", required=false, description="话题分类ID")
     * @ApiParams   (name="order", type="inter", required=false, description="排序方式:0=最新,1=热门,2=下载量")
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1602300901",
        "data": {
            "total": 1, //数据总数
            "list": [{
                "id": 1, //话题ID
                "title": "公司即将上市", //话题标题
                "cover": "http://qizhibang.brotop.cn/uploads/20201009/9bc002db9517fe79f93478550a979768.jpg", //封面图
                "content": "他咯土佐玉木桶浴", //话题内容
                "good_num": 3, //点赞量
                "appraise_num": 3, //评价量
                "download_num": 0, //下载量
                "createtime": "2020.09.29 09:11", //发布时间
                "user": { //发布者信息
                    "id": 15, //用户ID
                    "nickname": "", //昵称
                    "image": "" //头像
                },
                "imgs": [ //发布内容图片
                    "http://qizhibang.brotop.cn/uploads/20201010/dd2b86d8186f0e89735798d6f9a8f56c.jpg"
                ]
            }]
        }
    })
     */
    public function index()
    { 
        $topic_category_id = $this->request->param('topic_category_id');
        $order = $this->request->param('order',0);
        $page = $this->request->param('page', 1, 'intval');
        $page_num = $this->request->param('page_num', 10, 'intval');
        $where['status'] = '1'; //审核通过的显示 
        if(!empty($topic_category_id)){
            $where['topic_category_id'] = $topic_category_id;
        }
//        else{
//            $where['is_recommend'] = '1';
//        }
        switch ($order) {
            case 0:
                $order = ['createtime' => 'desc'];
                break;
            case 1:
                $order = ['hot_num' => 'desc'];
                break;
            case 2:
                $order = ['download_num' => 'desc'];
                break;
        }
        $data = $this->model
            ->with(['user'])
            ->withCount(['good'=>'good_num'])
            ->withCount(['appraise'=>'appraise_num'])
            ->where($where)
            ->order($order)
            ->paginate($page_num,false,['page'=>$page])
            ->each(function($v){
                // 发布时间格式化
                $v->createtime = date('Y.m.d H:i',$v->createtime);
                // 过滤标签
                $content1 = htmlspecialchars_decode($v->content);
                $content2 = str_replace(['&nbsp;','&emsp;'],['',''],$content1);
                $v->content = strip_tags($content2);
                // 拼装图片
                $imgs = str_replace('\\','',strip_tags($content2, '<img>'));
                //$matches[1] 为图片路径数组
                preg_match_all('/\<img\s+src\=\"([\w:\/\.]+)\"/', $imgs, $matches);
                // 不小于3张就显示3张,小于3张就显示1张图片
                $v->imgs = count($matches[1]) >=3 ? array_slice($matches[1],0,3) : (count($matches[1]) >=1 ? array_slice($matches[1],0,1) : []);
                // 显示数据
                $v->visible(['id','title','cover','content','createtime','download_num','user','good_num','appraise_num'])->append(['imgs']);
                $v->getRelation('user')->visible(['id','image','nickname']);
            })->toArray();
        $this->success('成功',['total'=>$data['total'],'list'=>$data['data']]);
    }

    /**
     * @ApiWeigh (93)
     * @ApiTitle    (搜索历史)
     * @ApiSummary  (搜索历史)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1601351666",
        "data": {
            "history_list": [ //搜索历史
                "公司即将上市3",
                "公司即将上市2",
                "公司即将上市1",
                "公司即将上市",
                "公司即将",
                "公司",
                "都是他舅",
                "啥",
                "嗯哼嘻嘻",
                "嗯哼"
            ],
            "hot_list": [ //热门搜索
                "123"
            ]
        }
    })
     */
    public function searchKeyword()
    {
        $history_list = Db::name('mobile_topic_keyword')
            ->where('user_id',$this->auth->id)
            ->order('updatetime desc')
            ->limit(10)
            ->column('keyword');
        $hot_list = Db::name('mobile_topic_keyword_hot')
            ->order('updatetime desc')
            ->limit(10)
            ->column('keyword');
        $this->success('成功',compact('history_list','hot_list'));
    }

    /**
     * @ApiWeigh (91)
     * @ApiTitle    (论坛搜索结果)
     * @ApiSummary  (论坛搜索结果)
     * @ApiMethod   (POST)
     *
     * @ApiParams (name="keyword", type="string", required=true, description="关键词")
     * @ApiParams (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1602300901",
        "data": {
            "total": 1, //数据总数
            "list": [{
                "id": 1, //话题ID
                "title": "公司即将上市", //话题标题
                "cover": "http://qizhibang.brotop.cn/uploads/20201009/9bc002db9517fe79f93478550a979768.jpg", //封面图
                "content": "他咯土佐玉木桶浴", //话题内容
                "good_num": 3, //点赞量
                "appraise_num": 3, //评价量
                "download_num": 0, //下载量
                "createtime": "2020.09.29 09:11", //发布时间
                "user": { //发布者信息
                    "id": 15, //用户ID
                    "nickname": "", //昵称
                    "image": "" //头像
                },
                "imgs": [ //发布内容图片
                    "http://qizhibang.brotop.cn/uploads/20201010/dd2b86d8186f0e89735798d6f9a8f56c.jpg"
                ]
            }]
        }
    })
     */
    public function search()
    {
        $keyword = $this->request->param('keyword');
        $page = $this->request->param('page', 1, 'intval');
        $page_num = $this->request->param('page_num', 10, 'intval');
        empty($keyword) && $this->error('请输入关键词');
        $has = TopicKeyword::where('user_id',$this->auth->id)
            ->where('keyword',$keyword)
            ->find();
        // 记录搜索历史
        if(!$has){
            $keyword_list = TopicKeyword::order('createtime asc')->select();
            // 超过10条的删除
            if(count($keyword_list) > 10){
                TopicKeyword::where('id',$keyword_list[0]['id'])->delete();
            }
            TopicKeyword::create([
                'user_id' => $this->auth->id,
                'keyword' => $keyword
            ],true);
        }else{
            $has->updatetime = time();
            $has->save();
        }
        $data = $this->model
            ->with(['user'])
            ->withCount(['good'=>'good_num'])
            ->withCount(['appraise'=>'appraise_num'])
            ->where('status','1')
            ->where('title','like','%'.$keyword.'%')
            ->order(['hot_num'=>'desc'])
            ->paginate($page_num,false,['page'=>$page])
            ->each(function($v){
                // 发布时间格式化
                $v->createtime = date('Y.m.d H:i',$v->createtime);
                // 过滤标签
                $content1 = htmlspecialchars_decode($v->content);
                $content2 = str_replace(['&nbsp;','&emsp;'],['',''],$content1);
                $v->content = strip_tags($content2);
                // 拼装图片
                $imgs = str_replace('\\','',strip_tags($content2, '<img>'));
                //$matches[1] 为图片路径数组
                preg_match_all('/\<img\s+src\=\"([\w:\/\.]+)\"/', $imgs, $matches);
                // 不小于3张就显示3张,小于3张就显示1张图片
                $v->imgs = count($matches[1]) >=3 ? array_slice($matches[1],0,3) : (count($matches[1]) >=1 ? array_slice($matches[1],0,1) : []);
                // 显示数据
                $v->visible(['id','title','cover','content','createtime','download_num','user','good_num','appraise_num'])->append(['imgs']);
                $v->getRelation('user')->visible(['id','image','nickname']);
            })->toArray();
        $this->success('成功',['total'=>$data['total'],'list'=>$data['data']]);
    }

    /**
     * @ApiWeigh (89)
     * @ApiTitle    (话题详情)
     * @ApiSummary  (话题详情)
     * @ApiMethod   (POST)
     *
     * @ApiParams (name="topic_id", type="int", required=true, description="话题ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1601356402",
        "data": {
            "id": 1, //话题ID
            "title": "公司即将上市", //标题
            "content": "这就是街舞", //内容
            "attachment": [{
                "id": 1, //附件ID
                "name": "wode" //附件名称
            }],
            "agree_num": 0, //赞同量
            "download_num": 0, //下载量
            "createtime": "2020.09.29 09:11", //发布时间
            "user": { //发布者信息
                "id": 15, //用户ID
                "nickname": "", //昵称
                "image": "" //头像
            }
        }
    })
     */
    public function info()
    {
        $topic_id = $this->request->param('topic_id');
        empty($topic_id) && $this->error('缺少必要参数');
        $info = $this->model->get($topic_id,['user','attachment']);
        if(!empty($info)){
            // 发布时间
            $info->createtime = date('Y.m.d H:i',$info->createtime);
            // 是否已点赞
            $good = TopicGood::get(['user_id'=>$this->auth->id,'topic_id'=>$topic_id]);
            $info->is_good = !empty($good) ? 1 : 0;
            // 点赞数量
            $info->good_num = TopicGood::where('topic_id',$topic_id)->count();
            // 评论数量
            $info->appraise_num = TopicAppraise::where('topic_id',$topic_id)->count();
            // 显示数据
            $info->visible(['id','title','cover','content','createtime','download_num','user','attachment'])->append(['is_good','good_num','appraise_num']);
            $info->getRelation('user')->visible(['id','image','nickname']);
            // 附件
            foreach ($info->getRelation('attachment') as $v) {
                $v->visible(['id','name']);
            }
        }
        $this->success('成功',$info);
    }

    /**
     * @ApiWeigh (88)
     * @ApiTitle    (话题详情-点赞或取消点赞)
     * @ApiSummary  (话题详情-点赞或取消点赞)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams (name="topic_id", type="int", required=true, description="话题ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1601356402",
        "data": null
    })
     */
    public function good()
    {
        $topic_id = $this->request->param('topic_id');
        empty($topic_id) && $this->error('缺少必要参数');
        $info = $this->model->get($topic_id);
        empty($info) && $this->error('话题信息不存在');
        $where = ['user_id'=>$this->auth->id,'topic_id'=>$topic_id];
        $good = TopicGood::get(['user_id'=>$this->auth->id,'topic_id'=>$topic_id]);
        if(empty($good)){
            TopicGood::create($where);
            // 增加话题热力值(点赞+1热力值)
            $info->setInc('hot_num');
            $this->success('点赞成功');
        }
        $good->delete();
        $this->success('取消点赞');
    }

    /**
     * @ApiWeigh (87)
     * @ApiTitle    (下载)
     * @ApiSummary  (下载)
     * @ApiMethod   (POST)
     *
     * @ApiParams (name="topic_id", type="int", required=true, description="话题ID")
     * @ApiParams (name="topic_attachment_id", type="int", required=true, description="附件ID")
     * @ApiParams (name="is_confirm", type="int", required=false, description="是否确认下载:0=否,1=是")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599046220",
        "data": {
            "url": "http://www.baidu.com", //附件地址
        }
    })
     */
    public function download()
    {
        $topic_id = $this->request->param('topic_id');
        $topic_attachment_id = $this->request->param('topic_attachment_id');
        $is_confirm = $this->request->param('is_confirm',0);
        if(!$topic_id || $topic_attachment_id){
            $this->error('缺少必需参数');
        }
        $topic = $this->model->get($topic_id);
        empty($topic) && $this->error('话题信息不存在');
        $topic_attachment = Attachment::get($topic_attachment_id);
        empty($topic_attachment) && $this->error('话题附件不存在');
        $user = $this->auth->getUser();
        $where = [
            'user_id' => $user['id'],
            'topic_id' => $topic_id,
            'topic_attachment_id' => $topic_attachment_id,
        ];
        $has = TopicAttachmentDownload::get($where);
        if(!$has){
            if($user['score'] < 15){
                if($user['group_id'] == 0){
                    $this->error('您当前的积分不足,可通过邀请好友或发帖赚取积分哦!',null,3);
                }else{
                    $this->error('您当前的积分不足了,快去充值积分吧!',null,4);
                }
            }else{
                if($is_confirm == 0){
                    $this->error('下载附件需消耗15积分,您是否确定下载?',null,2);
                }
            }
            TopicAttachmentDownload::create($where);
            // 减少积分
            \app\common\model\User::score(-15,$user['id'],'下载话题附件');
            // 增加话题下载量
            $topic->setInc('download_num');
            // 增加话题热力值(下载+20热力值)
            $topic->setInc('hot_num',20);
        }
        $this->success('成功',$topic_attachment['url']);
    }

    /**
     * @ApiWeigh (85)
     * @ApiTitle    (评论-列表)
     * @ApiSummary  (评论-列表)
     * @ApiMethod   (POST)
     *
     * @ApiParams (name="topic_id", type="int", required=true, description="话题ID")
     * @ApiParams (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
     * @ApiParams (name="order", type="inter", required=false, description="排序方式:1=时间排序,2=热度排序")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1602251159",
        "data": {
            "total": 1, //数据总条数
            "list": [{
                "id": 1, //评论ID
                "content": "这就是街舞", //评论内容
                "createtime": "10-09 19:43", //评论时间
                "good_num": 3, //点赞数量
                "user": { //用户信息
                    "id": 15, //用户ID
                    "nickname": "", //真实姓名
                    "image": "" //头像
                },
                "is_good": 1, //是否已点赞:0否,1是
                "child": { //子评论
                    "total": 2, //总条数
                    "list": [{
                        "id": 3, //评论ID
                        "content": "这就是街舞", //评论内容
                        "user": { //用户信息
                            "id": 15, //用户ID
                            "nickname": "" //真实姓名
                        },
                        "cue_appraise": { //@评论
                            "id": 2, //评论ID
                            "content": "这就是街舞", //评论内容
                            "user": { //用户信息
                                "id": 15, //用户ID
                                "nickname": "" //真实姓名
                            }
                        }
                    }]
                }
            }]
        }
    })
     */
    public function appraiseList()
    {
        $page = $this->request->param('page', 1, 'intval');
        $page_num = $this->request->param('page_num', 10, 'intval');
        $topic_id = $this->request->param('topic_id');
        $order = $this->request->param('order',0);
        switch ($order) {
            case 2:
                $order = ['good_num' => 'desc'];
                break;
            default:
                $order = ['createtime' => 'desc'];
                break;
        }
        $data = TopicAppraise::with(['user'])
            ->withCount(['good'=>'good_num'])
            ->where('topic_id',$topic_id)
            ->where('pid',0)
            ->order($order)
            ->paginate($page_num,false,['page'=>$page])
            ->each(function($v){
                // 是否已点赞
                $good = TopicAppraiseGood::get(['user_id'=>$this->auth->id,'topic_appraise_id'=>$v['id']]);
                $v->is_good = !empty($good) ? 1 : 0;
                // 评论时间
                $v->createtime = date('m-d H:i',$v['createtime']);
                // 子评论
                $childlist = TopicAppraise::with(['user'])
                    ->where('pid',$v['id'])
                    ->limit(2)
                    ->select();
                foreach ($childlist as $key => $value) {
                    $cue_appraise = [];
                    if($value['cue_appraise_id'] != $value['pid']){
                        $cue_appraise = TopicAppraise::get($value['cue_appraise_id'],['user'])->visible(['id','content','user']);
                        $cue_appraise->getRelation('user')->visible(['id','nickname']);
                    }
                    $value->cue_appraise = $cue_appraise;
                    // 子评论时间
                    $value->createtime = date('m-d H:i',$value->createtime);
                    $value->visible(['id','content','user'])->append(['cue_appraise']);
                    $value->getRelation('user')->visible(['id','nickname']);
                }
                $v->child = [
                    'total' => TopicAppraise::where('pid',$v['id'])->count(),
                    'list' => $childlist
                ];
                $v->visible(['id','content','good_num','createtime','user'])->append(['is_good','child']);
                $v->getRelation('user')->visible(['id','image','nickname']);
            })->toArray();
        $this->success('成功',['total'=>$data['total'],'list'=>$data['data']]);
    }

    /**
     * @ApiWeigh (83)
     * @ApiTitle    (评论-写评论)
     * @ApiSummary  (评论-写评论)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams (name="topic_id", type="int", required=true, description="话题ID")
     * @ApiParams (name="cue_appraise_id", type="int", required=true, description="被@评论ID")
     * @ApiParams (name="content", type="int", required=true, description="评论内容")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1600084253",
        "data": null
    })
     */
    public function appraise(){
        $topic_id = $this->request->param('topic_id');
        $cue_appraise_id = $this->request->param('cue_appraise_id');
        $content = $this->request->param('content');
        empty($topic_id) && $this->error('缺少必需参数');
        empty($content) && $this->error('请填写评论内容');
        $topic = $this->model->get($topic_id);
        empty($topic) && $this->error('话题信息不存在');
        $pid = 0;
        if(!empty($cue_appraise_id)){
            $info = TopicAppraise::get($cue_appraise_id);
            empty($info) && $this->error('评论不存在');
            $pid = $info['pid'] > 0 ? $info['pid'] : $info['id'];
        }
        TopicAppraise::create([
            'topic_id' => $topic_id,
            'pid' => $pid,
            'user_id' => $this->auth->id,
            'cue_appraise_id' => $cue_appraise_id,
            'content' => $content
        ]);
        if($topic['user_id'] != $this->auth->id){
            // 增加话题热力值(评论回复(本人除外)+5热力值)
            $topic->setInc('hot_num',5);
        }
        $this->success('评论成功');
    }

    /**
     * @ApiWeigh (81)
     * @ApiTitle    (评论-详情)
     * @ApiSummary  (评论-详情)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams (name="topic_appraise_id", type="int", required=true, description="顶级评论ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1602251541",
        "data": {
            "id": 1, //评论ID
            "content": "这就是街舞", //评论内容
            "createtime": "10-09 19:43", //评论时间
            "good_num": 3, //点赞数量
            "user": { //用户信心
                "id": 15, //用户ID
                "nickname": "", //真实姓名
                "image": "" //头像
            },
            "is_good": 1 //是否已点赞:0否,1是
        }
    })
     */
    public function appraiseInfo(){
        $topic_appraise_id = $this->request->param('topic_appraise_id');
        empty($topic_appraise_id) && $this->error('缺少必需参数');
        $info = TopicAppraise::get($topic_appraise_id,['user','good']);
        empty($info) && $this->error('评论信息不存在');
        // 是否已点赞
        $good = TopicAppraiseGood::get(['user_id'=>$this->auth->id,'topic_appraise_id'=>$topic_appraise_id]);
        $info->is_good = !empty($good) ? 1 : 0;
        // 评论时间
        $info->createtime = date('m-d H:i',$info->createtime);
        // 点赞数量
        $info->good_num = TopicAppraiseGood::where('topic_appraise_id',$topic_appraise_id)->count();
        $info->visible(['id','content','user','good_num','createtime'])->append(['is_good','good_num']);
        $info->getRelation('user')->visible(['id','image','nickname']);
        $this->success('成功',$info);
    }

    /**
     * @ApiWeigh (79)
     * @ApiTitle    (评论-详情-子评论列表)
     * @ApiSummary  (评论-详情-子评论列表)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
     * @ApiParams (name="topic_appraise_id", type="int", required=true, description="顶级评论ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1602251675",
        "data": {
            "total": 2, //数据总条数
            "list": [{
                "id": 3, //评论ID
                "content": "这就是街舞", //评论内容
                "createtime": "10-09 19:44", //评论时间
                "good_num": 1, //点赞数量
                "user": { //用户信息
                    "id": 15, //用户ID
                    "nickname": "", //真实姓名
                    "image": "" //头像
                },
                "is_good": 0, //是否已点赞:0否,1是
                "cue_appraise": { //@评论
                    "id": 2, //评论ID
                    "content": "这就是街舞", //评论内容
                    "user": { //用户信息
                        "id": 15, //用户ID
                        "nickname": "" //真实姓名
                    }
                }
            }]
        }
    })
     */
    public function getAppraiseChildlist(){
        $page = $this->request->param('page', 1, 'intval');
        $page_num = $this->request->param('page_num', 10, 'intval');
        $topic_appraise_id = $this->request->param('topic_appraise_id');
        empty($topic_appraise_id) && $this->error('缺少必需参数');
        $data = TopicAppraise::with(['user'])
            ->withCount(['good'=>'good_num'])
            ->where('pid',$topic_appraise_id)
            ->paginate($page_num,false,['page'=>$page])
            ->each(function($v){
                // 是否已点赞
                $good = TopicAppraiseGood::get(['user_id'=>$this->auth->id,'topic_appraise_id'=>$v['id']]);
                $v->is_good = !empty($good) ? 1 : 0;
                // 评论时间
                $v->createtime = date('m-d H:i',$v['createtime']);
                // @评论
                $cue_appraise = [];
                if($v['cue_appraise_id'] != $v['pid']){
                    $cue_appraise = TopicAppraise::get($v['cue_appraise_id'],['user'])->visible(['id','content','user']);
                    $cue_appraise->getRelation('user')->visible(['id','nickname']);
                }
                $v->cue_appraise = $cue_appraise;
                $v->visible(['id','content','user','good_num','createtime'])->append(['is_good','cue_appraise']);
                $v->getRelation('user')->visible(['id','image','nickname']);
            })->toArray();
        $this->success('成功',['total'=>$data['total'],'list'=>$data['data']]);
    }

    /**
     * @ApiWeigh (77)
     * @ApiTitle    (评论-点赞或取消点赞)
     * @ApiSummary  (评论-点赞或取消点赞)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams (name="topic_appraise_id", type="int", required=true, description="评论ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1601356402",
        "data": null
    })
     */
    public function goodAppraise()
    {
        $topic_appraise_id = $this->request->param('topic_appraise_id');
        empty($topic_appraise_id) && $this->error('缺少必要参数');
        $info = TopicAppraise::get($topic_appraise_id);
        empty($info) && $this->error('评论信息不存在');
        $where = ['user_id'=>$this->auth->id,'topic_appraise_id'=>$topic_appraise_id];
        $good = TopicAppraiseGood::get($where);
        if(empty($good)){
            TopicAppraiseGood::create($where);
            $this->success('点赞成功');
        }
        $good->delete();
        $this->success('取消点赞');
    }
}