/** * 这是为了便于Demo能在开发者的本地快速运行起来,采用Nodejs模拟服务端,开发者可根据此文件的注释,自行在业务系统中实现对应的功能 */ const express = require('express'); const fs = require('fs'); const path = require('path'); var urlencode = require('urlencode'); const formidable = require('formidable') var ini = require('ini') var regedit = require('regedit') const os = require('os'); const app = express() var cp = require('child_process'); var mode=-1; const querystring=require("querystring") //----开发者将WPS加载项集成到业务系统中时,需要实现的功能 Start-------- /** * 支持jsplugins.xml中,在线模式下,WPS加载项的请求地址 * 开发者可在业务系统部署时,将WPS加载项合并部署到服务端,提供jsplugins.xml中对应的请求地址即可 */ app.all('*', function (req, res, next) { res.header('Access-Control-Allow-Origin', '*'); console.log(getNow()+req.originalUrl) // res.setHeader('Content-Type','text/plain;charset=gbk'); //Access-Control-Allow-Headers ,可根据浏览器的F12查看,把对应的粘贴在这里就行 // res.header('Access-Control-Allow-Headers', 'Content-Type'); // res.header('Access-Control-Allow-Methods', '*'); // res.header('Content-Type', 'text/html;charset=utf-8'); next(); }); app.use(express.static(path.join(__dirname, "wwwroot"))); //wwwroot代表http服务器根目录 app.use('/plugin/et', express.static(path.join(__dirname, "../EtOAAssist"))); app.use('/plugin/wps', express.static(path.join(__dirname, "../WpsOAAssist"))); app.use('/plugin/wpp', express.static(path.join(__dirname, "../WppOAAssist"))); /** * 文件下载 * 开发者可将此方法更换为自己业务系统的文件下载方法, 请求中必须的参数为: filename * 如果业务系统有特殊的要求, 此方法可与各加载项中的 js / common / common.js: DownloadFile 方法对应着修改 */ app.use("/Download/:fileName", function (request, response) { var fileName = request.params.fileName; var filePath = path.join(__dirname, './wwwroot/file'); filePath = path.join(filePath, fileName); var stats = fs.statSync(filePath); if (stats.isFile()) { let name = urlencode(fileName, "utf-8"); response.set({ 'Content-Type': 'application/octet-stream', //'Content-Disposition': "attachment; filename* = UTF-8''" + name, 'Content-Disposition': "attachment; filename=" + name, 'Content-Length': stats.size }); fs.createReadStream(filePath).pipe(response); console.log(getNow() + "下载文件接被调用,文件路径:" + filePath) } else { response.writeHead(200, "Failed", { "Content-Type": "text/html; charset=utf-8" }); response.end("文件不存在"); } }); /** * 文件上传 * 开发者可将此方法更换为自己业务系统的接受文件上传的方法 * 如果业务系统有特殊的要求, 此方法可与加载项中的 js / common / common.js: UploadFile 方法对应着修改 */ app.post("/Upload", function (request, response) { const form = new formidable.IncomingForm(); var uploadDir = path.join(__dirname, './wwwroot/uploaded/'); form.encoding = 'utf-8'; form.uploadDir = uploadDir; console.log(getNow() + "上传文件夹地址是:" + uploadDir); //判断上传文件夹地址是否存在,如果不存在就创建 if (!fs.existsSync(form.uploadDir)) { fs.mkdirSync(form.uploadDir); } form.parse(request, function (error, fields, files) { for (let key in files) { let file = files[key] // 过滤空文件 if (file.size == 0 && file.name == '') continue var fileName = file.name if (!fileName) fileName = request.headers.filename let oldPath = file.path let newPath = uploadDir + fileName fs.rename(oldPath, newPath, function (error) { console.log(getNow() + "上传文件成功,路径:" + newPath) }) } response.writeHead(200, { "Content-Type": "text/html;charset=utf-8" }) response.end("测试"); }) }); /** * 模拟填充到文件的服务端数据( 加载本地的模拟json数据并提供请求) * 开发者可将此方法更换为自己业务系统的数据接口 * json数据格式及解析, 可与各加载项中的 js / common / func_docProcess.js: GetServerTemplateData 方法对应着修改 */ app.get('/getTemplateData', function (request, response) { var file = path.join(__dirname, './wwwroot/file/templateData.json'); //读取json文件 fs.readFile(file, 'utf-8', function (err, data) { if (err) { response.send('文件读取失败'); } else { response.send(data); } }); }); //----开发者将WPS加载项集成到业务系统中时,需要实现的功能 End-------- //获取file目录下文件列表 app.use("/FileList", function (request, response) { var filePath = path.join(__dirname, './wwwroot/file'); fs.readdir(filePath, function (err, results) { if (err) { response.writeHead(200, "OK", { "Content-Type": "text/html; charset=utf-8" }); response.end("没有找到file文件夹"); return; } if (results.length > 0) { var files = []; results.forEach(function (file) { if (fs.statSync(path.join(filePath, file)).isFile()) { files.push(file); } }) response.writeHead(200, "OK", { "Content-Type": "text/html; charset=utf-8" }); response.end(files.toString()); } else { response.writeHead(200, "OK", { "Content-Type": "text/html; charset=utf-8" }); response.end("当前目录下没有文件"); } }); }); //wps安装包是否正确的检测 app.use("/WpsSetup", (request, response) => { response.writeHead(200, "OK", { "Content-Type": "text/html; charset=utf-8" }) response.end("成功"); }); //wps加载项配置是否正确的检测 app.use("/OAAssistDeploy", (request, response) => { response.writeHead(200, "OK", { "Content-Type": "text/html; charset=utf-8" }) response.end("成功"); }); //检测WPS客户端环境 app.use("/WpsSetupTest", function (request, response) { configOem(request.query.pluginsMode,function (res) { response.writeHead(200, res.status, { "Content-Type": "text/html;charset=utf-8" }); response.write('
'); response.write("