'use strict'; /** * 材料调差 - 附件 * @author LanJianRong * @date 2020/06/30 * @version */ $(document).ready(function () { $('#upload-file-ok').click(function () { const files = Array.from($('#upload-fujian-file')[0].files) const valiData = files.map(v => { const ext = v.name.substring(v.name.lastIndexOf('.') + 1) return { size: v.size, ext } }) if (validateFiles(valiData)) { if (files.length) { const formData = new FormData() files.forEach(file => { formData.append('file', file) formData.append('name', file.name) formData.append('size', file.size) }) postDataWithFile(window.location.pathname + '/upload', formData, function (result) { const files = result.map(file => { let showDel = false // 只判断当前期,因为以往期都是只读的 if (file.mid === parseInt(mid) && file.tid === parseInt(tid) && file.user_id === parseInt(cur_uid)) { if (!curAuditor) { material.status === auditConst.status.uncheck && parseInt(cur_uid) === material.user_id && (showDel = true) material.status === auditConst.status.checkNo && parseInt(cur_uid) === material.user_id && (showDel = true) } else { curAuditor.aid === parseInt(cur_uid) && (showDel = true) } } return showDel ? {...file, canDel: true} : file }) $('#addfujian').modal('hide'); let html = ''; files.forEach((fileInfo, idx) => { html += ` ${idx + 1} ${fileInfo.file_name} ${fileInfo.file_size} 第${fileInfo.s_order}期 ${fileInfo.upload_time}` if (fileInfo.canDel ) { html += ` ` } else { html += `` } }) $('#file-list').empty(); $('#file-list').append(html); }); } } }) $('#file-checkbox').click(function() { let isCheck = false if($(this).is(':checked')) { isCheck = true } postData(window.location.pathname + '/find', {isCheck}, function(result) { const files = result.map(file => { let showDel = false // 只判断当前期,因为以往期都是只读的 if (file.mid === parseInt(mid) && file.tid === parseInt(tid) && file.user_id === parseInt(cur_uid)) { if (!curAuditor) { material.status === auditConst.status.uncheck && parseInt(cur_uid) === material.user_id && (showDel = true) material.status === auditConst.status.checkNo && parseInt(cur_uid) === material.user_id && (showDel = true) } else { curAuditor.aid === parseInt(cur_uid) && (showDel = true) } } return showDel ? {...file, canDel: true} : file }) let html = ''; files.forEach((fileInfo, idx) => { html += ` ${idx + 1 } ${fileInfo.file_name} ${fileInfo.file_size} 第${fileInfo.s_order}期 ${fileInfo.upload_time}` if (fileInfo.canDel ) { html += ` ` } else { html += `` } }) $('#file-list').empty(); $('#file-list').append(html); }) }) // 删除附件 $('body').on('click', '.delete-file', function () { let attid = $(this).data('attid'); let self = $(this); const data = {id: attid}; postData('/tender/measure/material/file/delete', data, function (result) { self.parents('tr').remove(); // 重新排序 let newsort = 1; $('#file-list tr').each(function(){ $(this).children('td').eq(0).text(newsort); newsort++; }); }); }); }); /** * 校验文件大小、格式 * @param {Array} files 文件数组 */ function validateFiles(files) { const reg = /(doc|docx|excel|pdf|xlsx|xls|txt|zip|jpg|jpeg|png|bmp|BMP|JPG|PNG|JPEG|gif)$/; return files.every(file => { if (file.size > 1024 * 1024 * 10) { toastr.error('文件大小限制为10MB'); return false } if (!reg.test(file.ext)) { toastr.error('请上传正确的格式文件'); return false } return true }) }