package com.miaxis.app.controller.wx;

import com.miaxis.common.constant.Constants;
import com.miaxis.common.utils.wx.SignUtil;
import com.miaxis.wx.service.IWxGzhService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 微信公众号相关
 * @author wwl
 * @version 1.0
 * @date 2021/10/26 9:28
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(Constants.OPEN_PREFIX + "/wx/gzh")
@Api(tags={"wx微信公众号相关"})
@Slf4j
public class WxgzhController {

    private final IWxGzhService wxGzhService;

    private final HttpServletRequest request;

    @Value("${cos.bucketName}")
    private String bucketName;
    @Value("${cos.path}")
    private String path;
    @Value("${cos.preffix}")
    private String preffix;



    /**
     * 处理服务器推送消息
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/notifyMsg",method = { RequestMethod.GET, RequestMethod.POST })
    @ApiOperation("处理服务器推送消息")
    public String checkSign(HttpServletRequest request, HttpServletResponse response) {
        log.info("-----------进入---------");
        try {
            String signature = request.getParameter("signature");
            String timestamp = request.getParameter("timestamp");
            String nonce = request.getParameter("nonce");
            String echostr = request.getParameter("echostr");
            log.info("本身" + signature);
            if(request.getMethod().equals("GET")){
                log.info("-----------微信验签---------");
                return SignUtil.checkSignature(signature, timestamp, nonce) == true ? echostr : null;
            }else{
                log.info("-----------推送消息处理---------");
                return wxGzhService.handlePublicMsg(request);
            }
        } catch (Exception e) {
            log.error("验证公众号token失败", e);
        }
        return null;
    }


}
