Exhibition.php 37.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 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
<?php


namespace app\api\controller;

use EasyWeChat\Factory;
use think\Db;
use think\Exception;

/**
 * 展览相关
 * @package app\api\controller
 */
class Exhibition extends BaseApi
{
    protected $noNeedLogin = ['setGiveGiftWechatNotify','setWechatNotify'];
    protected $noNeedRight = '*';

    /**
     * 查询展览时长
     * @ApiTitle    (查询展览时长)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/exhibition/getExhibitionPrice)
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
        "code": 1,
        "msg": "查询成功",
        "time": "1609748728",
        "data": [
            {
                "id": 1,
                "day": "几天",
                "price": "价格",
                "weigh": "排序",
                "createtime": "1970-01-01 08:00:00",
                "updatetime": "1970-01-01 08:00:00"
            }
        ]
        })
     */
    public function getExhibitionPrice(){
        //1.查询展览时长
        $list = model('exhibition_price')->order('weigh','desc')->select();
        //2.返回
        $this->success('查询成功',$list);
    }
    /**
     * 发布展览
     * @ApiTitle    (发布展览)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/exhibition/addExhibition)
     * @ApiParams   (name="title", type="string", required=true, description="展览标题")
     * @ApiParams   (name="describe", type="string", required=true, description="展览描述")
     * @ApiParams   (name="images", type="string", required=true, description="展览图片")
     * @ApiParams   (name="exhibition_price_id", type="integer", required=true, description="展览价格ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
        "code": 1,
        "msg": "获取支付成功",
        "time": "1609755422",
        "data": {
            "appId": "wx529923b385fe8f5b",
            "nonceStr": "5ff2eb1f1db87",
            "package": "prepay_id=wx0418170325184060863236bfe63f350000",
            "signType": "MD5",
            "paySign": "45258090229C705860FABCF2AD723221",
            "timestamp": "1609755423"
        }
        })
     */
    public function addExhibition(){
        //1.获取数据
        $data = $this->get_data_array([
            ['title','展览标题不能为空'],
            ['describe','展览描述不能为空'],
            ['images','展览图片不能为空'],
            ['exhibition_price_id','展览价格ID不能为空'],
        ]);
        //2.查询之前发布的展览 删除
        $exhibition_list = model('exhibition')->where('user_id',$this->auth->id)->where('createtime','<',time()-3600)->where('status',0)->column('id');
        if ($exhibition_list){
            model('exhibition')->whereIn('id',$exhibition_list)->delete();
        }
        //3.查询价格
        $exhibition_price = model('exhibition_price')->where('id',$data['exhibition_price_id'])->find();
        if (!$exhibition_price){
            $this->error('价格不存在');
        }
        //4.获取图片数据
        $files = str_replace('&quot;','"',$data['images']);
        $images = implode(',',json_decode($files,true));
        //5.重组数据
        $data['number'] = time() - mt_rand(10000,99999);
        $data['user_id'] = $this->auth->id;
        $data['avatar'] = $this->auth->avatar;
        $data['nickname'] = $this->auth->nickname;
        $data['price'] = $exhibition_price['price'];
        $data['images'] = $images;
        $data['images_num'] = count(json_decode($files,true));
        $data['start_time'] = time();
        $data['end_time'] = time() + ($exhibition_price['day'] * (24*60*60));
        $data = model('exhibition')->create($data);
        //6.获取配置信息
        $config = config('EasyWeChat');
        $app = Factory::payment($config);
        //7.查询用户的第三方登录信息
        $fa_third = Db::name('third')->where('user_id',$this->auth->id)->find();
        if (!$fa_third)$this->error('第三方授权数据不存在');
        //8.统一下单接口
        $result = $app->order->unify([
            'body' => '送出礼物',
            'out_trade_no' => $data['number'],
            //'total_fee' => $order['price'] * 100,
            'total_fee' => 1,
            'notify_url' =>this_url().'/api/exhibition/setWechatNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
            'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
            'openid' => $fa_third['openid'],
        ]);
        //9.判断返回结果
        if ($result['result_code'] == 'SUCCESS' && $result['return_code'] == 'SUCCESS') {
            $prepayId = $result['prepay_id'];
            $jssdk = $app->jssdk;
            $config = $jssdk->sdkConfig($prepayId);
            $this->success('获取支付成功',$config);
        } else {
            $this->error('失败',$result);
        }
    }
    /**
     * 支付回调
     * @ApiMethod   (POST)
     * @ApiInternal
     * @ApiRoute    (/api/exhibition/setWechatNotify)
     */
    public function setWechatNotify(){
        $config = config('EasyWeChat');
        $app = Factory::payment($config);
        $response = $app->handlePaidNotify(function ($message, $fail) {
            //将展览设置已经支付
            $order = model('exhibition')->where('number',$message['out_trade_no'])->find();
            if (!$order)$fail('订单不存在');
            if ($order['status'] == 1)$fail('您的订单已经支付');
            $order->status = 1;
            $order->save();
        });
        $response->send(); // Laravel 里请使用:return $response;
    }



