Controller 层 /** invitationId 局id,具体参数可以根据业务替换,这里生成的是局详情小程序码,所以需要传入局id * 显示小程序对应的短连接 */ @RequestMapping("getUrlScamel") @ResponseBody public Object getUrlScamel(HttpServletRequest request, Integer invitationId) { return wxService.getUrlScamel(invitationId); }
Service 实现层:
@Override public Object getUrlScamel(Integer invitationId) { //接口请求地址,ACCESS_TOKEN需要替换为小程序的access_token String postUrl = "https://api.weixin.qq.com/wxa/generatescheme?access_token=ACCESS_TOKEN"; // String appletAccsstoken = getAppletAccsstoken(); postUrl = postUrl.replace("ACCESS_TOKEN", getAppletAccsstoken()); //准备请求参数 Map<String, Object> param = new HashMap<>(); JumpWxaVO jumpWxaVO = new JumpWxaVO(); jumpWxaVO.setPath("pages/group-detail/group-detail"); //局详情页,必须在线上存在的页面,不可添加参数 jumpWxaVO.setQuery("invitationId=" + invitationId); //设置参数 param.put("jump_wxa", jumpWxaVO); param.put("is_expire", true); //设置是否会过期,不设置可以改为false long l = System.currentTimeMillis() / 1000; param.put("expire_time", l + 3600L * 24 * 30); //设置过期时间,过期时间为true必传 String post = HttpUtil.post(postUrl, JSONObject.toJSONString(param)); //hutool工具类 发送请求 //解析数据 JSONObject jsonObject = JSONObject.parseObject(post); String openlink = jsonObject.get("openlink").toString(); //获取小程序短连接 //todo 获取短连接之后自己保存好数据 return openlink;
JumpWxaVO实体类: @Data public class JumpWxaVO { /** * 小程序跳转路径 */ private String path; /** * 查询参数 */ private String query; }
hutool引用:
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.2.5</version> </dependency>