Article.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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 think\response\Json;
  14. use app\store\controller\Controller;
  15. use app\store\model\Article as ArticleModel;
  16. /**
  17. * 文章管理控制器
  18. * Class article
  19. * @package app\store\controller\content
  20. */
  21. class Article extends Controller
  22. {
  23. /**
  24. * 文章列表
  25. * @return Json
  26. * @throws \think\db\exception\DbException
  27. */
  28. public function list(): Json
  29. {
  30. $model = new ArticleModel;
  31. $list = $model->getList($this->request->param());
  32. return $this->renderSuccess(compact('list'));
  33. }
  34. /**
  35. * 文章详情
  36. * @param int $articleId
  37. * @return Json
  38. */
  39. public function detail(int $articleId): Json
  40. {
  41. $detail = ArticleModel::detail($articleId);
  42. // 获取image (这里不能用with因为编辑页需要image对象)
  43. !empty($detail) && $detail['image'];
  44. return $this->renderSuccess(compact('detail'));
  45. }
  46. /**
  47. * 添加文章
  48. * @return Json
  49. */
  50. public function add(): Json
  51. {
  52. // 新增记录
  53. $model = new ArticleModel;
  54. if ($model->add($this->postForm())) {
  55. return $this->renderSuccess('添加成功');
  56. }
  57. return $this->renderError($model->getError() ?: '添加失败');
  58. }
  59. /**
  60. * 更新文章
  61. * @param int $articleId
  62. * @return Json
  63. */
  64. public function edit(int $articleId): Json
  65. {
  66. // 文章详情
  67. $model = ArticleModel::detail($articleId);
  68. // 更新记录
  69. if ($model->edit($this->postForm())) {
  70. return $this->renderSuccess('更新成功');
  71. }
  72. return $this->renderError($model->getError() ?: '更新失败');
  73. }
  74. /**
  75. * 删除文章
  76. * @param int $articleId
  77. * @return Json
  78. */
  79. public function delete(int $articleId): Json
  80. {
  81. // 文章详情
  82. $model = ArticleModel::detail($articleId);
  83. if (!$model->setDelete()) {
  84. return $this->renderError($model->getError() ?: '删除失败');
  85. }
  86. return $this->renderSuccess('删除成功');
  87. }
  88. }