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

namespace app\api\controller;


use app\common\controller\Api;
use think\Cache;
use think\Db;
use think\Validate;
use app\api\model\House;
use app\api\model\HouseAdmin as HouseAdminModel;
use app\api\model\HouseBoard;
use app\api\model\UserHouse;
use app\api\model\HousePhone;
use app\api\model\Message;
use app\api\model\StoreComment;
use app\api\model\HouseComment;

 /**
 * 社区管理员
 */
class HouseAdmin extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**
     * @ApiTitle    (社区管理员-首页)
     * @ApiSummary  (社区管理员-首页)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="house_id", type="inter", required=false, description="社区ID")
     * @ApiReturn({
        "code": 1,
        "msg": "success",
        "time": "1598707632",
        "data": {
            "id": 4, //管理员ID
            "house_id": 17, //小区ID
            "is_direct": 1, //是否为主管理员 1是2否
            "nickname": "", //管理员昵称
            "avatar": "", //管理员头像
            "house": { // 小区信息
                "id": 17, //小区ID
                "name": "碧海花园小区" //小区名称
            }
        }
    })
     */
    public function index()
    {
        $user_id = $this->auth->id;
        $house_id = $this->request->param('house_id');
        if($house_id){
            $info = HouseAdminModel::get(['user_id'=>$user_id,'house_id'=>$house_id],['house']);
            empty($info) && $this->error('您不是该小区的管理员');
            $info->visible(['id','house_id','is_direct','nickname','avatar','house'])->toArray();
        }else{
            $info = HouseAdminModel::with(['house'])
            	->where(['user_id'=>$user_id])
                ->order(['is_direct'=>'asc','createtime'=>'asc'])
                ->field('id,house_id,is_direct,nickname,avatar')
                ->find();
            empty($info) && $this->error('您还不是管理员');
        }
        $info['avatar'] = cdnurl($info['avatar'],true);
        $info->getRelation('house')->visible(['id','name']);
        $this->success('success',$info);
    }

    /**
     * @ApiTitle    (社区管理员-选择社区)
     * @ApiSummary  (社区管理员-选择社区)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/house_admin/houseList)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="keyword", type="string", required=false, description="关键字")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "success",
        "time": "1598688508",
        "data": [{
            "id": 16, //小区ID
            "name": "兴安苑", //小区名称
        }]
    })
     */
    public function houseList()
    {
        $user = $this->auth->getUser();
        $keyword = $this->request->param('keyword');
        $where = [];
        if(!empty($keyword)){
            $where['h.name'] = ['like',"%$keyword%"];
        }
        // 查出该管理员已绑定的小区
        $list = HouseAdminModel::alias('ha')
            ->join('house h','h.id = ha.house_id')
            ->where('ha.user_id',$user['id'])
            ->where($where)
            ->field("h.id,h.name")
            ->order('ha.is_direct')
            ->select();
        $this->success('success',$list);
    }

    /**
     * @ApiTitle    (社区管理员-个人信息)
     * @ApiSummary  (社区管理员-个人信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/house_admin/profile)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="house_id", type="inter", required=false, description="社区ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "success",
        "time": "1598753244",
        "data": {
            "nickname": "", //昵称
            "avatar": "http://community.qiniu.brotop.cn", //头像
            "mobile": "" //手机号
        }
    })
     */
    public function profile()
    {
        $house_id = $this->request->param('house_id');
        empty($house_id) && $this->error('缺少必要参数');
        $info = HouseAdminModel::where(['user_id'=>$this->auth->id,'house_id'=>$house_id])->field('id,nickname,avatar')
            ->find();
        $info['avatar'] = cdnurl($info['avatar'],true);
        $this->success('success',$info);
    }

    /**
     * 社区管理员-个人信息编辑
     * @param string $house_id 社区ID
     * @param string $avatar   头像地址
     * @param string $nickname 昵称
     */
    public function profileEdit()
    {
        $house_id = $this->request->param('house_id');
        $nickname = $this->request->param('nickname');
        $avatar = $this->request->param('avatar', '', 'trim,strip_tags,htmlspecialchars');

        empty($house_id) && $this->error('缺少必要参数');
        $admin = HouseAdminModel::get(['user_id'=>$this->auth->id,'house_id'=>$house_id]);
        empty($admin) && $this->error('您不是该小区的管理员');

        $admin->nickname = $nickname;
        $admin->avatar = $avatar;
        $admin->save();
        $this->success();
    }

    /**
     * 社区管理员-修改手机号
     * @param string $house_id 社区ID
     * @param string $mobile   手机号
     * @param string $captcha 验证码
     */
    public function changemobile()
    {
        $house_id = $this->request->param('house_id');
        $mobile = $this->request->param('mobile');
        $captcha = $this->request->param('captcha');
        empty($house_id) && $this->error('缺少必要参数');
        empty($mobile) && $this->error('请输入手机号');
        empty($captcha) && $this->error('请输入验证码');
        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error('手机号格式不正确');
        }
        $admin = HouseAdminModel::get(['user_id'=>$this->auth->id,'house_id'=>$house_id]);
        empty($admin) && $this->error('您不是该小区的管理员');
        // 检查手机号是否存在
        $mobile_exists = HouseAdminModel::where('mobile',$mobile)
            ->where('house_id',$house_id)
            ->where('id', '<>', $admin->id)
            ->find();
        if ($mobile_exists) {
            $this->error('手机号已被占用');
        }
        $code = Cache::get($mobile);
        if ($code != $captcha) {
            $this->error('验证码不正确');
        }
        $admin->mobile = $mobile;
        $admin->save();

        Cache::rm($mobile); 
        $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="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "success",
        "time": "1598755666",
        "data": {
            "total_num": 1, //数据总条数
            "info": [{
                "id": 1, //公告id
                "title": "周六交电费", //社区公告标题
                "content": "", //社区公告内容
                "look_num": 2, //浏览人数
                "createtime": "2020-08-19", //时间
                "images": "", //社区公告图片
                "images_arr": [ //社区公告图片数组
                    "http://community.qiniu.brotop.cn"
                ],
                "nickname": "", //管理员昵称
                "avatar": "http://community.qiniu.brotop.cn", //管理员头像
                "is_delete": 2, //是否下架:1=下架,2=未下架
                "avatar_list": [{ //用户头像
                    "avatar": "",
                    "id": 1
                }]
            }]
        }
    })
     */
    public function house_board_list()
    {
        $page = $this->request->param('page', 1, 'intval');
        $pageNum = $this->request->param('pageNum', 10, 'intval');
        $house_id = $this->request->param('house_id');
        empty($house_id) && $this->error('社区id不能为空');
        $data['total_num'] = Db::name('house_board')
            ->where('house_id',$house_id)
            ->count();
        $data['info'] = $this->getBoardList(['hb.house_id'=>$house_id],$page,$pageNum);
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (社区管理员-公告管理-详情)
     * @ApiSummary  (社区管理员-公告管理-详情)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="house_board_id", type="inter", required=true, description="公告id")
     *
     *
     * @ApiReturn({
        "code": 1,
        "msg": "success",
        "time": "1598755666",
        "data": {
            "id": 1, //公告id
            "title": "周六交电费", //社区公告标题
            "content": "", //社区公告内容
            "look_num": 2, //浏览人数
            "createtime": "2020-08-19", //时间
            "images": "", //社区公告图片
            "images_arr": [ //社区公告图片数组
                "http://community.qiniu.brotop.cn"
            ],
            "nickname": "", //管理员昵称
            "avatar": "http://community.qiniu.brotop.cn", //管理员头像
            "is_delete": 2, //是否下架:1=下架,2=未下架
            "avatar_list": [{ //用户头像
                "avatar": "",
                "id": 1
            }]
        }
    })
     */
    public function house_board_detail()
    {
        $house_board_id = $this->request->param('house_board_id');
        empty($house_board_id) && $this->error('缺少必要参数');
        $data = $this->getBoardList(['hb.id'=>$house_board_id],1,1);
        empty($data) && $this->error('公告信息不存在');
        $this->success('success',$data[0]);
    }

    /**
     * 获取公告数据
     */
    private function getBoardList($where,$page,$pageNum){
        $list = HouseBoard::where($where)
            ->alias('hb')
            ->join('house_admin ha','ha.id = hb.admin_user_id')
            ->field('
                hb.id,
                hb.title,
                hb.content,
                hb.look_num,
                hb.createtime,
                hb.deletetime,
                hb.images,
                ha.nickname,
                ha.avatar
            ')
            ->order('weigh desc')
            ->page($page,$pageNum)
            ->select();
        foreach ($list as &$v){
            $v['avatar'] = cdnurl($v['avatar'],true);
            $v['createtime'] = date('Y-m-d',$v['createtime']);
            // 公告图片
            $images_arr = [];
            if(!empty($v['images'])){
                foreach (explode(',', $v['images']) as $key => $value) {
                    $images_arr[] = cdnurl($value,true);
                }
            }
            $v['images_arr'] = $images_arr;
            $v['is_delete'] = $v['deletetime'] > 0 ? 1 : 2;
            $v['avatar_list'] = 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();
            unset($v['deletetime']);
        }
        return $list;
    }

    /**
     * @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": "1598771268",
        "data": {
            "times": 2 //本周剩余发布次数
        }
    })
     */
    public function house_board_times()
    {
        $house_id = $this->request->param('house_id');
        empty($house_id) && $this->error('缺少必要参数');
        $count = HouseBoard::whereTime('createtime', 'week')->count();
        $times = $count >= 2 ? 0 : 2-$count;
        $this->success('success',compact('times'));
    }

    /**
     * @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="title", type="string", required=true, description="公告标题")
     * @ApiParams   (name="content", type="string", required=true, description="公告内容")
     * @ApiParams   (name="images", type="string", required=false, description="公告图片")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "success",
        "time": "1598755666"
    })
     */
    public function house_board_add()
    {
        $post = $this->request->param();
        empty($post['house_id']) && $this->error('缺少必要参数');
        empty($post['title']) && $this->error('请填写公告标题');
        empty($post['content']) && $this->error('请填写公告内容');
        $admin = HouseAdminModel::get(['user_id'=>$this->auth->id,'house_id'=>$post['house_id']]);
        empty($admin) && $this->error('您不是该小区的管理员');
        (new HouseBoard)->allowField(true)->save(array_merge([
            'admin_user_id' => $this->auth->id
        ],$post));
        $this->success('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="house_board_id", type="inter", required=true, description="社区公告ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "success",
        "time": "1598755666"
    })
     */
    public function house_board_delete()
    {
        $house_id = $this->request->param('house_id');
        $house_board_id = $this->request->param('house_board_id');
        if(!$house_id || !$house_board_id){
            $this->error('缺少必要参数');
        }
        $admin = HouseAdminModel::get(['user_id'=>$this->auth->id,'house_id'=>$house_id]);
        empty($admin) && $this->error('您不是该小区的管理员');
        $info = HouseBoard::get(['house_id'=>$house_id,'id'=>$house_board_id]);
        empty($info) && $this->error('公告信息不存在');

        $info->deletetime = time();
        $info->save();
        $this->success('success');
    }

    /**
     * @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": "1598755666"
    })
     */
    public function adminList()
    {
        $house_id = $this->request->param('house_id');
        empty($house_id) && $this->error('缺少必要参数');
        $list = HouseAdminModel::where('house_id',$house_id)
            ->field('id,nickname,user_id,mobile')
            ->select();
        $this->success('success',$list);
    }

    /**
     * @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="nickname", type="string", required=true, description="姓名")
     * @ApiParams   (name="user_id", type="inter", required=true, description="用户ID")
     * @ApiParams   (name="mobile", type="string", required=true, description="手机号")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "success",
        "time": "1598755666"
    })
     */
    public function adminAdd()
    {
        $post = $this->request->param();
        empty($post['house_id']) && $this->error('缺少必要参数');
        empty($post['nickname']) && $this->error('请填写姓名');
        empty($post['user_id']) && $this->error('请填写用户ID');
        empty($post['mobile']) && $this->error('请填写手机号');
        if (!Validate::regex($post['mobile'], "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        $user = $this->auth->getUser();
        $admin = HouseAdminModel::get(['user_id'=>$user['id'],'house_id'=>$post['house_id'],'is_direct'=>1]);
        empty($admin) && $this->error('您不是该小区的主管理员');
        (new HouseAdminModel)->allowField(true)->save(array_merge([
            'avatar' => $user['avatar'],
            'is_direct' => 2
        ],$post));
        $this->success('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="house_admin_id", type="inter", required=true, description="社区管理员ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "success",
        "time": "1598755666"
    })
     */
    public function adminDelete()
    {
        $house_id = $this->request->param('house_id');
        $house_admin_id = $this->request->param('house_admin_id');
        if(!$house_id || !$house_admin_id){
            $this->error('缺少必要参数');
        }
        $admin = HouseAdminModel::get(['user_id'=>$this->auth->id,'house_id'=>$house_id,'is_direct'=>1]);
        empty($admin) && $this->error('您不是该小区的主管理员');
        $info = HouseAdminModel::get(['id'=>$house_admin_id,'is_direct'=>2]);
        empty($info) && $this->error('子管理员不存在');

        $info->delete();
        $this->success('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="keyword", type="string", required=false, description="搜索内容")
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "success",
        "time": "1598838987",
        "data": {
            "total_num": 1, //总条数
            "list": [{
                "id": 1, //业主ID
                "avatar": "http://community.qiniu.brotop.cn", //业主头像
                "name": "liujianch", //业主名称
                "remark": "" //业主备注
            }]
        }
    })
     */
    public function userList()
    {
        $page = $this->request->param('page', 1, 'intval');
        $pageNum = $this->request->param('pageNum', 10, 'intval');
        $house_id = $this->request->param('house_id');
        $keyword = $this->request->param('keyword');
        empty($house_id) && $this->error('缺少必要参数');
        $where['uh.house_id'] = $house_id;
        $where['uh.status'] = 2;
        if(!empty($keyword)){
            $where['uh.name|uh.remark'] = ['like','%'.$keyword.'%'];
        }
        $list = UserHouse::alias('uh')
            ->join('user u','u.id = uh.user_id')
            ->where($where)
            ->field('uh.id,u.avatar,uh.name,uh.remark')
            ->order('uh.createtime desc')
            ->page($page,$pageNum)
            ->select();
        foreach($list as &$v){
            $v['avatar'] = cdnurl($v['avatar']);
        }
        $this->success('success',['total_num'=>count($list),'list'=>$list]);
    }

    /**
     * @ApiTitle    (社区管理员-业主列表-添加备注)
     * @ApiSummary  (社区管理员-业主列表-添加备注)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="user_house_id", type="inter", required=true, description="社区业主ID")
     * @ApiParams   (name="remark", type="string", required=true, description="备注")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "success",
        "time": "1598755666"
    })
     */
    public function remarkAdd()
    {
        $user_house_id = $this->request->param('user_house_id');
        $remark = $this->request->param('remark');
        empty($user_house_id) && $this->error('缺少必要参数');
        empty($remark) && $this->error('请填写备注');
        $info = UserHouse::get($user_house_id);
        empty($info) && $this->error('业主信息不存在');

        $info->remark = $remark;
        $info->save();
        $this->success('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="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
     *
     * @ApiReturn({
        'code': '1',
        'msg': '返回成功',
        "data": {
            "list": [{
                    "id": 3,
                    "user_id": 1,
                    "object_id": 1,
                    "object_user_id": 1,
                    "type": 消息类型1 = 平台2 = 商家3 = 社区,
                    "content": "文字内容",
                    "image": "图片",
                    "is_read_user": 是否已读1 = 是2 = 否,
                    "name": "平台/店铺/社区名称"
                    "createtime": "2020-08-30 10:43:55",
                },
                {
                    "id": 1,
                    "user_id": 1,
                    "object_id": 1,
                    "object_user_id": 1,
                    "type": 1,
                    "content": "123",
                    "image": "",
                    "is_read_user": 1,
                    "name": "平台消息"
                    "createtime": "2020-08-30 09:36:45",
                }
            ],
            "total_num": 总条数,
        }
    })
     */
    public function messageList()
    {
        $page = $this->request->param('page', 1, 'intval');
        $pageNum = $this->request->param('pageNum', 10, 'intval');
        $house_id = $this->request->param('house_id');
        empty($house_id) && $this->error('缺少必要参数');
        $list = Message::where('object_id',$house_id)
            ->where('object_user_id',$this->auth->id)
            ->page($page,$pageNum)
            ->order('createtime desc')
            ->select();
        foreach ($list as &$v){
            $v['name'] = '平台消息';
            if($v['type'] == 2) {
                $v['name'] = Db::name('store')->where('id',$v['object_id'])->value('store_name');
            }
            if($v['type'] == 3) {
                $v['name'] = Db::name('user')->where('id',$v['user_id'])->value('nickname');
            }
        }
        $this->success('success',['total_num'=>count($list),'list'=>$list]);
    }

    /**
     * @ApiTitle    (社区管理员-留言消息-详情)
     * @ApiSummary  (社区管理员-留言消息-详情)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="message_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": "success",
        "time": "1598839065",
        "data": {
            "total_num": 1, //总条数
            "list": [{
                "id": 1,
                "user_id": 1,
                "house_id": 16,
                "house_user_id": 2,
                "type": 1, //发送人1 = 用户2 = 社区 / 商家,
                "content": "你好", //文字内容
                "image": "", //图片
                "createtime": "1970-01-01 08:00:00",
                "user": {
                    "id": 1,
                    "nickname": "StTazBfCvMrqXc4UHJYn",
                    "avatar": "http://community.qiniu.brotop.cn/assets/img/avatar.png"
                }
            }]
        }
    })
     */
    public function messageDetail()
    {
        $page = $this->request->param('page', 1, 'intval');
        $pageNum = $this->request->param('pageNum', 10, 'intval');
        $message_id = $this->request->param('message_id');

        empty($message_id) && $this->error('缺少必要参数');
        $msg = Message::get($message_id);
        empty($msg) && $this->error('消息不存在');

        $msg->is_read_object = 1;
        $msg->save();

        $list = HouseComment::where([
                'user_id' => $msg['user_id'],
                'house_id' => $msg['object_id'],
                'house_user_id' => $msg['object_user_id'],
            ])->order(['createtime'=>'desc'])
            ->page($page,$pageNum)
            ->select();
        if(!empty($list)){
            $list = array_multisort(array_column($list,'createtime'),SORT_ASC,$list);
        }
        // 管理员头像
        $user = HouseAdminModel::where(['user_id'=>$this->auth->id,'house_id'=>$msg['object_id']])->field('id,nickname,avatar')->find();
        $user['avatar'] = cdnurl($user['avatar'],true);
        // 业主头像
        $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']);
            }
            $v['user'] = $user;
        }
        $this->success('success',['total_num'=>count($list),'list'=>$list]);
    }

    /**
     * @ApiTitle    (社区管理员-留言消息-回复)
     * @ApiSummary  (社区管理员-留言消息-回复)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="message_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 messageReply()
    {
        $message_id = $this->request->param('message_id');
        $content = $this->request->param('content');
        $image = $this->request->param('image');
        empty($message_id) && $this->error('缺少必要参数');
        if(empty($content) && empty($image)) {
            $this->error('请填写内容或上传图片');
        }
        $msg = Message::get($message_id);
        empty($msg) && $this->error('留言消息不存在');
        Db::startTrans();
        try{
            HouseComment::create([
                'user_id' => $msg['user_id'],
                'house_id' => $msg['object_id'],
                'house_user_id' => $msg['object_user_id'],
                'type' => 2,
                'content' => $content,
                'image' => $image,
            ]);
            // 记录最新内容
            $msg = Message::get([
                'user_id' => $msg['user_id'],
                'object_id' => $msg['object_id'],
                'object_user_id' => $msg['object_user_id'],
            ]);
            $msg->content = $content;
            $msg->image = $image;
            $msg->is_read_user = 2;
            $msg->is_read_object = 1;
            $msg->save();
            // 发送模板消息
//            $send_data = array(
//                "first"  => '',
//                'keyword1' => '',
//                "keyword2"   => '',
//                "keyword3"  => date('Y-m-s',time()),
//                "remark" => '',
//            );
//            $this->wxsendmessage('','','');
            Db::commit();
        } catch (PDOException $e) {
            Db::rollback();
            $this->error($e->getMessage());
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $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")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "success",
        "time": "1598755666"
    })
     */
    public function phoneList()
    {
        $house_id = $this->request->param('house_id');
        empty($house_id) && $this->error('缺少必要参数');
        $list = HousePhone::where('house_id',$house_id)
            ->field('id,name,phone,weigh')
            ->order('weigh asc')
            ->select();
        $this->success('success',$list);
    }

    /**
     * @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="name", type="string", required=true, description="标题")
     * @ApiParams   (name="phone", type="string", required=true, description="手机号")
     * @ApiParams   (name="weigh", type="inter", required=true, description="排序号")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "success",
        "time": "1598755666"
    })
     */
    public function phoneAdd()
    {
        $post = $this->request->param();
        empty($post['house_id']) && $this->error('缺少必要参数');
        empty($post['name']) && $this->error('请填写标题');
        empty($post['phone']) && $this->error('请填写手机号');
        empty($post['weigh']) && $this->error('请填写排序号');
        if (!Validate::regex($post['phone'], "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        $admin = HouseAdminModel::get(['user_id'=>$this->auth->id,'house_id'=>$post['house_id'],'is_direct'=>1]);
        empty($admin) && $this->error('您不是该小区的主管理员');

        (new HousePhone)->allowField(true)->save($post);
        $this->success('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="house_phone_id", type="inter", required=true, description="社区电话ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "success",
        "time": "1598755666"
    })
     */
    public function phoneDelete()
    {
        $house_id = $this->request->param('house_id');
        $house_phone_id = $this->request->param('house_phone_id');
        if(!$house_id || !$house_phone_id){
            $this->error('缺少必要参数');
        }
        $admin = HouseAdminModel::get(['user_id'=>$this->auth->id,'house_id'=>$house_id,'is_direct'=>1]);
        empty($admin) && $this->error('您不是该小区的主管理员');
        $info = HousePhone::get($house_phone_id);
        empty($info) && $this->error('电话信息不存在');

        $info->delete();
        $this->success('success');
    }
}