/** * 一键授权功能 * * @param request * @param response * @throws IOException * @throws AesException * @throws DocumentException */ @RequestMapping(value = "/goAuthor") public void goAuthor(HttpServletRequest request, HttpServletResponse response) throws IOException, AesException, DocumentException { ApiComponentToken apiComponentToken = new ApiComponentToken(); apiComponentToken.setComponent_appid(COMPONENT_APPID); apiComponentToken.setComponent_appsecret(COMPONENT_APPSECRET); WeixinOpenAccountEntity entity = getWeixinOpenAccount(APPID); apiComponentToken.setComponent_verify_ticket(entity.getTicket()); try { String component_access_token = JwThirdAPI.getAccessToken(apiComponentToken); // 预授权码 String preAuthCode = JwThirdAPI.getPreAuthCode(COMPONENT_APPID, component_access_token); String url = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=" + COMPONENT_APPID + "&pre_auth_code=" + preAuthCode + "&redirect_uri=" + ResourceUtil.getConfigByName("domain") + "/rest/openwx/authorCallback"; response.sendRedirect(url); } catch (WexinReqException e) { e.printStackTrace(); } }
public void replyApiTextMessage( HttpServletRequest request, HttpServletResponse response, String auth_code, String fromUserName) throws DocumentException, IOException { String authorization_code = auth_code; // 得到微信授权成功的消息后,应该立刻进行处理!!相关信息只会在首次授权的时候推送过来 System.out.println("------step.1----使用客服消息接口回复粉丝----逻辑开始-------------------------"); try { ApiComponentToken apiComponentToken = new ApiComponentToken(); apiComponentToken.setComponent_appid(COMPONENT_APPID); apiComponentToken.setComponent_appsecret(COMPONENT_APPSECRET); WeixinOpenAccountEntity entity = getWeixinOpenAccount(APPID); apiComponentToken.setComponent_verify_ticket(entity.getTicket()); String component_access_token = JwThirdAPI.getAccessToken(apiComponentToken); System.out.println( "------step.2----使用客服消息接口回复粉丝------- component_access_token = " + component_access_token + "---------authorization_code = " + authorization_code); net.sf.json.JSONObject authorizationInfoJson = JwThirdAPI.getApiQueryAuthInfo( COMPONENT_APPID, authorization_code, component_access_token); System.out.println( "------step.3----使用客服消息接口回复粉丝-------------- 获取authorizationInfoJson = " + authorizationInfoJson); net.sf.json.JSONObject infoJson = authorizationInfoJson.getJSONObject("authorization_info"); String authorizer_access_token = infoJson.getString("authorizer_access_token"); Map<String, Object> obj = new HashMap<String, Object>(); Map<String, Object> msgMap = new HashMap<String, Object>(); String msg = auth_code + "_from_api"; msgMap.put("content", msg); obj.put("touser", fromUserName); obj.put("msgtype", "text"); obj.put("text", msgMap); JwThirdAPI.sendMessage(obj, authorizer_access_token); } catch (WexinReqException e) { e.printStackTrace(); } }
/** * 保存Ticket * * @param xml */ void processAuthorizationEvent(String xml) { Document doc; try { doc = DocumentHelper.parseText(xml); Element rootElt = doc.getRootElement(); String ticket = rootElt.elementText("ComponentVerifyTicket"); if (oConvertUtils.isNotEmpty(ticket)) { LogUtil.info("8、推送component_verify_ticket协议-----------ticket = " + ticket); WeixinOpenAccountEntity entity = getWeixinOpenAccount(APPID); entity = entity == null ? new WeixinOpenAccountEntity() : entity; entity.setTicket(ticket); entity.setAppid(APPID); entity.setGetTicketTime(new Date()); systemService.saveOrUpdate(entity); } } catch (DocumentException e) { e.printStackTrace(); } }