ToolController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. *
  4. * @author Mike <m@9026.com>
  5. * @version 1.0
  6. * @date 2015年10月30日
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\Base;
  10. use App\Http\Controllers\Admin\Controller;
  11. use App\Services\Base\Attachment;
  12. use Request;
  13. //use App\Utils\OSS\Alioss;
  14. //use App\Utils\DirUtil;
  15. class ToolController extends Controller
  16. {
  17. private $_dirUtil;
  18. private $_aliossService;
  19. public function __construct() {
  20. if( !$this->_dirUtil ) $this->_dirUtil = new DirUtil();
  21. if( !$this->_aliossService ) $this->_aliossService = new Alioss();
  22. }
  23. /**
  24. * 专题上传
  25. */
  26. function specialupload() {
  27. $attachmentObj = new Attachment();
  28. $uploadControl = $attachmentObj -> initupload([
  29. 'file_types' => 'php|htm|html',
  30. ], [
  31. 'position' => 'special',
  32. 'folder' => 'at',
  33. ]);
  34. //遍历专题目录
  35. $data = array();
  36. $dirList = $this->_dirUtil->dirTraverse(public_path('resources/views/web/special'));
  37. if(!empty($dirList)){
  38. foreach ($dirList AS $key => $val){
  39. $data[$key]['name'] = trim(iconv("GBK", "UTF-8//IGNORE", substr(strrchr($val, '/'), 1, 50))) . '.html';
  40. $data[$key]['url'] = 'http://' . config('sys.sys_www_domain') . '/at/' . $data[$key]['name'];
  41. }
  42. }
  43. view()->share("uploadControl", $uploadControl);
  44. return view('admin.base.tool.specialupload', compact('data'));
  45. }
  46. /**
  47. * 阿里云上传
  48. */
  49. function alimanage()
  50. {
  51. $ok = $this->_aliossService->doesObjectExist("a/bg.jpg");
  52. $img_path = config('sys.sys_images_url');
  53. $file_path = config('sys.sys_file_url');
  54. $folder = trim(Request::input('folder'));
  55. $dirname = trim(Request::input('dirname'));
  56. //创建目录
  57. if(Request::method() == 'POST'){
  58. if($dirname){
  59. $this->_createDir($dirname, $folder);
  60. }else{
  61. $this->showWarning('目录名不能为空!');
  62. }
  63. }
  64. //构建路径
  65. $folderPath = '/<a href="' . U( 'Base/Tool/alimanage') . '?folder=">根目录</a>/';
  66. if($folder){
  67. $dirPath = '';
  68. $dirs = array_filter(explode('/', $folder));
  69. foreach ($dirs AS $dir){
  70. $dirPath .= $dir . '/';
  71. $folderPath .= '<a href="' . U( 'Base/Tool/alimanage') . '?folder=' . $dirPath . '">'. $dir .'</a>/';
  72. }
  73. }
  74. $itemObj = $this->_aliossService->listObjects(['prefix'=>$folder]);
  75. $item['obj'] = $itemObj->getObjectList(); // 文件列表
  76. $item['dir'] = $itemObj->getPrefixList(); // 目录列表
  77. $uploadControl = $this->_uploadControl('');
  78. return view('admin.base.tool.alimanage', compact('item', 'img_path', 'file_path', 'folderPath', 'uploadControl'));
  79. }
  80. private function _createDir($dirname, $folder)
  81. {
  82. if(strpos($dirname, '/') === false && preg_match('/^([0-9a-zA-Z\_]+)$/is', $dirname)){
  83. $newdir = $folder . $dirname;
  84. $code = $this->_aliossService->createObjectDir($newdir);
  85. if($code == null){
  86. $this->showMessage('目录创建成功!', U( 'Base/Tool/alimanage') . '?folder=' . $newdir . '/');
  87. }else{
  88. $this->showWarning('目录创建失败!');
  89. }
  90. }else{
  91. $this->showWarning('非法的目录名!');
  92. }
  93. }
  94. /**
  95. * 初始化文件上传组件
  96. */
  97. private function _uploadControl($folder, $position = 'ali')
  98. {
  99. //初始化文件上传组件
  100. $attachmentObj = new Attachment();
  101. return $attachmentObj -> initupload([
  102. 'file_types' => 'jpg|jpeg|gif|png|bmp',
  103. ], [
  104. 'position' => $position,
  105. 'folder' => $folder,
  106. ]);
  107. }
  108. }