zhangjq
2026-01-22 e6b749aef5621ab5b79fc2da880c3964302bb009
public/pages/property/contractCreateFee/contractCreateFee.js
@@ -15,8 +15,6 @@
                state: '',
                roomNum: '',
                moreCondition: false,
                excelFile: '',
                excelFileData: null,
                conditions: {
                    contractName: '',
                    contractNameLike: '',
@@ -165,123 +163,7 @@
            },
            // 打开导入弹窗
            _importContract: function () {
                // 使用原生JavaScript方式打开弹窗,避免jQuery选择器问题
                var modal = document.getElementById('importContractModal');
                if (modal) {
                    // 检查是否有Bootstrap Modal实例
                    if (window.$ && $.fn.modal) {
                        // 如果jQuery和Bootstrap Modal可用,使用jQuery方式
                        $('#importContractModal').modal('show');
                    } else {
                        // 否则尝试原生方式
                        modal.style.display = 'block';
                        modal.classList.add('show');
                    }
                } else {
                    console.error('导入弹窗元素未找到');
                    vc.toast('导入功能初始化失败,请刷新页面重试');
                }
            },
            // 获取导入文件
            getExcelFile: function (e) {
                vc.component.contractCreateFeeInfo.excelFile = e.target.files[0];
                // 读取文件内容到内存,避免文件被修改时上传失败
                if (vc.component.contractCreateFeeInfo.excelFile) {
                    var reader = new FileReader();
                    reader.onload = function(e) {
                        // 将文件内容存储为Blob对象
                        vc.component.contractCreateFeeInfo.excelFileData = new Blob([e.target.result], {
                            type: vc.component.contractCreateFeeInfo.excelFile.type
                        });
                    };
                    reader.readAsArrayBuffer(vc.component.contractCreateFeeInfo.excelFile);
                }
            },
            // 导入合同数据
            _importContractData: function () {
                // 检查是否选择了文件
                if (!vc.component.contractCreateFeeInfo.excelFile || vc.component.contractCreateFeeInfo.excelFile == '') {
                    vc.toast('请选择导入文件');
                    return;
                }
                // 添加文件验证
                if (!vc.component._checkFileValid(vc.component.contractCreateFeeInfo.excelFile)) {
                    return;
                }
                // 构建FormData,使用后端要求的参数格式
                var param = new FormData();
                // 使用内存中的文件数据,避免文件被修改时上传失败
                var fileToUpload = vc.component.contractCreateFeeInfo.excelFileData || vc.component.contractCreateFeeInfo.excelFile;
                param.append('uploadFile', fileToUpload, vc.component.contractCreateFeeInfo.excelFile.name); // 后端要求的文件名参数
                param.append('communityId', vc.getCurrentCommunity().communityId); // 小区编号
                param.append('importAdapt', 'importContractV2'); // 后端提供的导入适配器
                param.append('userId', vc.getData('/userInfo/userId')); // 操作员工编号
                // 显示导入进度提示
                vc.toast('正在导入中,请稍候...');
                // 使用正确的模块名和方法名格式
                vc.http.upload(
                    'assetImport',
                    'importData',
                    param, {
                        emulateJSON: true,
                        headers: {
                            "Content-Type": "multipart/form-data"
                        }
                    },
                    function (json, res) {
                        try {
                            var _json = JSON.parse(json);
                            if (_json.code === 0) {
                                $('#importContractModal').modal('hide');
                                // 检查_json.data的类型,确保正确显示
                                if (typeof _json.data === 'object' && _json.data !== null) {
                                    // 如果是对象,跳转到导入日志详情页
                                    vc.jumpToPage('/#/pages/property/assetImportLogDetail?logId=' + _json.data.logId + '&logType=importContractV2');
                                } else {
                                    // 如果是数字,显示导入成功信息
                                    vc.toast('导入成功,共导入 ' + _json.data + ' 条数据');
                                    vc.component.listContract(vc.component.currentPage, DEFAULT_ROW);
                                }
                                // 清空文件选择
                                vc.component.contractCreateFeeInfo.excelFile = '';
                                $('#contractExcelFile').val('');
                                return;
                            }
                            vc.toast(_json.msg || '导入失败', 10000);
                        } catch (e) {
                            console.error('导入响应解析错误:', e);
                            vc.toast('导入失败:数据解析错误', 10000);
                        }
                    },
                    function (errInfo, error) {
                        console.error('导入请求失败:', errInfo, error);
                        vc.toast('导入失败:' + (errInfo || '网络错误'), 10000);
                    }
                );
            },
            // 文件验证方法
            _checkFileValid: function (_file) {
                // 验证文件类型
                const fileName = _file.name;
                const fileExt = fileName.split('.').pop().toLowerCase();
                const acceptTypes = ['xlsx', 'xls', 'csv'];
                if (!acceptTypes.includes(fileExt)) {
                    vc.toast('文件类型不支持,请选择 xlsx、xls 或 csv 格式文件');
                    return false;
                }
                // 验证文件大小(20MB)
                const maxSize = 20 * 1024 * 1024;
                if (_file.size > maxSize) {
                    vc.toast('文件大小不能超过 20MB');
                    return false;
                }
                return true;
                vc.emit('importContract', 'openImportContractModal', {});
            }
        }
    });