diff --git a/README.md b/README.md index 994fa4b2266edaff0dbf9f66888e8c135cdcdf66..ef55ff76c21c6d69fa7f5e3870eed2f821ff8f9d 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,7 @@ uploadx.render({ | loadType | 请求url属性的request method类型 | String | get | | mainId | 对应的主键名是什么,这参数会影响请求时候,主键的参数名 | String | id | | downId | 对应的主键名是什么,这参数会影响下载时候,主键的参数名 | String | id | +| moduleId | 对应模板是什么,这参数用于页面多个上传组件,比如合同、设计图 | String | id | | **downUrl*** | 下载附件的url | String | - | | **delUrl*** | 删除附件的url | String | - | | successCode | 请求成功码,所有不是此码的请求,都被视失败。 | int | 200 | diff --git a/plugin/uploadx.js b/plugin/uploadx.js index 629b26d10d1b953808ea1e8b12897d2fac0e4387..4c0a8fa66c17645ea4bb8dae42851e78747ad1f1 100644 --- a/plugin/uploadx.js +++ b/plugin/uploadx.js @@ -3,71 +3,61 @@ * * 此插件基于layui.upload模块进行的拓展 */ + +var extConfig = [{ + reg: RegExp('[\\s\\S\\d\\D\\w\u4e00-\u9fa5]\\.(doc|docx)$', 'i'), + type: 'svg', + icon: '' +}, +{ + reg: RegExp('[\\s\\S\\d\\D\\w\u4e00-\u9fa5]\\.(xls|xlsx)$', 'i'), + type: 'svg', + icon: '' +}, +{ + reg: RegExp('[\\s\\S\\d\\D\\w\u4e00-\u9fa5]\\.(ppt|pptx)$', 'i'), + type: 'svg', + icon: '' +}, +{ + reg: RegExp('[\\s\\S\\d\\D\\w\u4e00-\u9fa5]\\.(rar|zip|7z)$', 'i'), + type: 'svg', + icon: '' +}, +{ + reg: RegExp('[\\s\\S\\d\\D\\w\u4e00-\u9fa5]\\.(pdf)$', 'i'), + type: 'svg', + icon: '' +}, +{ + reg: RegExp('[\\s\\S\\d\\D\\w\u4e00-\u9fa5]\\.(avi|mp4|wma|rmvb|rm|flash|3gp|flv)$', 'i'), + type: 'svg', + icon: '' +}, +{ + reg: RegExp('[\\s\\S\\d\\D\\w\u4e00-\u9fa5]\\.(mp3|wav|mid)$', 'i'), + type: 'svg', + icon: '' +}, +{ + reg: RegExp('[\\s\\S\\d\\D\\w\u4e00-\u9fa5]\\.(jpg|png|gif|bmp|jpeg)$', 'i'), + type: 'svg', + icon: '' +}]; layui.define(['jquery', 'util', 'form', 'laytpl', 'slider', 'laypage', 'upload', 'element'], function( exports) { "use strict"; - var MODULE_NAME = 'uploadx', - UN_UPLOAD = "未上传", - UPLOADED = "已上传", - OPEN_LAYER_ID = "LAY_UPLOADX",//弹出框id - $ = layui.jquery, - util = layui.util, - form = layui.form, - laytpl = layui.laytpl, - slider = layui.slider, - upload = layui.upload, - element = layui.element, - page = layui.laypage, - globalCount = 0, - addBtn = null,// 添加按钮 - hitObj = {},//提示多少文件的提示框 - uploadx = {}, - clazz = {}, - mainUpload = {}; + var $ = layui.jquery, + util = layui.util, + form = layui.form, + laytpl = layui.laytpl, + slider = layui.slider, + upload = layui.upload, + element = layui.element, + page = layui.laypage, + MODULE_NAME = 'uploadx'; /*目前支持的所有扩展名的配置*/ - var extConfig = [{ - reg: RegExp('[\\s\\S\\d\\D\\w\u4e00-\u9fa5]\\.(doc|docx)$', 'i'), - type: 'svg', - icon: '' - }, - { - reg: RegExp('[\\s\\S\\d\\D\\w\u4e00-\u9fa5]\\.(xls|xlsx)$', 'i'), - type: 'svg', - icon: '' - }, - { - reg: RegExp('[\\s\\S\\d\\D\\w\u4e00-\u9fa5]\\.(ppt|pptx)$', 'i'), - type: 'svg', - icon: '' - }, - { - reg: RegExp('[\\s\\S\\d\\D\\w\u4e00-\u9fa5]\\.(rar|zip|7z)$', 'i'), - type: 'svg', - icon: '' - }, - { - reg: RegExp('[\\s\\S\\d\\D\\w\u4e00-\u9fa5]\\.(pdf)$', 'i'), - type: 'svg', - icon: '' - }, - { - reg: RegExp('[\\s\\S\\d\\D\\w\u4e00-\u9fa5]\\.(avi|mp4|wma|rmvb|rm|flash|3gp|flv)$', 'i'), - type: 'svg', - icon: '' - }, - { - reg: RegExp('[\\s\\S\\d\\D\\w\u4e00-\u9fa5]\\.(mp3|wav|mid)$', 'i'), - type: 'svg', - icon: '' - }, - { - reg: RegExp('[\\s\\S\\d\\D\\w\u4e00-\u9fa5]\\.(jpg|png|gif|bmp|jpeg)$', 'i'), - type: 'svg', - icon: '' - }, - - ], - HIT={ + var HIT={ MSG1:'此操作将会清空等待上传的文件,你确定要删除吗?', MSG2:'此操作将会删除这个未上传的文件,你确定要删除吗?', MSG3:'此操作将会删除服务器的这个文件,你确定要删除吗?', @@ -80,14 +70,15 @@ layui.define(['jquery', 'util', 'form', 'laytpl', 'slider', 'laypage', 'upload', '
', '', '
', - '', + '', '{{# var finalExt = "*"; }}', '{{# var ext = d.ext; var check=true;}}', '{{# if(d.accept==="file"){ if(ext!=""){finalExt = ext;}check=false; } }}', '{{# if(d.accept==="video"){ finalExt = ext +"avi|mp4|wma|rmvb|rm|flash|3gp|flv";check=false; } }}', '{{# if(d.accept==="audio"){ finalExt = ext +"mp3|wav|mid";check=false; } }}', '{{# if(check){ finalExt = ext +"jpg|png|gif|bmp|jpeg";} }}', - '允许后缀({{=finalExt}})9', + '{{# var tipMsg = ""; if(finalExt!=null && finalExt!="*") { tipMsg = "格式:" + finalExt } }}', + '上传文件9{{=tipMsg}}', '
', '
' ].join(''), @@ -101,7 +92,8 @@ layui.define(['jquery', 'util', 'form', 'laytpl', 'slider', 'laypage', 'upload', '.upload-file-icon{text-align:center;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;}', '.upload-file-text{font-size:12px;text-align:center;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;}', '.upload-file-center{float:left;width:70px}', - '.upload-file-btns{position:relative;left:3px;}', + '.upload-file-center.hasImg:hover{position:relative;left:3px;}', + '.upload-file-btns{position:relative;left:3px;}', '.upload-file-progress{margin-top:5px;}', '.upload-file-loading{cursor:pointer;font-size: 30px;color: #1E9FFF;position: absolute;background-color: white;padding:10px 40px 10px 40px;border-radius: 5px;left: 12px;top: 10%;}', '.upload-file-loading-text{cursor:default;position: absolute;font-size: 12px;left: 43px;top: 85px;}', @@ -110,8 +102,8 @@ layui.define(['jquery', 'util', 'form', 'laytpl', 'slider', 'laypage', 'upload', '' ].join(''), FILE_ITEM = [ - '
', - '
', + '
', + '{{# var hasImg = false; var c = extConfig[extConfig.length-1]; if(c.reg.test(d.name+d.ext)){ hasImg = true; } }}', + `{{# if (hasImg) { }} +
+ {{# } else { }} +
+ {{# } }}`, + //'
', '
{{ d.icon }}
', '
{{ d.name }}
', '
', - '
', - '
', + '
', + '
', '删除', '删除', '重传', @@ -151,19 +149,35 @@ layui.define(['jquery', 'util', 'form', 'laytpl', 'slider', 'laypage', 'upload', ].join(''), ADD_FILE_ITEM = [ '
', - ' ', + ' ', '添加文件', '
', ].join(''), FILE_UPLOAD_PAGE = [ '', '
' - ].join(''), - Uploadx = function(options) { - var that = this; + ].join(''), + + + Uploadx = function (options) { + + var that = this; + + that.UN_UPLOAD = "未上传", + that.UPLOADED = "已上传", + that.OPEN_LAYER_ID = "LAY_UPLOADX",//弹出框id + that.globalCount = 0, + that.addBtn = null,// 添加按钮 + that.hitObj = {},//提示多少文件的提示框 + //that.uploadx = {}, + that.mainUpload = {}; + + that.v = '0.0.1'; that.configx = $.extend({}, that.configx, options.ex); - that.pageCfg = $.extend({}, that.pageCfg, options.page); + that.pageCfg = $.extend({}, that.pageCfg, options.page); + + var temOption = $.extend({}, that.config, options); delete temOption['ex']; delete temOption['page']; @@ -179,7 +193,8 @@ layui.define(['jquery', 'util', 'form', 'laytpl', 'slider', 'laypage', 'upload', url:'https://httpbin.org/post',//用于反显附件信息,会将mainId放入对应的url进行post请求 loadType:'get',//请求类别,可以自定义修改成get或者post form: '', //目标form的filter是什么 - mainId: 'id',//会去目标表单下找对应的mainId的名字是什么。 + mainId: 'id',//会去目标表单下找对应的mainId的名字是什么。 + moduleId:'',//一个页面多个上传功能得话,用于区分文件所属模块 downId: 'id' ,// 点击下载的时候,唯一标识 downUrl:'https://httpbin.org/post', //下载url delUrl:'https://httpbin.org/post', // 删除url @@ -199,8 +214,8 @@ layui.define(['jquery', 'util', 'form', 'laytpl', 'slider', 'laypage', 'upload', layout: ['count', 'prev', 'page', 'next', 'limit'] }, Uploadx.prototype.config = { - elem: '#uploadxBtn', - elemList: $('#uploadxContent'), + elem: '#uploadxBtn', + elemList: $('#uploadxContent'), accept: 'file', ext:'', bindAction: '#startUpload', @@ -224,440 +239,490 @@ layui.define(['jquery', 'util', 'form', 'laytpl', 'slider', 'laypage', 'upload', "name":"undefined.file", "size":0 }; - Uploadx.prototype.getFileInfo = function(name) { - var pointIndex = name.lastIndexOf('.'); - if (pointIndex > 0) { - return { - name: name.substr(0, pointIndex), - ext: name.substr(pointIndex, name.length) - } - } else { - return { - name: name, - ext: '未知扩展名' - } - } - }, - Uploadx.prototype.render = function() { - var that = this; - that.container(); - that.event(); - }, - Uploadx.prototype.match = function(name) { - var result = {}; - for (var o in extConfig) { - var c = extConfig[o]; - if (c.reg.test(name)) { - switch (c.type) { - case 'svg': //一般文件 - result = c.icon; - break; - case 'img': - result = ''; - break; - default: - result = otherFile; - break; - } - break; - } else { - result = otherFile; - } - } - return result; - }, - Uploadx.prototype.checkExist = function(files) { - var that = this, - keys = Object.keys(files), - len = keys.length, - chooseIndex = keys[len - 1], - chooseFile = files[keys[len - 1]], - chooseF = that.getFileInfo(chooseFile.name), - chooseExt = chooseF.ext, - chooseRealName = chooseF.name, - isFault = false; - if (len > 0) { - //如果是第一次选择,那么直接放行,否则,重命名校验 - if (len > 1) { - for (var i = 0, l = len - 2; i <= l; i++) { - //判断名字和扩展名是否都相同 - var curFile = files[keys[i]], - name = curFile.name, - f = that.getFileInfo(name), - ext = f.ext, - realName = f.name; - if (realName === chooseRealName && ext === chooseExt) { - isFault = true; - break; - } - } - } - } - return { - isFault:isFault, - chooseIndex:chooseIndex - }; - }, - Uploadx.prototype.operate = function(id,url,param,callb,type){ - var config = this.configx; - var t = type?type:'post', - formData = ''; - if('get'===t){ - //get 拼接参数 ?name=''& - url+=('?t='+new Date().getTime()); - var paramx = ''; - for(var p in param){paramx+=('&'+p+'='+param[p]);} - url+=paramx; - }else{ - formData = new FormData(); - formData.append(config.downId, id); - for(var p in param){formData.append(p, param[p]);} - } - - //提交文件 - $.ajax({ - url: url - ,type: t - ,data: formData - ,contentType: false - ,processData: false - ,dataType: 'json' - ,success: function(res){ - if(res[config.successName]&& - (config.successCode==res[config.successName]||(config.successCode+'')==res[config.successName])) - { - callb.success(res); - config.success(res); - }else{ - callb.error(res); - config.error(res); - } - } - ,error: function(res){callb.error(res);} - }); - }, - Uploadx.prototype.hasItem = function(){ - //除了本身拖拽上传组件,其他都被视为是添加的元素 - var flag = this.activeContent.children().length>1; - if(flag) - { - this.activeBtn.hide(); - }else{ - this.activeBtn.show(); - } - return flag; - }, - Uploadx.prototype.add = function(that,item){ - if(addBtn==null){ - that.activeContent.append(ADD_FILE_ITEM); - addBtn=that.activeContent.find('.upload-add-file'); - addBtn.before(item) - }else{ - addBtn.before(item) - } - }, - Uploadx.prototype.isUploaded = function(index){ - var done = this.activeContent.find('.upload-file-item-' + index), - children=done.children().eq(1).children(); - return UPLOADED===children.eq(6).html(); - }, - Uploadx.prototype.clear = function(){ - var children=this.activeContent.children(); - addBtn=null; - layui.each(children,function(index,value){ - var elem = $(value); - if(!elem.hasClass('undelete')){elem.remove();} - }); - }, - Uploadx.prototype.load = function(param){ - var that = this, - config = that.configx; - // 不放置id,因为是全量请求 - that.operate('',config.url,param,{ - success:function(res){ - if(res[config.resultName]) - { - var result = res[config.resultName]; - that.clear(); - layui.each(result,function(key,file){ - var file = $.extend({},loadItem,file), - name = file.name, - icon = that.match(name), - size = (file.size / 1024).toFixed(1), - f = that.getFileInfo(name), - ext = f.ext, - realName = f.name, - objData = { - index: file.id, - name: realName, - icon: icon, - size: size, - ext: ext, - state: UPLOADED - }; - laytpl(FILE_ITEM).render(objData, function(item) { - var item = $(item); - // 下载 - item.find('.uploadx-down').on('click', function(e) { - that.operate(objData.index,config.downUrl,{},{ - success:function(res){}, - error:function(res){} - }); - return false; - }); - // 删除(服务器) - item.find('.uploadx-delete-server').on('click', function(e) { - layer.confirm(HIT.MSG3, {icon: 3, title:'提示'}, function(i){ - that.operate(objData.index,config.delUrl,{},{ - success:function(res){ - item.remove(); - that.hasItem(); - }, - error:function(res){} - }); - layer.close(i); - }); - return false; - }); - that.add(that,item); - that.overDone(objData.index); - that.hasItem(); - element.render(); - }); - }); - var hasCount = globalCount=(res[config.countName]?res[config.countName]:0); - hasCount = hasCount>99?'99+':hasCount; - if(0!=hasCount){ - hitObj.text(hasCount); - hitObj.removeClass('upload-hide'); - } - } - - }, - error:function(res){} - },config.loadType); - }, - Uploadx.prototype.overDone = function(index){ - var done = this.activeContent.find('.upload-file-item-' + index), - children=done.children().eq(1).children(); - children.eq(6).html(UPLOADED); - children.eq(2).addClass('upload-hide'); - children.eq(3).removeClass('upload-hide'); - children.eq(0).addClass('upload-hide'); - children.eq(1).removeClass('upload-hide'); - element.progress('progress-upload-' + index,'100%'); - }, - Uploadx.prototype.onupload = function(mainId) { - var that = this, - config = that.configx, - c = that.config, - finalConfig = $.extend({}, { - data: { - mainId: mainId, - fileData:'' - }, - choose: function(obj) { - var files = that.files = this.files = obj.pushFile(); - //每次重新选择,都会刷新提交表单的数据 - mainUpload.config.data.fileData=''; - obj.preview(function(index, file, result) { - var name = file.name, - icon = that.match(name), - size = (file.size / 1024).toFixed(1), - f = that.getFileInfo(name), - ext = f.ext, - realName = f.name, - objData = { - index: index, - name: realName, - icon: icon, - size: size, - ext: ext, - state: UN_UPLOAD - }; - mainUpload.config.data.fileData+="["+index+"|"+name+"]"; - laytpl(FILE_ITEM).render(objData, function(item) { - var item = $(item); - //单个重传 - item.find('.uploadx-reupload').on('click', function(e) { - obj.upload(index, file); - return false; - }); - // 下载 - item.find('.uploadx-down').on('click', function(e) { - that.operate(index,config.downUrl,{},{ - success:function(res){}, - error:function(res){} - }); - return false; - }); - // 删除(服务器) - item.find('.uploadx-delete-server').on('click', function(e) { - layer.confirm(HIT.MSG3, {icon: 3, title:'提示'}, function(i){ - that.operate(index,config.delUrl,{},{ - success:function(res){ - item.remove(); - that.hasItem(); - }, - error:function(res){} - }); - layer.close(i); - }); - return false; - }); - //删除(等待上传) - item.find('.uploadx-delete').on('click', function(e) { - layer.confirm(HIT.MSG2, {icon: 3, title:'提示'}, function(i){ - if(!that.isUploaded(index)){ - delete files[index]; //删除对应的文件 - item.remove(); - that.hasItem(); - // mainUpload.config.elem.next()[0].value = ''; //清空 input file 值,以免删除后出现同名文件不可选 - layer.close(i); - }else{ - //这块逻辑是不会走,安全起见这里做了判断 - } - }); - return false; - }); - //用于批量 - item.find('.uploadx-delete-all').on('click', function(e) { - if(!that.isUploaded(index)){ - delete files[index]; //删除对应的文件 - item.remove(); - that.hasItem(); - // mainUpload.config.elem.next()[0].value = ''; //清空 input file 值,以免删除后出现同名文件不可选 - } - return false; - }); - that.add(that,item); - that.hasItem(); - element.render(); - }); - }); - }, - before: function(obj) { - if (!that.hasItem()) { - layer.msg(HIT.MSG4); - return false; - } - }, - error: function(index, upload) {}, - progress: function(n, elem, e, index) { - element.progress('progress-upload-' + index, n + '%'); - element.render(); - }, - done: function(res, index, upload) { - that.overDone(index); - delete this.files[index]; - return; - this.error(index, upload); - }, - allDone: function(obj) { - layer.msg('文件上传成功: 总数: ' + obj.total + ' ,成功: ' + obj.successful + ' ,失败: ' + obj.aborted, { - icon: 1 - }); - } - }, c); - mainUpload = upload.render(finalConfig); - }, - Uploadx.prototype.initPage = function(mainId){ - $('#'+OPEN_LAYER_ID).siblings(".layui-layer-btn").append(FILE_UPLOAD_PAGE); - var that = this, - cfg = $.extend({},this.pageCfg,{ - elem: 'file-upload-page', - count: globalCount, - jump: function(obj,first){ - if(!first){ - var reqPage = {}; - reqPage[that.pageCfg.limitName]=obj.limit; - reqPage[that.pageCfg.currentName]=obj.curr; - reqPage.mainId = mainId; - that.load(reqPage); - } - } - }); - page.render(cfg); - }, - Uploadx.prototype.event = function() { - var that = this, - config = that.configx, - targetForm = form.val(config.form), - mainId = targetForm[config.mainId]; - var reqPage = {}; - reqPage[that.pageCfg.limitName]=that.pageCfg.limit; - reqPage[that.pageCfg.currentName]=1; - reqPage.mainId = mainId; - uploadx.load(reqPage); - util.event('lay-active', { - open: function(e) { - that.openIndex = layer.open({ - type: 1, - id: OPEN_LAYER_ID, - title: config.title, - area: ['578px', '395px'], - shade: config.shade, - maxmin: false, - offset: config.offset, - content: that.container, - btn: ['关闭'], - yes: function() { - layer.close(that.openIndex); - }, - success: function(layero, index) { - that.onupload(mainId); - that.initPage(mainId); - }, - end: function() {} - }); - return false; - }, - addFile: function(e) { - that.activeBtn.click(); - return false; - }, - uploadAdd:function(e){ - that.activeBtn.click(); - return false; - }, - clearFile: function(e) { - layer.confirm(HIT.MSG1, {icon: 3, title:'提示'}, function(index){ - for (var o in that.files) { - var item = that.activeContent.find('.upload-file-item-' + o); - item.children().eq(1).children().eq(7).click(); - } - layer.close(index); - }); - return false; - } - }) - }, - Uploadx.prototype.container = function() { - var that = this, - config = that.configx, - c = that.config; - laytpl(HTML_CONTAINER).render({ - name: config.name, - ext:c.ext, - accept:c.accept - }, function(html) { - html = $(html); - config.elem.append(html); - hitObj=html.find('.layui-uploadx-count'); - config.elem.append(FILE_CONTAINER); - that.container = $("#xx01"); - that.activeBtn = $("#uploadxBtn"); - that.activeContent = $("#uploadxContent"); - }); - }, - clazz.render = function(options) { - uploadx = new Uploadx(options); - uploadx.render(); - }, - clazz.addExt = function(options) { - var arg = $.extend({},extcfg, options); - arg.reg = extpattern(arg.ext); - extConfig.splice(0, 0, arg); - }, - exports(MODULE_NAME, clazz); + Uploadx.prototype.getFileInfo = function (name) { + var pointIndex = name.lastIndexOf('.'); + if (pointIndex > 0) { + return { + name: name.substr(0, pointIndex), + ext: name.substr(pointIndex, name.length) + } + } else { + return { + name: name, + ext: '未知扩展名' + } + } + }, + Uploadx.prototype.render = function () { + var that = this; + that.container(); + that.event(); + }, + Uploadx.prototype.match = function (name) { + var result = {}; + for (var i = 0; i < extConfig.length; i++) { + var c = extConfig[i]; + if (c.reg.test(name)) { + switch (c.type) { + case 'svg': //一般文件 + result = c.icon; + break; + case 'img': + result = ''; + break; + default: + result = otherFile; + break; + } + break; + } else { + result = otherFile; + } + } + return result; + }, + Uploadx.prototype.checkExist = function (files) { + var that = this, + keys = Object.keys(files), + len = keys.length, + chooseIndex = keys[len - 1], + chooseFile = files[keys[len - 1]], + chooseF = that.getFileInfo(chooseFile.name), + chooseExt = chooseF.ext, + chooseRealName = chooseF.name, + isFault = false; + if (len > 0) { + //如果是第一次选择,那么直接放行,否则,重命名校验 + if (len > 1) { + for (var i = 0, l = len - 2; i <= l; i++) { + //判断名字和扩展名是否都相同 + var curFile = files[keys[i]], + name = curFile.name, + f = that.getFileInfo(name), + ext = f.ext, + realName = f.name; + if (realName === chooseRealName && ext === chooseExt) { + isFault = true; + break; + } + } + } + } + return { + isFault: isFault, + chooseIndex: chooseIndex + }; + }, + Uploadx.prototype.operate = function (id, url, param, callb, type) { + var config = this.configx; + var t = type ? type : 'post', + formData = ''; + if ('get' === t) { + //get 拼接参数 ?name=''& + url += ('?t=' + new Date().getTime()); + var paramx = ''; + for (var p in param) { paramx += ('&' + p + '=' + param[p]); } + url += paramx; + } else { + formData = new FormData(); + formData.append(config.downId, id); + for (var p in param) { formData.append(p, param[p]); } + } + + //提交文件 + $.ajax({ + url: url + , type: t + , data: formData + , contentType: false + , processData: false + , dataType: 'json' + , success: function (res) { + if (res[config.successName] && + (config.successCode == res[config.successName] || (config.successCode + '') == res[config.successName])) { + callb.success(res); + config.success(res); + } else { + callb.error(res); + config.error(res); + } + } + , error: function (res) { callb.error(res); } + }); + }, + Uploadx.prototype.hasItem = function () { + //除了本身拖拽上传组件,其他都被视为是添加的元素 + var flag = this.activeContent.children().length > 1; + if (flag) { + this.activeBtn.hide(); + } else { + this.activeBtn.show(); + } + return flag; + }, + Uploadx.prototype.add = function (that, item) { + if (that.addBtn == null) { + laytpl(ADD_FILE_ITEM).render({ + moduleId: that.configx.moduleId + }, function (str) { + that.activeContent.append(str); + that.addBtn = that.activeContent.find('.upload-add-file'); + that.addBtn.before(item) + }) + } else { + that.addBtn.before(item) + } + }, + Uploadx.prototype.isUploaded = function (index) { + var done = this.activeContent.find('.upload-file-item-' + index), + children = done.children().eq(1).children(); + return this.UPLOADED === children.eq(6).html(); + }, + Uploadx.prototype.clear = function () { + var children = this.activeContent.children(); + this.addBtn = null; + layui.each(children, function (index, value) { + var elem = $(value); + if (!elem.hasClass('undelete')) { elem.remove(); } + }); + }, + Uploadx.prototype.load = function (param) { + var that = this, + config = that.configx; + // 不放置id,因为是全量请求 + that.operate('', config.url, param, { + success: function (res) { + if (res[config.resultName]) { + var result = res[config.resultName]; + that.clear(); + layui.each(result, function (key, file) { + var file = $.extend({}, loadItem, file), + name = file.name, + icon = that.match(name), + size = (file.size / 1024).toFixed(1), + f = that.getFileInfo(name), + ext = f.ext, + realName = f.name, + objData = { + index: file.id, + name: realName, + icon: icon, + size: size, + ext: ext, + state: that.UPLOADED + }; + laytpl(FILE_ITEM).render(objData, function (item) { + var item = $(item); + // 下载 + item.find('.uploadx-down').on('click', function (e) { + that.operate(objData.index, config.downUrl, {}, { + success: function (res) { }, + error: function (res) { } + }); + return false; + }); + // 删除(服务器) + item.find('.uploadx-delete-server').on('click', function (e) { + layer.confirm(HIT.MSG3, { icon: 3, title: '提示' }, function (i) { + that.operate(objData.index, config.delUrl, {}, { + success: function (res) { + item.remove(); + that.hasItem(); + }, + error: function (res) { } + }); + layer.close(i); + }); + return false; + }); + that.add(that, item); + that.overDone(objData.index); + that.hasItem(); + element.render(); + }); + }); + var hasCount = that.globalCount = (res[config.countName] ? res[config.countName] : 0); + hasCount = hasCount > 99 ? '99+' : hasCount; + if (0 != hasCount) { + that.hitObj.text(hasCount); + that.hitObj.removeClass('upload-hide'); + } + } + + }, + error: function (res) { } + }, config.loadType); + }, + Uploadx.prototype.overDone = function (index) { + var done = this.activeContent.find('.upload-file-item-' + index), + children = done.children().eq(1).children(); + children.eq(6).html(this.UPLOADED); + children.eq(2).addClass('upload-hide'); + children.eq(3).removeClass('upload-hide'); + children.eq(0).addClass('upload-hide'); + children.eq(1).removeClass('upload-hide'); + element.progress('progress-upload-' + index, '100%'); + }, + Uploadx.prototype.onupload = function (mainId, moduleId) { + var that = this, + config = that.configx, + c = that.config, + finalConfig = $.extend({}, { + data: { + mainId: mainId, + fileData: '', + moduleId: moduleId + }, + choose: function (obj) { + var files = that.files = this.files = obj.pushFile(); + //每次重新选择,都会刷新提交表单的数据 + that.mainUpload.config.data.fileData = ''; + obj.preview(function (index, file, result) { + var name = file.name, + icon = that.match(name), + size = (file.size / 1024).toFixed(1), + f = that.getFileInfo(name), + ext = f.ext, + realName = f.name, + objData = { + index: index, + name: realName, + icon: icon, + size: size, + ext: ext, + state: that.UN_UPLOAD + }; + that.mainUpload.config.data.fileData += "[" + index + "|" + name + "]"; + laytpl(FILE_ITEM).render(objData, function (item) { + var item = $(item); + //单个重传 + item.find('.uploadx-reupload').on('click', function (e) { + obj.upload(index, file); + return false; + }); + // 下载 + item.find('.uploadx-down').on('click', function (e) { + that.operate(index, config.downUrl, {}, { + success: function (res) { }, + error: function (res) { } + }); + return false; + }); + // 删除(服务器) + item.find('.uploadx-delete-server').on('click', function (e) { + layer.confirm(HIT.MSG3, { icon: 3, title: '提示' }, function (i) { + that.operate(index, config.delUrl, {}, { + success: function (res) { + item.remove(); + that.hasItem(); + }, + error: function (res) { } + }); + layer.close(i); + }); + return false; + }); + //删除(等待上传) + item.find('.uploadx-delete').on('click', function (e) { + layer.confirm(HIT.MSG2, { icon: 3, title: '提示' }, function (i) { + if (!that.isUploaded(index)) { + delete files[index]; //删除对应的文件 + item.remove(); + that.hasItem(); + // mainUpload.config.elem.next()[0].value = ''; //清空 input file 值,以免删除后出现同名文件不可选 + layer.close(i); + } else { + //这块逻辑是不会走,安全起见这里做了判断 + } + }); + return false; + }); + //用于批量 + item.find('.uploadx-delete-all').on('click', function (e) { + if (!that.isUploaded(index)) { + delete files[index]; //删除对应的文件 + item.remove(); + that.hasItem(); + // mainUpload.config.elem.next()[0].value = ''; //清空 input file 值,以免删除后出现同名文件不可选 + } + return false; + }); + that.add(that, item); + that.hasItem(); + element.render(); + }); + }); + }, + before: function (obj) { + if (!that.hasItem()) { + layer.msg(HIT.MSG4); + return false; + } + }, + error: function (index, upload) { }, + progress: function (n, elem, e, index) { + element.progress('progress-upload-' + index, n + '%'); + element.render(); + }, + done: function (res, index, upload) { + that.overDone(index); + delete this.files[index]; + return; + this.error(index, upload); + }, + allDone: function (obj) { + layer.msg('文件上传成功: 总数: ' + obj.total + ' ,成功: ' + obj.successful + ' ,失败: ' + obj.aborted, { + icon: 1 + }); + } + }, c); + + //这里兼容多个组件存在 + var configxElem = that.getRootElem(); + finalConfig.elem = configxElem + " " + finalConfig.elem; + finalConfig.bindAction = configxElem + " " + finalConfig.bindAction; + if (that.mainUpload.reload != null) { + that.mainUpload.reload(finalConfig) + } else { + that.mainUpload = upload.render(finalConfig); + } + }, + Uploadx.prototype.getRootElem = function () { + let that = this; + return "#" + that.configx.elem[0].id; + }, + Uploadx.prototype.initPage = function (mainId, moduleId) { + var that = this; + var config = that.configx; + $('#' + that.OPEN_LAYER_ID + config.moduleId).siblings(".layui-layer-btn").append(FILE_UPLOAD_PAGE); + var configxElem = that.getRootElem(); + var page_elem = configxElem + " " + "#file-upload-page"; + + var cfg = $.extend({}, that.pageCfg, { + elem: $(page_elem)[0], + count: that.globalCount, + jump: function (obj, first) { + if (!first) { + var reqPage = {}; + reqPage[that.pageCfg.limitName] = obj.limit; + reqPage[that.pageCfg.currentName] = obj.curr; + reqPage.mainId = mainId; + reqPage.moduleId = moduleId; + that.load(reqPage); + } + } + }); + page.render(cfg); + }, + Uploadx.prototype.event = function () { + var that = this, + config = that.configx, + targetForm = form.val(config.form), + mainId = targetForm[config.mainId], + moduleId = config.moduleId; + var reqPage = {}; + reqPage[that.pageCfg.limitName] = that.pageCfg.limit; + reqPage[that.pageCfg.currentName] = 1; + reqPage.mainId = mainId; + reqPage.module = moduleId; + that.load(reqPage); + + util.event('lay-active', { + open: function (e) { + let moduleid = $(e).data("moduleid"); + let that = eval('window.' + moduleid); + let config = that.configx; + let targetForm = form.val(config.form); + let mainId = targetForm[config.mainId]; + let moduleId = config.moduleId; + that.openIndex = layer.open({ + type: 1, + id: that.OPEN_LAYER_ID + config.moduleId, + title: config.title, + area: ['578px', '395px'], + shade: config.shade, + maxmin: false, + offset: config.offset, + content: that.container, + btn: ['关闭'], + yes: function () { + layer.close(that.openIndex); + }, + success: function (layero, index) { + that.onupload(mainId, moduleId); + that.initPage(mainId, moduleId); + }, + end: function () { } + }); + return false; + }, + addFile: function (e) { + let moduleid = $(e).data("moduleid"); + let that = eval('window.' + moduleid); + //let config = that.configx; + //let targetForm = form.val(config.form); + //let mainId = targetForm[config.mainId]; + //let moduleId = config.moduleId; + that.activeBtn.click(); + return false; + }, + uploadAdd: function (e) { + let moduleid = $(e).data("moduleid"); + let that = eval('window.' + moduleid); + that.activeBtn.click(); + return false; + }, + clearFile: function (e) { + let moduleid = $(e).data("moduleid"); + let that = eval('window.' + moduleid); + layer.confirm(HIT.MSG1, { icon: 3, title: '提示' }, function (index) { + for (var o in that.files) { + var item = that.activeContent.find('.upload-file-item-' + o); + item.children().eq(1).children().eq(7).click(); + } + layer.close(index); + }); + return false; + } + }) + }, + Uploadx.prototype.container = function () { + var that = this, + config = that.configx, + c = that.config; + laytpl(HTML_CONTAINER).render({ + name: config.name, + ext: c.ext, + accept: c.accept, + moduleId: config.moduleId + }, function (html) { + html = $(html); + config.elem.append(html); + that.hitObj = html.find('.layui-uploadx-count'); + laytpl(FILE_CONTAINER).render({ + moduleId: config.moduleId + }, function (str) { + config.elem.append(str); + that.container = config.elem.find("#xx01"); + that.activeBtn = config.elem.find("#uploadxBtn"); + that.activeContent = config.elem.find("#uploadxContent"); + }) + + }); + }; + var clazz = { + render: function (options) { + let uploadx = new Uploadx(options); + eval("window." + options.ex.moduleId + " = uploadx"); + uploadx.render(); + return uploadx; + }, + addExt:function (options) { + var arg = $.extend({}, extcfg, options); + arg.reg = extpattern(arg.ext); + extConfig.splice(0, 0, arg); + }, + } + exports(MODULE_NAME, clazz); }); +