Article.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. */
  40. public function index()
  41. {
  42. $where = $this->request->getMore([
  43. ['title', ''],
  44. ['pid', 0, '', 'cid'],
  45. ]);
  46. $data = $this->service->getList($where);
  47. return app('json')->success($data);
  48. }
  49. /**
  50. * 保存文章数据
  51. * @return mixed
  52. */
  53. public function save()
  54. {
  55. $data = $this->request->postMore([
  56. ['id', 0],
  57. ['cid', ''],
  58. ['title', ''],
  59. ['author', ''],
  60. ['image_input', ''],
  61. ['content', ''],
  62. ['synopsis', 0],
  63. ['share_title', ''],
  64. ['share_synopsis', ''],
  65. ['sort', 0],
  66. ['url', ''],
  67. ['is_banner', 0],
  68. ['is_hot', 0],
  69. ['status', 1]
  70. ]);
  71. $this->service->save($data);
  72. return app('json')->success(100021);
  73. }
  74. /**
  75. * 获取单个文章数据
  76. * @param int $id
  77. * @return mixed
  78. */
  79. public function read($id = 0)
  80. {
  81. if (!$id) return app('json')->fail(100100);
  82. $info = $this->service->read($id);
  83. return app('json')->success($info);
  84. }
  85. /**
  86. * 删除文章
  87. * @param int $id
  88. * @return mixed
  89. */
  90. public function delete($id = 0)
  91. {
  92. if (!$id) return app('json')->fail(100100);
  93. $this->service->del($id);
  94. return app('json')->success(100002);
  95. }
  96. /**
  97. * 文章关联商品
  98. * @param int $id
  99. * @return mixed
  100. */
  101. public function relation($id)
  102. {
  103. if (!$id) return app('json')->fail(100100);
  104. list($product_id) = $this->request->postMore([
  105. ['product_id', 0]
  106. ], true);
  107. $res = $this->service->bindProduct($id, $product_id);
  108. if ($res) {
  109. return app('json')->success(400300);
  110. } else {
  111. return app('json')->fail(400301);
  112. }
  113. }
  114. /**
  115. * 取消商品关联
  116. * @param int $id
  117. * @return mixed
  118. */
  119. public function unrelation($id)
  120. {
  121. if (!$id) return app('json')->fail(100100);
  122. $res = $this->service->bindProduct($id);
  123. if ($res) {
  124. return app('json')->success(100019);
  125. } else {
  126. return app('json')->fail(100020);
  127. }
  128. }
  129. }