model = new \app\admin\model\FcPost; } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 审核信息 */ public function audit() { $ids = $this->request->post('ids'); $auditStatus = (int)$this->request->post('auditStatus'); $reason = $this->request->post('reason', ''); if (!in_array($auditStatus, [1, 2])) { $this->error('审核状态仅支持通过(1)/驳回(2)'); } $ids = is_array($ids) ? $ids : explode(',', (string)$ids); $ids = array_filter($ids); if (!$ids) { $this->error('请选择要审核的信息'); } $posts = Db::name('fc_post')->where('id', 'in', $ids)->select(); if (!$posts) { $this->error('信息不存在'); } foreach ($posts as $post) { Db::name('fc_post')->where('id', $post['id'])->update([ 'audit_status' => $auditStatus, 'audit_reason' => $reason, 'updatetime' => time(), ]); if ($auditStatus === 1 && !empty($post['category_id'])) { Db::name('fc_category')->where('id', $post['category_id'])->setInc('post_count'); } Db::name('fc_message')->insert([ 'user_id' => $post['user_id'], 'type' => 'system', 'title' => $auditStatus === 1 ? '审核通过' : '审核驳回', 'content' => $auditStatus === 1 ? '您发布的信息「' . $post['title'] . '」已审核通过' : '您发布的信息「' . $post['title'] . '」审核未通过' . ($reason ? ',原因:' . $reason : ''), 'isread' => 0, 'createtime' => time(), ]); } $this->success('审核成功'); } /** * 置顶 / 取消置顶(支持批量) * POST fc/post/top {ids:'1,2', isTop:1|0} * 说明:置顶信息在用户端主页与列表**排在非置顶之前**(Home::feed / Post::mine 均按 is_top desc 排序) */ public function top() { $ids = $this->request->post('ids'); $isTop = (int)$this->request->post('isTop', 1); if (!in_array($isTop, [0, 1], true)) { $this->error('置顶状态仅支持置顶(1)/取消置顶(0)'); } $ids = is_array($ids) ? $ids : explode(',', (string)$ids); $ids = array_filter($ids); if (!$ids) { $this->error('请选择要操作的信息'); } $posts = Db::name('fc_post')->where('id', 'in', $ids)->select(); if (!$posts) { $this->error('信息不存在'); } foreach ($posts as $post) { Db::name('fc_post')->where('id', $post['id'])->update([ 'is_top' => $isTop, 'updatetime' => time(), ]); Db::name('fc_message')->insert([ 'user_id' => $post['user_id'], 'type' => 'system', 'title' => $isTop === 1 ? '信息已置顶' : '已取消置顶', 'content' => $isTop === 1 ? '您发布的信息「' . $post['title'] . '」已被置顶,将在首页优先展示' : '您发布的信息「' . $post['title'] . '」已取消置顶', 'isread' => 0, 'createtime' => time(), ]); } $this->success($isTop === 1 ? '置顶成功' : '已取消置顶'); } /** * 下架信息 */ public function offline() { $ids = $this->request->post('ids'); $ids = is_array($ids) ? $ids : explode(',', (string)$ids); $ids = array_filter($ids); if (!$ids) { $this->error('请选择要下架的信息'); } $posts = Db::name('fc_post')->where('id', 'in', $ids)->select(); if (!$posts) { $this->error('信息不存在'); } foreach ($posts as $post) { Db::name('fc_post')->where('id', $post['id'])->update([ 'status' => 0, 'updatetime' => time(), ]); Db::name('fc_message')->insert([ 'user_id' => $post['user_id'], 'type' => 'system', 'title' => '信息已下架', 'content' => '您发布的信息「' . $post['title'] . '」已被下架', 'isread' => 0, 'createtime' => time(), ]); } $this->success('下架成功'); } }