SystemAttachmentCategory.php 3.4 KB

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