    /**
     * 展览收藏或取消收藏
     * @ApiTitle    (展览收藏或取消收藏)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/exhibition/setExhibitionCollect)
     * @ApiParams   (name="exhibition_id", type="integer", required=true, description="展览id")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
        "code": 1,
        "msg": "查询成功",
        "time": "1608886856",
        "data": {
        "code": "0取消收藏 1=收藏成功",
        "msg": "提示"
        }
    })
     */
    public function setExhibitionCollect(){
        //1.获取数据
        $exhibition_id = $this->get_data('exhibition_id','展览id不能为空');
        //2.查询展览数据
        $exhibition = model('exhibition')->where('id',$exhibition_id)->find();
        //3.查询点赞
        $exhibition_collect = model('exhibition_collect')
            ->where('user_id',$this->auth->id)
            ->where('exhibition_id',$exhibition_id)
            ->find();
        if ($exhibition_collect){
            $exhibition_collect->delete();
            $exhibition->shoucang_num -= 1;
            $exhibition->save();
            $this->success('查询成功',['code'=>0,'msg'=>'取消收藏成功']);
        }
        //4.增加点赞
        model('exhibition_collect')->create(['user_id'=>$this->auth->id,'exhibition_id'=>$exhibition_id]);
        //5.增加点赞数量
        $exhibition->shoucang_num += 1;
        $exhibition->save();
        //6.返回结果
        $this->success('查询成功',['code'=>1,'msg'=>'收藏成功']);
    }


    /**
     * 查询展览轮播图
     * @ApiTitle    (查询展览轮播图)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/exhibition/getExhibitionBanner)
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
        "code": 1,
        "msg": "查询成功",
        "time": "1609756367",
        "data": [
            {
                "id": 1,
                "image": "图片路径",
                "key_id": "外键id 展览id 为空不跳转",
                "weigh": "排序",
                "createtime": "1970-01-01 08:00:00",
                "updatetime": "1970-01-01 08:00:00"
            }
        ]
        })
     */
    public function getExhibitionBanner(){
        //1.查询展览时长
        $list = model('exhibition_banner')->order('weigh','desc')->select();
        //2.返回
        $this->success('查询成功',$list);
    }


    /**
     * 查询展览详情
     * @ApiTitle    (查询展览详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/exhibition/getExhibitionContent)
     * @ApiParams   (name="id", type="integer", required=true, description="展览标ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
        "code": 1,
        "msg": "查询成功",
        "time": "1609757334",
        "data": {
            "id": 1,
            "user_id": "用户ID",
            "avatar": "头像",
            "nickname": "署名",
            "number": "展览编号",
            "title": "展览标题",
            "describe": "展览描述",
            "exhibition_price_id": "价格ID",
            "price": "价格",
            "images": "展览图片",
            "show_num": "查看数量",
            "zan_num": "点赞数量",
            "ping_num": "评价数量",
            "price_num": "总价格",
            "xiang_num": "星星数量",
            "images_num": "图片数量",
            "status": "状态:0=未支付,2=已支付,3=已下架",
            "start_time": "开始时间",
            "end_time": "结束时间",
            "is_zan":"判断是否点赞",
            "createtime": "2021-01-04 18:04:25",
            "updatetime": "2021-01-04 18:04:25",
            "deletetime": null,
            "message": null
        }
        })
     */
    public function getExhibitionContent(){
        //1.查询id
        $id = $this->get_data('id','展览ID不能为空');
        //2.查询展览
        $exhibition = model('exhibition')->where('status',1)->order('id',$id)->find();
        if (!$exhibition){
            $this->error('展览不存在或未支付');
        }
        //3.判断点赞
        $exhibition['is_zan'] = 0;
        $exhibition_zan = model('exhibition_zan')
            ->where('user_id',$this->auth->id)
            ->where('exhibition_id',$exhibition['id'])
            ->find();
        if($exhibition_zan){
            $exhibition['is_zan'] = 1;
        }

        //3.返回
        $this->success('查询成功',$exhibition);
    }

