webio-node/route/api/log.js

47 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-03-22 05:35:45 +00:00
const express = require('express');
const router = express.Router();
const rt = require('../ResTool');
const config = require('../../config.json');
const mysql = require('../../libs/mysql_pool');
2017-03-22 05:35:45 +00:00
const tool = require('../../includes/apiTool');
router
.get('/', (req, res) => {
res.send({ name: 'WebIO Log API' })
})
.post('*', async(req,res,n) => {
try{
res.db = await mysql.getConn();
}catch(e){
console.log(`Get DB Connection ERROR ${e}`);
return n('ERR8100');
}
n();
})
2017-03-22 05:35:45 +00:00
.post('/getlog', (req, res, n) => {
if(!config.permission.log) return n('ERR9000');
let arr = req.body;
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;
let query = "select count(*) as num from ??.??";
let param = [config.db.db2, 'jciocert'];
res.db.query(query, param, (err, row) => {
2017-03-31 09:14:42 +00:00
if(err || row.length == 0) return rt.err(res,err,n,'ERR0023');
let page = mysql.recordPage(row[0].num, p, per);
2017-03-22 05:35:45 +00:00
let query = "select * from ??.?? order by `ioeventtst` desc ";
let limit = ` limit ${page.rec_start} , ${per} `;
res.db.query(query + limit, param, (err, row) => {
2017-03-31 09:14:42 +00:00
if(err) return rt.err(res,err,n,'ERR8000');
2017-03-22 05:35:45 +00:00
let data = {};
data.page = page;
data.record = tool.checkArray(row);
res.api_res = data;
return n();
});
})
})
.all('*', rt.send);
module.exports = router;