package com.hys.app.controller.erp.use;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.security.MessageDigest;
import java.util.Arrays;

@RestController
public class WeixinController {

    private static final String TOKEN = "ryks2023weixin";  // 替换为您在微信公众平台设置的Token

    @GetMapping("/weixin")
    public String validateWeixinToken(
            @RequestParam("signature") String signature,
            @RequestParam("timestamp") String timestamp,
            @RequestParam("nonce") String nonce,
            @RequestParam("echostr") String echostr) {
        System.out.println(echostr+"111111111111111111111111111111222222222222222222222222222-----------------------------------------");

//        String[] arr = {TOKEN, timestamp, nonce};
//        Arrays.sort(arr);
//        StringBuilder content = new StringBuilder();
//        for (String str : arr) {
//            content.append(str);
//        }
//
//        String generatedSignature = sha1(content.toString());
//
//        if (generatedSignature.equals(signature)) {
//            return echostr;
//        } else {
//            return "Failed to verify";
//        }
        return echostr;
    }

    private String sha1(String data) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(data.getBytes());
            byte[] bytes = md.digest();
            StringBuilder sb = new StringBuilder();
            for (byte b : bytes) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (Exception e) {
            return "";
        }
    }
}