通过微信文档中的URL link或URL Scheme方式生成,更多事项细节见官方文档
话不多说,直接上代码
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;/*** @Author: Tenk*/
@RequiredArgsConstructor
@Component
public class WxAppletNotifyUtils {private final WxAppletConfig wxAppletConfig;private static final String linkUrl="=ACCESS_TOKEN";private static final String tokenUrl="=client_credential&appid=APPID&secret=APPSECRET";/*** 生成小程序跳转短链 code* @param path 小程序页面路径 null为首页* @param query 与前端商量,携带的参数* @return*/public String jumpAppletShortUrl(String path,String query,Integer days){String accessToken = getAccessToken();String urlplace("ACCESS_TOKEN",accessToken);JSONObject body = ateObj();body.putOpt("path",path);body.putOpt("query",query);//链接过期类型:0时间戳 1间隔天数body.putOpt("expire_type",1);//指定失效天数,最多30days=(days==null||days>30)?30:days;body.putOpt("expire_interval",days);//小程序版本,正式版为 "release",体验版为"trial",开发版为"develop"body.putOpt("env_version","release");String post = HttpUtil.post(url, JSONString(2));JSONObject jsonObject = JSONUtil.parseObj(post);("url_link", String.class, false);}/*** scheme 跳转微信小程序,需中转H5* @param path* @param query* @param days* @return*/public String jumpAppletSchemeUrl(String path,String query,Integer days){String url = place("ACCESS_TOKEN", getAccessToken());JSONObject body = ateObj();JSONObject jumpWxa = ateObj();jumpWxa.putOpt("path",path);jumpWxa.putOpt("query",query);jumpWxa.putOpt("env_version","release");body.putOpt("jump_wxa",jumpWxa);//链接过期类型:0时间戳 1间隔天数body.putOpt("expire_type",1);body.putOpt("is_expire",true);//指定失效天数,最多30days=(days==null||days>30)?30:days;body.putOpt("expire_interval",days);String post = HttpUtil.post(url, JSONString(2));JSONObject result = JSONUtil.parseObj(post);JSONString(2);}public String getAccessToken(){String token;//缓存存在tokenif(RedisUtils.hasKey(CacheConstants.APPLET_ACCESS_TOKEN)){//redis直接获取小程序tokentoken = CacheObject(CacheConstants.APPLET_ACCESS_TOKEN);}else{//拼接urlString url = place("APPID", AppId()).replace("APPSECRET", Secret());//get请求String result = (url);//解析结果JSONObject jsonObject = JSONUtil.parseObj(result);//get AccessTokentoken("access_token",String.class,true);//get 过期时间 单位秒Long expiresIn = ("expires_in", Long.class, true);//存入redis并减少60秒RedisUtils.setCacheObject(CacheConstants.APPLET_ACCESS_TOKEN,token, Duration.ofSeconds(expiresIn-60));}return token;}
}
import lombok.Data;
import org.t.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Data
@Component
@ConfigurationProperties(prefix = "wx.applet")
public class WxAppletConfig {//小程序登录获取openid的urlprivate String sessionUrl;//小程序idprivate String appId ;//小程序密钥private String secret ;
}
{"errcode": 0,"errmsg": "ok","url_link": ""
}
{"errcode": 0,"errmsg": "ok","openlink": "weixin://dl/business/?t=xxxxxxxxx",
}
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.st.context.SpringBootTest;/*** @Author: Tenk*/
@SpringBootTest // 此注解只能在 springboot 主包下使用 需包含 main 方法与 yml 配置文件
@DisplayName("自定义测试")
public class CustomizeTest {@Autowiredprivate WxAppletNotifyUtils appletUtils;@DisplayName("测试微信跳转链接")@Testpublic void urlLink(){String shortUrl = appletUtils.jumpAppletShortUrl("/pages/login",null, 30);System.out.println(shortUrl);String schemeUrl = appletUtils.jumpAppletSchemeUrl("/pages/login", null, 1);System.out.println(schemeUrl);}
}
本文发布于:2024-02-02 12:46:56,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170684921643896.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |