123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- 'use strict';
- /**
- * 材料调差 - 附件
- * @author LanJianRong
- * @date 2020/06/30
- * @version
- */
- $(document).ready(function () {
- // 全局fileData初始化
- let fileData = fileList || []
- handleFileList(fileData)
- getAllList()
- $('#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) {
- handleFileList(result)
- $('#addfujian').modal('hide');
- if (!$('#file-list tr').length) {
- getAllList();
- } else {
- getAllList(parseInt($('#currentPage').text()));
- }
- });
- }
- }
- })
- // 选择/未选所有期列表
- $('#file-checkbox').click(function() {
- getAllList()
- })
- // 删除附件
- $('body').on('click', '.delete-file', function () {
- let attid = $(this).data('attid');
- const data = {id: attid};
- postData('/tender/measure/material/file/delete', data, function () {
- const idx = fileData.findIndex(file => file.id === parseInt(attid))
- idx !== -1 && fileData.splice(idx, 1)
- if ($('#file-list tr').length === 1) {
- getAllList(parseInt($('#currentPage').text()) - 1);
- } else {
- getAllList(parseInt($('#currentPage').text()));
- }
- // self.parents('tr').remove();
- // // 重新排序
- // let newsort = 1;
- // $('#file-list tr').each(function(){
- // $(this).children('td').eq(0).text(newsort);
- // newsort++;
- // });
- });
- });
- // 切换页数
- $('.page-select').on('click', function () {
- const totalPageNum = parseInt($('#totalPage').text());
- const lastPageNum = parseInt($('#currentPage').text());
- const status = $(this).attr('content');
- if (status === 'pre' && lastPageNum > 1) {
- getAllList(lastPageNum-1);
- } else if (status === 'next' && lastPageNum < totalPageNum) {
- getAllList(lastPageNum+1);
- }
- });
- // 生成所有附件列表
- function getAllList(currPageNum = 1) {
- // 每页最多几个附件
- const pageCount = 15;
- // 附件总数
- let total = fileData && fileData.length;
- // 未选中checkbox,需要过滤出来当前期的数据
- const filterFileData = fileData && fileData.filter(file => file.mid === parseInt(mid) && file.tid === parseInt(tid))
- if(!$('#file-checkbox').is(':checked')) {
- total = filterFileData.length
- }
- // 总页数
- const pageNum = Math.ceil(total/pageCount);
- $('#totalPage').text(pageNum);
- // total为0,当前还没上传过附件
- $('#currentPage').text(total ? currPageNum : 0);
- // 当前页附件内容
- const currPageAttData = fileData && $('#file-checkbox').is(':checked') ? fileData.slice((currPageNum-1)*pageCount, currPageNum*pageCount) : filterFileData.slice((currPageNum-1)*pageCount, currPageNum*pageCount);
- renderHtml(currPageAttData)
- }
- function renderHtml(list) {
- let html = '';
- list.forEach((fileInfo, idx) => {
- html += `<tr style="height: 31px;">
- <td>${idx + 1}</td>
- <td><a href="/${fileInfo.filepath}" target="_blank">${fileInfo.file_name}</a></td>
- <td>${fileInfo.file_size}</td>
- <td>第${fileInfo.s_order}期</td>
- <td>${fileInfo.upload_time}</td>`
- if (fileInfo.canDel ) {
- html += `<td>
- <a class="btn btn-light btn-sm delete-file" data-attid="${fileInfo.id}" title="删除附件">
- <span class="fa fa-trash text-danger"></span>
- </a>
- </td></tr>`
- } else {
- html += `<td></td></tr>`
- }
- })
- $('#file-list').empty();
- $('#file-list').append(html);
- }
- function handleFileList(fileList) {
- fileData = fileList.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
- })
- }
- });
- /**
- * 校验文件大小、格式
- * @param {Array} files 文件数组
- */
- function validateFiles(files) {
- if (files.length > 10) {
- toastr.error('至多同时上传10个文件');
- return false
- }
- return files.every(file => {
- if (file.size > 1024 * 1024 * 30) {
- toastr.error('文件大小限制为30MB');
- return false
- }
- if (whiteList.indexOf('.' + file.ext) === -1) {
- toastr.error('请上传正确的格式文件');
- return false
- }
- return true
- })
- }
|