From 0ff6508d2fd183b6b47a18b9aeca625de5f4d2a1 Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 14 Sep 2017 15:55:10 +0800 Subject: [PATCH] fin control panel --- route/dashboard.js | 4 +++ server.js | 4 ++- views/dashboard/setting.ejs | 12 ++++++- views/dashboard/statusview.ejs | 57 ++++++++++++++++++++++------------ views/install/index.ejs | 12 ++++++- 5 files changed, 67 insertions(+), 22 deletions(-) diff --git a/route/dashboard.js b/route/dashboard.js index e11165c..6c489e3 100644 --- a/route/dashboard.js +++ b/route/dashboard.js @@ -65,6 +65,8 @@ router.post('/api/print', KoaBody(), async (c, n) => { c.async = true let arr = c.request.body if (!arr.data) throw 'print data empty' + if (config.api.secret && !arr.key) throw 'access key enpty' + if (config.api.secret !== arr.key.trim()) throw 'access key error' let status = await Printer.printerString(arr.data) @@ -117,6 +119,8 @@ router.post('/api/print', KoaBody(), async (c, n) => { json.printer.serial = arr.tty json.printer.feed = feed + json.api.secret = arr.secret || '' + let wconfig = await new Promise((resolve, reject) => { fs.writeFile(path.resolve(process.env.PROJECT_ROOT, 'config.json'), JSON.stringify(json, null, 2), { mode: 0o664, diff --git a/server.js b/server.js index 22a5782..0624c5d 100644 --- a/server.js +++ b/server.js @@ -103,10 +103,12 @@ let systemStatus = { feed: 0 }, pcount: 0, - lastprint: '' + lastprint: '', + secret: '' } systemStatus.ble.enable = config.ble.enable +systemStatus.secret = config.api.secret if (config.ble.enable) { systemStatus.ble.mac = await getBTAddr() systemStatus.ble.service = config.ble.uuid.service diff --git a/views/dashboard/setting.ejs b/views/dashboard/setting.ejs index eb7fcce..a880f35 100644 --- a/views/dashboard/setting.ejs +++ b/views/dashboard/setting.ejs @@ -27,6 +27,12 @@ + +

WebAPI設定

+
+ + +
@@ -75,7 +81,8 @@ let json = { tty: '', feed: 0, - ble: '' + ble: '', + secret: '' } let dev = getValue(document.querySelector('select#tty')) let feed = getValue(document.querySelector('input#feed')) @@ -85,10 +92,13 @@ if(!dev) return alert('請選擇印表機連接埠') if (!isFinite(feed) || feed < 0) return alert('切紙前空行請輸入整數') + + let secret = getValue(document.querySelector('input#api-secret')) json.tty = dev json.feed = Math.floor(parseInt(feed)) json.ble = ble + json.secret = secret $.ajax({ url: '/api/write_config', diff --git a/views/dashboard/statusview.ejs b/views/dashboard/statusview.ejs index cd4fd53..384b3fc 100644 --- a/views/dashboard/statusview.ejs +++ b/views/dashboard/statusview.ejs @@ -2,33 +2,47 @@
- + +
-
+
-

藍芽狀態

+

系統狀態

-
啟用狀態
-
On
-
-
-
Mac Address
-
00:11:22:33:44:55:66
-
-
-
ServiceUUID
-
37B2975E-1A4B-4975-9211-9D47142B8183
-
-
-
CharacteristicUUID
-
37B2975E-1A4B-4975-9211-9D47142B8183
+
API Key
+
+
+
+
+

藍芽狀態

+
+
+
啟用狀態
+
On
+
+
+
Mac Address
+
00:11:22:33:44:55:66
+
+
+
ServiceUUID
+
37B2975E-1A4B-4975-9211-9D47142B8183
+
+
+
CharacteristicUUID
+
37B2975E-1A4B-4975-9211-9D47142B8183
+
+
+
+
+

印表機狀態

@@ -83,6 +97,8 @@ characteristic: '' } + var apikey = '' + var ws = new WebSocket('ws://' + location.host) ws.onmessage = msg => { // console.log(msg) @@ -106,6 +122,9 @@ setting.service = d.ble.service setting.characteristic = d.ble.characteristic + apikey = d.secret + $('#apikey').text(apikey) + $('#prt-con').text(d.printer.connect ? 'Connected' : 'Disconnected') $('#prt-port').text(d.printer.serial) $('#prt-feed').text(d.printer.feed) @@ -117,11 +136,11 @@ } } - $('#gen-qr').click(function () { + $('#gen-ble-qr, #gen-api-qr').click(function () { let qrdiv = document.querySelector('div#qr') qrdiv.innerHTML = '' let qr = new QRCode(qrdiv, { - text: JSON.stringify(setting, null, 2), + text: this.id === 'gen-ble-qr' ? JSON.stringify(setting, null, 2) : JSON.stringify({apikey}, null, 2), colorDark: "#000000", colorLight: "#ffffff", correctLevel: QRCode.CorrectLevel.L diff --git a/views/install/index.ejs b/views/install/index.ejs index bdda728..b2d3631 100644 --- a/views/install/index.ejs +++ b/views/install/index.ejs @@ -29,6 +29,12 @@
+

WebAPI設定

+
+ + +
+
@@ -76,7 +82,8 @@ let json = { tty: '', feed: 0, - ble: '' + ble: '', + secret: '' } let dev = getValue(document.querySelector('select#tty')) let feed = getValue(document.querySelector('input#feed')) @@ -87,9 +94,12 @@ if(!dev) return alert('請選擇印表機連接埠') if (!isFinite(feed) || feed < 0) return alert('切紙前空行請輸入整數') + let secret = getValue(document.querySelector('input#api-secret')) + json.tty = dev json.feed = Math.floor(parseInt(feed)) json.ble = ble + json.secret = secret $.ajax({ url: '/install/write_config',