SystemAttachmentCategory.php 3.5 KB

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