/**
|
* 文件上传和修改
|
* @author wangting
|
* @date 2021-5-11
|
*/
|
layui.define(['layer','element','BaseAction'],function(exports){
|
var webUtil = $webUtil;
|
var Class = function() {
|
this.MODELNAME = "BaseFileUploadAction";
|
this.moduleKey = "BaseFileUploadAction";
|
|
this.getContent = function (id) {
|
var that = this;
|
that.id = id;
|
var html = [
|
'<div class="FileUpload" id="FileUpload_', id, ' lay-filter="FileUpload_', id, '" style="display:block; text-align: center"></div>'
|
].join("");
|
return html;
|
};
|
|
this.doAction = function (options) {
|
var that = this;
|
webUtil.copyConfig(that, that.moduleKey);
|
that.id = options.id;
|
var paramVOS = {
|
url: 'vciFileUploadController/uploadFile',
|
backPath: configData.compatibility ? path : configData.fileServicePath,
|
method: 'post'
|
}
|
$.extend(paramVOS, options.paramVOS);
|
options.paramVOS = paramVOS;
|
|
var baseAction = layui['BaseAction'];
|
baseAction.callPreEvent(options, that, function (options) {
|
that.doUpload(options, function () {
|
baseAction.callPostEvent(options, that, options.callback);
|
});
|
});
|
};
|
|
|
this.doUpload = function (options,callback) {
|
var that = this;
|
var paramVOS = $webUtil.paramLow(options.paramVOS);
|
var selectData = options.dataStore;
|
|
if (paramVOS.updatefileflag == true || paramVOS.updatefileflag == "true") {
|
if (selectData.length < 1) {
|
$webUtil.showErrorMsg("请选择一条需要修改的数据");
|
return false;
|
}
|
if (selectData[0].btmname != 'fileobject') {
|
$webUtil.showErrorMsg("修改的数据需是文件对象");
|
return false;
|
}
|
if (!paramVOS.multi && selectData.length > 1) {
|
$webUtil.showErrorMsg("仅能选择一条数据来操作");
|
return false;
|
}
|
}
|
if (!options.sourceData) {
|
$webUtil.showErrorMsg("请选择来源数据");
|
return false;
|
}
|
var filter = "aFileUpload_" + options.id;
|
var form = layui.form;
|
var content = ['<div id="', filter, '" lay-filter="', filter, '" class="layui-form" style="margin:15px 30px 15px 0;" ></div>'
|
].join('');
|
var addSaveIndex = layer.open({
|
type: 1,
|
title: paramVOS.title ? $webUtil.replaceFreeMarker(paramVOS.title, selectData, options.sourceData[0]) : '上传文件',
|
btn: ['开始上传', '关闭'],
|
//skin: 'layui-layer-lan',
|
content: content,
|
area: [paramVOS.width || '800px', paramVOS.height || '500px'],
|
closeBtn: 1,
|
shade: true,
|
shadeClose: true,
|
resize: true,
|
resizing: function (layero) {
|
},
|
success: function (layero, layerIndex, classP) {
|
var attachHtml = [
|
'<div class="layui-upload" style="margin-left: 30px;">',
|
'<form id="form_', filter, '" lay-filter="form_', filter, '" class="layui-form" style="margin:15px 30px 0 -80px;" ></form>',
|
'<button id="', filter, '_selectBtn" type="button" class="layui-btn layui-btn-sm layui-btn-normal">选择文件</button>',
|
//'<button id="',filter,'_uploadAction" type="button" class="layui-btn layui-btn-sm" style="margin-left: 15px;">开始上传</button>',
|
'<input type="hidden" name="releaseFileOids">',
|
'<div class="layui-upload-list layui-table-view">',
|
'<table cellspacing="0" cellpadding="0" border="0" class="layui-table" style="width: 100%;">',
|
'<thead>',
|
'<tr><th>文件名</th><th>大小</th><th>状态</th><th style="width: 90px">操作</th></tr>' +
|
'</thead>',
|
'<tbody id="', filter, '_uploadList"></tbody>',
|
'</table>',
|
'</div>',
|
'</div>'
|
]
|
$('#' + filter + '').append(attachHtml.join(''))
|
if (configData.controlSecret == true || configData.controlSecret == 'true') {
|
$webUtil.get('webEnumController/getDataEnum', {
|
oid: $webUtil.replaceFreeMarker(options.sourceData.oid, options.sourceData, {}),
|
btmname: (options.sourceData.btmname && $webUtil.replaceFreeMarker(options.sourceData.btmname, options.sourceData, {})) || paramVOS.linktype || ''
|
}, function (result) {
|
if (result.success) {
|
layui.form.addItems('form_' + filter, [{
|
name: 'secretGrade',
|
text: '密级',
|
type: 'combox',
|
verify: 'number',
|
defaultValue: 1,
|
data: result.obj,
|
}], function () {
|
}, {}, {
|
defaultColumnOneRow: 1
|
});
|
} else {
|
$webUtil.showErrorMsg(result.msg || '获取表单失败');
|
}
|
|
}, function (xhr, error) {
|
$webUtil.showErrorMsg("获取表单出现了错误,可能是文件服务没有启动");
|
}, paramVOS.backpath)
|
|
}
|
var uploadListView = $('#' + filter + '_uploadList');
|
uploadListIns = layui.upload.render({
|
elem: '#' + filter + '_selectBtn'
|
, url: paramVOS.backpath + paramVOS.url
|
, accept: 'file'
|
, multiple: true
|
, auto: false//(layui.device().ie && layui.device().ie < 10)?true:false
|
, data: {}
|
, bindAction: '#' + filter + '_uploadAction'
|
, before: function (obj) {
|
var uploadData = {
|
fileDocClassify: paramVOS.filedocclassify,
|
fileDocClassifyName: paramVOS.filedocclassifyname,
|
ownbizOid: $webUtil.replaceFreeMarker(options.sourceData.oid, options.sourceData, {}),
|
ownBtmname: (options.sourceData.btmname && $webUtil.replaceFreeMarker(options.sourceData.btmname, options.sourceData, {})) || paramVOS.linktype || '',
|
updateFileFlag: paramVOS.updatefileflag || false,
|
effect: paramVOS.effect || true,
|
fileOid: paramVOS.updatefileflag == "true" ? selectData[0].oid : '',
|
name: 'file',
|
checkSameFile: true,
|
secretGrade: -1
|
}
|
if (configData.controlSecret == true || configData.controlSecret == 'true') {
|
uploadData.secretGrade = layui.form.getValues('form_' + filter, true).secretGrade;
|
if (uploadData.secretGrade == '') {
|
$webUtil.showErrorMsg("请选择密级");
|
return false;
|
}
|
if (options.sourceData.secretgrade || options.sourceData.secretGrade) {
|
var selectGrade = options.sourceData.secretgrade || options.sourceData.secretGrade;
|
selectGrade = parseInt(selectGrade);
|
if (parseInt(uploadData.secretGrade) > selectGrade) {
|
$webUtil.showErrorMsg("文件密级不能大于所选数据的密级");
|
return false;
|
}
|
}
|
}
|
obj.setData(uploadData)
|
$webUtil.showProgress("文件上传中……");
|
}
|
, choose: function (obj) {
|
var files = this.files = obj.pushFile();//将每次选择的文件追加到文件队列
|
var elemFiles= this.elemFiles = obj.getElemFiles()//针对ie
|
//读取本地文件
|
obj.preview(function (index, file, result) {
|
var tr = $(['<tr id="upload-' + index + '">'
|
, '<td>' + file.name + '</td>'
|
, '<td>' + (file.size ? (file.size / 1024).toFixed(1) + 'kb' : '') + '</td>'
|
, '<td>等待上传</td>'
|
, '<td>'
|
, '<a class="layui-btn layui-btn-xs attach-reload layui-hide">重传</a>'
|
, '<a class="layui-btn layui-btn-xs layui-btn-danger attach-delete">删除</a>'
|
, '</td>'
|
, '</tr>'].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('<span style="color: #5FB878;">' + (paramVOS.effect == "false" ? '传输完成' : '已生效') + '</span>');
|
tr.attr('oid', res.obj.oid);
|
tds.eq(0).text(res.obj.id);
|
tds.eq(3).find('.attach-reload').addClass('layui-hide').html(''); //清空操作
|
tds.eq(3).find('.attach-delete').addClass('layui-hide')
|
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);
|
},
|
allDone: function (obj) {
|
if (obj.total == obj.successful) {
|
if (callback) {
|
callback()
|
}
|
if (options.callback) {
|
options.callback()
|
}
|
$webUtil.closeProgress();
|
layer.close(addSaveIndex)
|
}
|
}
|
, error: function (index, upload, msg) {
|
var tr = uploadListView.find('tr#upload-' + index)
|
, tds = tr.children();
|
tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
|
tds.eq(3).find('.attach-reload').removeClass('layui-hide'); //显示重传
|
$webUtil.showErrorMsg(msg);
|
}
|
});
|
},
|
yes: function (layero) {
|
if (paramVOS.updatefileflag == "true") {
|
var trs = $('#' + filter + '_uploadList tr');
|
if (trs.length > 1) {
|
$webUtil.showErrorMsg("修改文件只能上传1个文件");
|
return false;
|
}
|
}
|
uploadListIns.upload();
|
},
|
btn2: function (layero) {
|
if (options.callback) {
|
options.callback()
|
}
|
layer.close(addSaveIndex)
|
},
|
cancel: function (layero) {
|
if (options.callback) {
|
options.callback()
|
}
|
}
|
});
|
}
|
|
this.refresh = function () {
|
var that = this;
|
|
};
|
|
this.destory = function () {
|
var that = this;
|
var filter = "FileUpload_" + that.id;
|
$('#' + filter).remove()
|
};
|
};
|
var cs = new Class();
|
exports(cs.MODELNAME,cs);
|
});
|