Article.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\store\controller\content;
  13. use app\store\controller\Controller;
  14. use app\store\model\Article as ArticleModel;
  15. /**
  16. * 文章管理控制器
  17. * Class article
  18. * @package app\store\controller\content
  19. */
  20. class Article extends Controller
  21. {
  22. /**
  23. * 文章列表
  24. * @return array
  25. * @throws \think\db\exception\DbException
  26. */
  27. public function list()
  28. {
  29. $model = new ArticleModel;
  30. $list = $model->getList($this->request->param());
  31. return $this->renderSuccess(compact('list'));
  32. }
  33. /**
  34. * 文章详情
  35. * @param int $articleId
  36. * @return array|bool|string
  37. */
  38. public function detail(int $articleId)
  39. {
  40. $detail = ArticleModel::detail($articleId);
  41. // 获取image (这里不能用with因为编辑页需要image对象)
  42. !empty($detail) && $detail['image'];
  43. return $this->renderSuccess(compact('detail'));
  44. }
  45. /**
  46. * 添加文章
  47. * @return array|mixed
  48. */
  49. public function add()
  50. {
  51. // 新增记录
  52. $model = new ArticleModel;
  53. if ($model->add($this->postForm())) {
  54. return $this->renderSuccess('添加成功');
  55. }
  56. return $this->renderError($model->getError() ?: '添加失败');
  57. }
  58. /**
  59. * 更新文章
  60. * @param int $articleId
  61. * @return array|mixed
  62. */
  63. public function edit(int $articleId)
  64. {
  65. // 文章详情
  66. $model = ArticleModel::detail($articleId);
  67. // 更新记录
  68. if ($model->edit($this->postForm())) {
  69. return $this->renderSuccess('更新成功');
  70. }
  71. return $this->renderError($model->getError() ?: '更新失败');
  72. }
  73. /**
  74. * 删除文章
  75. * @param $articleId
  76. * @return array
  77. */
  78. public function delete(int $articleId)
  79. {
  80. // 文章详情
  81. $model = ArticleModel::detail($articleId);
  82. if (!$model->setDelete()) {
  83. return $this->renderError($model->getError() ?: '删除失败');
  84. }
  85. return $this->renderSuccess('删除成功');
  86. }
  87. }