Article.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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\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. protected $service;
  23. public function __construct(App $app, ArticleServices $service)
  24. {
  25. parent::__construct($app);
  26. $this->service = $service;
  27. }
  28. /**
  29. * 获取列表
  30. * @return mixed
  31. */
  32. public function index()
  33. {
  34. $where = $this->request->getMore([
  35. ['title', ''],
  36. ['pid', 0, '', 'cid'],
  37. ]);
  38. $data = $this->service->getList($where);
  39. return app('json')->success($data);
  40. }
  41. /**
  42. * 保存文章数据
  43. * @return mixed
  44. */
  45. public function save()
  46. {
  47. $data = $this->request->postMore([
  48. ['id', 0],
  49. ['cid', ''],
  50. ['title', ''],
  51. ['author', ''],
  52. ['image_input', ''],
  53. ['content', ''],
  54. ['synopsis', 0],
  55. ['share_title', ''],
  56. ['share_synopsis', ''],
  57. ['sort', 0],
  58. ['url', ''],
  59. ['is_banner', 0],
  60. ['is_hot', 0],
  61. ['status', 1]
  62. ]);
  63. $this->service->save($data);
  64. return app('json')->success('添加成功!');
  65. }
  66. /**
  67. * 获取单个文章数据
  68. * @param $id
  69. * @return mixed
  70. */
  71. public function read($id)
  72. {
  73. if ($id) {
  74. $info = $this->service->read($id);
  75. return app('json')->success($info);
  76. } else {
  77. return app('json')->fail('参数错误');
  78. }
  79. }
  80. /**
  81. * 删除文章
  82. * @param $id
  83. * @return mixed
  84. * @throws \Exception
  85. */
  86. public function delete($id)
  87. {
  88. if ($id) {
  89. $this->service->del($id);
  90. return app('json')->success('删除成功!');
  91. } else {
  92. return app('json')->fail('参数错误');
  93. }
  94. }
  95. /**
  96. * 文章关联商品
  97. * @param int $id
  98. * @return mixed
  99. */
  100. public function relation($id)
  101. {
  102. if (!$id) return app('json')->fail('缺少参数');
  103. list($product_id) = $this->request->postMore([
  104. ['product_id', 0]
  105. ], true);
  106. $res = $this->service->bindProduct($id, $product_id);
  107. if ($res) {
  108. return app('json')->success('关联成功');
  109. } else {
  110. return app('json')->fail('关联失败');
  111. }
  112. }
  113. /**
  114. * 取消商品关联
  115. * @param int $id
  116. * @return mixed
  117. */
  118. public function unrelation($id)
  119. {
  120. if (!$id) return app('json')->fail('缺少参数');
  121. $res = $this->service->bindProduct($id);
  122. if ($res) {
  123. return app('json')->success('取消关联成功!');
  124. } else {
  125. return app('json')->fail('取消失败');
  126. }
  127. }
  128. }