hn_cloud_web/smz-web/js/work/ownPerson/AnnouncementForm.js

293 lines
12 KiB
JavaScript
Raw Normal View History

2025-11-27 16:55:35 +08:00
var element;
var idw;
var nums = 0;
var flag = false;
var form;
var num= 0;
let fileNum = 0;
let addLoadingMsg;
$(function () {
layui.use(['form', 'laydate', 'upload'], function () {
form = layui.form; //只有执行了这一步,部分表单元素才会自动修饰成功
var laydate = layui.laydate;
var upload = layui.upload;
form.render();
//多图片上传
var uploadcom1 = upload.render({
elem: '#test1',
2025-12-09 18:44:29 +08:00
url: smz_ht_url + '/Announcement/uploadFile', //改成您自己的上传接口
2025-11-27 16:55:35 +08:00
multiple: true, //是否允许多文件上传默认未false
dataType: "json",
// data: {"pickId": id},
// exts: 'jpg|png|jpeg|pdf',
exts: 'doc|docx|pdf|xls|xlsx|png|jpg|jpeg',
//acceptMime: 'image/jpg,image/png,image/jpeg,file/pdf,file/xlsx',
auto: false, //是否自动上传 默认为true
number: 10, //最大上传数量
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 length = $('#demo1 img').length;
console.log(length)
if (length > 9) {
delete files[index];//删除指定图片
$(this).remove();
return layer.msg("附件为1-10张请勿过多上传", {icon: 5});
}
// 判断文件是否为PDF
if (file.type === 'application/pdf') {
$('#demo1').append('<img src="../../../img/fileIcon/pdf.png" id="' + index + '" alt="' + file.name + '" style="height: 80px;width: 80px;margin-right: 10px" class="layui-upload-img ">');
}else if (file.type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' || file.type == 'application/vnd.ms-excel') {
// 判断文件是否为XLSX、XLS
$('#demo1').append('<img src="../../../img/fileIcon/excel.png" id="' + index + '" alt="' + file.name + '" style="height: 80px;width: 80px;margin-right: 10px" class="layui-upload-img ">');
}else if (file.type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' || file.type == 'application/msword') {
// 判断文件是否为DOC、DOCX
$('#demo1').append('<img src="../../../img/fileIcon/WORD.png" id="' + index + '" alt="' + file.name + '" style="height: 80px;width: 80px;margin-right: 10px" class="layui-upload-img ">');
}else{
$('#demo1').append('<img src="' + result + '" id="' + index + '" alt="' + file.name + '" style="height: 80px;width: 80px;margin-right: 10px" class="layui-upload-img ">');
}
$('#' + index).dblclick(function () { //双击删除指定预上传图片
delete files[index];//删除指定图片
$(this).remove();
if ($("#demo1 img").length == 0) {
$("#test1").text('文件上传');
} else {
$("#test1").text($("#demo1 img").length + "个文件");
}
});
// fileExt = file.name;
if ($("#demo1 img").length == 0) {
$("#test1").text('文件上传');
} else {
$("#test1").text($("#demo1 img").length + "个文件");
}
});
},
before: function (obj) {
// fileExt = fileExt.split(".")[1];
this.data = {
id: idw,
accessoryType: "1"
};
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) {
num++;
console.log(num)
if (num == fileNum) {
num = 0;
fileNum = 0;
top.layer.close(addLoadingMsg); //再执行关闭
top.layer.msg('保存成功', {icon: 1, time: 2000});
setTimeout("reloading()", 2200);
}
},
error: function (e) {
//请求异常回调
console.log(e)
}
});
// 验证成功后才会执行下面的操作
form.on('submit(formDemo)', function (data) {
var id = $("#id").val();
if (id == '') {
insertAnnouncement(data); // 新增公告
} else {
//updateSubcontractor(data);
}
});
});
});
function setInfo(filePath) {
if (filePath != '') {
var demo1 = $("#demo2");
var html = '';
var photos = filePath.split("@");
for (var i = 0; i < photos.length; i++) {
var path = '';
if (photos[i].indexOf("http") != -1) {
path = photos[i];
} else {
2025-12-09 18:44:29 +08:00
path = smz_ht_url + "/" + photos[i];
2025-11-27 16:55:35 +08:00
}
var imgPath=""
if(path.indexOf(".pdf") !== -1){
imgPath = "../../../img/fileIcon/pdf.png";
}else if(path.indexOf(".doc") !== -1 || path.indexOf(".docx") !== -1){
imgPath = "../../../img/fileIcon/WORD.png";
}else if(path.indexOf(".xls") !== -1 || path.indexOf(".xlsx") !== -1){
imgPath = "../../../img/fileIcon/excel.png";
}else{
imgPath = path;
}
if( path.indexOf(".doc") !== -1 || path.indexOf(".docx") !== -1 || path.indexOf(".xls") !== -1 || path.indexOf(".xlsx") !== -1){
html += '<img onclick="getDownload(\'' + path + '\')" isupload = "1" src="' + imgPath + '" id="' + i + '" name="' + photos[i] + ' " style="height: 80px;width: 80px;margin: 5px " class="layui-upload-img">';
}else{
html += '<img onclick="getDownloads(\'' + path + '\')" isupload = "1" src="' + imgPath + '" id="' + i + '" name="' + photos[i] + ' " style="height: 80px;width: 80px;margin: 5px " class="layui-upload-img">';
}
}
demo1.append(html);
}
}
function getDownload(path){
console.log(path)
window.location.href = path;
}
function getDownloads(path){
window.open(path)
}
//新增通知公告
function insertAnnouncement(formData) {
addLoadingMsg = top.layer.msg('数据上传中,请稍候...', {icon: 16, scrollbar: false, time: 0, shade: [0.8, '#393D49']});
2025-12-09 18:44:29 +08:00
var formUrl = smz_ht_url + "/Announcement";
2025-11-27 16:55:35 +08:00
console.log(formData.field)
$.ajax({
type: 'POST',
async: true, // 默认异步true,false表示同步
url: formUrl, // 请求地址
contentType: "application/json; charset=utf-8",
dataType: 'text', // 服务器返回数据类型
data: JSON.stringify(formData.field), //获取提交的表单字段
success: function (data) {
console.log(data)
if (data != "error") {
if($("#demo1").children().length == 0){
top.layer.close(addLoadingMsg); //再执行关闭
parent.layer.msg("保存成功", {icon: 1, time: 2000});
setTimeout("reloading()", 2100);
}else{
if(data.includes(",")){
var arr = data.split(",").filter(Boolean);
fileNum = arr.length;
console.log(arr);
for (var i = 0; i < arr.length; i++) {
(function(id) {
idw = id;
$("#hideUpload1").trigger("click");
})(arr[i]);
}
}else{
fileNum = 1;
idw = data;
$("#hideUpload1").trigger("click");
}
}
} else if (data.resMsg == "保存失败") {
parent.layer.msg('保存失败', {icon: 2, time: 2000});
setTimeout("reloading()", 2100);
} else {
parent.layer.msg(data, {icon: 2, time: 2000});
setTimeout("reloading()", 2100);
}
},
error: function (XMLHttpRequest, textStatus, e) {
layer.msg('数据请求发生异常,请稍后重试', {icon: 16, scrollbar: false});
}
});
}
// 删除的图片数组
var deletePhoto = [];
function delPhoto(i) {
var parentcolor = document.getElementById("demo" + i);
var childcolor = document.getElementById(i+"");
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 notices() {
var height = '85%';
var width = '65%';
var index = layer.open({
title: ['', 'color:#3B70A1;background-color:#E8ECEB;font-size:20px'],
type: 2,
content: 'workerNoticeForm.html',
area: [width, height],
maxmin: false,
btn: ['确定', '关闭'],
yes: function (index, layero) {
var formSubmit = layer.getChildFrame('form', index);
var submited = formSubmit.find('button')[1];
// 触发点击事件,会对表单进行验证,验证成功则提交表单,失败则返回错误信息
submited.click();
// example.ajax.reload(); // 刷新页面
},
btn2: function (index, layero) {
}
});
}
function setData(data) {
$("#ggbt").prop("readonly", true);
$("#ggnr").prop("readonly", true);
$("#name").prop("disabled", true);
document.getElementById("image1").style.display="none";
document.getElementById("image2").style.display="block";
console.log(data[0])
// var da
$("#id").val(data[0].id);
$("#workId").val(data[0].workId);
$("#idNumber").val(data[0].idNumber);
$("#ggbt").val(data[0].ggbt);
$("#ggnr").val(data[0].ggnr);
$("#name").val(data[0].userName);
// console.log(JSON.stringify(data));
var filePath = data[0].filePath;
if(filePath.length > 1){
setInfo(filePath)
}
}
//设置弹窗关闭
function reloading() {
var index = parent.layer.getFrameIndex(window.name); //先得到当前 iframe层的索引
parent.layer.close(index); //再执行关闭
window.parent.example.ajax.reload( null, false ); // 刷新页面
}