HouseBoard.php 32.5 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
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/10
 * Time: 17:32
 */

namespace app\api\controller;


use app\api\model\HouseComment;
use app\api\model\Message;
use app\common\controller\Api;
use think\Cache;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\Validate;
use EasyWeChat\Foundation\Application as WXPAY_APP;
use EasyWeChat\Payment\Order as WXPAY_ORDER;
use addons\third\model\Third;

 /**
 * 社区公告
 */
class HouseBoard extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * @ApiTitle    (社区公告-首页)
     * @ApiSummary  (社区公告-首页)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn({
        "code": 1,
        "msg": "success",
        "time": "1598867996",
        "data": {
            "is_binding": 1, //是否绑定1已绑定2未绑定
            "house": { // 小区信息
                "image": "http://community.qiniu.brotop.cn/uploads/20200819/ce66bda6f19bc3755029147429d3e549.jpg", //小区图片
                "name": "兴安苑", //小区名称
                "thumbnail": "/uploads/20200819/6887d4e270376751368e0aa358210f12.png", //小区二维码
                "bindnum": 0, //已绑定人数
                "have_message": 2 //是否有消息1=有,1=无
            },
            "house_board": { //公告信息
                "id": 1, //公告ID
                "title": "周六交电费", //公告标题
                "content": "<span style=\"white-space:normal;\">这周是双休,好好安排一下该这么过,今天又是没有尊严的一天</span>", //公告内容
                "look_num": 5, //浏览量
                "createtime": "2020-08-19", //发布时间
                "images": "", //社区公告图片
                "images_arr": [ //社区公告图片数组
                    "http://community.qiniu.brotop.cn"
                ],
                "is_new": 1, //是否是新 1是2否
                "avatar": [{ //用户头像
                    "avatar": "",
                    "id": 1
                }]
            }
        }
    })
     */
    public function index()
    {
        $user_id = $this->auth->id;
        $data = Db::name('user')->where('id',$user_id)->field('id,house_id')->find();
        if(empty($data['house_id'])){
            $info['is_binding'] = 2;
        }else{
            $info['is_binding'] = 1;
            /*小区信息*/
            $house = Db::name('house')
                ->where('id',$data['house_id'])
                ->field('image,name,thumbnail,bindnum')
                ->find();
            // 消息红点
            $message = Db::name('message')
                ->where('object_id',$data['house_id'])
                ->where('user_id',$user_id)
                ->where('is_read_user',2)
                ->field('id')
                ->find();
            $house['have_message'] = !empty($message) ? 1 : 2;
            $house['image'] = cdnurl($house['image'],true);
            $house['thumbnail'] = cdnurl($house['thumbnail'],true);
            /*最新公告信息*/
            $house_board = Db::name('house_board')
                ->where('house_id',$data['house_id'])
                ->order('createtime desc')
                ->field('id,title,content,look_num,createtime,images')
                ->find();
            if(!empty($house_board)){
                $house_board['createtime'] = date('Y-m-d',$house_board['createtime']);
                // 公告图片
                $images_arr = [];
                if(!empty($house_board['images'])){
                    foreach (explode(',', $house_board['images']) as $key => $value) {
                        $images_arr[] = cdnurl($value,true);
                    }
                }
                $house_board['images_arr'] = $images_arr;
                // 最新公告是否已读
                $is_read = Db::name('house_board_detail')
                    ->where('user_id',$user_id)
                    ->where('house_board_id',$house_board['id'])
                    ->find();
                $house_board['is_new'] = empty($is_read) ? 1 : 2;
                // 最新公告-用户头像
                $house_board['avatar'] = Db::name('house_board_detail')
                    ->alias('a')
                    ->join('user b','a.user_id = b.id')
                    ->where('a.house_board_id',$house_board['id'])
                    ->field('b.avatar,b.id')
                    ->limit(6)
                    ->select();
                foreach ($house_board['avatar'] as &$v) {
                    $v['avatar'] = cdnurl($v['avatar'],true);
                }
            }
            $info = array_merge($info,compact('house','house_board'));
        }
        $this->success('success',$info);
    }

    /**
     * @ApiTitle    (社区公告-业主信息)
     * @ApiSummary  (社区公告-业主信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/house/avatar)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="house_board_id", type="inter", required=true, description="公告id")
     *
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "id"://用户id
            "avatar"://用户头像
        }
    })
     */
    public function avatar()
    {
        (new House)->avatar();
    }

    /**
     * @ApiTitle    (社区公告-轮播图)
     * @ApiSummary  (社区公告-轮播图)
     * @ApiMethod   (POST)
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "id"://id
            "image"://图片
            "url"://跳转地址
            "typeswitch"://开关0关1开
            "createtime"://创建时间
        }
    })
     */
    public function banner()
    {
        $data = Db::name('advertising')->field('updatetime',true)->order('createtime desc')->select();
        foreach ($data as &$v){
            $v['image'] = cdnurl($v['image'],true);
            $v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
        }
        $this->success('success',$data);
    }


    /**
     * @ApiTitle    (社区公告-公告列表)
     * @ApiSummary  (社区公告-公告列表)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="house_id", type=inter, required=true, description="社区id")
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "total_num"://总条数
            "info":[
                "id"://公告id
                "title"://社区公告标题
                "content"://公告内容
                "look_num"://浏览人数
                "createtime"://时间
                "avatar"://用户头像
            ]
        }
    })
     */
    public function house_board_list()
    {
        $page = $this->request->param('page', 1, 'intval');
        $pageNum = $this->request->param('pageNum', 10, 'intval');
        $user_id = $this->auth->id;
        $house_id = $this->request->param('house_id');
        if(empty($house_id)){
            $this->error('社区id不能为空');
        }
        $data['total_num'] = Db::name('house_board')
            ->where('house_id',$house_id)
            ->count();
        $data['info'] = Db::name('house_board')
            ->where('house_id',$house_id)
            ->order('weigh desc')
            ->page($page,$pageNum)
            ->select();
        foreach ($data['info'] as &$v){
            $v['createtime'] = date('Y-m-d',$v['createtime']);
            //查询用户是否在已读公告详情中
            $house_board_detail = Db::name('house_board_detail')
                ->where('user_id',$user_id)
                ->where('house_board_id',$v['id'])
                ->find();
            if(empty($house_board_detail)){
                $info['user_id'] = $user_id;
                $info['house_board_id'] = $v['id'];
                $info['createtime'] = time();
                Db::name('house_board_detail')->insertGetId($info);
                Db::name('house_board')->where('id',$v['id'])->setInc('look_num');
            }

            $v['avatar'] = Db::name('house_board_detail')
                ->alias('a')
                ->join('user b','a.user_id = b.id')
                ->where('a.house_board_id',$v['id'])
                ->field('b.avatar,b.id')
                ->limit(6)
                ->select();
            foreach ($v['avatar'] as &$value) {
                $value['avatar'] = cdnurl($value['avatar'],true);
            }

        }
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (社区公告-服务电话)
     * @ApiSummary  (社区公告-服务电话)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="house_id", type="string", required=true, description="小区id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
              "id"://id
              "name"://电话名称
              "phone"://电话
        }
    })
     */
    public function house_phone()
    {
        $user_id = $this->auth->id;
        $house_id = $this->request->param('house_id');
        if(empty($house_id)){
            $this->error('缺少必要参数');
        }
        $data = Db::name('house_phone')->field('id,name,phone')->where('house_id',$house_id)->select();
        $this->success('success',$data);
    }


    /**
     * @ApiTitle    (社区公告-物业留言)
     * @ApiSummary  (社区公告-物业留言)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="house_id", type="string", required=true, description="小区id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "user_id"://用户id
            "avatar"://管理员头像
            "nickname"://昵称
            "is_have"://是否带有红标签 1有2没有
        }
    })
     */
    public function house_admin_list()
    {
        $user_id = $this->auth->id;
        $house_id = $this->request->param('house_id');
        if(empty($house_id)){
            $this->error('缺少必要参数');
        }

        $data = Db::name('house_admin')
            ->alias('a')
            ->join('user b','a.user_id = b.id')
            ->where('a.house_id',$house_id)
            ->field('a.user_id,b.avatar,b.nickname')
            ->order('a.is_direct')
            ->select();
        foreach ($data as &$v){
            $v['avatar'] = cdnurl($v['avatar'],true);
            $mes = Db::name('message')
                ->where('object_id',$house_id)
                ->where('object_user_id',$v['user_id'])
                ->where('user_id',$user_id)
                ->where('is_read_user',2)
                ->find();
            if(empty($mes)){
                $v['is_have'] = 2;
            }else{
                $v['is_have'] = 1;
            }
        }
        $this->success('success',$data);
    }

    /**
     * 留言提交
     * @ApiWeigh    (50)
     *
     * @ApiTitle    (留言提交)
     * @ApiSummary  (留言提交)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/house_board/comment_add)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="house_id", type="integer", required=true, description="社区id")
     * @ApiParams   (name="house_user_id", type="integer", required=true, description="管理员id")
     * @ApiParams   (name="content", type="string", required=true, description="留言内容")
     * @ApiParams   (name="image", type="string", required=true, description="图片")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function comment_add()
    {
        $param = $this->request->param();
        if(empty($param['house_id']) || empty($param['house_user_id'])){
            $this->error('缺少必要参数');
        }
        if(empty($param['content']) && empty($param['image'])) {
            $this->error('请填写内容或上传图片');
        }
        Db::startTrans();
        $result = false;
        try{
            $param['user_id'] = $this->auth->id;
            $model = new HouseComment();
            $result = $model->add($param);
            // 记录最新内容
            $msg_model = new Message();
            $where_m = [
                'user_id' => $this->auth->id,
                'object_id' => $param['house_id'],
                'object_user_id' => $param['house_user_id'],
            ];
            $msg = $msg_model->where($where_m)->find();
            if($msg) {
                $update = [
                    'id' => $msg['id'],
                    'type' => 1,
                    'content' => $param['content'],
                    'image' => $param['image'],
                    'is_read_user' => 1
                ];
                $res_msg = $msg_model->edit($update);
            } else {
                $insert = $where_m;
                $insert['type'] = 1;
                $insert['content'] = $param['content'];
                $insert['image'] = $param['image'];
                $insert['is_read_user'] = 2;
                $res_msg = $msg_model->add($insert);
            }
            // 发送模板消息
            $send_data = array(
                "first"  => '您有一条新的消息',
                'keyword1' => $param['content'] ? $param['content'] : '【图片消息】',
                "keyword2"  => date('Y-m-s H:i:s',time()),
                "remark" => '',
            );
            $openid = Db::name('third')->where('user_id',$param['house_user_id'])->value('openid');
            $url = config('option.vue_url') . '/liuyanxiaoxi?is_template=1&id='.$param['house_user_id'];
            $this->wxsendmessage($openid,$send_data,config('option.template')['msg'],$url);
            Db::commit();
        } catch (PDOException $e) {
            Db::rollback();
            $this->error($e->getMessage());
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if(!$result || !$res_msg) {
            $this->error('留言失败');
        }
        $this->success('留言成功');
    }

    /**
     * @ApiTitle    (社区公告-物业留言-详情)
     * @ApiSummary  (社区公告-物业留言-详情)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="house_id", type="inter", required=true, description="社区id")
     * @ApiParams   (name="user_id", type="inter", required=true, description="社区物业管理员用户id")
     * @ApiParams   (name="page", type="integer", required=true, description="页数")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "id"://id
            "user_id"://发送者id
            "house_id"://社区id
            "to_user_id"://接收者用户id
            "type"://消息类型1文字类型2图片类型
            "content"://文字消息
            "image"://图片消息
            "is_read"://是否已读1已读2未读
            "createtime"://创建时间
            "updatetime"://修改时间
            "nickname"://昵称
            "is_have"://是否带有红标签 1有2没有
            "is_left"://是否在右边  1是2否
        }
    })
     */
    public function chat_record()
    {
        $user_id = $this->auth->id;    //当前登录用户id
        $house_id = $this->request->param('house_id');      //社区id
        $wuye_user_id = $this->request->param('user_id');   //物业用户id
        $page = $this->request->param('page',1,'intval');
        if(empty($house_id) || empty($wuye_user_id)){
            $this->error('缺少必要参数');
        }

        $house_admin = Db::name('house_admin')->where('user_id',$wuye_user_id)->where('house_id',$house_id)->find();
        if(empty($house_admin)){
            $this->error('参数有误,社区与管理员id不匹配');
        }
        $where = [
            'where' => [
                'user_id'=>$this->auth->id,
                'object_id' => $house_id,
                'object_user_id' => $wuye_user_id
            ],
        ];
        // 修改用户阅读状态
        $msg_model = new Message();
        $msg = $msg_model->findOrFail($where);
        // 有消息则已读
        if($msg){
            $edit = [
                'id' => $msg['id'],
                'is_read_user' => 1,
            ];
            $msg_model->edit($edit);
        }
        // 获取记录列表
        $model = new HouseComment();
        $where_c = [
            'where' => [
                'user_id' => $this->auth->id,
                'house_id' => $house_id,
                'house_user_id' => $wuye_user_id,
            ],
        ];
        $order = ['createtime'=>'asc'];
        $comment = $model->pageSelect($page,$where_c,'*',$order,config('option.num'));
        $list = $comment->items();
        $user_model = new \app\api\model\User();
        foreach ($list as &$v) {
            if($v['type'] == 1) {
                $user = $user_model->field('id,nickname,avatar')->where('id',$v['user_id'])->find();
                $user['avatar'] = cdnurl($user['avatar']);
            } else {
                $user = [
                    'id' => $house_admin['user_id'],
                    'nickname' => $house_admin['nickname'],
                    'avatar' => cdnurl($house_admin['avatar'])
                ];
            }
            $v['user'] = $user;
        }
        $return = [
            'list' => $list,
            'this_page' => $comment->currentPage(),
            'total_page' => $comment->lastPage()
        ];
//        $data = Db::name('message')
//            ->where('house_id',$house_id)
//            ->where('user_id',$user_id)
//            ->where('to_user_id',$wuye_user_id)
//            ->order('createtime desc')
//            ->select();
//        foreach ($data as &$v){
//            $v['is_right'] = 1;
//            if($v['type'] == 2){
//                $v['image'] = cdnurl($v['image'],true);
//            }
//        }
//        $arr = Db::name('message')
//            ->where('house_id',$house_id)
//            ->where('user_id',$wuye_user_id)
//            ->where('to_user_id',$user_id)
//            ->order('createtime desc')
//            ->select();
//        foreach ($arr as &$v){
//            $v['is_right'] = 2;
//            if($v['type'] == 2){
//                $v['image'] = cdnurl($v['image'],true);
//            }
//        }
//
//        $info = array_merge($data,$arr);
//
//        array_multisort(array_column($info,'createtime'),SORT_DESC,$info);
//        foreach ($info as &$val){
//            $val['createtime'] = date('Y-m-d H:i:s',$val['createtime']);
//        }

        $this->success('success',$return);

    }

    /**
     * @ApiTitle    (社区公告-社区活动)
     * @ApiSummary  (社区公告-社区活动)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="house_id", type="inter", required=true, description="社区id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "success",
        "time": "1598587213",
        "data": [
            {
              "id": // 活动id
              "title": // 活动标题
              "spec_type": // 活动规格:0=无规格-免费,1=单规格,2=多规格
              "content": // 活动描述
              "activity_time": 活动日期
              "price": // 报名费用
              "status": { // 活动状态
                "text": // 状态名称
                "value": // 状态值 0=报名中,1=已报名,2=已到期
              }
            }
        ]
    })
     */
    public function activity()
    {
        $user_id = $this->auth->id;
        $house_id = $this->request->param('house_id');      //社区id
        empty($house_id) && $this->error('缺少必要参数');
        $list = $this->getActivityList("find_in_set({$house_id},house_ids)");
        $this->success('success',$list);
    }

    /**
     * @ApiTitle    (社区公告-社区活动-详情)
     * @ApiSummary  (社区公告-社区活动-详情)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="house_activity_id", type="inter", required=true, description="社区活动id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "success",
        "time": "1598587213",
        "data": {
            "id": // 活动id
            "title": // 活动标题
            "spec_type": // 活动规格:0=无规格-免费,1=单规格,2=多规格
            "content": // 活动描述
            "activity_time": 活动日期
            "price": // 报名费用
            "status": { // 活动状态
                "text": // 状态名称
                "value": // 状态值 0=报名中,1=已报名,2=已到期
            }
        }
    })
     */
    public function activityInfo()
    {
        $user_id = $this->auth->id;
        $house_activity_id = $this->request->param('house_activity_id');      //社区id
        empty($house_activity_id) && $this->error('缺少必要参数');
        $list = $this->getActivityList(['ha.id'=>$house_activity_id]);
        $info = !empty($list) ? $list[0] : [];
        $this->success('success',$info);
    }

    /**
     * 获取活动信息
     */
    private function getActivityList($where){
        $list = Db::name('house_activity')
            ->alias('ha')
            ->join('house_join hj','hj.house_activity_id = ha.id','left')
            ->where($where)
            ->field('
                ha.id,
                ha.title,
                ha.content,
                ha.start_time,
                ha.end_time,
                ha.spec_type,
                hj.join_status
            ')
            ->order("ha.createtime desc")
            ->select();
        foreach ($list as $k => &$v) {
            // 活动报名费用
            $price = Db::name('house_activity_spec')->where('house_activity_id',$v['id'])->value('price');
            $v['price'] = $price > 0 ? $price : 0;
            // 活动日期
            $v['activity_time'] = date('Y-m-d',$v['start_time']).'-'.date('Y-m-d',$v['end_time']);
            // 活动状态
            if($v['join_status'] == '1'){
                $status = ['text'=>'已报名','value'=>1];
            }else{
                $status = $v['end_time'] < time() ? ['text'=>'已到期','value'=>2] : ['text'=>'报名中','value'=>0];
            }
            $v['status'] = $status;
            unset($v['start_time'],$v['end_time'],$v['join_status']);
        }
        return $list;
    }

    /**
     * @ApiTitle    (社区公告-社区活动-获取规格)
     * @ApiSummary  (社区公告-社区活动-获取规格)
     * @ApiMethod   (POST)
     * @ApiParams   (name="house_activity_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": "1598600665",
            "data": {
                "spec_attr": [
                    {
                      "group_id": 25,
                      "group_name": "颜色",
                      "spec_items": [
                        {
                          "item_id": 52,
                          "spec_value": "白色"
                        },
                        {
                          "item_id": 54,
                          "spec_value": "黑色"
                        }
                      ]
                    }
                ],
                "spec_list": {
                    "52_50": {
                      "spec_sku_id": "52_50",
                      "price": "12.00"
                    }
                },
                "price": "12.00"
            }
        })
     */
    public function specData()
    {
        $house_activity_id = $this->request->param('house_activity_id');
        $activity = \app\admin\model\HouseActivity::get($house_activity_id);
        empty($activity) && $this->error('活动信息不存在');
        // 规格信息
        $spec_data = $activity['spec_type'] == '2' ? $activity->getManySpecData($activity['spec_rel'], $activity['spec']) : null;
        // 规格价格信息
        if($spec_data){
            $spec_list = [];
            foreach($spec_data['spec_list'] as $v){
                $spec_list[$v['spec_sku_id']]['spec_sku_id'] = $v['spec_sku_id'];
                $spec_list[$v['spec_sku_id']]['price'] = $v['form']['price'];
            }
            $spec_data['spec_list'] = $spec_list;
        }
        $price = Db::name('house_activity_spec')->where('house_activity_id',$activity['id'])->value('price');
        // 默认报名费
        $spec_data['price'] = $price > 0 ? $price : 0;
        $this->success(__('成功'),$spec_data);
    }

    /**
     * @ApiTitle    (社区公告-社区活动-报名)
     * @ApiSummary  (社区公告-社区活动-报名)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="house_activity_id", type="integer", required=true, description="活动id")
     * @ApiParams   (name="total_num", type="integer", required=true, description="数量")
     * @ApiParams   (name="spec_sku_id", type="integer", description="活动规格sku")
     * @ApiParams   (name="contact", type="string", required=true, description="姓名")
     * @ApiParams   (name="mobile", type="string", required=true, description="联系方式")
     * @ApiParams   (name="remark", type="string", description="备注")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    }
    })
     */
    public function join(){
        $house_activity_id = $this->request->param('house_activity_id');
        $total_num = $this->request->param('total_num');
        $spec_sku_id = $this->request->param('spec_sku_id','');
        $contact = $this->request->param('contact');
        $mobile = $this->request->param('mobile');
        $remark = $this->request->param('remark');
        // 验证
        empty($house_activity_id) && $this->error('缺少必要参数');
        empty($total_num) && $this->error('缺少必要参数');
        empty($contact) && $this->error('缺少必要参数');
        empty($mobile) && $this->error('缺少必要参数');
        $activity = \app\admin\model\HouseActivity::get($house_activity_id,['specRel']);
        empty($activity) && $this->error('活动信息不存在');
        if($activity['spec_type'] == '2' && empty($spec_sku_id)){
            $this->error('多规格活动,请传入spec_sku_id');
        }
        // 必要数据查询
        $price = 0; //报名费
        $total_price = 0; // 总报名费
        $house_activity_spec_id = 0; // 活动规格ID
        if($activity['spec_type'] > '0'){
            $house_activity_spec = Db::name('house_activity_spec')
                ->where('house_activity_id',$house_activity_id)
                ->where('spec_sku_id',$spec_sku_id)
                ->field('id,price')
                ->find();
            $price = $house_activity_spec['price'];
            $total_price = $price * $total_num;
            $house_activity_spec_id = $house_activity_spec['id'];
        }
        // 活动规格信息
        $activity_attr = '';
        if($activity['spec_type'] == '2'){
            $attrs = explode('_', $spec_sku_id);
            $spec_rel = array_column($activity['specRel'], null, 'id');
            foreach ($attrs as $specValueId) {
                $activity_attr .= $spec_rel[$specValueId]['spec']['spec_name'] . ':'
                    . $spec_rel[$specValueId]['spec_value'] . '; ';
            }
        }
        // 生成报名订单
        $house_join_id = Db::name('house_join')->insertGetId([
            'order_no' => date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8),
            'contact' => $contact,
            'mobile' => $mobile,
            'remark' => $remark,
            'total_price' => $total_price,
            'pay_price' => $total_price,
            'user_id' => $this->auth->id,
            'house_activity_id' => $house_activity_id,
            'price' => $price,
            'total_num' => $total_num,
            'spec_sku_id' => $spec_sku_id,
            'house_activity_spec_id' => $house_activity_spec_id,
            'activity_attr' => $activity_attr,
            'createtime' => time(),
            'title' => $activity['title'],
            'content' => $activity['content'],
            'spec_type' => $activity['spec_type'],
            'start_time' => $activity['start_time'],
            'end_time' => $activity['end_time']
        ]);
        if($total_price > 0){
            // 调起支付
            $this->init_wx_pay_for_gzh(true);
            $this->make_wx_pay('wechat',$house_join_id);
        }
        // 免费活动直接报名成功
        Db::name('house_join')->where('id',$house_join_id)->setField('join_status','1');
        $this->success(__('报名成功'));
    }

    private function init_wx_pay_for_gzh($Ischeck=false){
        //这里首先判断 此用户是否绑定了微信公众号
        if($Ischeck){
            $third = Third::where(['user_id' => $this->auth->id, 'platform' => 'wechat'])->find();
            if(!$third){
                //从这里自动绑定微信公众号的账户
                $this->error('您未绑定微信号',null,1008);
            }
        }

        $config = get_addon_config('litestore');

        $third_config = get_addon_config('third');
        $third_options = array_intersect_key($third_config, array_flip(['wechat']));
        $third_options = $third_options['wechat'];

        $options = [
            'debug'  => true,
            'log' => [
                'level' => 'debug',
                'file'  => '/tmp/easywechat.log',
            ],
            'app_id'   => $third_options['app_id'],
            'secret'   => $third_options['app_secret'],
            'payment' => [
                'merchant_id'        =>  $config['MCHIDGZH'],
                'key'                =>  $config['APIKEYGZH'],
                'notify_url'         =>  \think\Request::instance()->domain().'/index/notify/notifyHouseJoin',
            ],

        ];
        $this->wxapp = new WXPAY_APP($options);
    }

    private function make_wx_pay($platform,$house_join_id){
        // 报名是否存在
        $join = Db::name('house_join')->where('id',$house_join_id)->find();
        empty($join) && $this->error('报名信息不存在');
        $join['pay_status'] == '1' && $this->error('已支付,请勿重复操作');

        $third = Third::where(['user_id' => $this->auth->id, 'platform' => $platform])->find();
        $payment = $this->wxapp->payment;

        $attributes = [
            'trade_type'       => 'JSAPI',
            'body'             => $join['order_no'],
            'detail'           => 'OrderID:'.$join['id'],
            'out_trade_no'     => $join['order_no'],
            //'total_fee'        => $this->model['pay_price'] * 100, // 单位:分
            'total_fee'        => 1, // 单位:分
            'openid'           => $third['openid'],
        ];
        $order = new WXPAY_ORDER($attributes);

        $result = $payment->prepare($order);
        if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS'){
            $prepayId = $result->prepay_id;
            $config = $payment->configForJSSDKPayment($prepayId); // 返回数组
            return $this->success('预支付成功',$config);
        }
        return $this->error('微信支付调用失败',$result);
    }
}