model = new \app\admin\model\zy\Club; parent::__construct(); } public function index() { $params = $this->request->param(); $sub = Game::alias('g') ->join([Participant::$tableName => 'p'], 'p.game_id=g.id', 'LEFT') ->field('g.id,g.pid,g.club_id,g.act_id,g.date,g.name,g.start_time,g.end_time,g.cost,JSON_ARRAYAGG(p.avatar) as avatar,count(p.id) as join_num') ->where('g.date', '>=', date('Y-m-d')) ->order('g.date', 'asc')->group('g.id')->buildSql(); $query = $this->model->alias('c') ->join([Stadium::$tableName => 's'], 's.id = c.gym_id', 'LEFT') ->join([$sub => 'g'], 'g.club_id = c.id', 'LEFT') ->field("c.*, s.position,s.name as gym_name,JSON_ARRAYAGG(JSON_OBJECT( 'id', g.id, 'pid', g.pid, 'act_id', g.act_id, 'date', g.date, 'name', g.name, 'start_time', g.start_time, 'end_time', g.end_time, 'join_num', g.join_num, 'avatar', g.avatar, 'cost', g.cost )) AS games")->group('c.id'); if (isset($params['name'])) { $query->where('c.name', 'like', '%' . $params['name'] . '%'); } if (isset($params['tag'])) { $query->where('c.tags', 'like', '%' . $params['tag'] . '%'); } if (isset($params['gym_id'])) { $query->where('c.gym_id', $params['gym_id']); } if (isset($params['order'])) { $query->order($params['order'], $params['sort'] ?? NULL); } $res = $query->paginate($params['pageSize'] ?? 10); $list = $res->items(); foreach ($list as &$l) { $games = json_decode($l['games'], true); if (count($games) == 1 && empty($games[0]['id'])) $games = []; foreach ($games as &$g) { $g['cost'] = json_decode($g['cost'], true); } $l['games'] = $games; } $this->success('Success', ['list' => $list, 'count' => $res->total()]); } public function view() { $model = $this->model->get($this->request->param('id'))->toarray(); if (empty($model)) { $this->error(__('No rows were found')); } $menber = Menber::alias('m') ->join([User::$tableName => 'u'], 'u.id = m.user_id') ->where('club_id', $model['id'])->field('gender,count(*) as num')->group('u.gender') ->column('count(*) as num', 'gender'); $model['gender0'] = $menber[0] ?? 0; $model['gender1'] = $menber[1] ?? 0; $this->model->where('id', $model['id'])->setInc('attention'); $user = auth_user(); $visitor = Visitor::where('type', 0)->where('obj_id', $model['id'])->where('user_id', $user['id'])->find(); if (empty($visitor)) { $visitor = new Visitor; } $visitor->allowField(true)->save([ 'type' => 0, 'obj_id' => $model['id'], 'user_id' => $user['id'], 'nickname' => $user['nickname'], 'avatar' => $user['avatar'], 'gender' => $user['gender'], 'times' => empty($visitor->times) ? 1 : $visitor->times + 1 ]); $this->success('Success', $model); } public function add() { $result = false; $params = $this->request->param(); Db::startTrans(); try { $params['president'] = $this->auth->id; $result = $this->model->allowField(true)->save($params); $menber = new Menber; $menber->allowField(true)->save([ 'club_id' => $this->model->id, 'user_id' => $this->auth->id, 'role' => 3 ]); Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result === false) { $this->error('操作失败'); } $this->success('Success', $result); } // 获取俱乐部成员 public function menber() { $params = $this->request->param(); $sub = Relation::field('user_id,target_id,content')->where('user_id', $this->auth->id)->buildSql(); $query = Db::table(Menber::$tableName)->alias('m') ->join([User::$tableName => 'u'], 'u.id = m.user_id', 'LEFT') ->join([$sub => 'r'], 'r.target_id = m.user_id', 'LEFT') ->field('m.*,u.avatar,u.gender,u.nickname,u.wechat,u.qq,u.mobile,r.content'); if (empty($params['id'])) { return $this->error('参数错误'); } $query->where('club_id', $params['id']); if (isset($params['role'])) { $query->where('role', 'IN', explode(',', $params['role'])); } else { $query->where('role', '>', 0); } if (isset($params['nickname'])) { $query->where('nickname', 'LIKE', '%' . $params['nickname'] . '%'); } if (isset($params['order'])) { $query->order($params['order'], $params['sort'] ?? NULL); } $res = $query->paginate($params['pageSize'] ?? 10); $list = $res->items(); foreach ($list as &$l) { $l['content'] = json_decode($l['content'] ?? '[]', true); } $this->success('Success', ['list' => $list, 'count' => $res->total()]); } // 获取俱乐部活动 public function activity() { $params = $this->request->param(); $query = Db::table(Activity::$tableName) ->where('pid', 0) //主活动 ->field(''); if (isset($params['club_id'])) { $query->where('club_id', $params['club_id']); } if (isset($params['week'])) { $query->where('week', $params['week']); } $res = $query->select(); $this->success('Success', $res); } // 申请加入俱乐部 public function apply() { $params = $this->request->param(); Db::startTrans(); try { $club = $this->model->get($params['club_id']); if (empty($club)) { return $this->error('俱乐部不存在'); } if ($club['join_type'] > 1) { $this->error('该俱乐部不允许申请'); } $user = Menber::get(['club_id' => $club->id, 'user_id' => $this->auth->id]); if (empty($user)) { $user = new Menber; } else { if ($user['role'] > 0) { $this->error('您已经是俱乐部成员,无需重复申请'); } else if ($user['role'] == -1) { $this->error('您已被管理员拉黑'); } } if ($club['join_type'] == 0) { $user->allowField(true)->save([ 'club_id' => $club->id, 'user_id' => $this->auth->id, 'role' => 1 ]); } else { if (Apply::get(['type' => 1, 'user_id' => $this->auth->id, 'target_id' => $club->id, 'status' => 1])) { return $this->error('申请审核中'); } $apply = new Apply; $apply->allowField(true)->save([ // 记录申请 'type' => 1, 'user_id' => $this->auth->id, 'target_id' => $club->id, 'content' => json_encode(['reason' => $params['reason'] ?? '']), 'reason' => $params['reason'], 'status' => 1 ]); // (new Message())->allowField(true)->save([ // 消息通知 // 'type' => 3, // 'name' => $club->name, // 'avatar' => $club->logo, // 'from_id' => $club->id, // 'content' => json_encode([ // 'topic' => '俱乐部加入申请', // '俱乐部名称' => $club->name, // '申请人' => $this->user->nickname, // '申请时间' => date('Y-m-d H:i:s'), // 'reason' => $params['reason'] ?? '', // 'apply_id' => $apply->id // ]) // ]); } Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success('Success'); } //join_type 0:'开放加入,无需审核',1:'开放申请,审核加入',2:'会员邀请,无需审核',3:'会员邀请,审核加入',4:'仅管理员邀请,无需审核' // 邀请加入俱乐部 public function invite() { $params = $this->request->param(); Db::startTrans(); try { $club = $this->model->get($params['club_id']); if (empty($club)) { return $this->error('俱乐部不存在'); } $user = User::get($params['user_id']); if (empty($user)) { return $this->error('用户不存在'); } $menber = Menber::where('club_id', $params['club_id']) ->where('user_id', $this->auth->id) ->where('role', '>', 0)->find(); if (empty($menber)) { $this->error('您无权邀请加入俱乐部'); } $invate = Menber::get(['user_id' => $params['user_id'], 'club_id' => $params['club_id']]); if (empty($invate)) { $invate = new Menber; } else { if ($invate['role'] > 0) { $this->error('用户已在俱乐部'); } else if ($invate['role'] == -1) { $this->error('用户已被管理员拉黑'); } } if ($menber['role'] > 1 || $club['join_type'] == 0 || $club['join_type'] == 2) { // 管理员或者开放加入 $invate->allowField(true)->save([ 'club_id' => $club->id, 'user_id' => $user->id, 'role' => 1 ]); } else { if (Apply::get(['type' => 1, 'user_id' => $this->auth->id, 'target_id' => $user->id, 'status' => 1])) { return $this->error('申请审核中'); } $apply = new Apply; $apply->allowField(true)->save([ // 记录申请 'type' => 1, 'user_id' => $user->id, 'target_id' => $club->id, 'content' => json_encode(['reason' => $params['reason'] ?? '']), 'reason' => $params['reason'], 'status' => 1 ]); // (new Message())->allowField(true)->save([ // 消息通知 // 'type' => 3, // 'name' => $club->name, // 'avatar' => $club->logo, // 'from_id' => $club->id, // 'content' => json_encode([ // 'topic' => '俱乐部加入申请', // '俱乐部名称' => $club->name, // '申请人' => $user->nickname, // '申请时间' => date('Y-m-d H:i:s'), // 'reason' => $params['reason'] ?? '', // 'apply_id' => $apply->id, // ]) // ]); } Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success('Success'); } // 获取申请列表 public function applyList() { $params = $this->request->param(); $club = $this->model->get($params['club_id']); if (empty($club)) { return $this->error('俱乐部不存在'); } $menber = Menber::where('club_id', $params['club_id']) ->where('user_id', $this->auth->id) ->where('role', '>', 1)->find(); if (empty($menber)) { $this->error('您无权处理申请'); } $query = Apply::where('type', 1)->where('target_id', $params['club_id']); if (isset($params['status'])) { $query->where('status', $params['status']); } $applyList = $query->select(); $this->success('Success', $applyList); } // 处理申请 public function handle() { $params = $this->request->param(); Db::startTrans(); try { $apply = Apply::get(['id' => $params['apply_id'], 'status' => 1]); if (empty($apply)) { return $this->error('申请记录不存在'); } $menber = Menber::where('club_id', $apply['target_id']) ->where('user_id', $this->auth->id) ->where('role', '>', 1)->find(); if (empty($menber)) { $this->error('您无权处理申请'); } if ($apply['status'] != 1) { return $this->error('该申请已处理'); } if ($params['status'] == 2) { (new Menber)->allowField(true)->save([ 'club_id' => $apply['target_id'], 'user_id' => $apply['user_id'], 'role' => 1 ]); } $apply->allowField(true)->save([ 'status' => $params['status'], 'reply' => $params['reply'] ?? '' ]); Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success('Success'); } // 设置成员角色 public function setmember() { $params = $this->request->param(); $user = Menber::where('club_id', $params['club_id']) ->where('user_id', $this->auth->id) ->where('role', '>', 1)->find(); if (empty($user)) { $this->error('无权限'); } if (isset($params['role']) && ($params['role'] < -1 || $params['role'] > 2)) { $this->error('非法角色'); } $user_ids = explode(',', $params['user_id'] ?? ''); $menber = Menber::where('club_id', $params['club_id'])->where('user_id', 'IN', $user_ids)->select(); if (empty($menber)) { $this->error('成员不存在'); } if ($user['role'] != 3) { if (isset($params['role']) && $params['role'] > 1) { $this->error('只有会长才能设置管理员'); } foreach ($menber as $m) { if ($m['role'] > 1) $this->error('只有会长才能修改管理员'); } } else { foreach ($menber as $m) { if ($m['role'] == 3 && isset($params['role'])) { $this->error('不可修改会长角色'); } } } $update = []; if (isset($params['role'])) $update['role'] = $params['role']; if (isset($params['remark'])) $update['remark'] = $params['remark']; if (isset($params['tags'])) $update['tags'] = $params['tags']; Db::startTrans(); try { Menber::where('club_id', $params['club_id'])->where('user_id', 'IN', $user_ids)->update($update); Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success('Success'); } }