import Koa from 'koa';
|
import Router from 'koa-router';
|
const koaBody = require('koa-body');
|
import {sockteStore} from '@/stores/sockteInfo'
|
|
interface ResultInfo{
|
clientCode:string;
|
commCode :string;
|
deviceType:string;
|
deviceName :string;
|
result :string;
|
resultTime :string
|
}
|
|
const app = new Koa();
|
const router = new Router();
|
app.use(koaBody({
|
multipart: true
|
}));
|
|
router.get('/', async (ctx) => {
|
ctx.body = 'Hello World!';
|
});
|
router.post('/postResult', async (ctx)=>{
|
const body=ctx.request.body
|
console.log(body,'收到的参数')
|
writeResult(body)
|
ctx.body={
|
"code": 200,
|
"data": body,
|
"message": "接收成功"
|
}
|
// writeResult(body)
|
})
|
|
app.use(router.routes());
|
|
app.listen(3131,()=>{
|
console.log('koa服务已经启动了,端口号是3131')
|
});
|
const writeResult=(res: ResultInfo)=>{
|
console.log(res)
|
console.log(`写入结果类型${res.deviceType},写入结果=${res.result}`)
|
switch(res.deviceType){
|
case '读卡器':
|
console.log('读卡器收到消息')
|
if(res.clientCode!==null){
|
sockteStore().setdkqSockte(
|
{
|
deviceName:res.deviceNumber,
|
type:"读卡器",
|
result:res.result,
|
resultTime:res.resultTime,
|
state:2
|
}
|
)
|
}
|
break
|
case "体重秤":
|
sockteStore().setweightSockte(
|
{
|
deviceName:res.deviceNumber,
|
type:"体重秤",
|
result:Number(res.result).toString(),
|
resultTime:res.resultTime,
|
state:2
|
}
|
)
|
break
|
case "血压计":
|
sockteStore().setxyjSockte(
|
{
|
deviceName:res.deviceNumber,
|
type:"血压计",
|
result:res.result,
|
resultTime:res.resultTime,
|
state:2
|
}
|
)
|
break
|
default:
|
console.log('有配置类型没有匹配')
|
}
|
}
|