yn_hxy_web/hxy-web/js/wireManager/viewResult/uploadPhoto.js

213 lines
6.8 KiB
JavaScript
Raw 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.

//全局的页面遮盖
let loadingMsg;
var startWorkFiles = {};
var upload;
var id;
var formData;
var gtId;
var month;
var numTimes;
var userId;
var makeType;
var proId;
var nums = 0;
layui.use(['form', 'table', 'upload'], function () {
let form = layui.form; //只有执行了这一步,部分表单元素才会自动修饰成功
form.render();
upload = layui.upload;
gtId=$("#gtId").val();
month=$("#month").val();
numTimes=$("#numTimes").val();
userId=$("#userId").val();
makeType=$("#makeType").val();
proId=$("#proId").val();
uploadFile();
$("#resetBt").click(function () {
var index = parent.layer.getFrameIndex(window.name); //先得到当前 iframe层的索引
parent.layer.close(index); //再执行关闭
});
form.on('submit(formDemo)', function (data) {
console.log(startWorkFiles,"startWorkFiles")
if($.isEmptyObject(startWorkFiles)){
layer.msg("请上传文件!")
return false;
}
loadingMsg = layer.msg('数据上传中,请稍候...', {icon: 16, scrollbar: false, time: 0, shade: [0.8, '#393D49']}); //④创建一个formData对象
$("#hideUpload").trigger("click");
});
});
function setGtId(gtId,month,numTimes,resultUserId,makeType,proId){
console.log(gtId,"gtId");
console.log(month,"month");
$("#gtId").val(gtId);
$("#month").val(month);
$("#numTimes").val(numTimes);
$("#userId").val(resultUserId);
$("#makeType").val(makeType);
$("#proId").val(proId);
}
var ysUrls="";
var urls="";
function uploadFile(){
var uploadcom = upload.render({
elem: '#test',
url: FILE_URL + "/file/uploadmake" , //上传接口 //改成您自己的上传接口
multiple: true, //是否允许多文件上传默认未false
dataType: "json",
// exts: 'jpg|png',
// acceptMime: 'file',
accept: 'file',
auto: false, //是否自动上传 默认为true
number: 1, //最大上传数量
// size: 1024*5, //最大文件大小单位k
bindAction: '#hideUpload', //绑定的按钮
field: 'file', //传到后台的字段名,默认file
choose: function (obj) {
flag = false;
var suffixType = ''; //后缀类型
uploadcom.config.elem.next()[0].value = '';
obj.files = obj.pushFile();
startWorkFiles = obj.pushFile();
obj.preview(function (index, file, result) {
console.log(index); //得到文件索引
console.log(file.name); //得到文件对象
var files = $('#demo img')
var fileExtension = file.name.split('.').pop().toLowerCase();
// 检查文件是否为支持的格式
if("jpg|png".indexOf(fileExtension) == -1){
delete startWorkFiles[index]; //清空对应的文件
layer.msg('文件格式不支持', {time: 2000, icon: 5});
// 阻止文件上传
return false;
}
if(files.length <= 0){
nums++;
$('#demo').append(
'<div id="prove_' + index + '" style="display: inline-block">' +
'<div class="item">' +
'<div class="handle" style="margin-top: -28px" data-idx="prove_' + index + '">' +
'<i class="layui-icon layui-icon-delete" style="position: relative;top: 30px;left: 72px; font-size: 20px;color: red" data-idx="prove_' + index + '" ></i>' +
'</div>' +
'<img src="' + result + '" alt="' + file.name + '" title="' + file.name + '" data-index="prove_' + index + '" data-name="' + file.name + '" style="height: 100px;width: 100px;margin-right: 10px" class="layui-upload-img ">' +
'<div><span>' + (file.name.length > 8 ? (file.name.substr(0, 8) + "..") : file.name) + '</span></div>' +
'</div>'+
'</div>'
);
}else{
delete startWorkFiles[index]; //清空对应的文件
layer.msg('最多上传1张照片',{icon:2});
// 阻止文件上传
return false;
}
addDelete(startWorkFiles,obj.pushFile(),index);
});
},
before: function (obj) {
ysUrls="";
urls="";
this.data = {
gtId:gtId,
uploadType:"one",
photoType:"hxy",
month:month,
numTimes:numTimes
};
},
done: function (data) {
urls=data.data.url;
ysUrls=data.data.ysUrl;
},
allDone: function (data) {
console.log(data,"allDonedata");
if(ysUrls!=null&&urls!=null){
insertMakeGt();
setTimeout(function(){
layer.close(loadingMsg);
var iframeWindow = parent.window['layui-layer-iframe' + localStorage.getItem("resultindex")];
iframeWindow.reloadData();
var index = parent.layer.getFrameIndex(window.name); //先得到当前 iframe层的索引
parent.layer.close(index);
},1500)
}else{
layer.msg("上传错误!")
}
},
error: function (e) {
//请求异常回调
console.log(e)
}
});
}
function insertMakeGt(){
if(ysUrls.length == 0){
layer.msg('请上传文件', {icon: 2});
return;
}
$.ajax({
type:"post",
url : PATH_URL + "/makeInfo/insertMakeGt",
async:false,
data:{
gtId:gtId,
userId:userId,
makeMonth:month,
sFilePath:ysUrls,
filePath:urls,
makeType:makeType,
proId:proId,
numTimes:numTimes
},
dataType:"json",
success:function(res){
console.log(res,"res")
if(res.code==500){
parent.layer.msg(res.msg)
return;
}
},
error:function(data){
}
});
}
var deletePhoto = [];
function delPhoto(i,id) {
var parentcolor = document.getElementById("demo" + i);
var childcolor = document.getElementById(id + "");
var path = childcolor.name;
deletePhoto.push(path);
parentcolor.removeChild(childcolor);
var photoNum = $("#demo" + i).children().length;
if (photoNum == 0) {
$("#preview" + i).css("display", "none");
}
}
function addDelete(arr,pusharr,index) {
nums = 0;
$('#demo').click(function (e) {
var target = $(e.target)
if (target.hasClass('handle') || target.hasClass('layui-icon')) {
var dataIdx = target.attr('data-idx')
var filePath = $('#' + dataIdx).children().children().next().attr('file')
delete pusharr[index]; // 通过获取绑定在标签上的index删除files对象中对应的key属性即可
target.parents('.item').remove() // 页面文件标签也删除
}
});
}
//设置弹窗关闭
function reloading() {
var index = parent.layer.getFrameIndex(window.name); //先得到当前 iframe层的索引
parent.layer.close(index); //再执行关闭
window.parent.location.reload();
}