引入依赖
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-http</artifactId> <version>5.7.6</version> </dependency>
获取 scheme
官方文档:小程序码与小程序链接 / URL Scheme / 获取加密scheme码 (qq.com)
public static String getSchemeUrl(String id, String mobile) { String redirectUrl = weChatConfigStatic.getRedirectUrl(); String token = getAccessToken(); String url = WeChatConstant.GET_SCHEME + token; JSONObject jumpWxa = new JSONObject(); // scheme 码进入的小程序页面路径,必须是已经发布的小程序存在的页面,不可携带 query。path 为空时会跳转小程序主页 // FIXME jumpWxa.put("path", null); jumpWxa.put("query", "id=" + id + "&mobile=" + mobile); jumpWxa.put("env_version", weChatConfigStatic.getEnvVersion()); JSONObject reqMap = new JSONObject(); reqMap.put("jump_wxa", jumpWxa); try { HttpRequest request = HttpUtil.createPost(url); request.contentType("application/json"); request.body(reqMap.toJSONString()); String result = request.execute().body(); result = URLDecoder.decode(result, encoding); log.info("微信获取scheme请求报文 {}", JSON.toJSONString(result)); Map map = JSONObject.parseObject(result, Map.class); if (map.get("errcode").equals(0)) { return map.get("openlink").toString(); } } catch (IOException e) { e.printStackTrace(); } return null; }
获取加密URLLink
官方文档:小程序码与小程序链接 / URL Link / 获取加密URL Link (qq.com)
public static String getUrlLink(String id, String mobile) { String token = getAccessToken(); String url = WeChatConstant.GET_URL_LINK + token; // 构造 POST 请求 HttpPost httpPost = new HttpPost(url); // 构造请求体参数 JSONObject json = new JSONObject(); json.put("env_version", weChatConfigStatic.getEnvVersion()); json.put("query", "id=" + id + "&mobile=" + mobile); StringEntity stringEntity = new StringEntity(json.toString(), ContentType.APPLICATION_JSON); httpPost.setEntity(stringEntity); // 创建 HttpClient 对象 try (CloseableHttpClient httpClient = HttpClients.createDefault()) { // 发送请求并获取响应 try (CloseableHttpResponse response = httpClient.execute(httpPost)) { HttpEntity entity = response.getEntity(); if (entity != null) { String responseString = EntityUtils.toString(entity); Map map = JSONObject.parseObject(responseString, Map.class); log.info("获取urlLink结果: {}", map); if (map.get("errcode").equals(0)) { return map.get("url_link").toString(); } } } } catch (IOException e) { throw new RuntimeException(e); } return null; }
返回结果
{ "message": "操作成功", "data": "https://wxaurl.cn/BxOlgzooBmk", "apiCode": 0, "faultCode": 0, "success": true }
标签:URLLink,map,加密,String,JSONObject,获取,put,new From: https://www.cnblogs.com/ckfeng/p/18322491