SystemAttachmentCategory.php 3.8 KB

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