where('status', $status); } if ($keyword) { $query->where('title', 'like', "%{$keyword}%"); } $paginator = $query->order('is_top', 'desc') ->order('create_at', 'desc') ->paginate(['list_rows' => $limit, 'page' => $page]); $rows = []; foreach ($paginator->items() as $a) { $rows[] = [ 'id' => $a->id, 'title' => $a->title, 'is_top' => $a->is_top, 'is_recommend'=> $a->is_recommend, 'category' => $a->category->name ?? '-', 'status' => $a->status, 'view_count' => (int) $a->view_count, 'comment_count' => (int) $a->comment_count, 'create_at' => $a->create_at, 'edit_url' => (string) url('articles/backend.article/edit', ['id' => $a->id]), ]; } return json(['code' => 0, 'msg' => '', 'count' => $paginator->total(), 'data' => $rows]); } return View::fetch('article/index', ['status' => $status, 'keyword' => $keyword]); } public function save() { if (Request::isPost()) { return $this->update(0); } $categories = ArticleCategory::where('delete_at', 0)->where('status', 1)->select(); $tags = ArticleTag::where('delete_at', 0)->select(); return View::fetch('article/edit', [ 'article' => null, 'categories' => $categories, 'tags' => $tags, 'articleTags' => [], 'tagNamesStr' => '', ]); } public function edit($id = null) { $article = ArticleModel::with(['tags'])->find($id); if (!$article) { return redirect('/articles/backend/article'); } $categories = ArticleCategory::where('delete_at', 0)->where('status', 1)->select(); $tags = ArticleTag::where('delete_at', 0)->select(); $articleTags = $article->tags->column('id'); $tagNamesStr = implode(',', $article->tags->column('name')); return View::fetch('article/edit', [ 'article' => $article, 'categories' => $categories, 'tags' => $tags, 'articleTags' => $articleTags, 'tagNamesStr' => $tagNamesStr, ]); } public function update($id = 0) { $data = Request::post(); $id = $id ?: (int) ($data['id'] ?? 0); $data['update_at'] = time(); $data['is_top'] = isset($data['is_top']) ? 1 : 0; $data['is_recommend']= isset($data['is_recommend']) ? 1 : 0; $validate = new Validate([ 'title|标题' => 'require|max:255', 'content|内容' => 'require', 'cid|分类' => 'require|number', ]); if (!$validate->check($data)) { return json(['code' => 0, 'msg' => $validate->getError()]); } // 处理标签 $tagIds = []; if (!empty($data['tags'])) { $tagNames = is_array($data['tags']) ? $data['tags'] : explode(',', $data['tags']); foreach ($tagNames as $name) { $name = trim($name); if ($name === '') { continue; } $tag = ArticleTag::where('delete_at', 0)->where('name', $name)->find() ?: ArticleTag::create(['name' => $name, 'alias' => $name, 'create_at' => time(), 'update_at' => time()]); $tagIds[] = $tag->id; } } if ($id) { $article = ArticleModel::find($id); if (!$article) { return json(['code' => 0, 'msg' => '文章不存在']); } $article->save($data); } else { if (empty($data['uid'])) { $data['uid'] = $this->auth->info['uid'] ?? 0; } if (empty($data['author'])) { $data['author'] = $this->auth->info['nickname'] ?? ''; } $data['create_at'] = time(); $article = ArticleModel::create($data); $id = $article->id; } ArticleArticleTag::syncTags($id, $tagIds); return json(['code' => 1, 'msg' => '保存成功', 'url' => '/articles/backend/article']); } public function delete($id = 0) { $id = $id ?: (int) Request::post('id', 0); $article = ArticleModel::find($id); if ($article) { $article->delete(); return json(['code' => 1, 'msg' => '已删除']); } return json(['code' => 0, 'msg' => '文章不存在']); } public function recyclebin() { $page = Request::param('page', 1); $limit = Request::param('limit', 15); if (Request::isAjax()) { $list = ArticleModel::onlyTrashed() ->order('delete_at', 'desc') ->paginate(['list_rows' => $limit, 'page' => $page]); $rows = array_map(function ($a) { return [ 'id' => $a->id, 'title' => $a->title, 'delete_at' => $a->delete_at, ]; }, $list->items()); return json(['code' => 0, 'msg' => '', 'count' => $list->total(), 'data' => $rows]); } return View::fetch('article/recyclebin'); } public function restore($id = 0) { $id = $id ?: (int) Request::post('id', 0); $article = ArticleModel::onlyTrashed()->find($id); if ($article) { $article->restore(); return json(['code' => 1, 'msg' => '已恢复']); } return json(['code' => 0, 'msg' => '记录不存在']); } }