chenyc
2022-09-16 84e14a34a082e00aa2d47a64ee36398088c12aec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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('有配置类型没有匹配')
    }
}