/** * 按钮的基础服务 * @author weidy * @date 2021-03-22 */ layui.define(['layer','element'],function(exports) { var webUtil = $webUtil; var Class = function () { this.MODELNAME = "BaseAction"; this.moduleKey = "BaseAction"; this.id = "BaseAction"; /** * 替换文本中的${xxx} * @param text 文本 * @param dataStore 选择的数据 * @param sourceData 来源数据 * @returns 替换后的值,字符串 */ this.replaceFreeMarker = function (text,dataStore,sourceData) { return $webUtil.replaceFreeMarker(text,dataStore,sourceData); }; /** * 执行前置事件 * @param options 按钮的配置信息,前置事件里配置的参数会替换这个里的参数的信息 * @param buttonTarget 按钮js所在的对象 * @param callback 回调,如果存在前置事件,会在执行完成后执行回调,否则直接回调 */ this.callPreEvent = function (options,buttonTarget,callback,preEventName) { var that = this; var params = $webUtil.paramLow(options.paramVOS); options.paramVOS = params; var beforeEvent = params[preEventName || 'beforeevent']; if(beforeEvent) { var buttonParse = that.parseEventByUrl(beforeEvent,options,true); buttonParse.options.callback = callback; if($webUtil.isNull(buttonParse)){ buttonTarget[buttonParse.methodName](buttonParse); }else{ layui.use(buttonParse.jsPath,function () { layui[buttonParse.jsPath][buttonParse.methodName](options); }); } }else{ if(callback){ callback(options); } } }; /** * 执行后置时间 * @param options 按钮的配置信息,后置事件里配置的参数会替换这个里的参数的信息 * @param buttonTarget 按钮Js所在的对象 */ this.callPostEvent = function(options,buttonTarget,callback,postEventName){ var that = this; var params = $webUtil.paramLow(options.paramVOS); options.paramVOS = params; var afterEvent = params[postEventName || 'afterevent']; if(afterEvent) { var buttonParse = that.parseEventByUrl(afterEvent,options,false); if($webUtil.isNull(buttonParse)){ buttonTarget[buttonParse.methodName](buttonParse); }else{ layui.use(buttonParse.jsPath,function () { layui[buttonParse.jsPath][buttonParse.methodName](options); }); } }else{ if(callback){ callback(options); } } }; /** * 使用url获取事件的信息 * @param url 路径 * @param options 按钮的配置信息,会自动覆盖相同属性的参数 * @param isBefore 是否为前置事件,否则为后置 * @returns {{jsPath: js的路径, options: 按钮的配置信息, methodName: (string)方法的名字}} */ this.parseEventByUrl = function (url,options,isBefore) { //根据配置格式化事件 var jsPath = url; var methodName = isBefore?"doBefore":"doAfter"; var params = {}; if (url.indexOf("?")) { var temp = url.substring(0, url.indexOf("?")); if (temp.indexOf("#") > -1) { var array = temp.split("#"); if(array.length == 1){ jsPath = array[0]; }else{ jsPath = array[0]; methodName = array[1]; } }else{ jsPath = temp; } var paramArray = url.substring(url.indexOf("?") + 1).split("&"); layui.each(paramArray, function (_index, _item) { if (_item.indexOf("=") < 0) { $webUtil.showErrorMsg(isBefore?"前置事件":"后置事件" + "的参数配置错误,需要要xxx=yyy&zzz=a的方式"); return true; } params[_item.split("=")[0]] = _item.split("=")[1]; }); }else{ if (url.indexOf("#") > -1) { var array = url.split("#"); if(array.length == 1){ jsPath = array[0]; }else{ jsPath = array[0]; methodName = array[1]; } }else{ jsPath = url; } } for (var key in params) { options.paramVOS[key.toLowerCase()] = params[key]; } return { jsPath:jsPath, methodName:methodName, options:options }; }; this.renderUploadFile=function (filter,paramVOS,extraParams,callback){ var extendAttrMap = JSON.parse($(filter ).attr('extendAttrMap')); var data={ fileDocClassify: 'filePathField', fileDocClassifyName: '文件路径字段', updateFileFlag: false,//是否为修改 fileOid: ''//只在更新的时候才传递 } if(extraParams){ $.extend(data, extraParams); } layui.upload.render({ elem: filter , url: paramVOS.uploadfilebackpath + paramVOS.uploadfileurl , accept: extendAttrMap && extendAttrMap.accept ? extendAttrMap.accept : '' , exts: extendAttrMap && extendAttrMap.exts ? extendAttrMap.exts : '' , data: data , before: function (res) { $webUtil.showProgress('文件上传中'); } , done: function (res, index, upload) { $webUtil.closeProgress(); if (res.success) { layer.msg('上传成功'); if(layui.$( filter + ' .uploadDemoView').length>0 && res.obj){ layui.$( filter + ' .uploadDemoView').removeClass('layui-hide').find('img').attr('src', paramVOS.uploadfilebackpath + 'vciFileDownloadController/downloadByFileOid?fileOid=' + res.obj.oid + '&' + TOKEN_KEY + '=' + $webUtil.getToken()); layui.$( filter + ' .uploadDemoView input.uploadFileOid').val(res.obj.filePath); } if(callback){ callback(res.obj) } }else{ $webUtil.showErrorMsg(res.msg||"上传失败!"); } }, error: function (index, upload) { $webUtil.showErrorMsg("上传失败!"); } }); } //附件上传 this.renderUploadAttach=function (filter,paramVOS,extraParams){ var attachHtml=[ '
', '', '', '', '
', '', '', ''+ '', '', '
文件名大小状态操作
', '
', '
' ] $('#' + filter + '').append(attachHtml.join('')) var uploadListView = $('#'+filter+'_uploadList'); var data={ fileDocClassify: 'attachment', fileDocClassifyName: '附件', updateFileFlag: false, fileOid: '' } if(extraParams){ $.extend(data, extraParams); } var uploadListIns = layui.upload.render({ elem: '#'+filter+'_selectBtn' ,url: paramVOS.uploadfilebackpath + paramVOS.uploadfileurl ,accept: 'file' ,multiple: true ,auto: false ,data: data ,bindAction: '#'+filter+'_uploadAction' ,choose: function(obj){ var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列 var elemFiles= this.elemFiles = obj.getElemFiles()//针对ie //读取本地文件 obj.preview(function(index, file, result){ var tr = $(['' ,''+ file.name +'' , '' + (file.size ? (file.size / 1024).toFixed(1) + 'kb' : '') + '' ,'等待上传' ,'' ,'重传' ,'删除' ,'' ,''].join('')); //单个重传 tr.find('.attach-reload').on('click', function(){ obj.upload(index, file); }); //删除 tr.find('.attach-delete').on('click', function(){ delete files[index]; //删除对应的文件 if(layui.device().ie && layui.device().ie<10){ $('#'+elemFiles[index][0].id).remove() //elemFiles[index].parent()[0].removeChild(elemFiles[index][0]) delete elemFiles[index];//针对ie } var oid=tr.attr('oid'); if(oid){ var val=$('#' + filter + ' [name="releaseFileOids"]').val(); val =val.replace(oid,'').replace(',,',',') if(val.indexOf(',')==0){ val=val.replace(',','') } $('#' + filter + ' [name="releaseFileOids"]').val(val) } tr.remove(); uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免删除后出现同名文件不可选 $('.layui-upload-choose').remove(); }); uploadListView.append(tr); }); } ,done: function(res, index, upload){ if(res.success){ //上传成功 var tr = uploadListView.find('tr#upload-'+ index) ,tds = tr.children(); tds.eq(2).html('上传成功'); tr.attr('oid',res.obj.oid); tds.eq(3).find('.attach-reload').addClass('layui-hide').html(''); //清空操作 var val=$('#' + filter + ' [name="releaseFileOids"]').val().split(','); if(val[0]==''){ val[0]=res.obj.oid }else{ val.push(res.obj.oid) } $('#' + filter + ' [name="releaseFileOids"]').val(val.join(',')) delete this.files[index]; //删除文件队列已经上传成功的文件 if(layui.device().ie && layui.device().ie<10){ $('#'+this.elemFiles[index][0].id).remove() delete this.elemFiles[index]; } return ; } this.error(index, upload,res.msg); } ,error: function(index, upload,msg){ var tr = uploadListView.find('tr#upload-'+ index) ,tds = tr.children(); tds.eq(2).html('上传失败'); tds.eq(3).find('.attach-reload').removeClass('layui-hide'); //显示重传 $webUtil.showErrorMsg(msg); } }); } //编辑器 this.renderEditor=function (editId,initValue,paramVOS,callback){ function editorCb(){ //KindEditor.ready(function(K) { window.editor = KindEditor.create('#' + editId, { allowFileManager : true, resizeMode : 1, //编辑器只能调整高度 width:800, height:260, afterBlur: function () { this.sync(); }, afterCreate: function () { if(callback) callback() } }); //}); } $webUtil.createScript('jslib/kindeditor-4.1.7/kindeditor-min.js',editorCb) } //弹窗内容加载完后重新定位弹窗位置 this.relocationOpen=function (classP){ !(/^\d+%$/.test(classP.config.area[0]) || /^\d+%$/.test(classP.config.area[1])) && classP.auto(classP.index); classP.offset(); classP.config.type == 4 && classP.tips(); } }; var cs = new Class(); exports(cs.MODELNAME, cs); });