Library.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\store\controller\upload;
  10. use app\store\controller\Controller;
  11. use app\store\model\UploadFile as UploadFileModel;
  12. use app\store\model\UploadGroup as UploadGroupModel;
  13. class Library extends Controller
  14. {
  15. /**
  16. * 文件库列表
  17. * @param string $type
  18. * @param int $group_id
  19. * @return mixed
  20. * @throws \think\db\exception\DataNotFoundException
  21. * @throws \think\db\exception\ModelNotFoundException
  22. * @throws \think\exception\DbException
  23. */
  24. public function fileList($type = 'image', $group_id = -1)
  25. {
  26. // 分组列表
  27. $group_list = (new UploadGroupModel)->getList($type);
  28. // 文件列表
  29. $file_list = (new UploadFileModel)->getlist(intval($group_id), $type, 0);
  30. return $this->renderSuccess('success', '', compact('group_list', 'file_list'));
  31. }
  32. /**
  33. * 新增分组
  34. * @param $group_name
  35. * @param string $group_type
  36. * @return array
  37. */
  38. public function addGroup($group_name, $group_type = 'image')
  39. {
  40. $model = new UploadGroupModel;
  41. if ($model->add(compact('group_name', 'group_type'))) {
  42. $group_id = $model->getLastInsID();
  43. return $this->renderSuccess('添加成功', '', compact('group_id', 'group_name'));
  44. }
  45. return $this->renderError($model->getError() ?: '添加失败');
  46. }
  47. /**
  48. * 编辑分组
  49. * @param $group_id
  50. * @param $group_name
  51. * @return array
  52. * @throws \think\exception\DbException
  53. */
  54. public function editGroup($group_id, $group_name)
  55. {
  56. $model = UploadGroupModel::detail($group_id);
  57. if ($model->edit(compact('group_name'))) {
  58. return $this->renderSuccess('修改成功');
  59. }
  60. return $this->renderError($model->getError() ?: '修改失败');
  61. }
  62. /**
  63. * 删除分组
  64. * @param $group_id
  65. * @return array
  66. * @throws \think\exception\DbException
  67. */
  68. public function deleteGroup($group_id)
  69. {
  70. $model = UploadGroupModel::detail($group_id);
  71. if ($model->remove()) {
  72. return $this->renderSuccess('删除成功');
  73. }
  74. return $this->renderError($model->getError() ?: '删除失败');
  75. }
  76. /**
  77. * 批量删除文件
  78. * @param $fileIds
  79. * @return array
  80. */
  81. public function deleteFiles($fileIds)
  82. {
  83. $model = new UploadFileModel;
  84. if ($model->softDelete($fileIds)) {
  85. return $this->renderSuccess('删除成功');
  86. }
  87. return $this->renderError($model->getError() ?: '删除失败');
  88. }
  89. /**
  90. * 批量移动文件分组
  91. * @param $group_id
  92. * @param $fileIds
  93. * @return array
  94. */
  95. public function moveFiles($group_id, $fileIds)
  96. {
  97. $model = new UploadFileModel;
  98. if ($model->moveGroup($group_id, $fileIds) !== false) {
  99. return $this->renderSuccess('移动成功');
  100. }
  101. return $this->renderError($model->getError() ?: '移动失败');
  102. }
  103. }