Article.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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\v1\cms;
  12. use app\controller\admin\AuthController;
  13. use app\services\article\ArticleServices;
  14. use think\facade\App;
  15. /**
  16. * 文章管理
  17. * Class Article
  18. * @package app\controller\admin\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 $this->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. ['synopsis', 0],
  62. ['share_title', ''],
  63. ['share_synopsis', ''],
  64. ['sort', 0],
  65. ['url', ''],
  66. ['is_banner', 0],
  67. ['is_hot', 0],
  68. ['status', 1]
  69. ]);
  70. $data['content'] = $this->request->param('content','');
  71. $this->service->save($data);
  72. return $this->success('添加成功!');
  73. }
  74. /**
  75. * 获取单个文章数据
  76. * @param $id
  77. * @return mixed
  78. */
  79. public function read($id)
  80. {
  81. if ($id) {
  82. $info = $this->service->read($id);
  83. return $this->success($info);
  84. } else {
  85. return $this->fail('参数错误');
  86. }
  87. }
  88. /**
  89. * 删除文章
  90. * @param $id
  91. * @return mixed
  92. * @throws \Exception
  93. */
  94. public function delete($id)
  95. {
  96. if ($id) {
  97. $this->service->del($id);
  98. return $this->success('删除成功!');
  99. } else {
  100. return $this->fail('参数错误');
  101. }
  102. }
  103. /**
  104. * 文章关联商品
  105. * @param int $id
  106. * @return mixed
  107. */
  108. public function relation($id)
  109. {
  110. if (!$id) return $this->fail('缺少参数');
  111. [$product_id] = $this->request->postMore([
  112. ['product_id', 0]
  113. ], true);
  114. $res = $this->service->bindProduct($id, $product_id);
  115. if ($res) {
  116. return $this->success('关联成功');
  117. } else {
  118. return $this->fail('关联失败');
  119. }
  120. }
  121. /**
  122. * 取消商品关联
  123. * @param int $id
  124. * @return mixed
  125. */
  126. public function unrelation($id)
  127. {
  128. if (!$id) return $this->fail('缺少参数');
  129. $res = $this->service->bindProduct($id);
  130. if ($res) {
  131. return $this->success('取消关联成功!');
  132. } else {
  133. return $this->fail('取消失败');
  134. }
  135. }
  136. }