Files.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\store\controller;
  13. use think\response\Json;
  14. use cores\library\Version;
  15. use app\store\model\UploadFile as UploadFileModel;
  16. /**
  17. * 文件库管理
  18. * Class Files
  19. * @package app\store\controller
  20. */
  21. class Files extends Controller
  22. {
  23. /**
  24. * 文件列表
  25. * @return Json
  26. * @throws \cores\exception\BaseException
  27. * @throws \think\db\exception\DbException
  28. */
  29. public function list(): Json
  30. {
  31. $this->env();
  32. $model = new UploadFileModel;
  33. $list = $model->getList($this->request->param());
  34. return $this->renderSuccess(compact('list'));
  35. }
  36. /**
  37. * 编辑文件
  38. * @param int $fileId
  39. * @return Json
  40. */
  41. public function edit(int $fileId): Json
  42. {
  43. // 文件详情
  44. $model = UploadFileModel::detail($fileId);
  45. // 更新记录
  46. if ($model->edit($this->postForm())) {
  47. return $this->renderSuccess('更新成功');
  48. }
  49. return $this->renderError($model->getError() ?: '更新失败');
  50. }
  51. /**
  52. * 删除文件(批量)
  53. * @param array $fileIds 文件id集
  54. * @return Json
  55. * @throws \think\Exception
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\DbException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. */
  60. public function delete(array $fileIds): Json
  61. {
  62. $model = new UploadFileModel;
  63. if (!$model->setDelete($fileIds)) {
  64. return $this->renderError($model->getError() ?: '操作失败');
  65. }
  66. return $this->renderSuccess('操作成功');
  67. }
  68. /**
  69. * 移动文件到指定分组(批量)
  70. * @param int $groupId
  71. * @param array $fileIds
  72. * @return Json
  73. */
  74. public function moveGroup(int $groupId, array $fileIds): Json
  75. {
  76. $model = new UploadFileModel;
  77. if (!$model->moveGroup($groupId, $fileIds)) {
  78. return $this->renderError($model->getError() ?: '操作失败');
  79. }
  80. return $this->renderSuccess('操作成功');
  81. }
  82. /**
  83. * 临时方法:环境检测并删除废弃的库文件
  84. * 文件:vendor/topthink/framework/src/think/Filesystem.php
  85. * 文件:vendor/topthink/framework/src/think/facade/Filesystem.php
  86. * 文件:vendor/topthink/framework/tests/FilesystemTest.php
  87. * 目录:vendor/topthink/framework/src/think/filesystem
  88. * @return void
  89. * @throws \cores\exception\BaseException
  90. */
  91. private function env()
  92. {
  93. // 判断当前版本小于2.0.7则不执行
  94. if (Version::compare(Version::getVersion(), '2.0.7') === -1) {
  95. return;
  96. }
  97. // 要删除的文件列表
  98. $files = [
  99. 'vendor/topthink/framework/src/think/Filesystem.php',
  100. 'vendor/topthink/framework/src/think/facade/Filesystem.php',
  101. 'vendor/topthink/framework/tests/FilesystemTest.php'
  102. ];
  103. foreach ($files as $file) {
  104. $filePath = root_path() . $file;
  105. file_exists($filePath) && unlink($filePath);
  106. }
  107. // 要删除的目录列表
  108. $folders = ['vendor/topthink/framework/src/think/filesystem/'];
  109. foreach ($folders as $folder) {
  110. $folderPath = root_path() . $folder;
  111. is_dir($folderPath) && $this->deleteFolder($folderPath);
  112. }
  113. }
  114. /**
  115. * 临时方法:递归删除指定目录下所有文件
  116. * @param $path
  117. * @return void
  118. */
  119. private function deleteFolder($path): void
  120. {
  121. if (!is_dir($path)) {
  122. return;
  123. }
  124. // 扫描一个文件夹内的所有文件夹和文件
  125. foreach (scandir($path) as $val) {
  126. // 排除目录中的.和..
  127. if (!in_array($val, ['.', '..', '.gitignore'])) {
  128. // 如果是目录则递归子目录,继续操作
  129. if (is_dir($path . $val)) {
  130. // 子目录中操作删除文件夹和文件
  131. $this->deleteFolder($path . $val . '/');
  132. // 目录清空后删除空文件夹
  133. rmdir($path . $val . '/');
  134. } else {
  135. // 如果是文件直接删除
  136. unlink($path . $val);
  137. }
  138. }
  139. }
  140. }
  141. }