diff --git a/Database/index.js b/Database/index.js index f78e1ab..8ddf9d6 100644 --- a/Database/index.js +++ b/Database/index.js @@ -1,12 +1,23 @@ const mysql = require('mysql2/promise'); async function initSQL() { - const connection = await mysql.createConnection({ + const connection = await mysql.createPool({ host: 'localhost', user: 'root', database: 'rgvofficial', - password: 'root' + password: 'root', + waitForConnections: true, //连接超额是否等待 + connectionLimit: 10, //一次创建的最大连接数 + queueLimit: 0, //可以等待的连接的个数 + maxIdle: 10, // max idle connections, the default value is the same as `connectionLimit` + idleTimeout: 60000, // idle connections timeout, in milliseconds, the default value 60000 }); + connection.getConnection(function(err, conn) { + // Do something with the connection + conn.query(/* ... */); + // Don't forget to release the connection when finished! + pool.releaseConnection(conn); + }) global.SQL = connection } diff --git a/fileStorages/16766918623846755ab8d61.jpg b/fileStorages/16766918623846755ab8d61.jpg new file mode 100644 index 0000000..2edd23f Binary files /dev/null and b/fileStorages/16766918623846755ab8d61.jpg differ diff --git a/fileStorages/16766918705615362e992c39.jpg b/fileStorages/16766918705615362e992c39.jpg new file mode 100644 index 0000000..314c35c Binary files /dev/null and b/fileStorages/16766918705615362e992c39.jpg differ diff --git a/fileStorages/167669187579672ff2c2c574.jpg b/fileStorages/167669187579672ff2c2c574.jpg new file mode 100644 index 0000000..74e71b3 Binary files /dev/null and b/fileStorages/167669187579672ff2c2c574.jpg differ diff --git a/routes/container.js b/routes/container.js index 85dc2e9..60673ea 100644 --- a/routes/container.js +++ b/routes/container.js @@ -77,7 +77,7 @@ router.post('/createContainer', async (ctx, next) => { } const createTime = new Date().getTime() const [rows, fields] = await global.SQL.execute('INSERT INTO container (menuid, name, rank, config, body, createTime) VALUES (?, ?, ?, ?, ?, ?)', [arg.menuId, arg.name, arg.rank, arg.config, arg.body, createTime]); - ctx.body = ctx.body = { + ctx.body ={ data: rows, state: true, message: '创建成功' diff --git a/routes/menu.js b/routes/menu.js index d8c1533..9331222 100644 --- a/routes/menu.js +++ b/routes/menu.js @@ -4,19 +4,82 @@ const router = require('koa-router')() router.prefix('/menu') router.get('/getMenu', async (ctx, next) => { const [rows, fields] = await global.SQL.execute('SELECT * FROM `menu` WHERE `isdelete` = 0'); - ctx.body = rows + ctx.body = { + data: rows, + state: true, + message: '获取内容成功' + } }) router.delete('/deleteMenu', async (ctx, next) => { - const [rows, fields] = await global.SQL.execute('SELECT * FROM `menu` WHERE `isdelete` = 0'); - ctx.body = rows + const arg = ctx.request.query; + if(!arg.id){ + ctx.body = { + data: {}, + state: false, + message: '缺少id' + } + return + } + const [rows, fields] = await global.SQL.execute('UPDATE menu SET isdelete = 1 WHERE `id` = ?', [arg.id]); + ctx.body = { + data: rows, + state: true, + message: '删除菜单成功' + } }) router.put('/updateMenu', async (ctx, next) => { - const [rows, fields] = await global.SQL.execute('SELECT * FROM `menu` WHERE `isdelete` = 0'); - ctx.body = rows + const arg = ctx.request.body; + const field = ['name', 'route', 'rank', 'remarks', 'father', 'icon'] + const noField = [] + field.map(item => { + if (!Object.keys(arg).includes(item)) noField.push(item) + }) + if (noField.length != 0) { + ctx.body = { + data: {}, + state: false, + message: '缺少' + noField + } + return + } + const [rows, fields] = await global.SQL.execute('UPDATE menu SET name = ? , rank = ? , remarks = ? , route = ?, icon = ? WHERE id = ?', [arg.name, arg.rank, arg.remarks, arg.route, arg.icon, Number(arg.id)]); + ctx.body = ctx.body = { + data: rows, + state: true, + message: '更新成功' + } }) -router.post('/createMenu', async (ctx, next) => { - const [rows, fields] = await global.SQL.execute('SELECT * FROM `menu` WHERE `isdelete` = 0'); - ctx.body = rows +router.post('/createMneuItem', async (ctx, next) => { + const arg = ctx.request.body; + const field = ['name', 'route', 'rank', 'remarks', 'father', 'icon'] + const noField = [] + field.map(item => { + if (!Object.keys(arg).includes(item)) noField.push(item) + }) + if (noField.length != 0) { + ctx.body = { + data: {}, + state: false, + message: '缺少' + noField + } + return + } + const [checket, checketFields] = await global.SQL.execute('SELECT * FROM `menu` WHERE `isdelete` = 0 AND `route` = ?', [arg.route]); + if(checket.length != 0){ + ctx.body = { + data: {}, + state: false, + message: '路由标志已存在:' + arg.route + } + return + } + const createTime = new Date().getTime() + const [rows, fields] = await global.SQL.execute('INSERT INTO menu (name, rank, route, father, remarks, createTime) VALUES (?, ?, ?, ?, ?, ?)', [arg.name, arg.rank, arg.route, arg.father, arg.remarks, createTime]) + ctx.body = { + data: rows, + state: true, + message: '创建成功' + } }) module.exports = router