Article.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\cms;
  12. use app\adminapi\controller\AuthController;
  13. use app\services\article\ArticleServices;
  14. use think\facade\App;
  15. /**
  16. * 文章管理
  17. * Class Article
  18. * @package app\adminapi\controller\v1\cms
  19. */
  20. class Article extends AuthController
  21. {
  22. /**
  23. * @var ArticleServices
  24. */
  25. protected $service;
  26. /**
  27. * Article constructor.
  28. * @param App $app
  29. * @param ArticleServices $service
  30. */
  31. public function __construct(App $app, ArticleServices $service)
  32. {
  33. parent::__construct($app);
  34. $this->service = $service;
  35. }
  36. /**
  37. * 获取列表
  38. * @return mixed
  39. * @throws \ReflectionException
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public function index()
  45. {
  46. $where = $this->request->getMore([
  47. ['title', ''],
  48. ['pid', 0, '', 'cid'],
  49. ]);
  50. $data = $this->service->getList($where);
  51. return app('json')->success($data);
  52. }
  53. /**
  54. * 保存文章数据
  55. * @return mixed
  56. */
  57. public function save()
  58. {
  59. $data = $this->request->postMore([
  60. ['id', 0],
  61. ['cid', ''],
  62. ['title', ''],
  63. ['author', ''],
  64. ['image_input', ''],
  65. ['content', ''],
  66. ['synopsis', 0],
  67. ['share_title', ''],
  68. ['share_synopsis', ''],
  69. ['sort', 0],
  70. ['url', ''],
  71. ['is_banner', 0],
  72. ['is_hot', 0],
  73. ['status', 1]
  74. ]);
  75. $this->service->save($data);
  76. return app('json')->success(100021);
  77. }
  78. /**
  79. * 获取单个文章数据
  80. * @param int $id
  81. * @return mixed
  82. * @throws \ReflectionException
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\DbException
  85. * @throws \think\db\exception\ModelNotFoundException
  86. */
  87. public function read($id = 0)
  88. {
  89. if (!$id) return app('json')->fail(100100);
  90. $info = $this->service->read($id);
  91. return app('json')->success($info);
  92. }
  93. /**
  94. * 删除文章
  95. * @param int $id
  96. * @return mixed
  97. */
  98. public function delete($id = 0)
  99. {
  100. if (!$id) return app('json')->fail(100100);
  101. $this->service->del($id);
  102. return app('json')->success(100002);
  103. }
  104. /**
  105. * 文章关联商品
  106. * @param int $id
  107. * @return mixed
  108. */
  109. public function relation($id)
  110. {
  111. if (!$id) return app('json')->fail(100100);
  112. list($product_id) = $this->request->postMore([
  113. ['product_id', 0]
  114. ], true);
  115. $res = $this->service->bindProduct($id, $product_id);
  116. if ($res) {
  117. return app('json')->success(400300);
  118. } else {
  119. return app('json')->fail(400301);
  120. }
  121. }
  122. /**
  123. * 取消商品关联
  124. * @param int $id
  125. * @return mixed
  126. */
  127. public function unrelation($id)
  128. {
  129. if (!$id) return app('json')->fail(100100);
  130. $res = $this->service->bindProduct($id);
  131. if ($res) {
  132. return app('json')->success(100019);
  133. } else {
  134. return app('json')->fail(100020);
  135. }
  136. }
  137. }