SystemAttachmentCategory.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. ]);
  50. if ($where['name'] != '' || $where['all'] == 1) $where['pid'] = '';
  51. return app('json')->success($this->service->getAll($where));
  52. }
  53. /**
  54. * 新增表单
  55. * @return mixed
  56. * @throws \FormBuilder\Exception\FormBuilderException
  57. */
  58. public function create($id)
  59. {
  60. return app('json')->success($this->service->createForm($id));
  61. }
  62. /**
  63. * 保存新增
  64. * @return mixed
  65. */
  66. public function save()
  67. {
  68. $data = $this->request->postMore([
  69. ['pid', 0],
  70. ['name', '']
  71. ]);
  72. if (is_array($data['pid'])) $data['pid'] = end($data['pid']);
  73. if (!$data['name']) {
  74. return app('json')->fail(400100);
  75. }
  76. $this->service->save($data);
  77. return app('json')->success(100021);
  78. }
  79. /**
  80. * 编辑表单
  81. * @param $id
  82. * @return mixed
  83. * @throws \FormBuilder\Exception\FormBuilderException
  84. */
  85. public function edit($id)
  86. {
  87. return app('json')->success($this->service->editForm($id));
  88. }
  89. /**
  90. * 保存更新的资源
  91. * @param $id
  92. * @return mixed
  93. */
  94. public function update($id)
  95. {
  96. $data = $this->request->postMore([
  97. ['pid', 0],
  98. ['name', '']
  99. ]);
  100. if (is_array($data['pid'])) $data['pid'] = end($data['pid']);
  101. if (!$data['name']) {
  102. return app('json')->fail(400100);
  103. }
  104. if ($data['pid'] == $id) {
  105. return app('json')->fail('上级分类不能是自己');
  106. }
  107. $info = $this->service->get($id);
  108. $count = $this->service->count(['pid' => $id]);
  109. if ($count && $info['pid'] != $data['pid']) return app('json')->fail(400105);
  110. $this->service->update($id, $data);
  111. return app('json')->success(100001);
  112. }
  113. /**
  114. * 删除指定资源
  115. * @param int $id
  116. * @return \think\Response
  117. */
  118. public function delete($id)
  119. {
  120. $this->service->del($id);
  121. return app('json')->success(100002);
  122. }
  123. }