doum
2026-05-25 74190ebc24e6e850d418ad0ce041fd91b795c23e
server/visits/dmvisit_admin/src/main/java/com/doumee/device/ElectronicNotifyController.java
@@ -1,72 +1,35 @@
package com.doumee.device;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.doumee.core.annotation.trace.Trace;
import com.doumee.service.business.YwElectricalBizService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.*;
@Trace(exclude = true)
@RestController
@RequestMapping("/electronic")
@Slf4j
public class ElectronicNotifyController {
    @RequestMapping("/notify")
    public String notify(String response_content, String timestamp, String sign) {
        System.out.println("timestamp: " + timestamp);
        System.out.println("response_content: " + response_content);
        System.out.println("sign: " + sign);
        // 验签
        if(!checkSign(response_content, timestamp, sign)) {
            System.out.println("sign check failed");
            return "sign check failed";
        }
        System.out.println("sign check success");
        List<Map<String, Object>> responseContent= new ArrayList<>();
        JSONArray contentArray = JSON.parseArray(response_content);
        for(int i = 0; i < contentArray.size(); ++i) {
            HashMap<String, Object> contentMap = new HashMap<>();
            JSONObject contentObject = contentArray.getJSONObject(i);
            Set<String> keySet = contentObject.keySet();
            for(String key: keySet) {
                contentMap.put(key, contentObject.getObject(key, Object.class));
            }
            responseContent.add(contentMap);
        }
        System.out.println("接收异步通知数据:" + responseContent);
        return "SUCCESS";
    }
    @Autowired
    private YwElectricalBizService ywElectricalBizService;
    private boolean checkSign(String response_content, String timestamp, String sign) {
        // 随机字符串 后台获取
        String nonce = "XOfX547SeCIlhufeeBBwgZIN";
        String buf = response_content + timestamp + nonce;
        String encode = getMD5(buf);
        return encode.equals(sign);
    }
    // md5加密
    private String getMD5(String password) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            throw new RuntimeException(e);
    @PostMapping(value = "/electricalNotify", produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<String> electricalNotify(
            @RequestParam("response_content") String responseContent,
            @RequestParam("timestamp") String timestamp,
            @RequestParam("sign") String sign) {
        boolean ok = ywElectricalBizService.handleElectricalNotify(responseContent, timestamp, sign);
        if (!ok) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("FAIL");
        }
        byte[] byteArray = password.getBytes(StandardCharsets.UTF_8);
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuilder hexValue = new StringBuilder();
        for (byte md5Byte : md5Bytes) {
            int val = ((int) md5Byte) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
        return ResponseEntity.ok("SUCCESS");
    }
}