小程序生成外链【url_link】版

小程序生成外链【url_link】版

官方文档: 传送门

代码

package com.farbun.fission.util;

import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @Author: 小新
 * @Date: 2023/10/23 10:08
 */
@Slf4j
@Component
public class UrlLinkUtil {

    @Value("${wx.WeChat.AppID}")
    private String appid;   // 小程序appid

    @Value("${wx.WeChat.AppSecret}")
    private String secret;  // 小程序 appSecret

    @Value("${wx.WeChat.AppMiniPath}")
    private String path;   // 小程序已发布页面   跳转页面


    /**
     * 获取token
     * @return
     */
    public  String getAccessToken() {
       // miniAppToken = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"
        String url = String.format(ApiUrl.miniAppToken, appid, secret);

        try {
            return performHttpRequest(url);
        } catch (Exception e) {
            log.info("HTTP请求失败: {}" , e.getMessage());
        }

        return "";
    }

    /**
     * 拼接参数
     * @param query
     * @return
     */
    public  String jumpAppletShortUrl(String query) {
        String accessToken = getAccessToken();
        JSONObject resultObject = new JSONObject(accessToken);
        String AccessToken = resultObject.getString("access_token");
        // miniAppLink = "https://api.weixin.qq.com/wxa/generate_urllink?access_token=%s"
        String linkUrl = String.format(ApiUrl.miniAppLink, AccessToken);

        JSONObject requestBody = new JSONObject();
        requestBody.put("path", path);
        requestBody.put("query", query);
        requestBody.put("expire_type", 1);
        requestBody.put("expire_interval", 30);
        requestBody.put("env_version", "release");

        try {
            String response = performHttpPost(linkUrl, requestBody.toString());
            JSONObject jsonObject = new JSONObject(response);

            if (jsonObject.has("url_link")) {
                return jsonObject.getString("url_link");
            } else {
                log.info("JSON响应中没有url_link字段");
                return "";
            }
        } catch (Exception e) {
            log.info("发生异常: {}" ,e.getMessage());
            return "";
        }
    }

    /**
     * 发送获取token 请求
     * @param url
     * @return
     * @throws Exception
     */
    public  String performHttpRequest(String url) throws Exception {
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        con.setRequestMethod("GET");

        int responseCode = con.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            return response.toString();
        } else {
            return "";
        }
    }

    /**
     * 发送获取urlLink请求
     * @param url
     * @param requestBody
     * @return
     * @throws Exception
     */
    public  String performHttpPost(String url, String requestBody) throws Exception {
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json");
        con.setDoOutput(true);

        try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
            wr.writeBytes(requestBody);
            wr.flush();
        }

        int responseCode = con.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            return response.toString();
        } else {
            return "";
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值