wangzhibo
2026-07-28 2b662657c5bc5bc45b69f475c7b2edcf8fc92f0c
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { Inject, Post, Body, Headers } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { PushOpenService } from '../../service/open';
import { WeightUploadDto } from '../../dto/weight.dto';
import * as crypto from 'crypto';
import { BaseSysParamService } from '../../../base/service/sys/param';
 
 
 
/**
 * 开放无需验证的上报数据接口
 */
@CoolController({
  prefix: '/open/push',
})
export class AppPushOpenController extends BaseController {
  @Inject()
  pushOpenService: PushOpenService;
 
  @Inject()
  baseSysParamService: BaseSysParamService;
 
 
  @Post('/weight', { summary: '原始称重上报接口' })
  async uploadWeight(
    @Headers() headers: any,
    @Body() data: WeightUploadDto,
  ) {
 
    const CLIENT_ID = String(await this.baseSysParamService.dataByKey('yaohua.weight.clientid')); //'10001';
    const SECRET = String(await this.baseSysParamService.dataByKey('yaohua.weight.secret')); //'9001ac9676b8';
 
 
    const clientId = headers['clientid'];
    const sign = headers['sign'];
 
    // 1. clientId 校验
    if (clientId !== CLIENT_ID) {
      await this.pushOpenService.writePushAuditLog("weight", { "deviceNo": clientId, "requestContent": data, "errorMessage": "非法 clientId" });
      return {
        code: 1001,
        msg: '非法 clientId',
        data: null,
      };
    }
 
    // 2. 签名校验
    const signStr =
      [
        clientId,
        data.deviceNo,
        data.uploadTime,
        data.garbageType,
        data.garbageWeight,
        SECRET,
      ].join(',');
 
    const realSign = crypto
      .createHash('sha256')
      .update(signStr)
      .digest('hex')
      .toLowerCase();
 
    if (realSign !== sign?.toLowerCase()) {
      await this.pushOpenService.writePushAuditLog("weight", { "deviceNo": data.deviceNo, "requestContent": data, "errorMessage": "签名验证失败" });
 
      return {
        code: 1002,
        msg: '签名验证失败',
        data: null,
      };
    }
    try {
      await this.pushOpenService.reportWeight(data);
      return {
        code: 0,
        msg: '',
        data: 'success',
      };
    } catch (err) {
      await this.pushOpenService.writePushAuditLog("weight", { "deviceNo": data.deviceNo, "requestContent": data, "errorMessage": "系统异常,写入失败." });
 
      return {
        code: 500,
        msg: '系统异常',
        data: null,
      };
    }
  }
 
 
  @Post('/yingshiyun', { summary: '萤石云Webhook上报接口' })
  async saveYingshiyunMessage(
    @Body() body: any
  ) {
 
    const result = await this.pushOpenService.saveYingshiyunMessage(body);
    //return this.ok(result);
 
    return { "messageId": result }
 
  }
}