    /**
     * 查询展览列表
     * @ApiTitle    (查询展览列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/exhibition/getExhibitionList)
     * @ApiParams   (name="page", type="integer", required=true, description="分页次数")
     * @ApiParams   (name="num", type="integer", required=true, description="分页数量")
     * @ApiParams   (name="status", type="integer", required=true, description="0=全部展览,1=最新展览,2=最热展览")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ()
     */
    public function getExhibitionList(){
        //1.查询数据
        $data = $this->get_data_array([
            ['page','分页次数不能为空'],
            ['num','分页数量不能为空'],
            ['status','状态不能为空'],
        ]);
        //2.查询数组
        $exhibition = model('exhibition')
            ->field('id,images,title,show_num')
            ->where('status',1);
        //全部展览
        if ($data['status'] == 0){
            $exhibition = $exhibition->order('createtime','desc');
        }
        //最新展览
        if ($data['status'] == 1){
            $exhibition = $exhibition->order('createtime','desc')->page($data['page'],$data['num']);
        }
        //最热展览
        if ($data['status'] == 2){
            $exhibition = $exhibition->order('zan_num','desc')->page($data['page'],$data['num']);
        }
        $exhibition = $exhibition->select();
        //3.返回
        $this->success('查询成功',$exhibition);
    }


    /**
     * 点赞或取消点赞
     * @ApiTitle    (点赞或取消点赞)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/exhibition/setExhibitionZan)
     * @ApiParams   (name="exhibition_id", type="integer", required=true, description="展览ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
    "code": 1,
    "msg": "查询成功",
    "time": "1608886856",
    "data": {
    "code": "0取消点赞 1=点赞成功",
    "msg": "提示"
    }
    })
     */
    public function setExhibitionZan(){
        //1.获取数据
        $exhibition_id = $this->get_data('exhibition_id','展览id不能为空');
        //2.查询点赞
        $exhibition_zan = model('exhibition_zan')
            ->where('user_id',$this->auth->id)
            ->where('exhibition_id',$exhibition_id)
            ->find();
        if ($exhibition_zan){
            $exhibition_zan->delete();
            model('exhibition')->where('id',$exhibition_zan)->setDec('zan_num');
            $this->success('查询成功',['code'=>0,'msg'=>'取消点赞成功']);
        }
        //4.增加点赞
        model('exhibition_zan')->create(['user_id'=>$this->auth->id,'exhibition_id'=>$exhibition_id]);
        //5.增加点赞数量
        model('exhibition')->where('id',$exhibition_id)->setInc('zan_num');
        //6.返回结果
        $this->success('查询成功',['code'=>1,'msg'=>'点赞成功']);
    }

   
    /**
     * 获取展览评论列表
     * @ApiTitle    (获取展览评论列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/exhibition/getComment)
     * @ApiParams   (name="exhibition_id", type="integer", required=true, description="展览id")
     * @ApiParams   (name="page", type="integer", required=true, description="分页次数")
     * @ApiParams   (name="num", type="integer", required=true, description="分页数量")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
    "code": 1,
    "msg": "查询成功",
    "time": "1608890670",
    "data": [
    {
    "id": 2,
    "exhibition_id": "展览id",
    "user_id": "用户id",
    "content": "评论内容",
    "pid": "0一级评论 不是0是父级评论id",
    "zan_num": "点赞数量",
    "createtime": "2020-12-25 17:53:07",
    "updatetime": "2020-12-25 17:53:07",
    "deletetime": null,
    "message": null,
    "is_zan": "是否点赞  0未点赞 1点赞",
    "son_num":"子评论数量",
    "user": "用户信息参考首页列表注释",
    "str_time": "发布时间"
    }
    ]
    })
     */
    public function getComment(){
        Db::startTrans();
        try {
            //1.获取展览id
            $data = $this->get_data_array([
                ['exhibition_id','展览id不能为空'],
                ['page','分页次数不能为空'],
                ['num','分页数量不能为空']
            ]);
            //2.获取一级评论
            $exhibition_comment = model('exhibition_comment')
                ->with(['user'])
                ->where('exhibition_id',$data['exhibition_id'])
                ->where('pid',0)
                ->order('createtime','desc')
                ->order('deletetime',null)
                ->page($data['page'],$data['num'])
                ->select();
            //3.循环父级评论
            foreach ($exhibition_comment as $key => $val){
                //3.1 判断用户有没有点赞
                $is_zan = model('exhibition_comment_zan')
                    ->where('user_id',$this->auth->id)
                    ->where('exhibition_comment_id',$val['id'])
                    ->find();
                $exhibition_comment[$key]['is_zan'] = 1;
                if (!$is_zan){
                    $exhibition_comment[$key]['is_zan'] = 0;
                }
                //3.2 获取子评论数量
                $exhibition_comment[$key]['son_num'] = model('exhibition_comment')
                    ->where('pid',$val['id'])
                    ->count();
            }
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        //4.返回数据
        $this->success('查询成功',$exhibition_comment);
    }
    /**
     * 发布评论
     * @ApiTitle    (发布评论)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/exhibition/setComment)
     * @ApiParams   (name="exhibition_id", type="integer", required=true, description="展览id")
     * @ApiParams   (name="content", type="integer", required=true, description="评论内容")
     * @ApiParams   (name="pid", type="integer", required=true, description="父级评论id")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ( )
     */
    public function setComment(){
        Db::startTrans();
        try {
            //1.获取展览id
            $data= $this->get_data_array([
                ['exhibition_id','展览id不能为空'],
                ['content','评论内容'],
            ]);
            //2.发布评论
            $pid = empty(input('param.pid'))?0:input('param.pid');
            $data['pid'] = $pid;
            $data['user_id'] = $this->auth->id;
            $data['zan_num'] = 0;
            $res = model('exhibition_comment')->create($data);
            $res = model('exhibition_comment')->with(['user'])->where('id',$res['id'])->find();
            $res['is_zan'] = 0;
            //3.增加展览评论数据
            model('exhibition')->where('id',$data['exhibition_id'])->setInc('ping_num');
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        //3.发布成功
        $this->success('发布评论成功',$res);
    }
    /**
     * 根据父级评论获取子评论
     * @ApiTitle    (根据父级评论获取子评论)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/exhibition/getTwoComment)
     * @ApiParams   (name="exhibition_comment_id", type="integer", required=true, description="展览评论id")
     * @ApiParams   (name="page", type="integer", required=true, description="分页次数")
     * @ApiParams   (name="num", type="integer", required=true, description="分页数量")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
    "code": 1,
    "msg": "获取评论成功",
    "time": "1608892708",
    "data": [
    {
    "id": 2,
    "exhibition_id": "展览ID",
    "user_id": "用户ID",
    "content": "评论内容",
    "pid": "父级id 注意:第一次返回的第一个数据是父级数据 第二次不会返回",
    "zan_num": "点赞数量",
    "createtime": "2020-12-25 17:53:07",
    "updatetime": "2020-12-25 17:53:07",
    "deletetime": null,
    "message": null,
    "is_zan": "是否点赞",
    "is_this": "是不是自己发布的评论 1=是,0=不是",
    "user": "用户信息 参考首页列表注释",
    "str_time": "时间"
    }
    ]
    })
     */
    public function getTwoComment(){
        Db::startTrans();
        try {
            //1.获取展览id
            $data= $this->get_data_array([
                ['exhibition_comment_id','展览评论id不能为空'],
                ['page','分页次数不能为空'],
                ['num','分页数量不能为空']
            ]);
            //2.发布评论
            $father_exhibition_comment = model('exhibition_comment')
                ->with(['user'])
                ->where('id',$data['exhibition_comment_id'])
                ->find();
            if (!$father_exhibition_comment){
                $this->error('您的父级评论不存在');
            }
            //3.判断是不是第一次 如果是第一次添加进去父级评论
            $array = [];
            if ($data['page'] == 1){
                $array[] = $father_exhibition_comment;
            }
            //4.查询子级数据
            $son_exhibition_somment_array = model('exhibition_comment')
                ->with(['user'])
                ->where('pid',$father_exhibition_comment['id'])
                ->order('createtime','desc')
                ->page($data['page'],$data['num'])
                ->select();
            foreach ($son_exhibition_somment_array as $val){
                $array[] = $val;
            }
            //5.循环查询是否点赞
            foreach ($array as $key => $val){
                //3.1 判断用户有没有点赞
                $is_zan = model('exhibition_comment_zan')
                    ->where('user_id',$this->auth->id)
                    ->where('exhibition_comment_id',$val['id'])
                    ->find();
                $array[$key]['is_zan'] = 1;
                if (!$is_zan){
                    $array[$key]['is_zan'] = 0;
                }
                //3.2判断是不是自己发布的评论
                $array[$key]['is_this'] = 0;
                if ($val['user_id'] == $this->auth->id){
                    $array[$key]['is_this'] = 1;
                }
            }
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('获取评论成功',$array);
    }
    /**
     * 展览评论点赞或取消点赞{注意}
     * @ApiTitle    (展览评论点赞或取消点赞{注意})
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/exhibition/setExhibitionCommentZan)
     * @ApiParams   (name="exhibition_comment_id", type="integer", required=true, description="展览内容")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
    "code": 1,
    "msg": "查询成功",
    "time": "1608886856",
    "data": {
    "code": "0取消点赞 1=点赞成功",
    "msg": "提示"
    }
    })
     */
    public function setExhibitionCommentZan(){
        //1.获取数据
        $exhibition_comment_id = $this->get_data('exhibition_comment_id','展览评论id不能为空');
        //2.查询展览数据
        $exhibition_comment = model('exhibition_comment')->where('id',$exhibition_comment_id)->find();
        //3.查询点赞
        $exhibition_comment_zan = model('exhibition_comment_zan')
            ->where('user_id',$this->auth->id)
            ->where('exhibition_comment_id',$exhibition_comment_id)
            ->find();
        if ($exhibition_comment_zan){
            $exhibition_comment_zan->delete();
            $exhibition_comment->zan_num -= 1;
            $exhibition_comment->save();
            $this->success('查询成功',['code'=>0,'msg'=>'取消点赞成功']);
        }
        //4.增加点赞
        model('exhibition_comment_zan')->create(['user_id'=>$this->auth->id,'exhibition_comment_id'=>$exhibition_comment_id]);
        //5.增加点赞数量
        $exhibition_comment->zan_num += 1;
        $exhibition_comment->save();
        //6.返回结果
        $this->success('查询成功',['code'=>1,'msg'=>'点赞成功']);
    }

    /**
     * 举报展览
     * @ApiTitle    (举报展览)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/exhibition/setExhibitionReport)
     * @ApiParams   (name="exhibition_id", type="integer", required=true, description="展览id")
     * @ApiParams   (name="report_type_id", type="integer", required=true, description="举报类型ID")
     * @ApiParams   (name="content", type="integer", required=true, description="举报内容")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
        "code": 1,
        "msg": "举报成功",
        "time": "1609760150",
        "data": {
            "exhibition_id": "1",
            "report_type_id": "1",
            "content": "你说的啥啊",
            "user_id": 2,
            "avatar": "https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eo0mGhWdZVZMqyNCg4CWn7Xyplya5eEF1UUnFlOcF4vvZ9IibfWGocjiatViaP9b4JtosbuP9NibhlEWw/132",
            "nickname": "珍惜自己的名字",
            "username": "u2",
            "mobile": "",
            "status": 0,
            "report_type_name": "违法违禁",
            "createtime": "2021-01-04 19:35:51",
            "updatetime": "2021-01-04 19:35:51",
            "id": "8"
        }
        })
     */
    public function setExhibitionReport(){
        Db::startTrans();
        try {
            //1.获取展览id
            $data= $this->get_data_array([
                ['exhibition_id','展览ID不能为空'],
                ['report_type_id','举报类型ID不能为空'],
                ['content','举报内容不能为空'],
            ]);
            //2.查询举报内容
            $exhibition_report = model('exhibition_report')
                ->where('user_id',$this->auth->id)
                ->where('exhibition_id',$data['exhibition_id'])
                ->where('report_type_id',$data['report_type_id'])
                ->where('status',0)
                ->find();
            if ($exhibition_report){
                $this->error('您已经举报该展览,请等待管理员审核!');
            }
            //3.查询举报类型
            $data['user_id'] = $this->auth->id;
            $data['avatar'] = $this->auth->avatar;
            $data['nickname'] = $this->auth->nickname;
            $data['username'] = $this->auth->username;
            $data['mobile'] = $this->auth->mobile;
            $data['status'] = 0;
            $data['report_type_name'] = model('report_type')->where('id',$data['report_type_id'])->find()['name']??'';
            $res = model('exhibition_report')->create($data);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        //3.发布成功
        $this->success('举报成功',$res);
    }

    /**
     * 举报评论
     * @ApiTitle    (举报评论)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/exhibition/setExhibitionCommentReport)
     * @ApiParams   (name="exhibition_comment_id", type="integer", required=true, description="评论id")
     * @ApiParams   (name="report_type_id", type="integer", required=true, description="举报类型ID")
     * @ApiParams   (name="content", type="integer", required=true, description="举报内容")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ()
     */
    public function setExhibitionCommentReport(){
        try {
            //1.获取展览id
            $data= $this->get_data_array([
                ['exhibition_comment_id','评论ID不能为空'],
                ['report_type_id','举报类型ID不能为空'],
                ['content','举报内容不能为空'],
            ]);
            //2.查询举报内容
            $exhibition_report = model('exhibition_comment_report')
                ->where('user_id',$this->auth->id)
                ->where('exhibition_comment_id',$data['exhibition_comment_id'])
                ->where('report_type_id',$data['report_type_id'])
                ->where('status',0)
                ->find();
            if ($exhibition_report){
                $this->error('您已经举报该评论,请等待管理员审核!');
            }
            //3.查询举报类型
            $data['user_id'] = $this->auth->id;
            $data['avatar'] = $this->auth->avatar;
            $data['nickname'] = $this->auth->nickname;
            $data['username'] = $this->auth->username;
            $data['mobile'] = $this->auth->mobile;
            $data['status'] = 0;
            $data['report_type_name'] = model('report_type')->where('id',$data['report_type_id'])->find()['name']??'';
            $res = model('exhibition_comment_report')->create($data);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        //3.发布成功
        $this->success('举报成功',$res);
    }


    /**
     * 送给展览礼物
     * @ApiTitle    (送给展览礼物)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/exhibition/giveGift)
     * @ApiParams   (name="exhibition_id", type="integer", required=true, description="展览id")
     * @ApiParams   (name="gift_id", type="integer", required=true, description="礼物id")
     * @ApiParams   (name="price", type="integer", required=true, description="价格")
     * @ApiParams   (name="status", type="integer", required=true, description="是否是自定义价格:0=不是{需要填写gift_id},1=是{需要填写金额}")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
        "code": 1,
        "msg": "获取支付成功",
        "time": "1609812205",
        "data": {
            "appId": "wx529923b385fe8f5b",
            "nonceStr": "5ff3c8eeda51c",
            "package": "prepay_id=wx05100327610751364fcdebf9b209bb0000",
            "signType": "MD5",
            "paySign": "3FED5AA48E67759668E02568CFDBF031",
            "timestamp": "1609812206"
        }
        })
     */
    public function giveGift(){
        //1.获取数据
        $data = $this->get_data_array([
            ['exhibition_id','您的展览ID不能为空'],
            ['status','您的自定义价格字段不能为空']
        ]);
        //2.查询展览
        $exhibition = model('exhibition')->where('id',$data['exhibition_id'])->find();
        if (!$exhibition)$this->error('您的展览不存在,请您检查exhibition_id');
        //3.判断不是自定义价格
        if ($data['status'] == 0){
            //3.1 查询礼物
            $exhibition_gift = model('gift')->where('id',$this->get_data('gift_id','礼物id不能为空'))->find();
            if (!$exhibition_gift)$this->error('未查询到相关礼物,请您检查gift_id');
            //3.2 查询礼物订单
            $is_order = model('exhibition_gift_order')
                ->where('exhibition_id',$exhibition['id'])
                ->where('gift_id',$exhibition_gift['id'])
                ->where('is_custom',0)
                ->where('status',0)
                ->where('price',$exhibition_gift['price'])
                ->find();
            if (!$is_order){
                $a_data = [
                    'number'=>time()+rand(111111,999999),
                    'exhibition_id'=>$exhibition['id'],
                    'exhibition_user_id'=>$exhibition['user_id'],
                    'user_id'=>$this->auth->id,
                    'avatar'=>$this->auth->avatar,
                    'nickname'=>$this->auth->nickname,
                    'username'=>$this->auth->username,
                    'mobile'=>$this->auth->mobile,
                    'price'=>$exhibition_gift['price'],
                    'status'=>0,
                    'is_custom'=>0,
                    'gift_id'=>$exhibition_gift['id'],
                    'gift_name'=>$exhibition_gift['name'],
                ];
                $is_order = model('exhibition_gift_order')->create($a_data);
            }

        }
        //4.判断是自定义价格
        if ($data['status'] == 1){
            //4.1 查询礼物
            $price = $this->get_data('price','价格不能为空');
            //4.2 查询礼物订单
            $is_order = model('exhibition_gift_order')
                ->where('exhibition_id',$exhibition['id'])
                ->where('is_custom',1)
                ->where('status',0)
                ->where('price',$price)
                ->find();
            if (!$is_order){
                $a_data = [
                    'number'=>time()+rand(111111,999999),
                    'exhibition_id'=>$exhibition['id'],
                    'exhibition_user_id'=>$exhibition['user_id'],
                    'user_id'=>$this->auth->id,
                    'avatar'=>$this->auth->avatar,
                    'nickname'=>$this->auth->nickname,
                    'username'=>$this->auth->username,
                    'mobile'=>$this->auth->mobile,
                    'price'=>$price,
                    'status'=>0,
                    'is_custom'=>0,
                ];
                $is_order = model('exhibition_gift_order')->create($a_data);
            }
        }
        //------------ 支付 -------------
        //5.获取配置信息
        $config = config('EasyWeChat');
        $app = Factory::payment($config);
        //6.查询用户的第三方登录信息
        $fa_third = Db::name('third')->where('user_id',$this->auth->id)->find();
        if (!$fa_third)$this->error('第三方授权数据不存在');
        //3.统一下单接口
        $result = $app->order->unify([
            'body' => '送出礼物',
            'out_trade_no' => $is_order['number'],
            //'total_fee' => $order['price'] * 100,
            'total_fee' => 1,
            'notify_url' =>this_url().'/api/exhibition/setGiveGiftWechatNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
            'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
            'openid' => $fa_third['openid'],
        ]);
        //4.判断返回结果
        if ($result['result_code'] == 'SUCCESS' && $result['return_code'] == 'SUCCESS') {
            $prepayId = $result['prepay_id'];
            $jssdk = $app->jssdk;
            $config = $jssdk->sdkConfig($prepayId);
            $this->success('获取支付成功',$config);
        } else {
            $this->error('失败',$result);
        }
        $list = model('exhibition_gift')->order('weigh','desc')->limit(7)->select();
        //2.返回数据
        $this->success('获取数据成功',$list);
    }
    /**
     * 支付礼物回调
     * @ApiMethod   (POST)
     * @ApiInternal
     * @ApiRoute    (/api/exhibition/setWechatNotify)
     * @ApiReturn   ()
     */
    public function setGiveGiftWechatNotify(){
        $config = config('EasyWeChat');
        $app = Factory::payment($config);
        $response = $app->handlePaidNotify(function ($message, $fail) {
            //将礼物进行支付
            $order = model('exhibition_gift_order')->where('number',$message['out_trade_no'])->find();
            if (!$order)$fail('订单不存在');
            if ($order['status'] == 1)$fail('您的订单已经支付');
            $order->status = 1;
            $order->save();
            //查询用户和展览
            $user = model('user')->where('id',$order['user_id'])->find();
            $exhibition = model('exhibition')->where('id',$order['exhibition_id'])->find();
            //给礼物用户一个通知
            if ($user){
                $str = empty($order['gift_id'])?'自定义礼物':$order['gift_name'];
                $message = [
                    'user_id'=>$order['exhibition_user_id'],
                    'in_user_id'=>$user['id'],
                    'avatar'=>$user['avatar'],
                    'nickname'=>$user['nickname'],
                    'title'=>$exhibition['content'],
                    'brief'=>'用户:'.$this->auth->nickname.' 送给您的展览一个:'.$str,
                    'status'=>3,
                    'is_stu'=>4,
                    'key_id'=>$exhibition['id'],
                ];
                model('message')->create($message);
            }
            //将展览的金额增加
            model('exhibition')->where('id',$order['exhibition_id'])->setInc('price_num',$order['price']);
        });
        $response->send(); // Laravel 里请使用:return $response;
    }

}