微信公众号开发-关注回复小程序卡片

直接看效果


image.png

开发流程

1.进入公众号->开发->基本配置

获取appid,appSecret,填入服务器的ip白名单用于获取accessToken


image.png

2.获取accessToken

获取accessToken比较简单 大家可以百度一下接口是下面这个
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

3.开发接收微信转发公众号消息接口

查看 微信公众号开发指南

//此接口用来响应微信对接口的验证
@GetMapping("message")
    @ResponseBody
    public String customMessageCheck(HttpServletRequest request) {
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        try {
            if (SignUtil.checkSignature(Token, signature, timestamp, nonce)) {
                return echostr;
            }
        } catch (Exception e) {
            log.error("校验出错:signature:{},timestamp:{},nonce:{},echostr:{}", signature, timestamp, nonce, echostr, e);
        }
        return "";
    }

下面接口用来实际接收公众号消息

@PostMapping(value = "message", produces = {"application/xml; charset=UTF-8"})
    @ResponseBody
    public String customMessagePost(HttpServletRequest request) {
        Map result = null;
        try {
            result = WxAppUtils.parseXMLToMap(WxAppUtils.getWeiXinResponse(request));
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (result == null) {
            return "";
        }

        log.info("微信公众号开发接口返回result:{}",result);
        String openId = "";
        //非事件消息直接通知微信转发给客服,这样原来的公众号后台客服功能还可以使用
        if (!"event".equalsIgnoreCase(result.get("MsgType"))) {

            openId = result.get("FromUserName");
            String toUserName = result.get("ToUserName");

            //转发客服消息
            Map parameters = new HashMap<>();
            parameters.put("ToUserName",openId);
            parameters.put("FromUserName",toUserName);
            parameters.put("CreateTime",(System.currentTimeMillis()/1000)+"");
            parameters.put("MsgType","transfer_customer_service");

            String xmlResult = WxAppUtils.assembParamToXml(parameters);

            return xmlResult;
        }


        if ("subscribe".equalsIgnoreCase(result.get("Event"))) {

            openId = result.get("FromUserName");
            String toUserName = result.get("ToUserName");

            // 发送关注后消息,可以根据业务需要是否发送,该发送直接在当前接口回复
            Map parameters = new HashMap<>();
            parameters.put("ToUserName",openId);
            parameters.put("FromUserName",toUserName);
            parameters.put("CreateTime",(System.currentTimeMillis()/1000)+"");
            parameters.put("MsgType","text");
            parameters.put("Content","TODO... add message");
            String xmlResult = WxAppUtils.assembParamToXml(parameters);


            //发送小程序卡片,使用客服回复消息接口发送
            JSONObject subscribeInfoJson = new JSONObject();
            subscribeInfoJson.put("touser",openId);
            subscribeInfoJson.put("msgtype","miniprogrampage");
            JSONObject miniprogrampage = new JSONObject();
            miniprogrampage.put("title","title");
            miniprogrampage.put("appid","小程序appid");
            miniprogrampage.put("pagepath","小程序跳转链接");
            miniprogrampage.put("thumb_media_id","封面图片");
            subscribeInfoJson.put("miniprogrampage",miniprogrampage);

            //https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN
            String subscribeInfoResult = httpUtils.postJson("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accountApiService.getAccessToken(DDTConstants.CWJX_APPID),
                    subscribeInfoJson.toJSONString());

           

            return xmlResult;

        }

        return "";
    }

4.微信公众号后台填入服务器URL就是上面http://你的域名/你的项目/message接口链接

image.png

PS: 接口最好使用加密方式,这里为了简单演示没有用加密的形式

你可能感兴趣的:(微信公众号开发-关注回复小程序卡片)