53 lines
1023 B
JavaScript
53 lines
1023 B
JavaScript
/* eslint-disable no-throw-literal */
|
|
/* eslint-disable */
|
|
const KoaRouter = require('koa-router')
|
|
const router = new KoaRouter()
|
|
const fs = require('fs')
|
|
const path = require('path')
|
|
const Printer = require('../PrinterDev')
|
|
const KoaBody = require('koa-body')
|
|
const uuid = require('uuid')
|
|
/* eslint-enable */
|
|
|
|
router.use(async (c, n) => {
|
|
c.data = {
|
|
title: 'Printer System Dashboard'
|
|
}
|
|
let status = 1
|
|
try {
|
|
await n()
|
|
} catch (err) {
|
|
status = 0
|
|
if (typeof err === 'string') {
|
|
c.body = err
|
|
} else {
|
|
c.body = err.toString()
|
|
}
|
|
}
|
|
|
|
if (c.async) {
|
|
c.body = {
|
|
status,
|
|
msg: c.body
|
|
}
|
|
}
|
|
})
|
|
|
|
router.get('/', async (c, n) => {
|
|
await c.render('dashboard/index', c.data)
|
|
})
|
|
|
|
router.post('/api/print', KoaBody(), async (c, n) => {
|
|
c.async = true
|
|
let arr = c.request.body
|
|
if (!arr.data) throw 'print data empty'
|
|
|
|
let status = await Printer.printerString(arr.data)
|
|
|
|
if (!status) throw 'printer fail'
|
|
|
|
c.body = 'print success'
|
|
})
|
|
|
|
module.exports = router
|