package com.ruoyi.web.controller.basic;

import cn.hutool.crypto.SecureUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@RestController
public class WeChatController {
    @Value("${wechat.token}")
    String TOKEN;


    /**
     *
     * @param signature
     * @param timestamp
     * @param nonce
     * @param echostr
     * @return
     */
    @GetMapping("")
    public String check(@RequestParam("signature") String signature,
                        @RequestParam("timestamp") String timestamp,
                        @RequestParam("nonce") String nonce,
                        @RequestParam("echostr") String echostr){
        return this.check1(signature,timestamp,nonce,echostr);
    }

    public String check1(String signature,String timestamp,String nonce,String echostr) {
        String[] arr = new String[]{TOKEN,timestamp,nonce};
        Arrays.sort(arr);
        StringBuilder content = new StringBuilder();
        for (String s : arr) {
            content.append(s);
        }
        String temp = SecureUtil.sha1(content.toString());
        if (temp.equals(signature)){
            return echostr;
        }
        return null;
    }
}
