赞
踩
前言:首先我的业务通过短信发链接让用户点击链接跳转进小程序,但是在短信里点击默认跳转到浏览器,浏览器又不支持打开小程序的,正好微信有自带的短链接生成并自己跳转到微信里打开,废话不多说 直接看下面。
POST https://api.weixin.qq.com/wxa/generate_urllink?access_token=ACCESS_TOKEN
GET https://api.weixin.qq.com/cgi-bin/token
- package org.jeecg.modules.message.util;
-
- import cn.hutool.http.*;
- import cn.hutool.json.JSONObject;
- import cn.hutool.json.JSONUtil;
- import lombok.extern.slf4j.Slf4j;
- import org.jeecg.common.api.vo.Result;
- import org.jeecg.common.exception.JeecgBootException;
- import org.jeecg.common.util.RedisUtil;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
-
- import javax.annotation.Resource;
- import javax.crypto.Cipher;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- import java.io.UnsupportedEncodingException;
- import java.nio.charset.StandardCharsets;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.util.*;
-
- /**
- * @author rwy
- * @date 2023-08-04 9:30
- */
- @Slf4j
- @Component
- public class WeChatUtil {
-
- @Resource
- private RedisUtil redisUtil;
-
- //微信小程序appId
- @Value("${wx.xcx.appId}")
- private String appId;
-
- //微信小程序secret
- @Value("${wx.xcx.appSecret}")
- private String appSecret;
-
- //存入缓存中的缓存名称 自定义
- @Value("${spring.application.name}")
- private String appName;
-
- /**
- * 请求微信接口服务,获取小程序全局唯一后台接口调用凭据(access_token)
- * @return
- */
- public String getAccessToken() {
- //如果缓存有token直接拿
- if (redisUtil.hasKey(appName+":wx_accss_token")) {
- return (String) redisUtil.get(appName+":wx_accss_token");
- }
- //没有去取
- try {
- String baseUrl = "https://api.weixin.qq.com/cgi-bin/token";
- HashMap<String, Object> requestParam = new HashMap<>();
- // appId
- requestParam.put("grant_type", "client_credential");
- // 唯一凭证id appid:(换成自己的)
- requestParam.put("appid", appId);
- // appSecret(小程序的唯一凭证密钥,换成自己的)
- requestParam.put("secret", appSecret);
- // 发送GET请求读取调用微信接口获取openid用户唯一标识
- String result = HttpUtil.get(baseUrl, requestParam);
- JSONObject jsonObject = JSONUtil.parseObj(result);
- log.info("获取微信access_token:"+jsonObject.toString());
- if (jsonObject.containsKey("access_token")) {
- String access_token = jsonObject.getStr("access_token");
- redisUtil.set(appName+":wx_accss_token", access_token, 2 * 60 * 60);
- } else {
- throw new JeecgBootException("获取微信授权失败!");
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return (String) redisUtil.get(appName+":wx_accss_token");
- }
- }
- /**
- * 微信订阅短链接消息
- * @param
- * @param
- * @return
- */
- public String generateUrllink(){
- String result = null;
- try {
- // 接口调用凭证:accessToken
- String accessToken = weChatUtil.getAccessToken();
- System.out.println(accessToken);
-
- String baseUrl = "https://api.weixin.qq.com/wxa/generate_urllink?access_token="+accessToken;
- HashMap<String, Object> requestParam = new HashMap<>();
- //跳转自己小程序的链接
- requestParam.put("path","");
- //带参 例如:id=1
- requestParam.put("query","");
- String jsonStr = JSONUtil.toJsonStr(requestParam);
- cn.hutool.http.HttpResponse response = HttpRequest.post(baseUrl)
- .header(Header.CONTENT_ENCODING, "UTF-8")
- .header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded")
- .body(jsonStr)
- .execute();
- if (response.getStatus() == HttpStatus.HTTP_OK) {
- com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(response.body());
- String url_link = jsonObject.getString("url_link");
- result =url_link;
- System.out.println(result);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return result;
- }
温馨提示:记得在微信小程序开发管理里面配置域名啥的 不然跳转不了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。