package com.zd.base.message.controller;

import com.zd.base.message.properties.WeChatProperties;
import com.zd.base.message.utils.WXPublicUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/wx")
@Api(tags = "验证公众平台token")
@Slf4j
public class WXTokenController {

    @Autowired
    private WeChatProperties properties;

    @RequestMapping("/verify")
    public String verifyWXToken(HttpServletRequest request) {
        String msgSignature = request.getParameter("signature");
        String msgTimestamp = request.getParameter("timestamp");
        String msgNonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        if (WXPublicUtils.verifyUrl(msgSignature, msgTimestamp, msgNonce,properties.getToken())) {
            return echostr;
        }
        return null;
    }
}
