需求:微信訂閱號開發,點擊微信自定義菜單,獲取關注用戶的openid,並且傳到后台與前端頁面
說明:訂閱號無法直接獲取關注用戶的openid傳到前端,只能通過微信開放平台將訂閱號和服務號關聯起來,獲取用戶的uionid,這樣就可以確定當前操作的微信用戶
場景:
1. 菜單類型為 view,創建view的代碼:
// 網頁跳轉菜單 Map<String, String> menu1_1 = new HashMap<>(); menu1_1.put("type", "view"); menu1_1.put("name", "進入生態圈"); menu1_1.put("url", "http://域名/menu/auth?redirect_uri=http://域名/menu/index");
Controller 代碼:
/** * 授權入口:訂閱號設置了業務域名后才會進入此方法 * @param request * @param response */ @RequestMapping(value = "/auth",method = RequestMethod.GET) @ResponseBody public void auth(HttpServletRequest request, HttpServletResponse response) { try { Map<String, String[]> paramterMap = request.getParameterMap(); log.info("auth click Parameter: ["); paramterMap.forEach((k,v)->{ log.info("key : " + k + ";value : " + Arrays.toString(v)); }); log.info("]"); // 跳轉的URL必須要做encode String redirectUrl = URLEncoder.encode(request.getParameter("redirect_uri"), SysConst.ENCODE_UTF8); // 使用服務號的appid授權獲取用戶基本信息,然后跳轉到redirect_uri,跳轉到redirect_uri后,回接收到code String authUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+WeChatConfigDYH.APP_ID_FWH
+"&redirect_uri="+redirectUrl
+"&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect"; //跳轉授權頁面 response.sendRedirect(authUrl); } catch (Exception e) { e.printStackTrace(); } } /** * 授權接口跳轉uri入口 * @param request * @param response */ @RequestMapping(value = "/index",method = RequestMethod.GET) public String index(HttpServletRequest request, HttpServletResponse response) { try { Map<String, String[]> paramterMap = request.getParameterMap(); log.info("view click Parameter: ["); paramterMap.forEach((k,v)->{ log.info("key : " + k + ";value : " + Arrays.toString(v)); }); log.info("]"); String code = request.getParameter("code"); log.debug("auth redirect_uri code = " + code); JSONObject weChatCode = _dataCache.getWeChatCode(code); if (weChatCode == null) { //通過code換取網頁授權access_token(與基礎支持中的access_token不同) String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+WeChatConfigDYH.APP_ID_FWH
+"&secret="+WeChatConfigDYH.APP_SECRET_FWH
+"&code="+code+"&grant_type=authorization_code"; //返回信息:包含unionid,標志唯一用戶 String accessInfoStr = _restTemplate.getForObject(requestUrl, String.class); // 轉化成JSONObject weChatCode = JSONObject.parseObject(accessInfoStr); // 存入緩存 _dataCache.putWeChatCode(code, weChatCode); } log.debug(weChatCode.toString()); // 微信unionid,唯一 String unionid = weChatCode.getString("unionid"); // 客戶ID String fcustomerid495 = null; // 獲取用戶信息 Customer customer = _customerService.findByUnionId(unionid); if (customer != null) { fcustomerid495 = customer.getFcustomerid495(); } // 設置unionid和fcustomerid到前端 request.setAttribute("unionid", unionid); request.setAttribute("fcustomerid", fcustomerid495); } catch (Exception e) { e.printStackTrace(); } return "index"; }
2. 菜單類型為 click 創建代碼:
// 按鈕click Map<String, String> menu1_1 = new HashMap<>(); menu1_1.put("type", "click"); menu1_1.put("name", "進入生態圈"); menu1_1.put("key", "self_edit");
Controller代碼:
/** * 寫法二 * @param requestBody * @param signature * @param timestamp * @param nonce * @param openid * @param encType * @param msgSignature * @return */ @RequestMapping(method = RequestMethod.POST) public String post(HttpServletRequest request, HttpServletResponse response, @RequestBody String requestBody, @RequestParam("signature") String signature, @RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce, @RequestParam("openid") String openid, @RequestParam(name = "encrypt_type", required = false) String encType, @RequestParam(name = "msg_signature", required = false) String msgSignature) { // 返回給微信加密后的消息 String respMessageEncrypt = ""; try { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); Map<String, String> encryptMap = MessageUtil.fromEncryptedMap(requestBody, timestamp, nonce, msgSignature); log.info("菜單事件:" + encryptMap.toString()); // 消息處理 respMessageEncrypt = _weChatMessageService.newsMessage(encryptMap); } catch (Exception e) { e.printStackTrace(); } finally { } return respMessageEncrypt; }
/** * 處理消息發送:圖文消息 * @param request * @return 加密消息字符傳 */ public String newsMessage(Map<String, String> requestMap) { // 返回加密消息字符串 String respMessageEncrypt = null; try { // 發送方帳號(open_id) String fromUserName = requestMap.get("FromUserName"); // 公眾帳號 String toUserName = requestMap.get("ToUserName"); // 根據openid獲取客戶信息 String fcustomerid495 = null; Customer customer = _customerService.findByOpenId(fromUserName); if (customer != null) { fcustomerid495 = customer.getFcustomerid495(); } // 設置鏈接消息 NewsMessageResp newsMessage = new NewsMessageResp(); newsMessage.setToUserName(fromUserName); newsMessage.setFromUserName(toUserName); newsMessage.setCreateTime(new Date().getTime()); newsMessage.setMsgId(UUID.randomUUID().toString()); newsMessage.setMsgType(WeChatConst.RESP_MESSAGE_TYPE_NEWS); newsMessage.setArticleCount("1"); // Articles List<Article> articleList = new ArrayList<Article>(); Article article = new Article(); article.setTitle("生態圈鏈接"); article.setDescription("點擊進入【生態圈】"); article.setPicUrl("http://域名/img/htlogo.png"); article.setUrl("http://域名?openid=" + fromUserName + "&fcustomerid=" + fcustomerid495); articleList.add(article); newsMessage.setArticles(articleList); // 鏈接消息xml String respMessage = MessageUtil.newsMessageToXml(newsMessage); log.info("鏈接消息 = " + respMessage); // 消息加密 WxCryptUtil wxCryptUtil = new WxCryptUtil(); respMessageEncrypt = wxCryptUtil.encrypt(respMessage); log.info("加密后的鏈接消息 = " + respMessageEncrypt); } catch (Exception e) { e.printStackTrace(); } return respMessageEncrypt; }