Article.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace app\store\controller\content;
  3. use app\store\controller\Controller;
  4. use app\store\model\Article as ArticleModel;
  5. use app\store\model\article\Category as CategoryModel;
  6. /**
  7. * 文章管理控制器
  8. * Class article
  9. * @package app\store\controller\content
  10. */
  11. class Article extends Controller
  12. {
  13. /**
  14. * 文章列表
  15. * @return mixed
  16. * @throws \think\exception\DbException
  17. */
  18. public function index()
  19. {
  20. $model = new ArticleModel;
  21. $list = $model->getList();
  22. return $this->fetch('index', compact('list'));
  23. }
  24. /**
  25. * 添加文章
  26. * @return array|mixed
  27. */
  28. public function add()
  29. {
  30. $model = new ArticleModel;
  31. if (!$this->request->isAjax()) {
  32. // 文章分类
  33. $catgory = CategoryModel::getAll();
  34. return $this->fetch('add', compact('catgory'));
  35. }
  36. // 新增记录
  37. if ($model->add($this->postData('article'))) {
  38. return $this->renderSuccess('添加成功', url('content.article/index'));
  39. }
  40. return $this->renderError($model->getError() ?: '添加失败');
  41. }
  42. /**
  43. * 更新文章
  44. * @param $article_id
  45. * @return array|mixed
  46. * @throws \think\exception\DbException
  47. */
  48. public function edit($article_id)
  49. {
  50. // 文章详情
  51. $model = ArticleModel::detail($article_id);
  52. if (!$this->request->isAjax()) {
  53. // 文章分类
  54. $catgory = CategoryModel::getAll();
  55. return $this->fetch('edit', compact('model', 'catgory'));
  56. }
  57. // 更新记录
  58. if ($model->edit($this->postData('article'))) {
  59. return $this->renderSuccess('更新成功', url('content.article/index'));
  60. }
  61. return $this->renderError($model->getError() ?: '更新失败');
  62. }
  63. /**
  64. * 删除文章
  65. * @param $article_id
  66. * @return array
  67. * @throws \think\exception\DbException
  68. */
  69. public function delete($article_id)
  70. {
  71. // 文章详情
  72. $model = ArticleModel::detail($article_id);
  73. if (!$model->setDelete()) {
  74. return $this->renderError($model->getError() ?: '删除失败');
  75. }
  76. return $this->renderSuccess('删除成功');
  77. }
  78. }