'use strict';
/**
*
*
* @author EllisRan
* @date 2022/01/21
* @version
*/
$(document).ready(() => {
$.subMenu({
menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
key: 'menu.1.0.0',
miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
callback: function (info) {
if (info.mini) {
$('.panel-title').addClass('fluid');
$('#sub-menu').removeClass('panel-sidebar');
} else {
$('.panel-title').removeClass('fluid');
$('#sub-menu').addClass('panel-sidebar');
}
autoFlashHeight();
}
});
handleFileList(fileList);
$('#file-ok').click(function () {
const files = Array.from($('#file-modal')[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('name', file.name)
formData.append('size', file.size)
formData.append('file', file)
})
postDataWithFile(preUrl + '/file/upload', formData, function (result) {
handleFileList(result);
$('#file-cancel').click()
});
}
}
})
function handleFileList(files = []) {
$('#file-content').empty();
// const { uncheck, checkNo } = auditConst.status
const newFiles = files.map(file => {
let showDel = false;
if (file.uid === cur_uid) {
// if (!curAuditor) {
// advance.status === uncheck && cur_uid === advance.uid && (showDel = true)
// advance.status === checkNo && cur_uid === advance.uid && (showDel = true)
// } else {
// curAuditor.audit_id === cur_uid && (showDel = true)
// }
if (change.status === auditConst.status.checked) {
showDel = Boolean(file.extra_upload )
} else {
showDel = true
}
}
return {...file, showDel}
})
let html = change.filePermission ? `
上传附件 |
` : '';
newFiles.forEach((file, idx) => {
if (file.showDel) {
html += `${idx + 1} | ${file.filename} | ${file.username} | ${moment(file.upload_time).format('YYYY-MM-DD HH:mm:ss')} | |
`
} else {
html += `${idx + 1} | ${file.filename} | ${file.username} | ${moment(file.upload_time).format('YYYY-MM-DD HH:mm:ss')} | |
`
}
})
$('#file-content').append(html);
}
$('#file-content').on('click', 'a', function () {
if ($(this).hasClass('file-del')) {
const id = $(this).data('id');
postData(preUrl + '/file/delete', {id}, (result) => {
handleFileList(result);
})
}
});
// 回车提交
$('#project-table input').on('keypress', function () {
if(window.event.keyCode === 13) {
$(this).blur();
}
});
$('#project-table input').blur(function () {
const val_name = $(this).data('name');
let val = _.trim($(this).val()) !== '' ? _.trim($(this).val()) : null;
switch(val_name) {
case 'code':
if(!val) {
toastr.error('立项书编号不能为空');
$(this).val(change[val_name]);
return false;
}
break;
case 'name':
if(!val) {
toastr.error('变更工程名称不能为空');
$(this).val(change[val_name]);
return false;
} else if(val.length > 100) {
toastr.error('名称超过100个字,请缩减名称');
$(this).val(change[val_name]);
return false;
}
break;
case 'org_price':
case 'change_price':
case 'crease_price':
val = val ? parseFloat(val) : null;
if(val && !_.isNumber(val)) {
toastr.error('请输入数字');
$(this).val(change[val_name]);
return false;
}
break;
default:
if(val && val.length > 255) {
toastr.error('超出字段范围,请缩减');
$(this).val(change[val_name]);
return false;
}
break;
}
if(change[val_name] !== val) {
const _self = $(this);
postData(preUrl + '/save', { name: val_name, val}, function (result) {
change[val_name] = val;
_self.val(change[val_name]);
if (val_name === 'code') {
$('#change-project-code').text(change[val_name]);
}
}, function () {
_self.val(change[val_name]);
})
} else {
$(this).val(change[val_name]);
}
});
$('#project-table textarea').blur(function () {
const val_name = $(this).data('name');
let val = _.trim($(this).val()) !== '' ? _.trim($(this).val()) : null;
if(change[val_name] !== val) {
const _self = $(this);
postData(preUrl + '/save', { name: val_name, val}, function (result) {
change[val_name] = val;
_self.val(change[val_name]);
}, function () {
_self.val(change[val_name]);
})
} else {
$(this).val(change[val_name]);
}
});
$('#project-table select').change(function () {
const val_name = $(this).attr('data-name');
let val = _.trim($(this).val()) !== '' ? _.trim($(this).val()) : null;
if(change[val_name] !== val) {
const _self = $(this);
postData(preUrl + '/save', { name: val_name, val}, function (result) {
change[val_name] = val;
_self.val(change[val_name]);
}, function () {
_self.val(change[val_name]);
})
} else {
$(this).val(change[val_name]);
}
})
});
/**
* 校验文件大小、格式
* @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
})
}