diff --git a/config.json b/config.json index 2057132..bc936bd 100644 --- a/config.json +++ b/config.json @@ -20,7 +20,8 @@ "db8": "jciocln", "db9": "jcbtwristband", "db10": "jcmblog", - "db11": "jciocserver" + "db11": "jciocserver", + "db12": "jcioccam" }, "permission": { "dio": true, diff --git a/includes/language/zh.json b/includes/language/zh.json index d6c6bc7..0d681d7 100644 --- a/includes/language/zh.json +++ b/includes/language/zh.json @@ -66,6 +66,9 @@ "ERR0064": "MAC Addr 輸入錯誤", "ERR0065": "版本輸入錯誤", "ERR0066": "SNMP Level輸入錯誤", + "ERR0067": "型號輸入錯誤", + "ERR0068": "事件數量輸入錯誤", + "ERR0069": "圖片數量輸入錯誤", "ERR7000": "命令執行失敗", diff --git a/route/api/ipcam.js b/route/api/ipcam.js index 7bbe0ab..0279cde 100644 --- a/route/api/ipcam.js +++ b/route/api/ipcam.js @@ -13,22 +13,22 @@ router .get('/', (req, res, n) => { res.send({ name: 'WebIO IPCam API' }); }) - .post('*', async(req,res,n) => { - try{ + .post('*', async(req, res, n) => { + try { res.db = await mysql.getConn(); - }catch(e){ + } catch (e) { console.log(`Get DB Connection ERROR ${e}`); return n('ERR8100'); } n(); }) - .post('/getipcamlist', (req,res,n) => { - if(!config.permission.ipcam) return n('ERR9000'); + .post('/getipcamlist', (req, res, n) => { + if (!config.permission.ipcam) return n('ERR9000'); let query = "select * from ??.??"; - let param = [config.db.db1, 'ipcam']; + let param = [config.db.db1, 'jcioccamset']; res.db.query(query, param, (err, row) => { - if(err) return rt.err(res,err,n,'ERR8000'); + if (err) return rt.err(res, err, n, 'ERR8000'); res.api_res = { record: tool.checkArray(row) @@ -37,62 +37,131 @@ router n(); }); }) - .post('/addipcam', (req,res,n) => { - if(!config.permission.ipcam) return n('ERR9000'); - if(!tool.checkPermission(req)) return n('ERR9000'); + .post('/addipcam', (req, res, n) => { + if (!config.permission.ipcam) return n('ERR9000'); + if (!tool.checkPermission(req)) return n('ERR9000'); let arr = req.body; - if(!arr.data) return n('ERR0000'); - if(!arr.data.ip) return n('ERR0010'); - if(!arr.data.name) return n('ERR0026'); - - if(!/^\d{1,3}\.\d{1,3}.\d{1,3}.\d{1,3}$/.test(arr.data.ip)) return n('ERR0025'); + if (!arr.data) return n('ERR0000'); + if (!arr.data.ip) return n("ERR0010"); + if (!arr.data.name) return n('ERR0026'); + if (!arr.data.model) return n('ERR0067'); + if (!arr.data.maxevents) return n('ERR0068'); + if (!arr.data.maximg) return n('ERR0069'); + if (!/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(arr.data.ip)) return n('ERR0025'); + + 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; + + let active = arr.data.active == 1 ? 1 : 0; let query = "select count(*) as c from ??.?? where `ip` = ?"; - let param = [config.db.db1, 'ipcam', arr.data.ip]; + let param = [config.db.db1, 'jcioccamset', arr.data.ip]; res.db.query(query, param, (err, row) => { - if(err || row.length == 0) return rt.err(res,err,n,'ERR8000'); - if(row[0]['c'] > 0) return n('ERR0027'); + if (err || row.length == 0) return rt.err(res, err, n, 'ERR8000'); + + if (row[0].c >= 1) return n('ERR0027'); + + let query = "insert into ??.?? (`ip`, `name`, `model`, `maxevents`, `maximg`, `active`, `cuser`, `muser`, `ctime`, `mtime` ) \ + values (?, ?, ?, ?, ?, ?, ?, ?, unix_timestamp(), unix_timestamp())"; + let param = [config.db.db1, 'jcioccamset', arr.data.ip, arr.data.name, arr.data.model, arr.data.maxevents, arr.data.maximg, active, u, u]; - let query = "insert into ??.?? (`name`, `ip`) values (?,?)"; - let param = [config.db.db1, 'ipcam', arr.data.name, arr.data.ip]; res.db.query(query, param, (err, row) => { - if(err) return rt.err(res,err,n,'ERR8001'); + if (err) return rt.err(res, err, n, 'ERR8001'); + res.api_res = { record: [] - }; + } n(); - }); + }) + }) + }) + .post('/editipcam', (req, res, n) => { + if (!config.permission.ipcam) return n('ERR9000'); + if (!tool.checkPermission(req)) return n('ERR9000'); + let arr = req.body; + if (!arr.data) return n('ERR0000'); + if (!arr.data.id) return n('ERR0028'); + if (!arr.data.ip) return n("ERR0010"); + if (!arr.data.name) return n('ERR0026'); + if (!arr.data.model) return n('ERR0067'); + if (!arr.data.maxevents) return n('ERR0068'); + if (!arr.data.maximg) return n('ERR0069'); + if (!/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(arr.data.ip)) return n('ERR0025'); + + 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; + + let active = arr.data.active == 1 ? 1 : 0; + + let query = "select count(*) as c from ??.?? where `ip` = ? and `uid` != ?"; + let param = [config.db.db1, 'jcioccamset', arr.data.ip, arr.data.id]; + + res.db.query(query, param, (err, row) => { + if (err || row.length == 0) return rt.err(res, err, n, 'ERR8000'); + + if (row[0].c >= 1) return n('ERR0027'); + + let query = "update ??.?? set \ + `ip` = ?, \ + `name` = ?, \ + `model` = ?, \ + `maxevents` = ?, \ + `maximg` = ?, \ + `active` = ?, \ + `muser` = ?, \ + `mtime` = unix_timestamp() \ + where \ + `uid` = ?"; + let param = [config.db.db1, 'jcioccamset', arr.data.ip, arr.data.name, arr.data.model, arr.data.maxevents, arr.data.maximg, active, u, arr.data.id]; + res.db.query(query, param, (err, row) => { + if (err) return rt.err(res, err, n, 'ERR8002'); + + res.api_res = { + record: [] + } + n(); + }) }); }) - .post('/delipcam', (req,res,n) => { - if(!config.permission.ipcam) return n('ERR9000'); - if(!tool.checkPermission(req)) return n('ERR9000'); + .post('/delipcam', (req, res, n) => { + if (!config.permission.ipcam) return n('ERR9000'); + if (!tool.checkPermission(req)) return n('ERR9000'); let arr = req.body; - if(!arr.data) return n('ERR0000'); - if(!arr.data.id) return n('ERR0028'); + if (!arr.data) return n('ERR0000'); + if (!arr.data.id) return n('ERR0028'); + + res.db.query(`use ${config.db.db1}`); + let query = "delete from ??.?? \ + where `uid` = ?"; + let param = [config.db.db1, 'jcioccamset', arr.data.id]; - let query = "delete from ??.?? where `uid` = ?"; - let param = [config.db.db1, 'ipcam', arr.data.id]; res.db.query(query, param, (err, row) => { - if(err) return rt.err(res,err,n,'ERR8003'); + if (err) return rt.err(res, err, n, 'ERR8003'); + res.api_res = { record: [] - }; + } n(); }) }) - .post('/editipcam', (req,res,n) => { - if(!config.permission.ipcam) return n('ERR9000'); - if(!tool.checkPermission(req)) return n('ERR9000'); + .post('/swipcamactive', (req, res, n) => { + if (!config.permission.ipcam) return n('ERR9000'); + if (!tool.checkPermission(req)) return n('ERR9000'); let arr = req.body; - if(!arr.data) return n('ERR0000'); - if(!arr.data.id) return n('ERR0028'); - if(!arr.data.name) return n('ERR0026'); + if (!arr.data) return n('ERR0000'); + if (!arr.data.id) return n('ERR0028'); + + let query = "update ??.?? set \ + `active` = case when `active` = 1 then 0 else 1 end \ + where \ + `uid` = ?"; + let param = [config.db.db1, 'jcioccamset', arr.data.id]; + + res.db.query(query, param, (err, row) => { + if (err) return rt.err(res, err, n, 'ERR8002'); - let query = "update ??.?? set `name` = ? where `uid` = ?"; - let param = [config.db.db1, 'ipcam', arr.data.name, arr.data.id]; - res.db.query(query, param, (err,row) => { - if(err) return rt.err(res,err,n,'ERR8002'); res.api_res = { record: [] } diff --git a/src/components/AdminPage/IPCam/IPCamModal.js b/src/components/AdminPage/IPCam/IPCamModal.js new file mode 100644 index 0000000..9cfec2b --- /dev/null +++ b/src/components/AdminPage/IPCam/IPCamModal.js @@ -0,0 +1,68 @@ +import React from 'react'; +import { Modal, Form, Grid, Button, Input, Checkbox } from 'semantic-ui-react'; + +const IPCamModal = ({ i18n, open, type, data, closeModal, submitModal }) => { + + return ( + + + +
{ + e.preventDefault(); + submitModal(type, d.formData); + }} serializer={e=>{ + let json = {}; + + let name = e.querySelector('input[name="name"]'); + if(name && 'value' in name) json.name = name.value; + let ip = e.querySelector('input[name="ip"]'); + if(ip && 'value' in ip) json.ip = ip.value; + let model = e.querySelector('select[name="model"]'); + if(model && 'value' in model) json.model = model.value; + let maxevents = e.querySelector('input[name="maxevents"]'); + if(maxevents && 'value' in maxevents) json.maxevents = maxevents.value; + let maximg = e.querySelector('input[name="maximg"]'); + if(maximg && 'value' in maximg) json.maximg = maximg.value; + let active = e.querySelector('input[type="checkbox"]'); + if(active && 'checked' in active) json.active = active.checked; + if(type == 1) json.id = data.uid; + + return json; + }}> + + + + + + + + + + + + + + + + + + + + +