diff --git a/app/cms/api/index.js b/app/cms/api/index.js index 792867b..c8741b6 100644 --- a/app/cms/api/index.js +++ b/app/cms/api/index.js @@ -1,16 +1,20 @@ 'use strict'; module.exports = { - doc : "默认页面", - path : "cms/index/", - method : ["get", "post"], - middleware: [], - params : { + // path : "cms/api/index",//可以覆盖自动生成的路由地址 + params: { "age": "int?" }, - return : {}, - fun : async (ctx, app) => { - const User = await app.table("User").where(where).findAll() - ctx.body = {res} + fun : async (ctx, app) => { + + const body = ctx.params + const User = await app.db.table("User").find() + //成功返回的2种方式 + // ctx.body = app.res.success(User,"success",200)//"success",200可以省略 + return User + + //异常返回的2种方式 + // ctx.body = app.res.error(User, "error", 400)//"error",400可以省略 + // return false } } diff --git a/app/cms/schedule/cms_test.js b/app/cms/schedule/cms_test.js index d214c95..e9dc4d6 100644 --- a/app/cms/schedule/cms_test.js +++ b/app/cms/schedule/cms_test.js @@ -2,11 +2,11 @@ // 定时任务 module.exports = { time: { - cron: '0/5 * * * * ?', + cron: '0/2 * * * * ?', // every: 2000, // limit: 100, }, fun: (app) => { - console.log("test",new Date()); + // console.log("test",new Date()); } } diff --git a/app/cms/schedule/test2.js b/app/cms/schedule/test2.js index 9fff7e8..a0b7149 100644 --- a/app/cms/schedule/test2.js +++ b/app/cms/schedule/test2.js @@ -2,11 +2,11 @@ // 定时任务 module.exports = { time: { - cron: '0/5 * * * * ?', + cron: '0/2 * * * * ?', // every: 2000, // limit: 100, }, fun: (app) => { - console.log("test2",new Date()); + // console.log("test2",new Date()); } } diff --git a/app/message/io/test.js b/app/message/io/test.js index 98fccbd..a294951 100644 --- a/app/message/io/test.js +++ b/app/message/io/test.js @@ -1,3 +1,3 @@ -module.exports = (msg, callback)=>{ +module.exports = (app, msg, callback) => { console.log('接收到的消息:', msg); } diff --git a/extend/api/config.js b/extend/api/config.js deleted file mode 100644 index 729794f..0000000 --- a/extend/api/config.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - path:"app/*/api/*.js" -} diff --git a/extend/api/index.js b/extend/api/index.js deleted file mode 100644 index 7554886..0000000 --- a/extend/api/index.js +++ /dev/null @@ -1,13 +0,0 @@ -/** - * 加载api路由 - * 加载app/api目录下的文件到路由 - */ -const config = require("./config") -module.exports = async (app) => { - let list = await app.load(config.path) - list.forEach(item => { - app.use(async (ctx, next) => { - return await item.res.fun(ctx, next) - }) - }) -} diff --git a/extend/body/body.js b/extend/body/body.js new file mode 100644 index 0000000..65a1482 --- /dev/null +++ b/extend/body/body.js @@ -0,0 +1,16 @@ +module.exports = { + success: (body, msg, status) => { + return { + res : body || null, + status: status || 200, + message : msg || "success" + } + }, + error : (body, msg, status) => { + return { + res : body || null, + status: status || 400, + message : msg || "error" + } + } +} diff --git a/extend/body/config.js b/extend/body/config.js new file mode 100644 index 0000000..631f375 --- /dev/null +++ b/extend/body/config.js @@ -0,0 +1,2 @@ +module.exports = { +} diff --git a/extend/body/index.js b/extend/body/index.js new file mode 100644 index 0000000..2139109 --- /dev/null +++ b/extend/body/index.js @@ -0,0 +1,9 @@ +/** + * 封装api返回结果 + */ +const config = require("./config") +const body = require("./body") + +module.exports = async (app) => { + app.res = body +} diff --git a/extend/bullmq/index.js b/extend/bullmq/index.js index 4a89836..aae84fe 100644 --- a/extend/bullmq/index.js +++ b/extend/bullmq/index.js @@ -3,7 +3,7 @@ * 加载schedule文件夹下的任务 * https://blog.pincman.com/archives/18/ * https://docs.bullmq.io - * + * https://github.com/OptimalBits/bull/blob/develop/REFERENCE.md */ const config = require("./config") // const schedule = require("./schedule") @@ -15,17 +15,23 @@ module.exports = async (app) => { const queueName = "boobam_schedule" const queue = mq.Queue(queueName); mq.QueueScheduler(queueName);//处理延时任务 - await queue.obliterate();//清空所有定时任务 + try { + await queue.drain();//清空所有定时任务 + await queue.obliterate();//清空所有定时任务 + } catch (err) { + console.log("清空任务失败,新改动的任务可能没有生效:", err) + } + const list = await app.load(config.path) //等待所有插件载入完成后,启动Worker app.willReadyList.push(async () => { for (let el of list) { mq.Worker(queueName, async job => { - if (job.name===el.parse.name){ + if (job.name === el.parse.name) { el.res.fun(app) } }); - await queue.add(el.parse.name,{},{ + await queue.add(el.parse.name, {}, { repeat: el.res.time, }) } diff --git a/extend/middleware/config.js b/extend/middleware/config.js index cb886ac..932b25b 100644 --- a/extend/middleware/config.js +++ b/extend/middleware/config.js @@ -1,3 +1,3 @@ module.exports = { - path:"middleware/*.js" + path:"middleware/*/index.js" } diff --git a/extend/middleware/index.js b/extend/middleware/index.js index 2404f80..870d70f 100644 --- a/extend/middleware/index.js +++ b/extend/middleware/index.js @@ -7,9 +7,15 @@ module.exports = async (app) => { let list = await app.load(config.path) list = app.xe.orderBy(list, "res.sort") list = list.filter(item => item.res.use) - list.forEach(item => { - app.use(async (ctx, next) => { - return await item.res.fun(ctx, next) - }) + list.forEach(async item => { + if (item.res.loadFun) { + //如果中间件定义了特殊加载方法 + await item.res.loadFun(app, item.res.fun) + } else { + app.use(async (ctx, next) => { + return await item.res.fun(ctx, next, app) + }) + } + }) } diff --git a/extend/socket.io/index.js b/extend/socket.io/index.js index 6a846f1..3614682 100644 --- a/extend/socket.io/index.js +++ b/extend/socket.io/index.js @@ -12,29 +12,34 @@ module.exports = async (app) => { const io = require('socket.io')(server); let list = await app.load(config.path) app.io = io - //默认命名空间 - io.on('connection', socket => { - //通知客户端已连接 - socket.emit('open', { - msg:"ok", - data:{ - id:socket.id + //等待所有插件载入完成后 + app.willReadyList.push(async () => { + //默认命名空间 + io.on('connection', socket => { + //通知客户端已连接 + socket.emit('open', { + msg : "ok", + data: { + id: socket.id + } + }); + console.log('连接=>', "id:" + socket.id); + //监听disconnect事件 + socket.on('disconnect', (eventName, callback) => { + console.log('断开=X', "id:" + socket.id) + }) + for (let el of list) { + //空间名称 + // const namespace = path.basename(path.resolve(el.parse.dir, '..')) + //事件名称 + const onname = el.parse.name + socket.on(onname, (msg, callback) => { + el.res(app, msg, callback) + }) } }); - console.log('连接=>',"id:"+socket.id); - //监听disconnect事件 - socket.on('disconnect', (eventName, callback) => { - console.log('断开=X',"id:"+socket.id) - }) - for (let el of list) { - //空间名称 - // const namespace = path.basename(path.resolve(el.parse.dir, '..')) - //事件名称 - const onname = el.parse.name - socket.on(onname, el.res) - } - }); - + + }) //覆盖启动方法 diff --git a/lib/bamboo/index.js b/lib/bamboo/index.js index 6710bc2..e38bae6 100644 --- a/lib/bamboo/index.js +++ b/lib/bamboo/index.js @@ -59,6 +59,7 @@ module.exports = class Bamboo extends Koa { // console.log("获取到的文件:", files); files = files.map(item => { const parse = path.parse(item); + parse.father = path.basename(path.resolve(parse.dir, '..')) return { parse, res: require(path.resolve(path_root + "/" + item)) diff --git a/middleware/bodyparser.js b/middleware/bodyparser/index.js similarity index 100% rename from middleware/bodyparser.js rename to middleware/bodyparser/index.js diff --git a/middleware/error.js b/middleware/error/index.js similarity index 100% rename from middleware/error.js rename to middleware/error/index.js diff --git a/middleware/koaLogger.js b/middleware/koaLogger/index.js similarity index 100% rename from middleware/koaLogger.js rename to middleware/koaLogger/index.js diff --git a/middleware/params/index.js b/middleware/params/index.js new file mode 100644 index 0000000..6004ce9 --- /dev/null +++ b/middleware/params/index.js @@ -0,0 +1,12 @@ +'use strict'; +/*封装入参*/ +module.exports = { + sort: 3, //排序 + use : true, // 是否使用 + fun : async (ctx, next, app) => { + + ctx.params = ctx.request.body || ctx.query + + await next() + } +} diff --git a/middleware/router/config.js b/middleware/router/config.js new file mode 100644 index 0000000..3c29701 --- /dev/null +++ b/middleware/router/config.js @@ -0,0 +1,5 @@ +module.exports = { + path : "app/*/api/*.js", + prefix : "",//接口前缀 + statusTobody: true,//是否跟随body结果(如需接口报错也返回200,那么设置为false) +} diff --git a/middleware/router/index.js b/middleware/router/index.js new file mode 100644 index 0000000..c27c625 --- /dev/null +++ b/middleware/router/index.js @@ -0,0 +1,50 @@ +'use strict'; +/** + * 加载api路由 + * 加载app/api目录下的文件到路由 + */ +const Router = require('koa-router') +const config = require("./config") + +// 错误处理 +module.exports = { + sort : 999, //排序 + use : true, // 是否使用 + loadFun: async (app, fun) => { // 自行定义中间件的加载方式,将覆盖默认加载方法 + const router = await fun(app) + app.use(router.routes()).use(router.allowedMethods()) + }, + fun : async (app) => { + const router = new Router({ //设置前缀 + prefix: config.prefix + }); + let list = await app.load(config.path) + list.forEach(item => { + const url = item.res.path ? item.res.path : '/' + item.parse.father + '/api/' + item.parse.name + router.all(url, async (ctx, next) => { + /*处理body结果*/ + try { + const res = await item.res.fun(ctx, app) + if (!ctx.body) { + if (res) { + ctx.body = app.res.success(res) + } else if (res === false) { + ctx.body = app.res.error() + } else { + ctx.body = app.res.success() + } + } + } catch (err) { + console.log(err); + ctx.body = app.res.error(null, "系统错误", 500) + ctx.status = 500 + } + + //是否跟随body结果 + ctx.status = config.statusTobody ? ctx.body.status || 400 : 200 + await next(); + }) + }) + return router + } +} diff --git a/middleware/time.js b/middleware/time/index.js similarity index 100% rename from middleware/time.js rename to middleware/time/index.js diff --git a/package.json b/package.json index f02b25c..ad4a792 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "koa-json-error": "^3.1.2", "koa-logger": "^2.0.1", "koa-redis": "^4.0.1", + "koa-router": "^7.4.0", "log4js": "^6.4.4", "mysql2": "^2.3.3", "node-schedule": "^2.1.0",