huwhois 2 سال پیش
والد
کامیت
b0b38ca2b7

+ 86 - 0
app/common/command/FileConsole.php

@@ -0,0 +1,86 @@
+<?php
+declare (strict_types = 1);
+
+namespace app\common\command;
+
+use DirectoryIterator;
+use SplFileInfo;
+
+// 引入框架内置类
+use think\console\Command;
+use think\console\Input;
+use think\console\input\Argument;
+use think\console\input\Option;
+use think\console\Output;
+use think\facade\Config;
+use think\File;
+
+use app\common\model\FileManager;
+
+class FileConsole extends Command
+{
+    protected function configure()
+    {
+        // 指令配置
+        $this->setName('fileconsole')
+            ->addArgument('name', Argument::OPTIONAL, "console name")
+            ->addOption('dir', null, Option::VALUE_REQUIRED, 'directory name')
+            ->setDescription('the fileconsole command');
+    }
+
+    protected function execute(Input $input, Output $output)
+    {
+        $name = trim($input->getArgument('name'));
+      	$name = $name ?: 'thinkphp';
+
+		if ($input->hasOption('dir')) {
+        	$dir = str_replace("\\", '/', app()->getRootPath() . $input->getOption('dir'));
+        } else {
+        	$dir = str_replace("\\", '/', Config::get('filesystem.disks.public.root')) .'/';
+        }
+
+        // 指令输出
+        $output->writeln('fileconsole :' . $name . "--dir: " . $dir);
+        
+        $this->$name($dir);
+
+        $output->writeln('..ok');
+    }
+
+    /**
+     * 建立文件管理目录索引(递归遍历目录)
+     */
+    public function scan($dir)
+    {
+        if (is_file($dir)) {
+            $file = new File($dir);
+
+            FileManager::saveFileInfo($file);
+        } else {
+            $dirs = new DirectoryIterator($dir);
+            
+            foreach ($dirs as $fileInfo) {
+                if($fileInfo->isDot() || $fileInfo->getFilename() == '.gitignore') {
+                    continue;
+                }
+                
+                $this->scan($dirs->getPath() . '/' . $fileInfo->getFilename());
+            }
+        }
+    }
+
+    /**
+     * 清除无效目录索引
+     */
+    public function clear($dir = '')
+    {
+        $list = FileManager::select();
+        
+        foreach ($list as $value) {
+            $file = app()->getRootPath() . 'public/' . $value->filepath;
+            if (!is_file($file)) {
+                FileManager::destroy($value->fileid);
+            }
+        }
+    }
+}

+ 64 - 0
app/common/model/FileManager.php

@@ -0,0 +1,64 @@
+<?php
+declare(strict_types=1);
+
+namespace app\common\model;
+
+use think\facade\Config;
+
+class FileManager extends \think\Model
+{
+    protected $pk = "fileid";
+
+    protected $schema = [
+        'fileid'        => "int",      // '文件id'
+        'filename'      => "varchar",  // '文件名称'
+        'filesize'      => "int",      // '文件大小字节'
+        'filetime'      => "int",      // '文件上传时间'
+        'filepath'      => "varchar",  // '文件路径'
+        'fileextension' => "varchar",  // '文件扩展名'
+        'title'         => "varchar",  // '文件title'
+        'type'          => "int",      // '0为附件,1为图片,2为Flash文件,3为多媒体文件'
+        'onclick'       => "int",      // '下载量'
+        'username'      => "varchar",  // '上传者'
+        'cid'           => "int",      // '栏目ID'
+        'infoid'        => "int",      // '信息ID', 
+        'cjid'          => "int",       // '信息临时ID'
+        'hash_md5'       => "varcahr"       // hash值(MD5)
+    ];
+
+    protected $autoWriteTimestamp = false;
+
+    public static function queryPage(array $param = [])
+    {
+        $limit = (int)$param['limit'];
+
+        $where = [];
+
+        return self::where($where)->field('fileid,filename,filesize,filetime,filepath,fileextension,title,username')->paginate(['list_rows'=>$limit, 'query' => $param]);
+    }
+
+    public static function saveFileInfo(\think\File $file)
+    {
+        $fileinfo = self::where('hash_md5', $file->md5())->find();
+
+        $publicRootPath = str_replace('\\', '/', Config::get('filesystem.disks.public.root'));
+
+        $publicUrlPath =  Config::get('filesystem.disks.public.url');
+
+        if ($fileinfo == null) {
+            $fileinfo = new static();
+        } elseif ($publicRootPath . $fileinfo->filepath == $file->getPathname())  { // 路径不同的文件
+            $fileinfo = new static();
+        }
+
+        $fileinfo->filename       = $file->getFilename();
+        $fileinfo->filesize       = $file->getSize();
+        $fileinfo->filetime       = $file->getCTime();
+        $fileinfo->filepath       = $publicUrlPath . str_replace($publicRootPath, '', $file->getPathname());
+        $fileinfo->fileextension  = $file->getExtension();
+        $fileinfo->username       = 'system';
+        $fileinfo->hash_md5       = $file->md5();
+
+        $fileinfo->save();
+    }
+}

