/**
* 附件管理
* 包括:
* 文件查看
* 文件上传
* 文件预览
* 文件分享
* weidy重新调整一下
* 所有的配置项需需要先传入主键,所有的操作都需要先传入主键
* 以前的存在不同的页面都使用这个组件时,配置信息会被覆盖的情况
*/
layui.define(['layer','util','table','form','upload','code','FilePreviewBase'],function(exports){
var fileManager = function(){
this.moduleKey='vciWebFileManager';
this.backPath = configData.compatibility?path:configData.fileServicePath;
this.processPath = configData.compatibility?path:configData.processServicePath;
this.url={
process:{//流程相关的url
dataGrid:'/webProcessDefineController/dataGridFileInProcess',
previewUrlByFileOid:'/webFileController/previewFile',//在流程中的单个文件的预览
previewUrlByDataOid:'/webProcessDefineController/getPreviewUrlByDataOid'//根据业务数据来获取文件预览的路径
},
bus:{//普通业务相关的url
controller:'vciFileQueryController/',
dataGrid:'gridFiles',
previewUrlByFileOid:'previewFile'//在流程中的单个文件的预览
},
downloadByFileOid:'vciFileDownloadController/downloadByFileOid',//下载文件
reviseByFileOid:'vciFileUploadController/uploadFile'//修改文件
//这个组件只能提供公共的路径,特殊业务的按钮需要自行注册
};
this.id="vciWebFileManager";
this.sourceData = {};
this.columns = [];
this.config = {};
this.defaultConfig = {
inProcess:false,//是否是流程中的值
showInDialog : false,//显示到窗口中
dataIsProcessInstance : false,//是否为流程实例
reviseButton : false
};
this.defaultButtonInfos = [{
uniqueFlag: "DOWNLOADFILE",
alias: '下载文件'
},{
uniqueFlag:'PREVIEW',
alias:'预览'
}];
this.defaultReviseButtonInfo = {
uniqueFlag: 'revisefile',
alias: '修改'
};
this.buttonIconMap = {
DOWNLOADFILE:"layui-icon-download-circle",
PREVIEW:"layui-icon-carousel",
revisefile:"layui-icon-upload"
};
this.getContent = function(){//不调用
return "";
};
this.getButtonHtml = function(id){
//按钮的html
var that = this;
var options = that.getConfig(id);
if(options && $webUtil.isNull(options['toolbarId'])){
that.updateConfig(id,"toolbarId",'#toolbar_' + id);
that.updateConfig(id,'destoryToolbar',true);
return '
';
}else{
return '';
}
};
this.getTableHtml = function(id){
var that = this;
//表格的html
var options = that.getConfig(id);
if(options && $webUtil.isNull(options['tableId'])){
that.updateConfig(id,'tableId','#table_' + id);
that.updateConfig(id,'destoryTable',true);
return '';
}else{
return '';
}
};
this.init = function(){//不调用
var that = this;
$webUtil.copyConfig(that,that.moduleKey);
};
this.setConfig = function(id,options){
var that = this;
//设置配置项,自动继承默认的配置
var ops = {};
$.extend(ops,that.defaultConfig,options);
/**
* 配置相关的内容
*
*/
that.config[id] = ops;
};
this.updateConfig = function(id,key,value){
//更新配置
var that = this;
var options = that.getConfig(id);
options[key] = value;
that.config[id]=options;
};
this.getConfig = function(id){
//获取配置项
var that = this;
if(id in that.config){
return that.config[id];
}
return {};
};
this.destoryFileManage = function(id){
//销毁
var that = this;
var options = that.getConfig(id);
if(options.destoryToolbar){
//是这个组件里自己加的
$(options.toolbarId).remove();
}else{
//删除已经存在的按钮
$(options.toolbarId).find('button').remove();
var searchInput = $(options.toolbarId).find('[name="fast_search_select_value"]');
searchInput.prev().remove();
searchInput.remove();
}
if(options.destoryTable){
$(options.tableId).remove();
}else{
//把以前的表格移除
var tableContent = $(options.tableId).children();
if(tableContent){
tableContent.remove();
}
}
delete that.config[id];
};
this.render = function(id,options){
var that = this;
if($webUtil.isNull(id)){
$webUtil.showErrorMsg("文件管理页面错误,在调用render方法时,没有设置主键");
return false;
}
if(id in that.config){
//如果已经存在某个页面,则先销毁
that.destoryFileManage(id);
}
that.setConfig(id,options);
options = that.getConfig(id);
//最先处理Html
that.renderHtml(id,options);
//处理button
that.renderButton(id,options);
//处理表格
that.renderTable(id,options);
};
this.renderHtml = function(id,options){
//处理html,如果没有传递html的id对象
var that = this;
if($webUtil.isNull(options.elem)){
$webUtil.showErrorMsg("没有elem属性");
return false;
}
$(options.elem).prepend(that.getButtonHtml(id) + that.getTableHtml(id));
};
this.renderButton = function(id,options){
//处理按钮
var that = this;
var buttonHtml = [];
var buttonIdPrefix = options.buttonIdPrefix;
if(!buttonIdPrefix){
buttonIdPrefix = id;
}
var buttonIconMap = options.buttonIconMap;
if(!buttonIconMap){
buttonIconMap = that.buttonIconMap;
}
//是在流程中,那就要全部添加
var buttonInfoMap = options.buttonInfoMap;
if(!buttonInfoMap && ( options.inProcess || options.buttonSameProcess)){
//流程的才添加按钮
buttonInfoMap = $.extend(true, [], that.defaultButtonInfos);
if(options.reviseButton){
buttonInfoMap.push( $.extend(true,{},that.defaultReviseButtonInfo));
}
}
if(!buttonInfoMap){
//这个是说明不存在,从后台去获取按钮
var sourceData = $.extend(true,{},options.sourceData);
$webUtil.createButtonHtmlEx(sourceData, buttonIconMap, $(options.toolbarId), buttonIdPrefix, [{uniqueFlag: 'refresh',alias: '刷新'}], [], function (buttons, hasViewRight, hasQueryRight) {
$webUtil.bindDefultButtonLisenter(that, buttonIdPrefix);
if(hasQueryRight){
$webUtil.createSearchHtml({
name: '文件名称'
}, $(options.toolbarId), buttonIdPrefix);
}
$(options.toolbarId).append('');
});
}else{
//说明已经有按钮了
buttonInfoMap.push({
uniqueFlag: 'refresh',
alias: '刷新'
});//刷新是都有的
if(buttonInfoMap && buttonInfoMap.length > 0){
layui.each(buttonInfoMap,function(_index,_item){
if(_item.uniqueFlag != 'DOWNLOADFILE' || options.inProcess || options.hasDownloadRight ){
//下载,需要获取权限
buttonHtml.push($webUtil.getButtonHtmlFromBtnObject(_item, buttonIdPrefix, buttonIconMap));
}
});
}
$(options.toolbarId).prepend(buttonHtml.join(''));
$(options.toolbarId).append('');
$webUtil.bindDefultButtonLisenter(that,buttonIdPrefix);
$webUtil.createSearchHtml({
name: '文件名称'
}, $(options.toolbarId), id);
}
};
this.renderTable = function(id,options){
//处理表格
var that = this;
var fileTableColumns = options.fileTableColumns;
if(!fileTableColumns){
fileTableColumns = that.getFileTableColumns(id,((options.inProcess || options.hasDownloadRight) == true));
}
if($webUtil.isNull(options.dataOid)){
$webUtil.showErrorMsg("没有业务数据的主键信息,前端调用有问题");
return false;
}
if(!options.inProcess && $webUtil.isNull(options.dataBtmType)){
$webUtil.showErrorMsg("没有业务类型的主键信息,前端调用有问题");
return false;
}
var queryMap = {
currentButtonKey:'VIEW',
ownbizOid:options.ownbizOid,
ownbizBtm:options.ownbizBtm
};
if(options.dataIsProcessInstance){
//是流程实例
queryMap['executionid'] = options.dataOid;
queryMap['dataOid'] = "";
delete queryMap['dataOid'];
}
if(options.inProcess){
//在流程中需要绕过权限,传输下面这三个参数
queryMap['viewProcessLinkBusinessDataToken']=options.sourceData['viewProcessLinkBusinessToken'];
queryMap['executionidOnlyNumber']=options.sourceData['executionidNoInProcess'];
queryMap['businessOids']=options.sourceData['businessOidsInProcess'];
}
if(options.where){
$.extend(queryMap,options.where);
}
var table=layui.table;
if(!options.pageObject){
options.pageObject = {
limit:25,
page:1
};
}
if(options.inProcess){
options.pageObject = false;
}
var url = options.url;
if($webUtil.isNull(url)){
url = options.inProcess?that.url.process.dataGrid:(that.url.bus.controller + that.url.bus.dataGrid);
}
var tableOption = {
elem : options.tableId,
url : url,
backPath:that.backPath,
where : queryMap,
selectMode : table.selectMode.muti,
id : id,
page : options.pageObject,
cols : [fileTableColumns],
height:(options.tableHeight?options.tableHeight:''),
done:function(res,cur,total){
table.on('tool(' + id + ')',function(obj){
var data = obj.data;//当前选择行的数据
var layEvent = obj.event;//点的是什么按钮
if(layEvent == 'PREVIEW'){
//that.doPreview(data.oid,id,data.id?data.id:data.name);
//文件预览
layui.use('BaseFileDownloadAction', function () {
var vciWebFilePreview = layui['BaseFileDownloadAction'];
vciWebFilePreview.PREVIEW(data.oid,{fileOid:data.oid},function (){
$webUtil.showErrorMsg("文件预览失败,请联系管理员")
})
})
}
});
}
};
table.render(tableOption);
}
this.reloadByWhere = function(id,dataOid,dataBtmType,where){
//利用数据重新加载列表
var that = this;
var queryMap = {
currentButtonKey:'VIEW'
};
if(dataOid){
queryMap['dataOid'] = dataOid;
}
if(dataBtmType){
queryMap['dataBtmType'] = dataBtmType;
}
var options = that.getConfig(id);
if(options.dataIsProcessInstance && dataOid){
queryMap['executionid'] = dataOid;
queryMap['dataOid'] = "";
delete queryMap['dataOid'];
}
if(where){
if(!options.where){
options.where = {};
}
$.extend(queryMap,options.where,where);
}
layui.table.reload(id,{
where:queryMap,
done:function(){
//delete this.where
}
});
};
this.getFileTableColumns = function(filecomponseoid,hasDownloadFileRight){
//获取文件的列表
var that = this;
var table = layui.table;
return [
table.getIndexColumn(),
table.getCheckColumn(),
{
field:'name',
title:'文件名称',
width:250,
templet:function(d){
if(hasDownloadFileRight){
//说明有下载文件的权限,那么我们就添加一个超链接
return '' + ($webUtil.isNotNull(d.id)?d.id:d.name) + '';
}else{
if($webUtil.isNotNull(d.id)){
return d.id;
}
return d.name;
}
}
},{
field:'fileSize',
title:'大小',
width:80,
templet:function(d){
if(!d.fileSize || d.fileSize == null || d.fileSize*1 == 0 || isNaN(d.fileSize*1) ){
return "未知大小";
}else{
//原始大小是B
var filesize = d.fileSize*1;
if(filesize>1024*1024*1024*1024){
return parseInt(filesize/(1024*1024*1024*1024)) + "TB";
}else if(filesize> 1024*1024*1024){
return parseInt(filesize/(1024*1024*1024)) + "GB";
}else if(filesize> 1024*1024){
return parseInt(filesize/(1024*1024)) + "MB";
}else if(filesize> 1024){
return parseInt(filesize/1024) + "KB";
}else {
return filesize + "B";
}
}
}
},{
field:'secretGradeText',
title:'密级',
width:60,
hidden:(!configData.controllerSecret)
},{
field:'fileDocClassifyName',
title:'文档类别',
width:130
},{
field:'creator',
title:'上传人/时间',
width:210,
templet:function(d){
return d.creator + "(" + $webUtil.formateDateTimeNoSecond(d.createTime) + ")";
}
},{
field:'downloadCount',
title:'下载数量',
hidden: true,
width:90
}
];
};
this.refresh = function(event,thisButtonPoint){
var that = this;
//刷新,需要找到id
var filecomponentid = that.getFileComponentId(thisButtonPoint);
that.reloadByWhere(filecomponentid);
};
this.doPreview = function(fileoid,filecomponentid,fileName){
//执行预览
var that = this;
if($webUtil.isNull(fileoid) || $webUtil.isNull(filecomponentid)){
return false;
}
var options = that.getConfig(filecomponentid);
var url = options.previewUrl;
if($webUtil.isNull(url)){
url = that.url.bus.controller + that.url.bus.previewUrlByFileOid;
if(options.inProcess){
url = that.url.process.previewUrlByFileOid;
}
}
//后台主要是控制权限
$webUtil.post(url,{ownbizOid:options.ownbizOid,ownbizBtm:options.ownbizBtm,fileOid:fileoid},function(result){
if(result.success){
//直接调用预览的页面
//弹出窗口,还是选项卡显示
var title = '预览【' + fileName + "】";
if("tab" == options.showType){
portal.showTabByMenu(options.id + "_preview",{
text:title,
id:filecomponentid + "_preview",
multiCompent:true,
url:'usejs:FilePreviewBase?fileOids=' + fileoid + "&reswitch=" + (options.reswitch?options.reswitch:"true") + "&viewtype=" + (options.viewType?options.viewType:"pdf")
});
}else {
//默认是弹出窗口
$webUtil.dialog({
content:'',
title:title,
fullScreen:true,
success:function(layero){
var preview = layui['FilePreviewBase'];
$("#" + filecomponentid + "_preview").html(preview.getContent(filecomponentid));
preview.init(filecomponentid,{
id:filecomponentid,
fileOids:fileoid,
reswitch:(options.reswitch?options.reswitch:"true"),
viewtype:(options.viewType?options.viewType:"pdf"),
showType:'inner',
height:$webUtil.getDialogScreenHeight({fullScreen: true})-55,
ownbizOid:options.ownbizOid,
ownbizBtm:options.ownbizBtm
})
}
})
}
}else if(result.code == "notsupportpreview"){
$webUtil.showMsg("您选择的文件不支持预览,将为您跳转至下载文件",function(){
that.downloadFileByOid(fileoid,filecomponentid);
});
}else{
$webUtil.showErrorMsg(result.msg);
}
},function(xhr,error){
$webUtil.showErrorMsg("在验证是否可以预览的时候出现了错误,可能是文件服务没有启动");
},options.inProcess?that.processPath:that.backPath);
};
this.downloadFileByOid =function(oid,filecomponentid){
var that = this;
//下载文件
var iframeId = MD5(oid + $webUtil.getSystemVar($webUtil.systemValueKey.userOid));
$webUtil.fileDownload(that.backPath + that.url.downloadByFileOid + "?fileOid=" + oid +"&downloadUUID=" + iframeId);
};
this.getFileComponentId = function(button){
//根据按钮所在的位置获取文件组件的ID;
var that = this;
var filecomponentid = $(button).parent().find('[name="filecomponentid"]').val();
if($webUtil.isNull(filecomponentid)){
$webUtil.showErrorMsg("filecomponentid属性没有值,请前端开发人员检查");
return ;
}
return filecomponentid;
};
this.PREVIEW = function(event,thisButtonPoint){
//预览按钮
var that = this;
var filecomponentid = that.getFileComponentId(thisButtonPoint);
var fileoid = $webUtil.getOidFromGrid(filecomponentid,true,true);
if(!fileoid){
return false;
}
var name = $webUtil.getOidFromGrid(filecomponentid,false,false,'name');
that.doPreview(fileoid,filecomponentid,name);
};
this.DOWNLOADFILE = function(event,thisButtonPoint){
//下载按钮
var that = this;
var filecomponentid = that.getFileComponentId(thisButtonPoint);
var fileoid = $webUtil.getOidFromGrid(filecomponentid,true,true);
if(!fileoid){
return false;
}
that.downloadFileByOid(fileoid,filecomponentid);
};
this.revisefile = function(event,thisButtonPoint){
var that = this;
var filecomponentid = that.getFileComponentId(thisButtonPoint);
var fileoid = $webUtil.getOidFromGrid(filecomponentid,true,true);
if(!fileoid){
return false;
}
var options = that.getConfig(filecomponentid);
var form = layui.form;
var filter ='upload_form'+options.id;
var uploadIndex = layer.open({
type:1,
title:'修改文件',
//skin:'layui-layer-lan',
content:'',
area:['370px','200px'],
closeBtn:1,
shade:true,
shadeClose:true,
success:function(layero){
form.addItems(filter,[{
type:'text',
name:'oid',
text:'文件主键',
hidden:true
}],function(){
if($("#NCFile_upload_form_file" + filter )){
$("#NCFile_upload_form_file" + filter).remove();
}
form.setValues({
fileOid:fileoid,
updateFileFlag:true
},filter);
$("#form_" + filter).append('');
var upload = layui.upload;
//执行实例
var uploadForm = upload.render({
elem: '#button_' + filter //绑定元素
,accept:'file'
,auto:true
,url: that.backPath + that.url.reviseByFileOid//上传接口
,before:function(obj){
if(form.validata(filter)){
var values = form.getValues(filter);
obj.setData(values);
$webUtil.showProgress("文件上传中……");
return true;
}else{
$webUtil.showErrorMsg("请先输入内容后再选择文件");
return false;
}
}
,done: function(result){
if(result.success){
layer.close(uploadIndex);
$webUtil.showMsg("修改文件成功");
$webUtil.closeProgress();
that.reloadByWhere(filecomponentid);
}else{
$webUtil.showErrorMsg(result.msg);
$webUtil.closeProgress();
}
}
,error: function(){
//请求异常回调
$webUtil.showErrorMsg("上传文件出现了异常");
}
});
},{},{
defaultColumnOneRow:1
});
}
});
};
this.showFileDialog = function (id,options){
var that = this;
if(!("inProcess" in options)) {
options.inProcess = false;
}
if(options.onUse){
if(!options.buttonIconMap){
options.buttonIconMap=[];
}
options.buttonInfoMap = [{
uniqueFlag: 'DOWNLOADFILE',
alias: '下载'
},{
uniqueFlag:'PREVIEW',
alias:'预览'
}];
options.hasDownloadRight=true;
}
that.setConfig(id,options);
$webUtil.dialog({
title:(options.title?options.title:'文件管理'),
content:'',
area:[(($webUtil.isNotNull(options.width) && options.width*1>1) ?options.width*1 : 950) +'px',
(($webUtil.isNotNull(options.height) && options.height*1>1) ?options.height*1 : 670) + 'px'],
success:function(layero){
options.elem ='#referGrid_toolbar_' + id ;
that.render(id,options);
}
});
};
};
var fm = new fileManager();
exports("vciWebFileManager",fm);
});