Library.php 3.0 KB

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