package cn.axzo.msg.center.wx.notices.facade.openapi;

import cn.axzo.msg.center.wx.notices.compont.WxCpTpProcessorFactory;
import cn.axzo.trade.web.annotation.EnableResponseAdvice;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.cp.util.crypto.WxCpTpCryptUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 2023/5/31 14:21
 */
@Slf4j
@RestController
@EnableResponseAdvice(enable = false)
@RequiredArgsConstructor
@RequestMapping("webApi/open/cp/tp/callback")
public class CpTpApiController {
    private final WxCpTpProcessorFactory wxCpProcessorFactory;

    @GetMapping(produces = "text/plain;charset=utf-8", value = "/{corpId}")
    public String authGet(@PathVariable String corpId,
                          @RequestParam(name = "msg_signature", required = false) String signature,
                          @RequestParam(name = "timestamp", required = false) String timestamp,
                          @RequestParam(name = "nonce", required = false) String nonce,
                          @RequestParam(name = "echostr", required = false) String echostr) {
        log.info("\n接收到来自微信服务器的认证消息：signature = [{}], timestamp = [{}], nonce = [{}], echostr = [{}]",
                signature, timestamp, nonce, echostr);
        if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
            throw new IllegalArgumentException("请求参数非法，请核实!");
        }
        String s = new WxCpTpCryptUtil(wxCpProcessorFactory.getConfig(corpId))
                .decryptContent(signature, timestamp, nonce, echostr);
        log.info("get解密参数:{}", s);
        return s;
    }
}
