From 761e208d00dfdeb4109d997265156e85b96f4305 Mon Sep 17 00:00:00 2001 From: Anyon Date: Thu, 4 Jun 2020 10:06:41 +0800 Subject: [PATCH] Update upload.js --- app/admin/view/api/upload.js | 51 ++++++++++++++---------------------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/app/admin/view/api/upload.js b/app/admin/view/api/upload.js index 598bcdfed..ef9659a72 100644 --- a/app/admin/view/api/upload.js +++ b/app/admin/view/api/upload.js @@ -1,28 +1,24 @@ define(['md5'], function (SparkMD5, allowExtsMimes) { allowExtsMimes = JSON.parse('{$exts|raw}'); return function (element, UploadedHandler, options) { - /*! 定义初始化变量 */ + /*! 初始化变量 */ options = {element: $(element), exts: [], mimes: [], files: {}, cache: {}, loading: 0}; - options.count = {total: 0, uploaded: 0}; - options.type = options.element.data('type') || ''; - options.safe = options.element.data('safe') ? 1 : 0; - options.types = options.type ? options.type.split(',') : []; - options.field = options.element.data('field') || 'file'; - options.input = $('[name="_field_"]'.replace('_field_', options.field)); - options.uptype = options.safe ? 'local' : options.element.attr('data-uptype') || ''; - options.multiple = options.element.attr('data-multiple') > 0; + options.count = {total: 0, uploaded: 0}, options.type = options.element.data('type') || ''; + options.safe = options.element.data('safe') ? 1 : 0, options.types = options.type ? options.type.split(',') : []; + options.field = options.element.data('field') || 'file', options.input = $('[name="_field_"]'.replace('_field_', options.field)); + options.uptype = options.safe ? 'local' : options.element.attr('data-uptype') || '', options.multiple = options.element.attr('data-multiple') > 0; /*! 文件的选择筛选 */ - for (var index in options.types) if (allowExtsMimes[options.types[index]]) { - options.exts.push(options.types[index]), options.mimes.push(allowExtsMimes[options.types[index]]); + for (var i in options.types) if (allowExtsMimes[options.types[i]]) { + options.exts.push(options.types[i]), options.mimes.push(allowExtsMimes[options.types[i]]); } /*! 初始化上传组件 */ options.uploader = layui.upload.render({ - auto: false, multiple: options.multiple, accept: 'file', elem: element, + auto: false, elem: element, accept: 'file', multiple: options.multiple, exts: options.exts.join('|'), acceptMime: options.mimes.join(','), choose: function (obj) { - for (var index in options.files = obj.pushFile()) { + for (var i in (options.files = obj.pushFile())) { options.loading = $.msg.loading('上传进度 0%'); - options.count.total++, options.files[index].index = index, options.cache[index] = options.files[index], delete options.files[index]; - md5file(options.cache[index]).then(function (file) { + options.count.total++, options.files[i].index = i, options.cache[i] = options.files[i], delete options.files[i]; + md5file(options.cache[i]).then(function (file) { jQuery.ajax("{:url('admin/api.upload/state')}", { data: {xkey: file.xkey, uptype: options.uptype, safe: options.safe, name: file.name}, method: 'post', success: function (ret) { file.xurl = ret.data.url; @@ -52,21 +48,16 @@ define(['md5'], function (SparkMD5, allowExtsMimes) { } }, progress: function (n) { $('[data-upload-progress]').html(n + '%'); - }, done: function (ret, index, file) { - if (++options.count.uploaded >= options.count.total) { - layer.close(options.loading); - } - if (typeof ret.code === 'number' && parseInt(ret.code) === 0) { - return $.msg.tips(ret.info || '文件上传失败!'); - } - file = options.cache[index]; - if (typeof file.xurl !== 'string') return $.msg.tips('无效的文件对象!'); - if (typeof ret.uploaded === 'undefined' && typeof file.xurl === 'string') { - ret = {uploaded: true, url: file.xurl}; + }, done: function (ret, index) { + if (++options.count.uploaded >= options.count.total) layer.close(options.loading); + if (typeof ret.code === 'number' && parseInt(ret.code) === 0) return $.msg.tips(ret.info || '文件上传失败!'); + if (typeof options.cache[index].xurl !== 'string') return $.msg.tips('无效的文件对象!'); + if (typeof ret.uploaded === 'undefined' && typeof options.cache[index].xurl === 'string') { + ret = {uploaded: true, url: options.cache[index].xurl}; } if (ret.uploaded) { if (typeof UploadedHandler === 'function') { - UploadedHandler.call(options.element, ret.url, file); + UploadedHandler.call(options.element, ret.url, options.cache[index]); } else { options.input.val(ret.url).trigger('change'); } @@ -99,10 +90,8 @@ define(['md5'], function (SparkMD5, allowExtsMimes) { return jQuery.when(loadNextChunk(file)); function setFileXdata(file, xmd5) { - file.xmd5 = xmd5; - file.xkey = file.xmd5.substr(0, 2) + '/' + file.xmd5.substr(2, 30) + '.' + file.xext; - delete file.chunk_idx, delete file.chunk_size, delete file.chunk_total; - return file; + file.xmd5 = xmd5, file.xkey = file.xmd5.substr(0, 2) + '/' + file.xmd5.substr(2, 30) + '.' + file.xext; + return delete file.chunk_idx, delete file.chunk_size, delete file.chunk_total, file; } function loadNextChunk(file) {