HouseBoard.php 18.3 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
<?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;
 /**
 * 社区公告
 */
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": "成功",
        "time": "1571492001",
        "data": {
              "is_binding"://是否绑定1已绑定2未绑定
              "house"://社区信息
              "house_board"://最新公告信息
        }
    })
     */
    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('house_id',$data['house_id'])
                    ->where('to_user_id',$user_id)
                    ->where('is_read',2)
                    ->field('id')
                    ->find();
            $house['have_message'] = !empty($message) ? 1 : 2;
            $house['image'] = cdnurl($house['image'],true);
            /*最新公告信息*/
            $house_board = Db::name('house_board')
                ->where('house_id',$data['house_id'])
                ->order('createtime desc')
                ->field('id,title,content,look_num,createtime')
                ->find();
            if(!empty($house_board)){
                $house_board['createtime'] = date('Y-m-d',$house_board['createtime']);
                // 最新公告是否已读
                $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();
            }
            $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()
    {
        $qiniu = get_addon_config('qiniu')['cdnurl'];
        $data = Db::name('advertising')->field('updatetime',true)->order('createtime desc')->select();
        foreach ($data as &$v){
            $v['image'] = $qiniu.$v['image'];
            $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();
        }
        $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": {
            "id"://id
            "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.id,a.user_id,b.avatar,b.nickname')
            ->order('a.is_direct')
            ->select();
        foreach ($data as &$v){
            $mes = Db::name('message')
                ->where('house_id',$house_id)
                ->where('user_id',$v['user_id'])
                ->where('to_user_id',$user_id)
                ->where('is_read',2)
                ->find();
            if(empty($mes)){
                $v['is_have'] = 2;
            }else{
                $v['is_have'] = 1;
            }
        }
        $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="user_id", type="inter", required=true, description="社区物业管理员用户id")
     *
     * @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()
    {
        $qiniu = get_addon_config('qiniu')['cdnurl'];
        $user_id = $this->auth->id;    //当前登录用户id
        $house_id = $this->request->param('house_id');      //社区id
        $wuye_user_id = $this->request->param('user_id');   //物业用户id
        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不匹配');
        }

        $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'] = $qiniu.$v['image'];
            }
        }
        $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'] = $qiniu.$v['image'];
            }
        }

        $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',$info);

    }

    /**
     * @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.pay_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['pay_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['pay_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"
                    }
                }
            }
        })
     */
    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;
        }
        $this->success(__('成功'),$spec_data);
    }
}