HouseAdmin.php 8.0 KB
<?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;

 /**
 * 社区管理员
 */
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 = $this->auth->getUser();
        $house_id = $this->request->param('house_id');
        if($house_id){
            $info = HouseAdminModel::get(['user_id'=>$user['id'],'house_id'=>$house_id]);
            empty($info) && $this->error('您不是该小区的管理员');
            $info->visible(['id','house_id','is_direct'])->toArray();
        }else{
            $info = HouseAdminModel::where(['user_id'=>$user['id']])
                ->order(['is_direct'=>'asc','createtime'=>'asc'])
                ->field('id,house_id,is_direct')
                ->find();
            empty($info) && $this->error('您还不是管理员');
        }
        $data = [
            'nickname' => $user['admin_nickname'],
            'avatar' => cdnurl($user['admin_avatar'],true),
            'house' => House::where('id',$info['house_id'])->field('id,name')->find(),
        ];
        $data = array_merge($info->toArray(),$data);
        $this->success('success',$data);
    }

    /**
     * @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")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "success",
        "time": "1598753244",
        "data": {
            "nickname": "", //昵称
            "avatar": "http://community.qiniu.brotop.cn", //头像
            "mobile": "" //手机号
        }
    })
     */
    public function profile()
    {
        $user = $this->auth->getUser();
        $data = [
            'nickname' => $user['admin_nickname'],
            'avatar' => cdnurl($user['admin_avatar'],true),
            'mobile' => $user['admin_mobile'],
        ];
        $this->success('success',$data);
    }

    /**
     * 社区管理员-修改会员个人信息
     *
     * @param string $avatar   头像地址
     * @param string $nickname 昵称
     */
    public function profileEdit()
    {
        $user = $this->auth->getUser();
        $nickname = $this->request->request('nickname');
        $avatar = $this->request->request('avatar', '', 'trim,strip_tags,htmlspecialchars');
        $user->admin_nickname = $nickname;
        $user->admin_avatar = $avatar;
        $user->save();
        $this->success();
    }

    /**
     * 社区管理员-修改手机号
     *
     * @param string $mobile   手机号
     * @param string $captcha 验证码
     */
    public function changemobile()
    {
        $user = $this->auth->getUser();
        $mobile = $this->request->request('mobile');
        $captcha = $this->request->request('captcha');
        if (!$mobile || !$captcha) {
            $this->error(__('Invalid parameters'));
        }
        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        if (\app\common\model\User::where('admin_mobile', $mobile)->where('id', '<>', $user->id)->find()) {
            $this->error(__('Mobile already exists'));
        }
        $code = Cache::get($mobile);
        if ($code != $captcha) {
            $this->error(__('Captcha is incorrect'));
        }
        $user->admin_mobile = $mobile;
        $user->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": "周六交电费", //社区公告标题
                "look_num": 2, //浏览人数
                "createtime": "2020-08-19", //时间
                "is_delete": 2, //是否下架:1=下架,2=未下架
                "avatar": [{ //用户头像
                    "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'] = Db::name('house_board')
            ->where('house_id',$house_id)
            ->field('id,title,look_num,createtime,deletetime')
            ->order('weigh desc')
            ->page($page,$pageNum)
            ->select();
        foreach ($data['info'] as &$v){
            $v['createtime'] = date('Y-m-d',$v['createtime']);
            $v['is_delete'] = $v['deletetime'] > 0 ? 1 : 2;
            $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();
            unset($v['deletetime']);
        }
        $this->success('success',$data);
    }
}