From 493dd36e1852478974ade66f361fb441171383a1 Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 27 Mar 2017 17:32:25 +0800 Subject: [PATCH] change mysql connection to connection pool --- includes/language/zh.json | 1 + route/ResTool.js | 1 + route/api/dio.js | 30 +-------- route/api/index.js | 9 ++- route/api/iocmd.js | 9 --- route/api/iogroup.js | 32 ---------- route/api/ipcam.js | 41 ++++-------- route/api/leone.js | 55 ---------------- route/api/link.js | 40 ------------ route/api/log.js | 12 +--- route/api/modbus.js | 128 -------------------------------------- route/api/schedule.js | 40 ------------ route/api/system.js | 56 +---------------- 13 files changed, 29 insertions(+), 425 deletions(-) diff --git a/includes/language/zh.json b/includes/language/zh.json index fe71e46..e9d9f60 100644 --- a/includes/language/zh.json +++ b/includes/language/zh.json @@ -64,5 +64,6 @@ "ERR8001": "資料新增失敗", "ERR8002": "資料更新失敗", "ERR8003": "資料刪除失敗", + "ERR8100": "DB連線失敗", "ERR9000": "操作權限不足" } \ No newline at end of file diff --git a/route/ResTool.js b/route/ResTool.js index a4bfcb2..8d624e0 100644 --- a/route/ResTool.js +++ b/route/ResTool.js @@ -2,6 +2,7 @@ const errList = require('../includes/errorManager'); function send(req, res) { if ('db' in res && typeof res.db == 'object' && 'close' in res.db && typeof res.db.close == 'function') res.db.close(); + if ('db' in res && typeof res.db == 'object' && 'release' in res.db && typeof res.db.release == 'function') res.db.release(); let lngs = req.headers['accept-language'].split(','); let lng = null; diff --git a/route/api/dio.js b/route/api/dio.js index ba10c89..36b527f 100644 --- a/route/api/dio.js +++ b/route/api/dio.js @@ -3,7 +3,7 @@ const router = express.Router(); const rt = require('../ResTool'); const config = require('../../config.json'); const fs = require('fs'); -const mysql = require('../../libs/mysql_cls'); +const mysql = require('../../libs/mysql_pool'); const tool = require('../../includes/apiTool'); const exec = require('child_process').exec; const so = require('../../includes/storeObject'); @@ -65,7 +65,7 @@ router }) .catch(e => n()); }) - .post('/dotrun', (req, res, n) => { + .post('/dotrun', (req, res, n) => { if (!config.permission.dio) return n('ERR9000'); let arr = req.body; if (!arr.data) return n('ERR0000'); @@ -77,14 +77,6 @@ router record: [] }; - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let obj = so.get(req.headers['x-auth-token']); let u = ''; if (obj != null && 'user' in obj && 'account' in obj.user) u = obj.user.account; @@ -101,17 +93,9 @@ router }); }); }) - .post('/getdioinfo', (req, res, n) => { + .post('/getdioinfo', (req, res, n) => { if (!config.permission.dio) return n('ERR9000'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let q = "select * from ??.??"; let pdi = tool.promiseQuery(res, q, [config.db.db1, 'dilist'], 'di'); let pdo = tool.promiseQuery(res, q, [config.db.db1, 'dolist'], 'do'); @@ -146,14 +130,6 @@ router record: [] } - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let query = "update ??.?? set ?? = unix_timestamp() , "; let sub = " ?? = case ?? "; let dos = []; diff --git a/route/api/index.js b/route/api/index.js index 17ba639..d74ba11 100644 --- a/route/api/index.js +++ b/route/api/index.js @@ -2,12 +2,13 @@ const express = require('express'); const router = express.Router(); const errMng = require('../../includes/errorManager'); const so = require('../../includes/storeObject'); +const mysql = require('../../libs/mysql_pool'); router .get('/', (req, res) => { res.send({ name: 'WebIO API System' }); }) - .all('*', (req,res,n)=>{ + .all('*', async(req,res,n)=>{ if('x-auth-token' in req.headers) { let token = req.headers['x-auth-token']; if(so.chkKey(token)){ @@ -17,6 +18,11 @@ router } } } + try{ + res.db = await mysql.getConn(); + }catch(e){ + return n('ERR8100'); + } n(); }) .get('/showallso', (req,res)=>{ @@ -36,6 +42,7 @@ router // api error handler router.use((err, req, res, n) => { if ('db' in res && typeof res.db == 'object' && 'close' in res.db && typeof res.db.close == 'function') res.db.close(); + if ('db' in res && typeof res.db == 'object' && 'release' in res.db && typeof res.db.release == 'function') res.db.release(); let lngs = req.headers['accept-language'].split(','); let lng = null; diff --git a/route/api/iocmd.js b/route/api/iocmd.js index 7d38ead..25ea9b4 100644 --- a/route/api/iocmd.js +++ b/route/api/iocmd.js @@ -30,15 +30,6 @@ router data.record = []; res.api_res = data; - - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let dos = []; let les = []; let ios = []; diff --git a/route/api/iogroup.js b/route/api/iogroup.js index 141c821..3196f82 100644 --- a/route/api/iogroup.js +++ b/route/api/iogroup.js @@ -19,14 +19,6 @@ router if (!arr.data.id) return n('ERR0028'); } - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let query = "select * from ??.?? "; let order = " order by `iogroupuid` desc "; let param = [config.db.db1, 'iogroup']; @@ -99,14 +91,6 @@ router if (!arr.data.name) return n('ERR0026'); if (!arr.data.devs) return n('ERR0029'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let query = "insert into ??.?? (`iogroupname`,`iogroupid`,`iogroup_add_date`) values (?, ?, unix_timestamp())"; let param = [config.db.db1, 'iogroup', arr.data.name, arr.data.devs]; @@ -128,14 +112,6 @@ router if (!arr.data.name) return n('ERR0026'); if (!arr.data.devs) return n('ERR0029'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let query = "update ??.?? set \ `iogroupname` = ?, \ `iogroupid` = ?, \ @@ -159,14 +135,6 @@ router if (!arr.data) return n('ERR0000'); if (!arr.data.id) return n('ERR0028'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let query = "delete from ??.?? where `iogroupuid` = ? "; let param = [config.db.db1, 'iogroup', arr.data.id]; res.db.query(query, param, (err, row) => { diff --git a/route/api/ipcam.js b/route/api/ipcam.js index 96b470f..6a47566 100644 --- a/route/api/ipcam.js +++ b/route/api/ipcam.js @@ -24,38 +24,23 @@ router .post('/getipcamlist', async (req,res,n) => { if(!config.permission.ipcam) return n('ERR9000'); - // res.db = new mysql(); - // res.db.user = config.db.user; - // res.db.password = config.db.pass; - // res.db.host = config.db.host; - // res.db.port = config.db.port; - // res.db.database = config.db.db1; - // res.db.connect(); - - // let query = "select * from ??.??"; - // let param = [config.db.db1, 'ipcam']; - // res.db.query(query, param, (err, row) => { - // if(err) return n('ERR8000'); - - // res.api_res = { - // record: tool.checkArray(row) - // } - - // n(); - // }); try{ - res.pdb = await mysql.getConn(); - res.pdb.query('select * from ??.??', [config.db.db1, 'dilist'], (err, row) => { - console.log(err, row) - if(err) return n('ERR8000') - res.api_res = { - record: row - } - n(); - }) + res.db = await mysql.getConn(); }catch(e){ return n('ERR8100'); } + + let query = "select * from ??.??"; + let param = [config.db.db1, 'ipcam']; + res.db.query(query, param, (err, row) => { + if(err) return n('ERR8000'); + + res.api_res = { + record: tool.checkArray(row) + } + + n(); + }); }) .post('/addipcam', (req,res,n) => { if(!config.permission.ipcam) return n('ERR9000'); diff --git a/route/api/leone.js b/route/api/leone.js index 52a2780..3b71095 100644 --- a/route/api/leone.js +++ b/route/api/leone.js @@ -38,13 +38,6 @@ router ! function chkEnd() { fs.exists(config.cmdpath.scanleone_end, exists => { if (exists) { - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); let query = "select `leonename`, `leoneip`, `leonelistuid` from ??.?? where `temporary` = '1' "; let param = [config.db.db1, 'leonelist']; @@ -73,14 +66,6 @@ router if (!arr.data.id) return n('ERR0028'); } - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let query = "select * from ??.?? where `temporary` = '0' "; let param = [config.db.db1, 'leonelist']; if (s) { @@ -110,14 +95,6 @@ router if (!arr.data.ip) return n('ERR0010'); if (!arr.data.password) return n('ERR0017'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let query = "select count(*) as num from ??.?? where `temporary` = '0' "; let param = [config.db.db1, 'leonelist']; res.db.query(query, param, (err, row) => { @@ -150,14 +127,6 @@ router if (!arr.data) return n('ERR0000'); if (!arr.data.id) return n('ERR0028'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let query = "delete from ??.?? where `leonelistuid` = ? "; let param = [config.db.db1, 'leonelist', arr.data.id]; res.db.query(query, param, (err, row) => { @@ -177,14 +146,6 @@ router if (!arr.data.name) return n("ERR0026"); if (!arr.data.password) return n('ERR0017'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let query = "update ??.?? set \ `leonename` = ?,\ `leonepassword` = ?,\ @@ -220,14 +181,6 @@ router if (!arr.data) return n('ERR0000'); if (!arr.data.id || !Array.isArray(arr.data.id)) return n('ERR0028'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let ids = []; for (var i in arr.data.id) { let t = arr.data.id[i]; @@ -262,14 +215,6 @@ router if (!config.permission.leone) return n('ERR9000'); if (!tool.checkPermission(req)) return n('ERR9000'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let query = "delete from ??.?? where `temporary` = '1'"; let param = [config.db.db1, 'leonelist']; res.db.query(query, param, (err, row) => { diff --git a/route/api/link.js b/route/api/link.js index 74f3d85..e95d206 100644 --- a/route/api/link.js +++ b/route/api/link.js @@ -12,14 +12,6 @@ router .post('/getlinklist', (req, res, n) => { if (!config.permission.link) return n('ERR9000'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db8; - res.db.connect(); - let query = "select * from ??.?? \ where \ `jcioclntuid` not in ( \ @@ -50,14 +42,6 @@ router if (!arr.data) return n('ERR0000'); if (!arr.data.id) return n('ERR0028'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db8; - res.db.connect(); - res.api_res = { record: [{ root: arr.data.id }], rt: { @@ -123,14 +107,6 @@ router if (!('active' in arr.data)) return n('ERR0032'); if (!arr.data.action) return n('ERR0030'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db8; - res.db.connect(); - ! function runLoop(unit, cb) { if (typeof unit != 'object' || Object.keys(unit).length == 0) return cb(0); if (unit.type == 'ln') { @@ -204,14 +180,6 @@ router if (!arr.data) return n('ERR0000'); if (!arr.data.id) return n('ERR0028'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db8; - res.db.connect(); - let query = "update ??.?? set \ `lnactive` = (case when `lnactive` = 1 then 0 else 1 end), \ `lnmodtst` = unix_timestamp() \ @@ -237,14 +205,6 @@ router if (!arr.data) return n('ERR0000'); if (!arr.data.id) return n('ERR0028'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db8; - res.db.connect(); - let query = "select ??.??(?) as ids"; let param = [config.db.db8, 'getLink', arr.data.id]; diff --git a/route/api/log.js b/route/api/log.js index 7594f95..eebc3e3 100644 --- a/route/api/log.js +++ b/route/api/log.js @@ -2,7 +2,7 @@ const express = require('express'); const router = express.Router(); const rt = require('../ResTool'); const config = require('../../config.json'); -const mysql = require('../../libs/mysql_cls'); +const mysql = require('../../libs/mysql_pool'); const tool = require('../../includes/apiTool'); router @@ -15,19 +15,11 @@ router let p = arr.data && arr.data.p && isFinite(arr.data.p) && arr.data.p > 0 ? arr.data.p : 1; let per = arr.data && arr.data.perpage && isFinite(arr.data.perpage) && arr.data.perpage > 0 ? arr.data.perpage : config.perpage; - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db2; - res.db.connect(); - let query = "select count(*) as num from ??.??"; let param = [config.db.db2, 'jciocert']; res.db.query(query, param, (err, row) => { if(err || row.length == 0) return n('ERR0023'); - let page = res.db.recordPage(row[0].num, p, per); + let page = mysql.recordPage(row[0].num, p, per); let query = "select * from ??.?? order by `ioeventtst` desc "; let limit = ` limit ${page.rec_start} , ${per} `; diff --git a/route/api/modbus.js b/route/api/modbus.js index 0aabb92..00110be 100644 --- a/route/api/modbus.js +++ b/route/api/modbus.js @@ -14,14 +14,6 @@ router .post('/getmodbuslist', (req, res, n) => { if (!config.permission.modbus) return n('ERR9000'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db5; - res.db.connect(); - let query = "select * from ??.?? order by `uid` desc"; let param = [config.db.db5, 'device']; @@ -42,14 +34,6 @@ router if (!arr.data) return n('ERR0000'); if (!arr.data.id) return n('ERR0028'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db5; - res.db.connect(); - let query = "select * from ??.?? where `uid` = ?"; let param = [config.db.db5, 'device', arr.data.id]; @@ -93,14 +77,6 @@ router .post('/getporttype', (req, res, n) => { if (!config.permission.modbus) return n('ERR9000'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db5; - res.db.connect(); - let query = "select * from ??.?? order by `uid` "; let param = [config.db.db5, 'porttype']; @@ -120,14 +96,6 @@ router if (!arr.data.name) return n('ERR0026'); if (!('node' in arr.data)) return n('ERR0038'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db5; - res.db.connect(); - let u = ''; let obj = so.get(req.headers['x-auth-token']); if (obj != null && 'user' in obj && 'account' in obj.user) u = obj.user.account; @@ -154,14 +122,6 @@ router if (!arr.data.name) return n('ERR0026'); if (!('node' in arr.data) || !('original_node' in arr.data)) return n('ERR0038'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db5; - res.db.connect(); - let query = "select count(*) as num from ??.?? where `node` = ? and `uid` != ?"; let param = [config.db.db5, 'device', arr.data.node, arr.data.id]; res.db.query(query, param, (err, row) => { @@ -202,14 +162,6 @@ router if (!arr.data) return n('ERR0000'); if (!arr.data.id) return n('ERR0028'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db5; - res.db.connect(); - let query = "delete d,i,s,rt from ??.?? d \ left join ??.?? i \ on d.`uid` = i.`devuid` \ @@ -235,14 +187,6 @@ router if (!arr.data.id) return n('ERR0028'); if (!arr.data.type) return n('ERR0009'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db5; - res.db.connect(); - let query = "select rt.*, a.`name` as name from ??.?? rt \ left join ??.?? d \ on d.`node` = rt.`node` \ @@ -272,14 +216,6 @@ router if (!arr.data) return n('ERR0000'); if (!arr.data.id) return n('ERR0028'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db5; - res.db.connect(); - let query = "select * from ??.?? where `uid` = ? "; let param = [config.db.db5, 'iolist', arr.data.id]; res.db.query(query, param, (err, row) => { @@ -302,14 +238,6 @@ router if (!arr.data.addr) return n('ERR0048'); if (!arr.data.num) return n('ERR0049'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db5; - res.db.connect(); - let query = "select count(*) as c from ??.?? \ where \ `devuid` = ? \ @@ -342,14 +270,6 @@ router if (!arr.data.addr) return n('ERR0048'); if (!arr.data.num) return n('ERR0049'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db5; - res.db.connect(); - let query = "select count(*) as c from ??.?? i \ left join ??.?? i2 \ on i2.`type` = i.`type` \ @@ -388,14 +308,6 @@ router if (!arr.data) return n('ERR0000'); if (!arr.data.id) return n('ERR0028'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db5; - res.db.connect(); - let query = "delete i, a from ??.?? i \ left join ??.?? a \ on a.`iouid` = i.`uid` \ @@ -417,14 +329,6 @@ router if (!arr.data) return n('ERR0000'); if (!arr.data.id) return n('ERR0028'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db5; - res.db.connect(); - let query = "select a.* from ??.?? a \ left join ??.?? i \ on a.`iouid` = i.`uid` \ @@ -446,14 +350,6 @@ router if (!arr.data) return n('ERR0000'); if (!arr.data.id) return n('ERR0028'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db5; - res.db.connect(); - let query = "select * from ??.?? where `uid` = ?"; let param = [config.db.db5, 'aioset', arr.data.id]; res.db.query(query, param, (err, row) => { @@ -472,14 +368,6 @@ router if (!arr.data) return n('ERR0000'); if (!arr.data.id) return n('ERR0028'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db5; - res.db.connect(); - let query = "delete from ??.?? where `uid` = ?"; let param = [config.db.db5, 'aioset', arr.data.id]; res.db.query(query, param, (err, row) => { @@ -503,14 +391,6 @@ router if (!('scale_min' in arr.data)) return n('ERR0052'); if (!('scale_max' in arr.data)) return n('ERR0053'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db5; - res.db.connect(); - let query = "select count(*) as count from ??.?? where `iouid` = ? and `portnum` = ?"; let param = [config.db.db5, 'aioset', arr.data.iouid, arr.data.portnum]; res.db.query(query, param, (err, row) => { @@ -546,14 +426,6 @@ router if (!('scale_min' in arr.data)) return n('ERR0052'); if (!('scale_max' in arr.data)) return n('ERR0053'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db5; - res.db.connect(); - let query = "select count(*) as count from ??.?? \ where \ `iouid` = ? \ diff --git a/route/api/schedule.js b/route/api/schedule.js index 851c767..16453c6 100644 --- a/route/api/schedule.js +++ b/route/api/schedule.js @@ -23,14 +23,6 @@ router if (!arr.data.id) return n('ERR0028'); } - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let query = "select * from ??.?? "; let order = " order by `ioscheduleuid` desc "; let param = [config.db.db1, 'ioschedulet']; @@ -116,14 +108,6 @@ router if (!arr.data) return n('ERR0000'); if (!arr.data.id) return n('ERR0028'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let query = "delete from ??.?? where `ioscheduleuid` = ? "; let param = [config.db.db1, 'ioschedulet', arr.data.id]; @@ -145,14 +129,6 @@ router // let active = arr.data.active == 1 ? 1 : 0; - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let query = "update ??.?? set `ioscheduleactive` = (case when `ioscheduleactive` = '1' then '0' else '1' end) where `ioscheduleuid` = ? "; let param = [config.db.db1, 'ioschedulet', arr.data.id]; @@ -184,14 +160,6 @@ router let tmp = arr.data.action.split(','); if (tmp.length != 2) return n('ERR0030'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let active = arr.data.active == 1 ? 1 : 0; let d = ''; @@ -249,14 +217,6 @@ router let tmp = arr.data.action.split(','); if (tmp.length != 2) return n('ERR0030'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let active = arr.data.active == 1 ? 1 : 0; let d = ''; diff --git a/route/api/system.js b/route/api/system.js index 761f7e4..4fb6b62 100644 --- a/route/api/system.js +++ b/route/api/system.js @@ -101,14 +101,6 @@ router if (!arr.data.account) return n('ERR0016'); if (!arr.data.password) return n('ERR0017'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let query = "select * from ??.?? where `account` = ? and `user_password` = ?"; res.db.query(query, [config.db.db1, 'userlist', arr.data.account, arr.data.password], (err, row) => { if (err) return n('ERR8000'); @@ -159,13 +151,6 @@ router if (!arr.data) return n('ERR0000'); if (!arr.data.account) return n('ERR0016'); } - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); let query = "select * from ??.??"; let param = [config.db.db1, 'userlist'] @@ -195,14 +180,6 @@ router if (!arr.data.account) return n('ERR0016'); if (arr.data.account == 'admin') return n('ERR0037'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let query = "delete from ??.?? where `account` = ?"; let param = [config.db.db1, 'userlist', arr.data.account]; res.db.query(query, param, (err, row) => { @@ -229,14 +206,6 @@ router r = 1; } - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let query = "update ??.?? set `write_privilege` = ? , `read_privilege` = ? " + (pass.length > 0 ? " , `user_password` = ? " : "") + " where `account` = ? "; let param = [config.db.db1, 'userlist', w.toString(), r.toString()]; @@ -261,14 +230,6 @@ router let w = arr.data.write_privilege && arr.data.write_privilege == '1' ? 1 : 0; let r = arr.data.read_privilege && arr.data.read_privilege == '1' ? 1 : 0; - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - let query = "insert into ??.?? (`account`,`user_password`,`write_privilege`,`read_privilege`,`user_add_date`) values (?,?,?,?,unix_timestamp())"; let param = [config.db.db1, 'userlist', arr.data.account, arr.data.password, w.toString(), r.toString()]; @@ -282,14 +243,7 @@ router }); }) .post('/dashboard', (req, res, n) => { - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - + let data = { record: [], rt: {} @@ -399,14 +353,6 @@ router if (!arr.data) return n('ERR0000'); if (!arr.data.type) return n('ERR0009'); - res.db = new mysql(); - res.db.user = config.db.user; - res.db.password = config.db.pass; - res.db.host = config.db.host; - res.db.port = config.db.port; - res.db.database = config.db.db1; - res.db.connect(); - res.api_res = { record: [] };