Files.php 4.9 KB

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