Kaynağa Gözat

增加生成链接

xueyiming 4 hafta önce
ebeveyn
işleme
f5eda95af4

+ 100 - 0
common-module/src/main/java/com/tzld/piaoquan/growth/common/service/Impl/MiniProgramLinkServiceImpl.java

@@ -0,0 +1,100 @@
+package com.tzld.piaoquan.growth.common.service.Impl;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import java.util.Date;
+
+@Service
+public class MiniProgramLinkServiceImpl {
+
+    //    @Value("${wx.appid}")
+    private String appId = "wxbdd2a2e93d9a6e25";
+
+    //    @Value("${wx.secret}")
+    private String appSecret = "f9bf6d45fd324ba72be16c52222e3752";
+
+    private static final String ACCESS_TOKEN_URL =
+            "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
+
+    private static final String GENERATE_SCHEME_URL =
+            "https://api.weixin.qq.com/wxa/generatescheme?access_token=%s";
+
+    private static final String GENERATE_URL_LINK =
+            "https://api.weixin.qq.com/wxa/generate_urllink?access_token=%s";
+
+    /**
+     * 获取微信Access Token
+     */
+    public String getAccessToken() {
+        String url = String.format(ACCESS_TOKEN_URL, appId, appSecret);
+
+        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
+            HttpGet request = new HttpGet(url);
+            HttpResponse response = httpClient.execute(request);
+
+            String jsonResponse = EntityUtils.toString(response.getEntity());
+            JsonNode rootNode = new ObjectMapper().readTree(jsonResponse);
+
+            return rootNode.path("access_token").asText();
+        } catch (Exception e) {
+            throw new RuntimeException("Failed to get access token", e);
+        }
+    }
+
+    /**
+     * 生成URL Link
+     */
+    public String generateUrlLink(String path, String query, Integer expireInterval) {
+        String accessToken = getAccessToken();
+        String url = String.format(GENERATE_URL_LINK, accessToken);
+
+        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
+            HttpPost request = new HttpPost(url);
+
+            // 构建请求体
+            ObjectMapper mapper = new ObjectMapper();
+            ObjectNode body = mapper.createObjectNode();
+            body.put("path", path);
+            body.put("query", query);
+
+            // 设置过期时间
+            if (expireInterval != null) {
+                body.put("expire_type", 1); // 1表示绝对时间过期
+                body.put("expire_interval", expireInterval); // 秒级时间戳
+            } else {
+                body.put("expire_type", 0); // 0表示永久有效
+            }
+
+            request.setEntity(new StringEntity(body.toString(), "UTF-8"));
+            request.setHeader("Content-Type", "application/json");
+
+            HttpResponse response = httpClient.execute(request);
+            String jsonResponse = EntityUtils.toString(response.getEntity());
+            JsonNode rootNode = mapper.readTree(jsonResponse);
+
+            return rootNode.path("url_link").asText();
+        } catch (Exception e) {
+            throw new RuntimeException("Failed to generate URL link", e);
+        }
+    }
+
+
+    public static void main(String[] args) {
+        MiniProgramLinkServiceImpl miniProgramLinkService = new MiniProgramLinkServiceImpl();
+        String validPath = "pages/category"; // 你的实际小程序页面路径
+        String validQuery = "jumpPage=pages%2Fuser-videos%3Fid%3D54400707%26fromGzh%3D1%26rootShareId%3D54eb451e-9cf7-4ad4-9350-826f05d6c65f%26shareId%3D54eb451e-9cf7-4ad4-9350-826f05d6c65f%26rootSourceId%3Dtouliu_tencentqw_20250615_54400707_aeeea416";
+        String s = miniProgramLinkService.generateUrlLink(validPath,validQuery, 2);
+        System.out.println(s);
+    }
+}