Article.php 2.4 KB

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