微信公众号开发-关注回复小程序卡片
直接看效果
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<String, String> 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<String,String> 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<String,String> 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: 接口最好使用加密方式,这里为了简单演示没有用加密的形式
说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 微信公众号开发-关注回复小程序卡片
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 微信公众号开发-关注回复小程序卡片