Commit 5df9ec53 by chenjinjing

no message

parent 1439523c
......@@ -7,3 +7,5 @@
/video
*.logs
*.zip
/res
/files
<config>
<port>9097</port>
<sign>xxx90909082fsdahfjosadjfpoiwausjorip2hjklrhn1ioud0u124rx0qwejfokasjfolksaujfoas</sign>
<dbServer>http://192.168.0.189:9096</dbServer>
<fileIP>http://127.0.0.1:40004</fileIP>
<dbServer>http://192.168.0.71:40012</dbServer>
<fileIP>http://192.168.0.71:40004</fileIP>
<SECRET_TOKEN>Ngz86cuAKxblwXR9OiKSWbfkj7oZ8R0lMU8pTfpVYBDCkvtUb0ZwbaBvwWyfv2O9</SECRET_TOKEN>
</config>
......@@ -10,6 +10,6 @@ export enum OPERATIONALDATATYPE {
*/
export enum TABLENAME {
企业用户表='enterprise_user',
管理后台用户 = 'adminUser',
管理后台用户 = 'adminuser',
企业基础信息表 ='enterprise'
}
......@@ -5,7 +5,7 @@ export enum ERRORENUM {
身份验证过期,
参数错误,
文件上传失败,
只能上传docdocxpngjpg图片,
只能上传docdocxexcelpngjpg图片,
上传时缺失关键参数,
预检请求,
空文件失败
......
......@@ -19,7 +19,6 @@ export async function parseFormParam(req, res, next) {
maxFildsSize:10*1024*1024,
keepExtensions:true
});
form.parse(req, (err, fields, files)=>{
if (err) {
......@@ -43,14 +42,16 @@ export async function parseFormParam(req, res, next) {
if (!req.files.formData || !req.files.formData.name) {
return next(new BizError(ERRORENUM.文件上传失败) );
}
console.log(req.files.formData.type);
//获取文件扩展名
const fileExtension = req.files.formData.name.split('.').pop().toLowerCase();
if ( req.files.formData.type == 'image/png') {
req.fileType = '.png';
return next();
} else if (req.files.formData.type == 'image/jpg' || req.files.formData.type == 'image/jpeg') {
req.fileType = '.jpg';
return next();
} else if (req.files.formData.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") {
} else if (req.files.formData.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" || fileExtension == 'docx') {
req.fileType = '.docx';
return next();
} else if (req.files.formData.type == "application/msword") {
......@@ -62,8 +63,11 @@ export async function parseFormParam(req, res, next) {
} else if (req.files.formData.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"){
req.fileType = '.xlsx';
return next();
} else if (req.files.formData.type == "application/vnd.ms-excel"){
req.fileType = '.xls';
return next();
} else {
return next(new BizError(ERRORENUM.只能上传docdocxpngjpg图片) )
return next(new BizError(ERRORENUM.只能上传docdocxexcelpngjpg图片) )
}
}
})
......
......@@ -5,30 +5,63 @@ import compression = require('compression');
import { watch } from '../middleware/watch';
import { httpErrorHandler } from '../middleware/httpErrorHandler';
import { authentication } from '../middleware/user';
import path = require('path');
export class httpServer {
static createServer(port:number) {
var httpServer = express();
const SECRET_TOKEN = "Ngz86cuAKxblwXR9OiKSWbfkj7oZ8R0lMU8pTfpVYBDCkvtUb0ZwbaBvwWyfv2O9";
const FILES_BASE_PATH = '/yuyi/files';
// 全局 CORS 设置(处理 OPTIONS 预检请求)
httpServer.all('*', (req, res, next) => {
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header('Access-Control-Allow-Headers', 'Content-Type,request-origin,userid,Userid,Token,token,uptype');
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header('Access-Control-Allow-Origin', req.headers.origin || '*');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, authorization, request-origin, userid, Userid, Token, token, uptype');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.header('Access-Control-Allow-Credentials', 'true');
res.header("X-Powered-By", ' 3.2.1');
res.header('X-Powered-By', '3.2.1');
// 直接响应 OPTIONS 预检请求
if (req.method === 'OPTIONS') {
return res.status(200).end();
}
next();
});
// httpServer.use(bodyParser.json({limit:'5mb'}));
// httpServer.use(bodyParser.urlencoded({limit:'5mb', extends:true}));
// 文件下载的授权验证中间件(跳过 OPTIONS 请求)
httpServer.use(FILES_BASE_PATH, (req, res, next) => {
const filePath = path.join(__dirname, '../../files', req.url.replace(FILES_BASE_PATH, ''));
console.log('请求文件路径:', filePath); // 调试日志
console.log('请求方法:', req.method); // 添加日志
if (req.method === 'OPTIONS' || req.method === 'HEAD') {
return next();
}
const authHeader = req.headers['token'];
console.log('认证token:', authHeader); // 调试日志
if (authHeader === SECRET_TOKEN) {
next();
} else {
res.status(401).json({
code: 401,
message: 'Unauthorized: Missing or invalid Authorization header'
});
}
});
// 设置静态文件服务,只对/yuyi/files路径生效
// httpServer.use(FILES_BASE_PATH, express.static(path.join(__dirname, '../../files')));
httpServer.use(FILES_BASE_PATH, express.static(path.join(__dirname, '../../files/yuyi/files')));
httpServer.use(watch);
httpServer.use(authentication);
httpServer.use( express.static("./files") );
// httpServer.use( express.static("./files") );
httpServer.use(compression());
......@@ -39,4 +72,16 @@ export class httpServer {
httpServer.listen(port);
console.log('server listen on port:'+port);
}
}
\ No newline at end of file
}
// // 验证函数
// function validateToken(Authorization: string): boolean {
// let result = false;
// if (Authorization == "Ngz86cuAKxblwXR9OiKSWbfkj7oZ8R0lMU8pTfpVYBDCkvtUb0ZwbaBvwWyfv2O9") {
// result = true;
// }
// return result; // 示例代码,实际使用时需要替换
// }
/**
* 总路由入口
*/
import * as asyncHandler from 'express-async-handler'
import asyncHandler = require('express-async-handler');
import { parseFormParam } from '../middleware/fromParam';
import { getFilesId } from '../tools/systemTools';
const fs = require('fs');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment