const Controller = require('../core/base_controller'); const fs = require('fs'); const path = require('path'); const fileName = "Download"; class DownloadController extends Controller { // 导出小鹅通导入用户 async importXiaoeTech() { const { ctx } = this; try { let rdmcode = await this.checkUid(ctx); if (!rdmcode) { this.setResponse(ctx, 401, 'token过期或错误', "none"); return; } const uid = JSON.parse(rdmcode).uid; const { form, searchMsg, importForm, dataHeaders } = ctx.request.body; const result = await ctx.service.download.importXiaoeTech(uid, searchMsg, importForm); if (result == "error") { this.setResponse(ctx, 500, '服务器错误', "none"); return; } if (result.code) { const file = this.downloadData(form, result.data, dataHeaders); if (file) { const { filePath, completeName, buf } = file; try { fs.writeFileSync(filePath, Buffer.from(buf)); const data = { // 本机服务器地址 // 'http://127.0.0.1:8020' // 'http://192.168.31.28:8020' // 公司服务器地址 // 'http://123.57.205.46:8020' // 'http://certadmin.hwasmart.com//bdTrainingExamManager' fileUrl: 'http://127.0.0.1:8020' + `/public/${completeName}?time=${new Date().getTime()}`, fileName: completeName, } this.setResponse(ctx, 200, '文件已成功写入', "deconstruction", data); setTimeout(() => { fs.unlinkSync(filePath); }, 600000); } catch (error) { this.setResponse(ctx, 500, '写入文件时发生错误', "none"); } } else { this.setResponse(ctx, 404, '操作失败', "none"); } } else { this.setResponse(ctx, 404, '操作失败', "none"); } } catch (error) { const blockName = fileName + "--" + "importXiaoeTech"; ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error); this.setResponse(ctx, 500, '服务器错误', "none"); } } // 导出小鹅通开课学员 async commencementXiaoeTech() { const { ctx } = this; try { let rdmcode = await this.checkUid(ctx); if (!rdmcode) { this.setResponse(ctx, 401, 'token过期或错误', "none"); return; } const uid = JSON.parse(rdmcode).uid; const { form, searchMsg, importForm, dataHeaders } = ctx.request.body; const result = await ctx.service.download.commencementXiaoeTech(uid, searchMsg, importForm); if (result == "error") { this.setResponse(ctx, 500, '服务器错误', "none"); return; } if (result.code) { const file = this.downloadData(form, result.data, dataHeaders); if (file) { const { filePath, completeName, buf } = file; try { fs.writeFileSync(filePath, Buffer.from(buf)); const data = { // 本机服务器地址 // 'http://127.0.0.1:8020' // 'http://192.168.31.28:8020' // 公司服务器地址 // 'http://123.57.205.46:8020' // 'http://certadmin.hwasmart.com//bdTrainingExamManager' fileUrl: 'http://127.0.0.1:8020' + `/public/${completeName}?time=${new Date().getTime()}`, fileName: completeName, } this.setResponse(ctx, 200, '文件已成功写入', "deconstruction", data); setTimeout(() => { fs.unlinkSync(filePath); }, 600000); } catch (error) { this.setResponse(ctx, 500, '写入文件时发生错误', "none"); } } else { this.setResponse(ctx, 404, '操作失败', "none"); } } else { this.setResponse(ctx, 404, '操作失败', "none"); } } catch (error) { const blockName = fileName + "--" + "commencementXiaoeTech"; ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error); this.setResponse(ctx, 500, '服务器错误', "none"); } } // 导出批量报名导入 excel 表 async importApplicationData() { const { ctx } = this; try { let rdmcode = await this.checkUid(ctx); if (!rdmcode) { this.setResponse(ctx, 401, 'token过期或错误', "none"); return; } const uid = JSON.parse(rdmcode).uid; const { form, searchMsg, importForm, dataHeaders } = ctx.request.body; const result = await ctx.service.download.importApplicationData(uid, searchMsg, importForm); if (result == "error") { this.setResponse(ctx, 500, '服务器错误', "none"); return; } if (result.code) { const file = this.downloadData(form, result.data, dataHeaders); if (file) { const { filePath, completeName, buf } = file; try { fs.writeFileSync(filePath, Buffer.from(buf)); const data = { // 本机服务器地址 // 'http://127.0.0.1:8020' // 'http://192.168.31.28:8020' // 公司服务器地址 // 'http://123.57.205.46:8020' // 'http://certadmin.hwasmart.com//bdTrainingExamManager' fileUrl: 'http://127.0.0.1:8020' + `/public/${completeName}?time=${new Date().getTime()}`, fileName: completeName, } this.setResponse(ctx, 200, '文件已成功写入', "deconstruction", data); setTimeout(() => { fs.unlinkSync(filePath); }, 600000); } catch (error) { this.setResponse(ctx, 500, '写入文件时发生错误', "none"); } } else { this.setResponse(ctx, 404, '操作失败', "none"); } } else { this.setResponse(ctx, 404, '操作失败', "none"); } } catch (error) { const blockName = fileName + "--" + "importApplicationData"; ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error); this.setResponse(ctx, 500, '服务器错误', "none"); } } // 导出考试星学员导入 excel 表 async importExamStars() { const { ctx } = this; try { let rdmcode = await this.checkUid(ctx); if (!rdmcode) { this.setResponse(ctx, 401, 'token过期或错误', "none"); return; } const uid = JSON.parse(rdmcode).uid; const { form, searchMsg, importForm, dataHeaders } = ctx.request.body; const result = await ctx.service.download.importExamStars(uid, searchMsg, importForm); if (result == "error") { this.setResponse(ctx, 500, '服务器错误', "none"); return; } if (result.code) { const file = this.downloadData(form, result.data, dataHeaders); if (file) { const { filePath, completeName, buf } = file; try { fs.writeFileSync(filePath, Buffer.from(buf)); const data = { // 本机服务器地址 // 'http://127.0.0.1:8020' // 'http://192.168.31.28:8020' // 公司服务器地址 // 'http://123.57.205.46:8020' // 'http://certadmin.hwasmart.com//bdTrainingExamManager' fileUrl: 'http://127.0.0.1:8020' + `/public/${completeName}?time=${new Date().getTime()}`, fileName: completeName, } this.setResponse(ctx, 200, '文件已成功写入', "deconstruction", data); setTimeout(() => { fs.unlinkSync(filePath); }, 600000); } catch (error) { this.setResponse(ctx, 500, '写入文件时发生错误', "none"); } } else { this.setResponse(ctx, 404, '操作失败', "none"); } } else { this.setResponse(ctx, 404, '操作失败', "none"); } } catch (error) { const blockName = fileName + "--" + "importExamStars"; ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error); this.setResponse(ctx, 500, '服务器错误', "none"); } } // 导出培训证书发放用户导入 excel 表 async importIssuance() { const { ctx } = this; try { let rdmcode = await this.checkUid(ctx); if (!rdmcode) { this.setResponse(ctx, 401, 'token过期或错误', "none"); return; } const uid = JSON.parse(rdmcode).uid; const { form, searchMsg, importForm, dataHeaders } = ctx.request.body; const result = await ctx.service.download.importIssuance(uid, searchMsg, importForm); if (result == "error") { this.setResponse(ctx, 500, '服务器错误', "none"); return; } if (result.code) { const file = this.downloadData(form, result.data, dataHeaders); if (file) { const { filePath, completeName, buf } = file; try { fs.writeFileSync(filePath, Buffer.from(buf)); const data = { // 本机服务器地址 // 'http://127.0.0.1:8020' // 'http://192.168.31.28:8020' // 公司服务器地址 // 'http://123.57.205.46:8020' // 'http://certadmin.hwasmart.com//bdTrainingExamManager' fileUrl: 'http://127.0.0.1:8020' + `/public/${completeName}?time=${new Date().getTime()}`, fileName: completeName, } this.setResponse(ctx, 200, '文件已成功写入', "deconstruction", data); setTimeout(() => { fs.unlinkSync(filePath); }, 600000); } catch (error) { this.setResponse(ctx, 500, '写入文件时发生错误', "none"); } } else { this.setResponse(ctx, 404, '操作失败', "none"); } } else { this.setResponse(ctx, 404, '操作失败', "none"); } } catch (error) { const blockName = fileName + "--" + "importIssuance"; ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error); this.setResponse(ctx, 500, '服务器错误', "none"); } } // 导出培训证书发放用户照片文件夹 async importUserPhoto() { const { ctx } = this; try { let rdmcode = await this.checkUid(ctx); if (!rdmcode) { this.setResponse(ctx, 401, 'token过期或错误', "none"); return; } const uid = JSON.parse(rdmcode).uid; const { searchMsg } = ctx.request.body; const result = await ctx.service.download.importUserPhoto(uid, searchMsg); if (result == "error") { this.setResponse(ctx, 500, '服务器错误', "none"); return; } if (result.code) { const directoryPath = path.join(__dirname, '../public/userPhoto'); const outputZipPath = path.join(__dirname, '../public/userPhoto.zip'); // const directoryPath = '/disk/a/Download/userPhoto'; // const outputZipPath = '/disk/a/Download/userPhoto.zip'; if (!fs.existsSync(directoryPath)) { fs.mkdirSync(directoryPath); console.log('文件夹创建成功'); } else { console.log('文件夹已存在'); fs.rmSync(directoryPath, { recursive: true }); console.log('文件夹已删除'); fs.mkdirSync(directoryPath); console.log('文件夹创建成功'); } result.data.forEach((element, index) => { const imagePath = path.join(__dirname.replace(/\bbd-training-exam-manager\b/g, "bd-training-exam"), `../public/userPhoto/${element.identity_card}.jpg`); // const imagePath = `/disk/a/userPhoto/${element.identity_card}.jpg`; const newImagePath = path.join(directoryPath, `${element.identity_card}.jpg`); if (fs.existsSync(imagePath)) { fs.copyFileSync(imagePath, newImagePath); console.log('文件复制成功'); } else { console.log('文件不存在'); } }); // 压缩用户照片文件夹 this.zipFolder(directoryPath, outputZipPath); ctx.set({ 'Content-Disposition': 'attachment; filename="userPhoto.zip"', 'Content-Type': 'application/octet-stream', }); const data = { // 本机服务器地址 // 'http://127.0.0.1:8020' // 'http://192.168.31.28:8020' // 公司服务器地址 // 'http://123.57.205.46:8020' // 'http://certadmin.hwasmart.com//bdTrainingExamManager' fileUrl: 'http://127.0.0.1:8020' + `/public/userPhoto.zip?time=${new Date().getTime()}`, fileName: 'userPhoto.zip', } this.setResponse(ctx, 200, '下载用户照片文件夹压缩文件', "deconstruction", data); setTimeout(() => { fs.rmSync(directoryPath, { recursive: true }); fs.unlinkSync(outputZipPath); }, 600000); } else { this.setResponse(ctx, 404, '操作失败', "none"); } } catch (error) { const blockName = fileName + "--" + "importUserPhoto"; ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error); this.setResponse(ctx, 500, '服务器错误', "none"); } } // 上传用户照片 async uploadUserPhoto() { const { ctx } = this; try { let rdmcode = await this.checkUid(ctx); if (!rdmcode) { this.setResponse(ctx, 401, 'token过期或错误', "none"); return; } const { files } = ctx.request; const uploadDir = path.join(__dirname.replace(/\bbd-training-exam-manager\b/g, "bd-training-exam"), '../public/userPhoto'); // 设置文件保存目录 // const uploadDir = '/disk/a/userPhoto'; // 设置文件保存目录 files.forEach(element => { const { filename, filepath } = element; const newPath = path.join(uploadDir, filename); // 构造新文件路径 // 确保上传目录存在 if (!fs.existsSync(uploadDir)) { fs.mkdirSync(uploadDir, { recursive: true }); } // 使用 copyFileSync 复制文件,而不是 renameSync fs.copyFileSync(filepath, newPath); // 删除临时文件 fs.unlinkSync(filepath); }); this.setResponse(ctx, 200, '照片上传结束', "none"); } catch (error) { const blockName = fileName + "--" + "uploadUserPhoto"; ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error); this.setResponse(ctx, 500, '服务器错误', "none"); } } // 导出考试成绩导入 excel 表 async importGradesData() { const { ctx } = this; try { let rdmcode = await this.checkUid(ctx); if (!rdmcode) { this.setResponse(ctx, 401, 'token过期或错误', "none"); return; } const uid = JSON.parse(rdmcode).uid; const { form, formClone, dataType, passingScore, gradesType, dataHeaders } = ctx.request.body; const result = await ctx.service.download.importGradesData(uid, formClone, dataType, passingScore, gradesType); if (result == "error") { this.setResponse(ctx, 500, '服务器错误', "none"); return; } if (result.code) { const file = this.downloadData(form, result.data, dataHeaders); if (file) { const { filePath, completeName, buf } = file; try { fs.writeFileSync(filePath, Buffer.from(buf)); const data = { // 本机服务器地址 // 'http://127.0.0.1:8020' // 'http://192.168.31.28:8020' // 公司服务器地址 // 'http://123.57.205.46:8020' // 'http://certadmin.hwasmart.com//bdTrainingExamManager' fileUrl: 'http://127.0.0.1:8020' + `/public/${completeName}?time=${new Date().getTime()}`, fileName: completeName, } this.setResponse(ctx, 200, '文件已成功写入', "deconstruction", data); setTimeout(() => { fs.unlinkSync(filePath); }, 600000); } catch (error) { this.setResponse(ctx, 500, '写入文件时发生错误', "none"); } } else { this.setResponse(ctx, 404, '操作失败', "none"); } } else { this.setResponse(ctx, 404, '操作失败', "none"); } } catch (error) { const blockName = fileName + "--" + "importGradesData"; ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error); this.setResponse(ctx, 500, '服务器错误', "none"); } } } module.exports = DownloadController;