Article.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\article;
  12. use crmeb\basic\BaseController;
  13. use app\common\repositories\article\ArticleCategoryRepository;
  14. use app\common\repositories\article\ArticleContentRepository;
  15. use app\common\repositories\article\ArticleRepository;
  16. use FormBuilder\Exception\FormBuilderException;
  17. use think\App;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. use app\validate\admin\ArticleValidate;
  22. class Article extends BaseController
  23. {
  24. /**
  25. * @var ArticleRepository
  26. */
  27. protected $repository;
  28. /**
  29. * Article constructor.
  30. * @param App $app
  31. * @param ArticleRepository $repository
  32. */
  33. public function __construct(App $app,ArticleRepository $repository)
  34. {
  35. parent::__construct($app);
  36. $this->repository = $repository;
  37. }
  38. /**
  39. * @return mixed
  40. * @author Qinii
  41. */
  42. public function getList()
  43. {
  44. [$page, $limit] = $this->getPage();
  45. $where = $this->request->params(['cid','title']);
  46. return app('json')->success($this->repository->search($this->request->merId(),$where, $page, $limit));
  47. }
  48. /**
  49. * 添加
  50. * @param ArticleValidate $validate
  51. * @param ArticleCategoryRepository $repository
  52. * @return mixed
  53. * @author Qinii
  54. */
  55. public function create(ArticleValidate $validate,ArticleCategoryRepository $repository)
  56. {
  57. $data = $this->checkParams($validate);
  58. $data['admin_id'] = $this->request->adminId();
  59. $data['mer_id'] = $this->request->merId() ;
  60. if (!$repository->merExists(0,$data['cid']))
  61. return app('json')->fail('分类不存在');
  62. $this->repository->create($data);
  63. return app('json')->success('添加成功');
  64. }
  65. public function detail($id)
  66. {
  67. if (!$this->repository->merExists($this->request->merId(),$id))
  68. return app('json')->fail('数据不存在');
  69. return app('json')->success($this->repository->get($id,$this->request->merId()));
  70. }
  71. /**
  72. * 更新
  73. * @param $id
  74. * @param ArticleValidate $validate
  75. * @param ArticleCategoryRepository $articleCategoryRepository
  76. * @return mixed
  77. * @author Qinii
  78. */
  79. public function update($id,ArticleValidate $validate,ArticleCategoryRepository $articleCategoryRepository)
  80. {
  81. $data = $this->checkParams($validate);
  82. if (!$this->repository->merExists($this->request->merId(),$id))
  83. return app('json')->fail('数据不存在');
  84. if (!$articleCategoryRepository->merExists($this->request->merId(),$data['cid']))
  85. return app('json')->fail('分类不存在');
  86. $this->repository->update($id,$data);
  87. return app('json')->success('编辑成功');
  88. }
  89. /**
  90. * 删除
  91. * @param $id
  92. * @return mixed
  93. * @throws DataNotFoundException
  94. * @throws DbException
  95. * @throws ModelNotFoundException
  96. * @author Qinii
  97. */
  98. public function delete($id)
  99. {
  100. if (!$this->repository->merExists($this->request->merId(),$id))
  101. return app('json')->fail('数据不存在');
  102. $this->repository->delete($id,$this->request->merId());
  103. return app('json')->success('删除成功');
  104. }
  105. /**
  106. * @param ArticleValidate $validate
  107. * @return array
  108. * @author Qinii
  109. */
  110. public function checkParams(ArticleValidate $validate)
  111. {
  112. $data = $this->request->params([['cid', 0], 'title', 'content', 'author', 'image_input','status','sort','synopsis','is_hot','is_banner','url']);
  113. $validate->check($data);
  114. return $data;
  115. }
  116. }