hn_cloud_web/smz-web/js/work/basic/proImport.js

320 lines
12 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

var projectId;
var CODE_1 = 0;
var CODE_2 = 0;
var num = 0;
$(function () {
layui.use(['form','upload'], function () {
var form = layui.form; //只有执行了这一步,部分表单元素才会自动修饰成功
var upload1 = layui.upload;
var upload2 = layui.upload;
form.render();
form.verify();
//多图片上传
//项目保障金附件
var uploadcom1 =upload1.render({
elem: '#test1',
url: smz_ht_url + '/project/savePayment', //改成您自己的上传接口
multiple: true, //是否允许多文件上传默认未false
dataType: "json",
// data: {"pickId": id},
exts: 'jpg|png|jpeg|pdf',
// acceptMime: 'image/jpg,image/png,image/jpeg,file/pdf',
auto: false, //是否自动上传 默认为true
number: 3, //最大上传数量
size: 1024*5, //最大文件大小单位k
bindAction: '#hideUpload1', //绑定的按钮
field: 'file', //传到后台的字段名,默认file
choose: function (obj) {
uploadcom1.config.elem.next()[0].value = '';
files = obj.pushFile();
obj.preview(function (index, file, result) {
var sum = $('#demo1').children("img").length;
CODE_1++;
if(sum > 2){
parent.layer.msg("最多可上传三张图片",{icon:2,time:2000});
return false;
}else{
console.log(index); //得到文件索引
console.log(file.name); //得到文件对象
$('#demo1').append('<img src="' + result + '" id="' + index + '" alt="' + file.name + '" style="height: 80px;width: 80px;margin-right: 10px" class="layui-upload-img ">');
$('#' + index).bind('dblclick', function () {//双击删除指定预上传图片
delete files[index];//删除指定图片
CODE_1--;
$(this).remove();
});
}
});
},
before: function (obj) {
this.data = {
'proId':projectId
};
if ($("[name='brand']").val() == '') {
return false;
} else {
console.log(files);
var names = '';
layui.each(files, function (index, file) {
console.log(file.name)
names += file.name + ',';
});
$("[name='image']").val(names)
console.log(names);
}
return true;
},
done: function (data) {
},
allDone: function (data) {
// reloading();
// fileIsNotNull();
num ++;
if(num == 2){
num = 0;
top.layer.msg('保存成功', {icon: 1, time: 2000});
setTimeout("reloading()", 2100);
}
},
error: function (e) {
//请求异常回调
console.log(e)
}
});
//多图片上传
//维权告示牌附件
var uploadcom2 =upload2.render({
elem: '#test2',
url: smz_ht_url + '/project/uploadFile', //改成您自己的上传接口
multiple: true, //是否允许多文件上传默认未false
dataType: "json",
// data: {"pickId": id},
exts: 'jpg|png|jpeg',
acceptMime: 'image/jpg,image/png,image/jpeg',
auto: false, //是否自动上传 默认为true
number: 3, //最大上传数量
size: 1024*5, //最大文件大小单位k
bindAction: '#hideUpload2', //绑定的按钮
field: 'file', //传到后台的字段名,默认file
choose: function (obj) {
uploadcom2.config.elem.next()[0].value = '';
files = obj.pushFile();
obj.preview(function (index, file, result) {
var sum = $('#demo2').children("img").length;
CODE_2++;
if(sum > 2){
parent.layer.msg("最多可上传三张图片",{icon:2,time:2000});
return false;
}else{
console.log(index); //得到文件索引
console.log(file.name); //得到文件对象
$('#demo2').append('<img src="' + result + '" id="' + index + '" alt="' + file.name + '" style="height: 80px;width: 80px;margin-right: 10px" class="layui-upload-img ">');
$('#' + index).bind('dblclick', function () {//双击删除指定预上传图片
delete files[index];//删除指定图片
CODE_2--;
$(this).remove();
});
}
});
},
before: function (obj) {
this.data = {
'type': '1', // 携带参数到后台
'proId':projectId
};
if ($("[name='brand']").val() == '') {
return false;
} else {
console.log(files);
var names = '';
layui.each(files, function (index, file) {
console.log(file.name)
names += file.name + ',';
});
$("[name='image']").val(names)
console.log(names);
}
return true;
},
done: function (data) {
},
allDone: function (data) {
// reloading();
// fileIsNotNull();
num ++;
if(num === 2){
num = 0;
top.layer.msg('保存成功', {icon: 1, time: 2000});
setTimeout("reloading()", 2100);
}
},
error: function (e) {
//请求异常回调
console.log(e)
}
});
// 验证成功后才会执行下面的操作
form.on('submit(formDemo)', function (data) {
if (CODE_1 === 0){
parent.layer.msg("请上传项目保障金附件", {icon: 2, time: 2000});
}else if(CODE_2 === 0) {
parent.layer.msg("请上传维权告示牌附件", {icon: 2, time: 2000});
}else {
importExcel();
}
});
});
$('#myModal_edit').on('show.bs.modal', centerModals);
});
//表格导入
function importExcel() {
//④创建一个formData对象
var formData = new FormData();
//⑤获取传入元素的val
var name = $("#engineeringInformationFile").val();
//⑥获取files
var files = $("#engineeringInformationFile")[0].files[0];
//⑦将name 和 files 添加到formData中键值对形式
formData.append("file", files);
formData.append("name", name);
$.ajax({
url : smz_ht_url + "/project/projectInfoImport",
type : 'POST',
data : formData,
dataType : 'json',
processData : false,// ⑧告诉jQuery不要去处理发送的数据
contentType : false, // ⑨告诉jQuery不要去设置Content-Type请求头
beforeSend : function() {
},
success : function(result) {
$("#engineeringInformationFile").val("");
if (result.success) {
layer.confirm(result.msg, {
btn: ['确认', '关闭'] //按钮
,
yes: function(index, layero) {
projectId = result.data;
$("#hideUpload1").trigger("click"); //触发上传文件
$("#hideUpload2").trigger("click"); //触发上传文件
},
cancel: function(index, layero) {
//取消操作点击右上角的X
}
}, function() {
}, function() {
});
} else {
if (result.msg === "导入数据仅支持一条"){
layer.alert(result.msg,{icon: 2});
}else {
showModal(result);
}
}
},
error : function(result) {
console.log(result);
$("#engineeringInformationFile").val("");
$.dialog({title: '提示', content: result.msg});
}
});
}
function showModal(result) {
if (result.status == 1000) {
$.dialog({title: '提示', content: result.msg});
return;
}
$('#myModal_edit').modal('hide');
var tableStr ="<table class='table table-striped'>";
tableStr = tableStr
+ "<thead>"
+ "<tr>"
+ "<th style='width: 50px;text-align:center;'>序号</th>"
+ "<th style='width: 80px;text-align:center;'>行号</th>"
+ "<th style='width: 80px;text-align:center;'>列名</th>"
+ "<th style='width: 150px;text-align:center;'>错误值</th>"
+ "<th style='text-align:center;'>错误信息</th>"
+ "</tr>"
+ "</thead>";
for (var i = 0;i < result.data.length;i++){
if (result.data[i].storePhone == "" || typeof(result.data[i].storePhone) == "undefined" ){
result.data[i].storePhone = "无";
}
tableStr = tableStr +"<tbody>"
+"<tr>"
+"<td style='text-align:center'>" + (i+1) + "</td>"
+"<td style='text-align:center'>" + result.data[i].errorRow + "</td>"
+"<td style='text-align:center'>" + result.data[i].errorLine + "</td>"
+"<td style='text-align:center'>" + result.data[i].errorData + "</td>"
+"<td style='text-align:center'>" + result.data[i].errorMsg + "</td>"
+"</tr>"
+"</tbody>";
}
tableStr = tableStr +"</table>";
$("#tableAjax").html(tableStr);
$('#myModal_edit').modal('show');
}
//pdf
function fileIsNotNull() {
var formData = new FormData($("#fm")[0]);
var testReportFile = formData.get("projectInsuranceFile").name;
if (testReportFile == "") {
parent.layer.msg("请上传附件", {icon: 2, time: 2000});
} else {
var ext = testReportFile.split('.').pop();
/*if (ext == "pdf") {*/
saveSolve(formData,ext);
/*} else {
parent.layer.msg("请上传pdf格式的文件", {icon: 2, time: 2000});
}*/
}
}
/**
* 文件上传
*/
function saveSolve(formData,ext) {
var formUrl = smz_ht_url + '/project/savePayment?ext='+ ext + '&proId=' + projectId;
$.ajax({
type: 'post',
async: false, // 默认异步true,false表示同步
url: formUrl, // 请求地址
data: formData, //获取提交的表单字段、
cache: false,
contentType: false,
processData: false,
success: function (data) {
layer.msg('上传成功');
parent.location.reload();
layer.closeAll();
},
error: function (XMLHttpRequest, textStatus, e) {
layer.msg('数据请求发生异常,请稍后重试', {icon: 16, scrollbar: false});
}
});
}
function reloading() {
var index = parent.layer.getFrameIndex(window.name); //先得到当前 iframe层的索引
parent.layer.close(index); //再执行关闭
window.parent.location.reload();
}
function centerModals() {
$('#myModal_edit').each(function (i) {
var $clone = $(this).clone().css('display', 'block').appendTo('body');
var top = Math.round(($clone.height() - $clone.find('.modal-content').height()) / 3);
top = top > 0 ? top : 0;
$clone.remove();
$(this).find('.modal-content').css("margin-top", top);
});
};