add setting page
This commit is contained in:
@@ -7,6 +7,7 @@ const path = require('path')
|
||||
const Printer = require('../PrinterDev')
|
||||
const KoaBody = require('koa-body')
|
||||
const uuid = require('uuid')
|
||||
const config = require('../config.json')
|
||||
/* eslint-enable */
|
||||
|
||||
router.use(async (c, n) => {
|
||||
@@ -34,6 +35,29 @@ router.use(async (c, n) => {
|
||||
})
|
||||
|
||||
router.get('/', async (c, n) => {
|
||||
c.data.route = 'index'
|
||||
await c.render('dashboard/index', c.data)
|
||||
})
|
||||
|
||||
router.get('/setting', async (c, n) => {
|
||||
c.data.route = 'setting'
|
||||
|
||||
let ttys = await new Promise((resolve, reject) => {
|
||||
fs.readdir(path.resolve('/dev'), (err, list) => {
|
||||
if (err) return resolve([])
|
||||
let arr = []
|
||||
for (let it of list) {
|
||||
if (/^tty[A-z]/.test(it)) {
|
||||
arr.push(path.resolve('/dev', it))
|
||||
}
|
||||
}
|
||||
resolve(arr)
|
||||
})
|
||||
})
|
||||
c.data.ttys = ttys
|
||||
|
||||
c.data.defConfig = config
|
||||
|
||||
await c.render('dashboard/index', c.data)
|
||||
})
|
||||
|
||||
@@ -49,4 +73,63 @@ router.post('/api/print', KoaBody(), async (c, n) => {
|
||||
c.body = 'print success'
|
||||
})
|
||||
|
||||
.post('/api/printer_test', KoaBody(), async (c, n) => {
|
||||
c.async = true
|
||||
let arr = c.request.body
|
||||
if (!arr.device) throw '請輸入印表機連接埠'
|
||||
let device = arr.device
|
||||
let feed = arr.feed || 8
|
||||
if (!isFinite(feed) || feed < 0) throw '切紙前換行必須是整數'
|
||||
feed = typeof feed !== 'number' ? Math.floor(parseInt(feed)) : feed
|
||||
|
||||
let oriDev = Printer.serial
|
||||
let oriFeed = Printer.feed
|
||||
|
||||
if (Printer.isOpen) {
|
||||
await Printer.close()
|
||||
}
|
||||
|
||||
Printer.serial = device
|
||||
Printer.feed = feed
|
||||
|
||||
let status = await Printer.printTestPage()
|
||||
|
||||
if (!status) c.body = '測試失敗'
|
||||
else c.body = '測試成功'
|
||||
|
||||
Printer.serial = oriDev
|
||||
Printer.feed = oriFeed
|
||||
await Printer.connect()
|
||||
})
|
||||
.post('/api/write_config', KoaBody(), async (c, n) => {
|
||||
c.async = true
|
||||
let arr = c.request.body
|
||||
if (!arr.tty) throw '請輸入印表機連接埠'
|
||||
if (!arr.ble || (arr.ble !== 'yes' && arr.ble !== 'no')) throw '請輸入藍牙啟用設定'
|
||||
let feed = arr.feed || 8
|
||||
if (!isFinite(feed) || feed < 0) throw '切紙前換行必須是整數'
|
||||
feed = typeof feed !== 'number' ? Math.floor(parseInt(feed)) : feed
|
||||
|
||||
let json = Object.assign({}, config)
|
||||
|
||||
json.ble.enable = arr.ble === 'yes'
|
||||
|
||||
json.printer.serial = arr.tty
|
||||
json.printer.feed = feed
|
||||
|
||||
let wconfig = await new Promise((resolve, reject) => {
|
||||
fs.writeFile(path.resolve(process.env.PROJECT_ROOT, 'config.json'), JSON.stringify(json, null, 2), {
|
||||
mode: 0o664,
|
||||
encoding: 'utf8',
|
||||
flag: 'w'
|
||||
}, err => {
|
||||
if (err) return resolve(false)
|
||||
return resolve(true)
|
||||
})
|
||||
})
|
||||
|
||||
if (!wconfig) throw '設定檔寫入失敗'
|
||||
c.body = '設定檔寫入成功'
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
|
||||
Reference in New Issue
Block a user