+ 0 - 1
app/common/service/FileService.php

@@ -12,7 +12,6 @@ declare(strict_types=1);
 namespace app\common\service;
 
 use think\Image;
-use think\facade\Filesystem;
 use think\Exception;
 use think\File;
 

+ 85 - 54
app/sys/controller/FileManager.php

@@ -1,4 +1,5 @@
 <?php
+declare(strict_types=1);
 
 /**
  * upload文件管理
@@ -10,13 +11,14 @@
 
 namespace app\sys\controller;
 
+use Exception;
 use think\facade\View;
 use think\File;
 use think\Image;
 use think\facade\Config;
 
 use app\common\service\FileService;
-use Exception;
+use app\common\model\FileManager as FileManagerModel;
 
 class FileManager extends Base
 {
@@ -25,59 +27,43 @@ class FileManager extends Base
     protected $height = 300;
 
     /**
-     * 处理上传的图片
+     * 图片列表
      */
-    protected function dealUploadImg(File $file, $water, $thumb, $width, $height, $overwrite)
-    {
-        $savename = "";
-        $thumbname = "";
+    // public function index($mod=0)
+    // {
 
-        if ($water == true || $thumb == true) {
-            $image = Image::open($file);
+    // }
 
-            if ($water) {
-                $type = $this->system->water_type ?: Config::get('filesystem.water.type');
+    public function index()
+    {
+        // $data = $this->explorer();
 
-                if ($type == 'water') {
-                    $watemark = $this->system->watermark ?: Config::get('filesystem.water.watermark');
-                    $image->water($watemark);
-                } else {
-                    $watetext = $this->system->watertext ?: Config::get('filesystem.water.watertext');
-                    $image->text($watetext, Config::get('filesystem.water.waterfont'), 30, '#ffffff30');
-                }
-            }
+        // View::assign('dirs', $data['dirs']);
+        // View::assign('files', $data['files']);
+        // View::assign('counts', $data['counts']);
+        // View::assign('activeurl', $data['activeurl']);
+        // View::assign('activepath', $data['activepath']);
 
-            $savename = $file->hashName();
-            $realPath = Config::get('filesystem.disks.public.root') . '/' . $savename;
+        $param = $this->request->param();
 
-            if ($thumb == true) {
-                if ($overwrite == true) {
-                    $image->thumb($width, $height, 1);
+        $param['limit'] = isset($param['limit']) ? (int) $param['limit'] : Config::get('app.page_size');
 
-                    $image->save($realPath);
-                } else {
-                    $image->save($realPath);
+        $list = FileManagerModel::queryPage($param);
 
-                    $image->thumb($width, $height, 1);
+        View::assign('list', $list);
 
-                    $ext = $file->extension();
+        return View::fetch();
+    }
 
-                    $thumbname = str_replace('.' . $ext, '', str_replace('\\', '/', $savename)) . $this->t_suffix . '.' . $ext;
-                    // halt(Config::get('filesystem.disks.public.root') .'/' . $thumbname);
-                    $image->save(Config::get('filesystem.disks.public.root') . '/' . $thumbname);
-                }
-            } else {
-                $image->save($realPath);
-            }
-            unset($image);
-        } else {
-            $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
-        }
+    public function uploadFile()
+    {
+        $files = $this->request->file('upload_file');
 
-        return [
-            'picname'   => str_replace('\\', '/', $savename),
-            'thumbname' => $thumbname
-        ];
+        $savename = [];
+        
+        foreach($files as $file){
+            $savename[] = \think\facade\Filesystem::disk('public')->putFile('/', $file);
+        }
     }
 
     /**
@@ -257,18 +243,7 @@ class FileManager extends Base
         ]);
     }
 
-    public function index()
-    {
-        $data = $this->explorer();
 
-        View::assign('dirs', $data['dirs']);
-        View::assign('files', $data['files']);
-        View::assign('counts', $data['counts']);
-        View::assign('activeurl', $data['activeurl']);
-        View::assign('activepath', $data['activepath']);
-
-        return View::fetch();
-    }
 
     /**
      * 获取当前文件相关信息
@@ -539,6 +514,62 @@ class FileManager extends Base
     //     }
     // }
 
+        /**
+     * 处理上传的图片
+     */
+    protected function dealUploadImg(File $file, $water, $thumb, $width, $height, $overwrite)
+    {
+        $savename = "";
+        $thumbname = "";
+
+        if ($water == true || $thumb == true) {
+            $image = Image::open($file);
+
+            if ($water) {
+                $type = $this->system->water_type ?: Config::get('filesystem.water.type');
+
+                if ($type == 'water') {
+                    $watemark = $this->system->watermark ?: Config::get('filesystem.water.watermark');
+                    $image->water($watemark);
+                } else {
+                    $watetext = $this->system->watertext ?: Config::get('filesystem.water.watertext');
+                    $image->text($watetext, Config::get('filesystem.water.waterfont'), 30, '#ffffff30');
+                }
+            }
+
+            $savename = $file->hashName();
+            $realPath = Config::get('filesystem.disks.public.root') . '/' . $savename;
+
+            if ($thumb == true) {
+                if ($overwrite == true) {
+                    $image->thumb($width, $height, 1);
+
+                    $image->save($realPath);
+                } else {
+                    $image->save($realPath);
+
+                    $image->thumb($width, $height, 1);
+
+                    $ext = $file->extension();
+
+                    $thumbname = str_replace('.' . $ext, '', str_replace('\\', '/', $savename)) . $this->t_suffix . '.' . $ext;
+                    // halt(Config::get('filesystem.disks.public.root') .'/' . $thumbname);
+                    $image->save(Config::get('filesystem.disks.public.root') . '/' . $thumbname);
+                }
+            } else {
+                $image->save($realPath);
+            }
+            unset($image);
+        } else {
+            $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
+        }
+
+        return [
+            'picname'   => str_replace('\\', '/', $savename),
+            'thumbname' => $thumbname
+        ];
+    }
+
     /**
      * ckeditor 富文本编辑器上传图片
      */

+ 1 - 0
config/console.php

@@ -5,5 +5,6 @@
 return [
     // 指令定义
     'commands' => [
+        'fileconsole' => 'app\common\command\FileConsole',
     ],
 ];

+ 1 - 1
view/sys/advertise/save.html

@@ -136,7 +136,7 @@
                 dataType: 'json',
                 success: function (res) {
                     // console.log(res);
-                    if (res.code = 0) {
+                    if (res.code == 0) {
                         layer.msg(data.msg, {
                             icon: 5,
                             time: 1000

+ 131 - 0
view/sys/file_manager/index.html

@@ -0,0 +1,131 @@
+<style>
+.progress {
+    width: 600px;
+    height: 10px;
+    border: 1px solid #ccc;
+    border-radius: 10px;
+    margin: 10px 0px;
+    overflow: hidden;
+}
+
+/* 初始状态设置进度条宽度为0px */
+.progress>div {
+    width: 0px;
+    height: 100%;
+    background-color: yellowgreen;
+    transition: all .3s ease;
+}
+</style>
+<article class="cl pd-10">
+    <div class="cl pd-5 bg-1 bk-gray mt-20">
+        <span class="l">
+            <a class="btn radius btn-secondary"><i class="Hui-iconfont Hui-iconfont-jifen"></i>数据库模式</a>
+            <a class="btn radius btn-default" href="{:url('explorer')}"><i class="Hui-iconfont Hui-iconfont-pages"></i> 目录模式 </a>
+            <a class="btn btn-primary radius" onclick="uploadFile();"><i class="Hui-iconfont Hui-iconfont-add"></i>上传附件</a>
+            <input type="file" id="upload_file" hidden multiple onchange ="fileChange()">
+            <div class="progress">
+                <div></div>
+            </div>
+        </span>
+        <span class="r">共有数据:<strong id="total">{$list->total()}</strong>条</span>
+    </div>
+
+    <div class="cl mt-20">
+        <table class="table table-border table-bordered table-bg">
+            <thead>
+                <tr class="text-c">
+                    <th width="25px;">
+                        <input type="checkbox" name="allcheck" value="">
+                    </th>
+                    <th width="60px;">ID</th>
+                    <th width="120px;">文件title</th>
+                    <th style="min-width: 260px;">文件名</th>
+                    <th>文件类型</th>
+                    <th>文件大小</th>
+                    <th>增加者</th>
+                    <th>增加时间</th>
+                    <th width="100px;">操作</th>
+                </tr>
+            </thead>
+            <tbody>
+                {foreach $list as $val}
+                <tr class="text-c">
+                    <td>
+                        <input type="checkbox" value="{$val.fileid}" name="checkbox[]">
+                    </td>
+                    <td>{$val.fileid}</td>
+                    <td class="text-l">{$val.title}</td>
+                    <td class="text-l">
+                        <a href="{$val.filepath}" title="{$val.filepath}" target="_blank">{$val.filename}</a>
+                    </td>
+                    <td>{$val.fileextension}</td>
+                    <td>{$val.filesize|format_bytes}</td>
+                    <td>{$val.username}</td>
+                    <td>{$val.filetime|date="Y-m-d"}</td>
+                    <td><a class="btn btn-danger radius" title="删除" onClick="del(this,'{$val.fileid}')"><i class="Hui-iconfont Hui-iconfont-del2"></i></a></td>
+                </tr>
+                {/foreach}
+            </tbody>
+        </table>
+    </div>
+</article>
+
+<script>
+    //通过点击图片来触发文件上传按钮
+    function uploadFile(params) {
+        $("#upload_file").click();
+    }
+
+    // 上传处理
+    function fileChange() {
+        let data = $("#upload_file").get(0).files;
+        console.log(data);
+        if (data == undefined || data == null) {
+            layer.msg("请选择视频文件", { icon: 5, time: 1000 });
+            return false;
+        }
+
+        let formData = new FormData();
+        formData.append("upload_file", data);
+        $.ajax({
+            url: '{:url("file_manager/uploadFile")}',
+            type: 'post',
+            dataType: 'json',
+            data: formData,
+            processData: false,
+            contentType: false,
+            xhr: function () {
+                var xhr = new XMLHttpRequest();
+                //使用XMLHttpRequest.upload监听上传过程,注册progress事件,打印回调函数中的event事件
+                xhr.upload.addEventListener('progress', function (e) {
+                    console.log(e);
+                    //loaded代表上传了多少
+                    //total代表总数为多少
+                    var progressRate = (e.loaded / e.total) * 100 + '%';
+
+                    //通过设置进度条的宽度达到效果
+                    $('.progress > div').css('width', progressRate);
+                })
+
+                return xhr;
+            },
+            success: function (res) {
+                console.log(res);
+                if (res.code == 1) {
+                    layer.msg(data.msg, {
+                            icon: 5,
+                            time: 1000
+                        });
+                    window.location.reload();
+                } else {
+                    layer.msg(res.msg, {
+                        icon: 5,
+                        time: 1000
+                    });
+                    return false;
+                }
+            }
+        });
+    }
+
+</script>

+ 0 - 2
view/sys/file_manager/uploadimg.html

@@ -244,8 +244,6 @@
     </div>
 </div>
 
-
-
 <!--请在下方写此页面业务相关的脚本-->
 <script type="text/javascript">
     jQuery.Huitab = function (tabBar, tabCon, class_name, tabEvent, i) {