From 41075e2d34770a9ab60d7f543b2e772183437a5d Mon Sep 17 00:00:00 2001 From: Karson <karsonzhang@163.com> Date: Wed, 3 May 2017 23:39:12 +0800 Subject: [PATCH] 新增上传到本机时文件名规则和大小控制 修复无法编辑管理员的BUG 添加批量上传参数配置 --- application/admin/controller/Ajax.php | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- application/admin/controller/auth/Admin.php | 2 +- application/common/model/Configvalue.php | 2 ++ public/assets/js/require-upload.js | 24 ++++++++++++++---------- 4 files changed, 69 insertions(+), 15 deletions(-) diff --git a/application/admin/controller/Ajax.php b/application/admin/controller/Ajax.php index 2933540..0ccfe78 100644 --- a/application/admin/controller/Ajax.php +++ b/application/admin/controller/Ajax.php @@ -4,7 +4,9 @@ namespace app\admin\controller; use app\common\controller\Backend; use fast\Http; +use fast\Random; use fast\Tree; +use think\Config; use think\Db; use think\Lang; @@ -181,12 +183,58 @@ class Ajax extends Backend { $this->code = -1; $file = $this->request->file('file'); - $uploaddir = '/public/uploads/'; - $info = $file->move(ROOT_PATH . $uploaddir); - if ($info) + $upload = Config::get('upload'); + + preg_match('/(\d+)(\w+)/', $upload['maxsize'], $matches); + $type = strtolower($matches[2]); + $typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3]; + $size = (int) $upload['maxsize'] * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0); + $fileInfo = $file->getInfo(); + $suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION)); + $suffix = $suffix ? $suffix : 'file'; + $replaceArr = [ + '{year}' => date("Y"), + '{mon}' => date("m"), + '{day}' => date("d"), + '{hour}' => date("H"), + '{min}' => date("i"), + '{sec}' => date("s"), + '{random}' => Random::alnum(16), + '{random32}' => Random::alnum(32), + '{filename}' => $suffix ? substr($fileInfo['name'], 0, strripos($fileInfo['name'], '.')) : $fileInfo['name'], + '{suffix}' => $suffix, + '{.suffix}' => $suffix ? '.' . $suffix : '', + '{filemd5}' => md5_file($fileInfo['tmp_name']), + ]; + $savekey = $upload['savekey']; + $savekey = str_replace(array_keys($replaceArr), array_values($replaceArr), $savekey); + + $uploadDir = substr($savekey, 0, strripos($savekey, '/') + 1); + $fileName = substr($savekey, strripos($savekey, '/') + 1); + // + $splInfo = $file->validate(['size' => $size])->move(ROOT_PATH . '/public' . $uploadDir, $fileName); + if ($splInfo) { + $imagewidth = $imageheight = 0; + if (in_array($suffix, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf'])) + { + $imgInfo = getimagesize($splInfo->getPathname()); + $imagewidth = isset($imgInfo[0]) ? $imgInfo[0] : $imagewidth; + $imageheight = isset($imgInfo[1]) ? $imgInfo[1] : $imageheight; + } + $params = array( + 'filesize' => $fileInfo['size'], + 'imagewidth' => $imagewidth, + 'imageheight' => $imageheight, + 'imagetype' => $suffix, + 'imageframes' => 0, + 'mimetype' => $fileInfo['type'], + 'url' => $uploadDir . $splInfo->getSaveName(), + 'uploadtime' => time() + ); + model("attachment")->create(array_filter($params)); $this->code = 200; - $this->data = $uploaddir . $info->getSaveName(); + $this->data = $uploadDir . $splInfo->getSaveName(); } else { diff --git a/application/admin/controller/auth/Admin.php b/application/admin/controller/auth/Admin.php index ac1133b..f6638bf 100644 --- a/application/admin/controller/auth/Admin.php +++ b/application/admin/controller/auth/Admin.php @@ -96,7 +96,7 @@ class Admin extends Backend { if ($params['password']) { - $params['salt'] = Random::basic(4); + $params['salt'] = Random::alnum(); $params['password'] = md5(md5($params['password']) . $params['salt']); } $row->save($params); diff --git a/application/common/model/Configvalue.php b/application/common/model/Configvalue.php index 295868a..dfbbec6 100644 --- a/application/common/model/Configvalue.php +++ b/application/common/model/Configvalue.php @@ -32,6 +32,7 @@ class Configvalue extends Model $uploadcfg = $uploadcfg ? $uploadcfg : []; $uploadcfg = array_merge($uploadcfg, $params); $uploadcfg['bucket'] = isset($uploadcfg['bucket']) ? $uploadcfg['bucket'] : ''; + $multiple = isset($uploadcfg['multiple']) ? $uploadcfg['multiple'] : false; $savekey = isset($uploadcfg['savekey']) ? $uploadcfg['savekey'] : ''; $uploadcfg['save-key'] = isset($uploadcfg['save-key']) ? $uploadcfg['save-key'] : $savekey; $expiration = time() + (isset($uploadcfg['expire']) ? $uploadcfg['expire'] : 600); @@ -68,6 +69,7 @@ class Configvalue extends Model 'maxsize' => isset($uploadcfg['maxsize']) ? $uploadcfg['maxsize'] : '', 'mimetype' => isset($uploadcfg['mimetype']) ? $uploadcfg['mimetype'] : '', 'multipart' => $multipart, + 'multiple' => $multiple, ]; } diff --git a/public/assets/js/require-upload.js b/public/assets/js/require-upload.js index c1d45b7..e2571ea 100755 --- a/public/assets/js/require-upload.js +++ b/public/assets/js/require-upload.js @@ -15,6 +15,7 @@ define(['jquery', 'bootstrap', 'backend', 'config', 'plupload'], function ($, un var maxsize = $(this).data("maxsize"); var mimetype = $(this).data("mimetype"); var multipart = $(this).data("multipart"); + var multiple = $(this).data("multiple"); //上传URL url = url ? url : Config.upload.uploadurl; //最大可上传 @@ -23,10 +24,13 @@ define(['jquery', 'bootstrap', 'backend', 'config', 'plupload'], function ($, un mimetype = mimetype ? mimetype : Config.upload.mimetype; //请求的表单参数 multipart = multipart ? multipart : Config.upload.multipart; + //是否支持批量上传 + multiple = multiple ? multiple : Config.upload.multiple; + //生成Plupload实例 Upload.list[id] = new Plupload.Uploader({ runtimes: 'html5,flash,silverlight,html4', - multi_selection: false, //是否允许多选批量上传 - browse_button: id, // you can pass an id... + multi_selection: multiple, //是否允许多选批量上传 + browse_button: id, // 浏览按钮的ID container: $(this).parent().get(0), //取按钮的上级元素 flash_swf_url: '/assets/libs/plupload/js/Moxie.swf', silverlight_xap_url: '/assets/libs/plupload/js/Moxie.xap', @@ -63,19 +67,19 @@ define(['jquery', 'bootstrap', 'backend', 'config', 'plupload'], function ($, un //document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML += (' [Url]: ' + '<a href="' + url + '" target="_blank">' + url + '</a>'); //这里建议不修改 try { - var data = JSON.parse(info.response); - if (data.hasOwnProperty('code')) { - data.code = data.code == 200 ? 0 : data.code; - if (data.hasOwnProperty("url")) { - data.content = data.url; + var response = JSON.parse(info.response); + if (response.hasOwnProperty('code')) { + response.code = response.code == 200 ? 1 : response.code; + if (response.hasOwnProperty("url")) { + response.data = response.url; } - $("input[data-plupload-id='" + id + "-text']").val(data.content); + $("input[data-plupload-id='" + id + "-text']").val(response.data); var afterUpload = $("#" + id).data("after-upload"); if (afterUpload && typeof Upload.api.custom[afterUpload] == 'function') { - Upload.api.custom[afterUpload].call(info, id, data); + Upload.api.custom[afterUpload].call(info, id, response); } if (typeof onAfterUpload == 'function') { - onAfterUpload.call(info, id, data); + onAfterUpload.call(info, id, response); } } else { Toastr.error(e.message + "(code:-2)"); -- libgit2 0.24.0