request->param(); $sub = CircleModel::alias('c') ->join([Likes::$tableName => 'l'], 'c.id = l.circle_id', 'LEFT') ->field("c.*,JSON_ARRAYAGG(JSON_OBJECT( 'id', l.id, 'user_id', l.user_id, 'nickname', l.nickname, 'avatar', l.avatar, 'gender', l.gender )) AS likes")->group('c.id')->buildSql(); $query = Comment::alias('m') ->join([$sub => 'c'], 'c.id = m.circle_id', 'RIGHT') ->field("c.*, JSON_ARRAYAGG(JSON_OBJECT( 'id', m.id, 'pid', m.pid, 'puser_id', m.puser_id, 'pnickname', m.pnickname, 'user_id', m.user_id, 'nickname', m.nickname, 'avatar', m.avatar, 'gender', m.gender, 'content', m.content, 'create_time', m.create_time )) AS comment")->group('c.id'); if (isset($params['status'])) { $query->where('c.status', $params['status']); } else { $query->where('c.status', 1); } if (isset($params['club_id'])) { $query->where('c.club_id', $params['club_id']); } if (isset($params['user_id'])) { $query->where('c.user_id', $params['user_id']); } if (isset($params['friend'])) { $friend = Relation::where('user_id', $this->auth->id)->where('status', 'IN', explode(',', $params['friend']))->column('target_id'); $query->where('c.user_id', 'IN', $friend); } $query->order(['c.top' => 'desc', 'c.create_time' => 'desc']); $res = $query->paginate($params['pageSize'] ?? 10); $list = $res->items(); foreach ($list as &$r) { $r['likes'] = json_decode($r['likes'], true); if (count($r['likes']) == 1 && empty($r['likes'][0]['id'])) $r['likes'] = []; $r['comment'] = buildTree(json_decode($r['comment'], true)); } $this->success('Success', ['list' => $list, 'count' => $res->total()]); } public function view() { $model = CircleModel::get($this->request->param('id')); if (empty($model)) { $this->error(__('No rows were found')); } $this->success('Success', $model); } public function add() { $params = $this->request->param(); if (empty($params['content'])) { $this->error('内容不能为空'); } $user = auth_user(); $club = Menber::alias('m')->join([Club::$tableName => 'c'], 'm.club_id=c.id') ->field('c.name') ->where(['m.club_id' => $params['club_id'], 'm.user_id' => $user['id']]) ->where('m.role', '>', 0)->find(); if (empty($club)) { $this->error('你无权在此俱乐部发布'); } $params['user_id'] = $user['id']; $params['nickname'] = $user['nickname']; $params['avatar'] = $user['avatar']; $params['gender'] = $user['gender']; $params['club_name'] = $club['name']; $params['status'] = 0; Db::startTrans(); try { $result = (new CircleModel)->allowField(true)->save($params); Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result === false) { $this->error('操作失败'); } $this->success('Success'); } // 点赞 public function like() { $params = $this->request->param(); $circle = CircleModel::get($params['circle_id']); if (empty($circle)) { $this->error('数据不存在'); } $user = auth_user(); Db::startTrans(); try { $like = Likes::get(['circle_id' => $params['circle_id'], 'user_id' => $user['id']]); if (empty($like)) { // 点赞 (new Likes)->allowField(true)->save([ 'circle_id' => $params['circle_id'], 'user_id' => $user['id'], 'nickname' => $user['nickname'], 'avatar' => $user['avatar'], 'gender' => $user['gender'], ]); // (new Message())->allowField(true)->save([ // 消息通知 // 'type' => 1, // 'name' => '互动消息', // 'avatar' => '', // 'from_id' => 0, // 'user_id' => $circle->user_id, // 'content' => json_encode([ // 'topic' => '点赞', // 'content' => $user['nickname'] . '点赞了你的帖子', // 'circle_id' => $circle->id // ]) // ]); } else { // 取消点赞 $like->delete(); } Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success('Success'); } // 评论 public function comment() { $params = $this->request->param(); $circle = CircleModel::get($params['circle_id']); if (empty($circle)) { $this->error('数据不存在'); } if (empty($params['content'])) { $this->error('内容不能为空'); } $pcomment = Comment::get($params['pid']); if (!empty($params['pid']) && empty($pcomment)) { $this->error('回复的评论不存在'); } $user = auth_user(); Db::startTrans(); try { (new Comment)->allowField(true)->save([ 'circle_id' => $params['circle_id'], 'pid' => $params['pid'], 'puser_id' => $pcomment['user_id'] ?? 0, 'pnickname' => $pcomment['nickname'] ?? '', 'user_id' => $user['id'], 'nickname' => $user['nickname'], 'avatar' => $user['avatar'], 'gender' => $user['gender'], 'content' => $params['content'], ]); // (new Message())->allowField(true)->save([ // 消息通知 // 'type' => 1, // 'name' => '互动消息', // 'avatar' => '', // 'from_id' => 0, // 'target_id' => $circle->user_id, // 'content' => json_encode([ // 'topic' => '评论', // 'content' => $user['nickname'] . '评论了你的帖子', // 'circle_id' => $circle->id // ]) // ]); Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success('Success'); } public function approve() { $params = $this->request->param(); $ids = explode(',', $params['ids'] ?? ''); if (empty($ids) || empty($params['club_id'])) { $this->error('参数错误'); } if (empty($params['status']) || ($params['status'] != -1 && $params['status'] != 1)) { $this->error('status:参数错误'); } $member = Menber::get(['club_id' => $params['club_id'], 'user_id' => $this->auth->id]); if (empty($member) || $member->role < 2) { $this->error('无权审核'); } $models = CircleModel::where('id', 'IN', $ids)->where('club_id', $params['club_id'])->select(); if (empty($models)) { $this->error('数据不存在'); } Db::startTrans(); try { foreach ($models as $model) { if ($model->status != 0) { throw new Exception($model->id . ' 此记录不在待审核状态'); } $model->save(['status' => $params['status']]); // (new Message())->allowField(true)->save([ // 消息通知 // 'type' => 3, // 'name' => '通知消息', // 'avatar' => '', // 'from_id' => 1, // 'user_id' => $model->user_id, // 'content' => json_encode([ // 'topic' => '影圈审核', // 'result' => ($params['status'] == 1) ? '通过' : '不通过', // 'circle_id' => $model->id // ]) // ]); } Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success('Success', count($models)); } public function delete() { $params = $this->request->param(); $ids = explode(',', $params['ids'] ?? ''); if (empty($ids) || empty($params['club_id'])) { $this->error('参数错误'); } $member = Menber::get(['club_id' => $params['club_id'], 'user_id' => $this->auth->id]); if (empty($member) || $member->role < 2) { $this->error('无权删除'); } $result = CircleModel::where('id', 'IN', $ids)->where('club_id', $params['club_id'])->delete(); $this->success('Success', $result); } public function top() { $params = $this->request->param(); $model = CircleModel::get($params['id'] ?? null); if (empty($model)) { $this->error('数据不存在'); } $member = Menber::get(['club_id' => $model['club_id'], 'user_id' => $this->auth->id]); if (empty($member) || $member->role < 2) { $this->error('无权操作'); } $result = CircleModel::where('club_id', $model['club_id'])->column('max(top) as top'); $model->save(['top' => $result[0] + 1]); $this->success('Success', $model); } }