SystemAttachmentCategory.php 3.9 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\file;
  12. use app\adminapi\controller\AuthController;
  13. use app\services\system\attachment\SystemAttachmentCategoryServices;
  14. use think\facade\App;
  15. /**
  16. * 图片分类管理类
  17. * Class SystemAttachmentCategory
  18. * @package app\adminapi\controller\v1\file
  19. */
  20. class SystemAttachmentCategory extends AuthController
  21. {
  22. /**
  23. * @var SystemAttachmentCategoryServices
  24. */
  25. protected $service;
  26. /**
  27. * @param App $app
  28. * @param SystemAttachmentCategoryServices $service
  29. */
  30. public function __construct(App $app, SystemAttachmentCategoryServices $service)
  31. {
  32. parent::__construct($app);
  33. $this->service = $service;
  34. }
  35. /**
  36. * 显示资源列表
  37. * @return \think\Response
  38. * @throws \ReflectionException
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function index()
  44. {
  45. $where = $this->request->getMore([
  46. ['name', ''],
  47. ['pid', 0],
  48. ['all', 0],
  49. ['type', 0],
  50. ]);
  51. if ($where['name'] != '' || $where['all'] == 1) $where['pid'] = '';
  52. return app('json')->success($this->service->getAll($where));
  53. }
  54. /**
  55. * 新增表单
  56. * @return mixed
  57. * @throws \FormBuilder\Exception\FormBuilderException
  58. */
  59. public function create()
  60. {
  61. [$id, $type] = $this->request->getMore([
  62. ['id', 0],
  63. ['type', 0],
  64. ], true);
  65. return app('json')->success($this->service->createForm($id, $type));
  66. }
  67. /**
  68. * 保存新增
  69. * @return mixed
  70. */
  71. public function save()
  72. {
  73. $data = $this->request->postMore([
  74. ['pid', 0],
  75. ['name', ''],
  76. ['type', 0],
  77. ]);
  78. if (is_array($data['pid'])) $data['pid'] = end($data['pid']);
  79. if (!$data['name']) {
  80. return app('json')->fail(400100);
  81. }
  82. $this->service->save($data);
  83. return app('json')->success(100021);
  84. }
  85. /**
  86. * 编辑表单
  87. * @param $id
  88. * @return mixed
  89. * @throws \FormBuilder\Exception\FormBuilderException
  90. */
  91. public function edit($id)
  92. {
  93. return app('json')->success($this->service->editForm($id));
  94. }
  95. /**
  96. * 保存更新的资源
  97. * @param $id
  98. * @return mixed
  99. */
  100. public function update($id)
  101. {
  102. $data = $this->request->postMore([
  103. ['pid', 0],
  104. ['name', '']
  105. ]);
  106. if (is_array($data['pid'])) $data['pid'] = end($data['pid']);
  107. if (!$data['name']) {
  108. return app('json')->fail(400100);
  109. }
  110. if ($data['pid'] == $id) {
  111. return app('json')->fail('上级分类不能是自己');
  112. }
  113. $info = $this->service->get($id);
  114. $count = $this->service->count(['pid' => $id]);
  115. if ($count && $info['pid'] != $data['pid']) return app('json')->fail(400105);
  116. $this->service->update($id, $data);
  117. return app('json')->success(100001);
  118. }
  119. /**
  120. * 删除指定资源
  121. * @param int $id
  122. * @return \think\Response
  123. */
  124. public function delete($id)
  125. {
  126. $this->service->del($id);
  127. return app('json')->success(100002);
  128. }
  129. }