isAjax()) { $limit = input('param.limit', 15); $page = input('param.page', 1); $offset = ($page - 1) * $limit; $title = input('param.title', ''); $status = input('param.status', ''); $where = []; if ($title) { $where[] = ['title', 'like', "%{$title}%"]; } if ($status !== '') { $where[] = ['status', '=', $status]; } $res = News::where($where) ->order('id', 'desc') ->limit($offset, $limit) ->select(); $total = News::where($where)->count(); return $this->renderTable('获取成功', $total, $res, 0); } return View::fetch('News/index'); } /** * 添加资讯 */ public function add() { if (request()->isPost()) { $param = input('post.'); $validate = $this->validate($param, [ 'title' => 'require', // 'author_id' => 'require|number', ]); if (true !== $validate) { return $this->renderError($validate); } $param['author_id'] = 1; // 如果状态为发布,确保有发布时间 if (isset($param['status']) && $param['status'] == 1 && empty($param['publish_time'])) { $param['publish_time'] = date('Y-m-d H:i:s'); } $res = News::create($param); if ($res) { return $this->renderSuccess('添加成功', ['url' => (string) url('news')]); } else { return $this->renderError('添加失败'); } } return View::fetch('News/add'); } /** * 编辑资讯 */ public function edit() { $id = input('param.id', 0); $info = News::where('id', $id)->find(); if (empty($info)) { return $this->renderError('数据不存在'); } if (request()->isPost()) { $param = input('post.'); $validate = $this->validate($param, [ 'title' => 'require', // 'author_id' => 'require|number', ]); if (true !== $validate) { return $this->renderError($validate); } $param['author_id'] = 1; // 如果状态为发布,确保有发布时间 if (isset($param['status']) && $param['status'] == 1 && empty($param['publish_time'])) { $param['publish_time'] = date('Y-m-d H:i:s'); } $res = News::update($param, ['id' => $id]); if ($res) { return $this->renderSuccess('编辑成功', ['url' => (string) url('news')]); } else { return $this->renderError('编辑失败'); } } View::assign('info', $info); return View::fetch('News/edit'); } /** * 修改资讯状态(发布/草稿) */ public function status() { $id = input('param.id', 0); $status = input('param.status', 0); // 如果状态为发布,确保有发布时间 $update = ['status' => $status]; if ($status == 1) { $info = News::where('id', $id)->find(); if (!$info->publish_time) { $update['publish_time'] = date('Y-m-d H:i:s'); } } $res = News::update($update, ['id' => $id]); if ($res) { return $this->renderSuccess('操作成功'); } else { return $this->renderError('操作失败'); } } /** * 修改热榜状态 */ public function hot() { $id = input('param.id', 0); $is_hot = input('param.is_hot', 0); $res = News::update(['is_hot' => $is_hot], ['id' => $id]); if ($res) { return $this->renderSuccess('操作成功'); } else { return $this->renderError('操作失败'); } } /** * 修改精选状态 */ public function featured() { $id = input('param.id', 0); $is_featured = input('param.is_featured', 0); $res = News::update(['is_featured' => $is_featured], ['id' => $id]); if ($res) { return $this->renderSuccess('操作成功'); } else { return $this->renderError('操作失败'); } } /** * 删除资讯 */ public function del() { $id = input('param.id', 0); $res = News::destroy($id); if ($res) { return $this->renderSuccess('删除成功'); } else { return $this->renderError('删除失败'); } } }