Annex.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace app\admin\controller\system;
  3. use app\common\controller\AdminController;
  4. use app\http\IResponse;
  5. use Lettered\Support\Exceptions\FailedException;
  6. use Lettered\Support\File;
  7. use Lettered\Support\Upload;
  8. class Annex extends AdminController
  9. {
  10. /**
  11. *
  12. * @author 许祖兴 < zuxing.xu@lettered.cn>
  13. * @date 2020/3/19 21:09
  14. *
  15. * @return \think\response\Json
  16. * @throws FailedException
  17. */
  18. public function index()
  19. {
  20. // 当前路径
  21. $path = input('dir', '/');
  22. $page = input('page', 1);
  23. $limit = input('limit', 10);
  24. // 实列
  25. $filesystem = File::init('uploads' . $path);
  26. // 数据
  27. $annex = [];
  28. // 取出文件夹
  29. $dirs = [];
  30. foreach ($filesystem->getDirs() as $dir) {
  31. $dirs[] = [
  32. 'name' => $dir,
  33. 'type' => 'dir',
  34. 'isDir' => true,
  35. 'create_at' => time() + 99
  36. // 'create_at' => filectime(app()->getRootPath() . '/public/uploads/' . rawurlencode($dir))
  37. ];
  38. }
  39. // halt($dirs);
  40. // 取出本地存储文件
  41. foreach ($filesystem->getFiles() as $file) {
  42. $type = get_file_ext($file);
  43. $info = [
  44. 'name' => $file,
  45. 'type' => $type,
  46. 'isDir' => false,
  47. 'url' => get_annex_url($path . '/' . $file),
  48. 'create_at' => filectime(app()->getRootPath() . "/public/uploads/" . $path . '/' . $file)
  49. ];
  50. if (in_array($type, ['png', 'jpg', 'jpeg', 'gif'])) {
  51. $info['thumbs'] = get_annex_url($path . '/' . $file);
  52. }
  53. $annex[] = $info;
  54. }
  55. $total = count($annex);
  56. $array = [];
  57. // 分页处理
  58. for ($j = $limit * ($page -1); $j < ($limit * $page ) && $j < $total;++$j){//循环条件控制显示图片张数
  59. $array[] = $annex[$j];
  60. }
  61. // 获取数据库存储
  62. $model = model('common/SystemAnnex')->where(['path' => input('dir', '')])
  63. ->limit((($page - 1) * $limit) . "," . $limit);
  64. // 取出文件
  65. foreach ($model->order(['created_at' => 'desc'])->select() as $file) {
  66. $type = get_file_ext($file['url']);
  67. $info = [
  68. 'name' => $file['url'],
  69. 'type' => $type,
  70. 'isDir' => false,
  71. 'url' => get_annex_url($file['path'] . '/' . $file['url']),
  72. 'create_at' => $file['created_at']
  73. ];
  74. if (in_array($type, ['png', 'jpg', 'jpeg', 'gif'])) {
  75. $info['thumbs'] = get_annex_url($file['path'] . '/' . $file['url'],$file['driver']);
  76. }
  77. $array[] = $info;
  78. }
  79. $array = array_values(array_unique($array, SORT_REGULAR ));
  80. array_multisort(array_column($array,'create_at'),SORT_DESC,$array);
  81. $array = array_merge($dirs, $array);
  82. return IResponse::success([
  83. 'count' => $total,
  84. 'list' => $array
  85. ]);
  86. }
  87. /**
  88. *
  89. * @author 许祖兴 < zuxing.xu@lettered.cn>
  90. * @date 2020/3/19 21:09
  91. *
  92. * @return \think\response\Json
  93. * @throws FailedException
  94. */
  95. public function operation()
  96. {
  97. $params = $this->request->param();
  98. // 是否文件夹操作
  99. $folder = (isset($params['folder']) && $params['folder'] == 'true');
  100. // 内置验证
  101. $valid = $this->validate($params,[
  102. //'name|名称' => 'require|regex:([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{1,14}',
  103. 'dir|文件夹' => 'require'
  104. ],[
  105. 'name.regex' => '名称需英文名称以及英文后缀!'
  106. ]);
  107. // 验证失报错
  108. (true !== $valid) && IResponse::failure($valid);
  109. // 文件操作实例
  110. $filesystem = File::init('uploads' . input('dir', '/'));
  111. switch ($this->request->method()) {
  112. case "GET":
  113. $contents = $filesystem->getFile('/' . $params['name']);
  114. return IResponse::success(['name' => $params['name'],'content' => $contents]);
  115. break;
  116. case "POST":
  117. $filesystem->{$folder ? 'mkdir' : 'touch'}('/' . $params['name']);
  118. break;
  119. case "PUT":
  120. // 更新文件内容或者重命名文件夹
  121. if (isset($params['content'])){
  122. // 更新文件内容
  123. $filesystem->changeFile('/' . $params['name'], $params['content']);
  124. }else{
  125. // 重命名
  126. $filesystem->renameFile('/' . $params['name'],'/' .$params['change']);
  127. }
  128. return IResponse::success();
  129. break;
  130. case "DELETE":
  131. //todo 删除文件夹 删除操作应该是转移目录
  132. if ($folder) {
  133. // 初始化到删除目录
  134. File::init('uploads' . input('dir', '') . '/' . $params['name'] )->moveAll();
  135. } else {
  136. // 删除数据库内容
  137. $file = model('common/SystemAnnex')->getBy(['path' => input('dir', '')]);
  138. if ($file && $file->driver == 'qiniu') {
  139. // 七牛云的删除
  140. } else {
  141. // 本地删除
  142. $file && $file->delete();
  143. $filesystem->removeFile('/' . $params['name']);
  144. }
  145. }
  146. break;
  147. default:
  148. return IResponse::failure('错误的操作!');
  149. }
  150. return IResponse::success([], '操作成功!');
  151. }
  152. /**
  153. * 普通附件上传
  154. *
  155. * @author 许祖兴 < zuxing.xu@lettered.cn>
  156. * @date 2020/3/19 21:09
  157. *
  158. * @param $name
  159. *
  160. * @return \think\response\Json
  161. */
  162. public function upload()
  163. {
  164. $upload = new Upload(config('upload.'));
  165. $upload->setPath(input('dir','/'))->upload($this->request->file('file'));
  166. return IResponse::success([],'上传成功');
  167. }
  168. /**
  169. * ck编辑器附件上传
  170. *
  171. * @author 许祖兴 < zuxing.xu@lettered.cn>
  172. * @date 2020/6/8 21:05
  173. *
  174. * @return \think\response\Json
  175. */
  176. public function ckeditor()
  177. {
  178. $file = $this->request->file('upload');
  179. $info = $file->move( 'uploads/ckeditor' ,'');
  180. return json([
  181. 'fileName' => $info->getSaveName(),
  182. 'uploaded' => 1,
  183. 'url' => 'http://'.$_SERVER['SERVER_NAME'] . '/uploads/ckeditor/'. $info->getSaveName()
  184. ]);
  185. // return IResponse::success([
  186. // 'name' => $info->getSaveName()
  187. // ]);
  188. }
  189. }