85 lines
2.6 KiB
JavaScript
85 lines
2.6 KiB
JavaScript
/**
|
|
* @param {Egg.Application} app - egg application
|
|
*/
|
|
const fs = require('fs').promises;
|
|
const path = require('path');
|
|
module.exports = app => {
|
|
const { router, controller,io } = app;
|
|
// 定义一个需要鉴权的路由组
|
|
function mapRoutes(routes) {
|
|
routes.forEach(route => {
|
|
const [path, method, auth] = route.split('|');
|
|
const [controllerName, methodName] = path.split('/');
|
|
const handler = controller[controllerName][methodName];
|
|
|
|
let middleware = null;
|
|
if (auth === '1') {
|
|
middleware = app.middleware.auth;
|
|
}
|
|
|
|
if (handler && typeof handler === 'function') {
|
|
if (method === 'get') {
|
|
if(middleware){
|
|
router.get(`/${controllerName}/${methodName}`, middleware, handler);
|
|
}else{
|
|
router.get(`/${controllerName}/${methodName}`, handler);
|
|
}
|
|
} else if (method === 'post') {
|
|
if(middleware){
|
|
router.post(`/${controllerName}/${methodName}`, middleware, handler);
|
|
}else{
|
|
router.post(`/${controllerName}/${methodName}`, handler);
|
|
}
|
|
}
|
|
// Add more methods as needed
|
|
} else {
|
|
console.error(`Handler function ${controllerName}.${methodName} is not defined or is not a function.`);
|
|
}
|
|
});
|
|
}
|
|
|
|
const authRoutesList = [
|
|
'auth/getLoginercode|get|0',
|
|
'auth/getLoginstatus|post|0',
|
|
'tgdp/ll|post|0',
|
|
'tgdp/jh|post|0',
|
|
'tgdp/fh|post|0',
|
|
'tgdp/db|post|0',
|
|
'workspace/index|post|1',
|
|
'robot/index|post|1',
|
|
'tool/index|post|1',
|
|
'tool/backMsg|post|0',
|
|
'tool/createOrder|post|0',
|
|
'dashboard/index|post|1',
|
|
'pda/index|get|0',
|
|
'miniProgram/index|post|1',
|
|
'dgj/Print|post|0',
|
|
'dgj/Print1|post|0',
|
|
'dgj/Print2|post|0',
|
|
'dgj/CreateOrder|post|0',
|
|
'express/aotoCheckLanshou|get|0',
|
|
'sandan/scan|post|0',
|
|
'sandan/scan2|post|0',
|
|
'sandan/createCache|post|0'
|
|
];
|
|
mapRoutes(authRoutesList);
|
|
|
|
// 通用请求接口: POST /system { "name": "someAction", "data": { ... } }
|
|
router.post('/system', controller.system.index);
|
|
//home 首页
|
|
//router.get('/', controller.home.index);
|
|
router.get('/', async ctx => {
|
|
try {
|
|
ctx.page = true;
|
|
const filePath = path.join(app.baseDir, 'app/assets/index.html');
|
|
const content = await fs.readFile(filePath);
|
|
ctx.type = 'html';
|
|
ctx.body = content;
|
|
} catch (err) {
|
|
ctx.status = 404;
|
|
ctx.body = 'File not found';
|
|
}
|
|
});
|
|
io.of('/').route('genericEvent', io.controller.generic.generic);
|
|
};
|