Annex.php 6.9 KB

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