获取小程序 scheme 码,拉起小程序

获取小程序 scheme 码,适用于短信、邮件、外部网页、微信内等拉起小程序的业务场景。


import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson15.JSONObject;
import com.http.HttpClient;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

public class demo {

    public static String encoding = "UTF-8";
    //微信APPID
    private static String weiXinAppId="";
    //微信秘钥
    private static String weiXinSecret="";

    private static String getAccessToken(){
        String url = "https://api.weixin.qq.com/cgi-bin/token";
        Map<String, String> reqMap = new HashMap<String, String>();
        reqMap.put("grant_type","client_credential");
        reqMap.put("appid",weiXinAppId);
        reqMap.put("secret",weiXinSecret);
        String result="";
        try {
            result = HttpClient.doGet(url,reqMap);
            result = URLDecoder.decode(result, encoding);
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        System.out.print("微信获取accessToken回调报文"+jsonObject);
        return jsonObject.getString("access_token");
    }

    private static String getOpenLink(String redirectUrl){
        String token = getAccessToken();
        String url = "https://api.weixin.qq.com/wxa/generatescheme?access_token="+token;
        JSONObject jumpWxa = new JSONObject();
        jumpWxa.put("path","");
        jumpWxa.put("query","action=epPay&payUrl="+redirectUrl);

        JSONObject reqMap = new JSONObject();
        reqMap.put("jump_wxa",jumpWxa);
        String result="";
        try {
            HttpRequest request = HttpUtil.createPost(url);
            request.contentType("application/json");
            request.body(reqMap.toJSONString());
            result = request.execute().body();
            result = URLDecoder.decode(result, encoding);
            System.out.print("微信获取getOpenLink回调报文"+result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}

注意事项(getOpenLike中的redirectUrl参数是在小程序内设置,参考下图)
在这里插入图片描述
在小程序页面路径中填入需要跳转过来页面的路径

在这里插入图片描述

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
以下是一个简单的 Java示例,用于在微信小程序获取 scheme : ```java import java.util.HashMap; import java.util.Map; import com.alibaba.fastjson.JSON; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; public class WeChatMiniProgram { private static final String APP_ID = "你的小程序AppID"; private static final String APP_SECRET = "你的小程序AppSecret"; private static final String API_BASE_URL = "https://api.weixin.qq.com"; /** * 获取微信小程序的 accessToken * * @return accessToken * @throws Exception */ public String getAccessToken() throws Exception { OkHttpClient client = new OkHttpClient().newBuilder().build(); String url = API_BASE_URL + "/cgi-bin/token?grant_type=client_credential&appid=" + APP_ID + "&secret=" + APP_SECRET; Request request = new Request.Builder().url(url).method("GET", null).build(); Response response = client.newCall(request).execute(); String responseBody = response.body().string(); Map<String, Object> resultMap = JSON.parseObject(responseBody, HashMap.class); return (String) resultMap.get("access_token"); } /** * 获取微信小程序scheme * * @param accessToken * @param pagePath * @return scheme * @throws Exception */ public String getScheme(String accessToken, String pagePath) throws Exception { OkHttpClient client = new OkHttpClient().newBuilder().build(); String url = API_BASE_URL + "/wxa/generatescheme?access_token=" + accessToken; MediaType mediaType = MediaType.parse("application/json"); Map<String, Object> requestBody = new HashMap<>(); requestBody.put("is_expire", false); requestBody.put("jump_wxa", new HashMap<String, Object>() {{ put("path", pagePath); put("query", ""); }}); RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(requestBody)); Request request = new Request.Builder().url(url).method("POST", body).build(); Response response = client.newCall(request).execute(); String responseBody = response.body().string(); Map<String, Object> resultMap = JSON.parseObject(responseBody, HashMap.class); return (String) resultMap.get("scheme"); } public static void main(String[] args) throws Exception { WeChatMiniProgram weChatMiniProgram = new WeChatMiniProgram(); String accessToken = weChatMiniProgram.getAccessToken(); String pagePath = "pages/index/index"; // 小程序页面路径 String scheme = weChatMiniProgram.getScheme(accessToken, pagePath); System.out.println("scheme: " + scheme); } } ``` 其中,`getAccessToken()` 方法用于获取小程序的 access_token,`getScheme()` 方法用于获取小程序scheme 。在 `main()` 方法中,你可以使用 `getScheme()` 方法获取指定页面的 scheme ,然后在你的小程序中使用该 scheme

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值