您好,欢迎访问代理记账网站
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

微信小程序用URL也能打开了

URL Link-微信官方文档

获取URLLink

 @RequestMapping(value = "/getURLLink", method = RequestMethod.GET)
    private @ResponseBody Message getURLLink(@RequestParam String path ,@RequestParam String query,@RequestParam Integer type){

        try {
            query = URLDecoder.decode(query,"UTF-8");
            String token = this.getTokenByType(type);
            StringBuffer httpsBuffer = new StringBuffer();
            httpsBuffer.append("https://api.weixin.qq.com/wxa/generate_urllink?");
            httpsBuffer.append("access_token=").append(token);
            Map<String,String> value = new HashMap<>();
            value.put("path",path);
            value.put("query",query);
//            value.put("expire_type",1+"");
//            value.put("expire_time",1+"");

            String result =  HttpUtil.post(httpsBuffer.toString(),value);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if(!Objects.equals(jsonObject.getString("errcode") , "0")){
                throw new RuntimeException(jsonObject.getString("errmsg"));
            }
            String urlLink = jsonObject.getString("url_link");
            return MessageUtil.createSuccessMessage(urlLink);

        } catch (Exception e) {
            e.printStackTrace();
            return MessageUtil.createErrorMessage(e.getMessage());
        }
    }

POST方法

 public static <T> String post(String url, T t) throws URISyntaxException {
        RestTemplate restTemplate = new RestTemplate();

        HttpHeaders headers = new HttpHeaders();

        headers.setContentType(MediaType.APPLICATION_JSON);

        RequestEntity<T> requestEntity = RequestEntity
                .post(new URI(url))
                .contentType(MediaType.APPLICATION_JSON)
                .body(t);

        ResponseEntity<String> value = restTemplate.exchange(requestEntity,String.class);
        if(value == null){
            return "error";
        }
        String result = value.getBody();

        return result;
    }

获取token

  private String getTokenByType(Integer type) throws IOException {
        String key = "TOKEN_"+ type;
       String token = (String) redisUtil.get(key);
       if(token !=null){
           return token;
       }
        ConstantUtils constantUtils = ConstantUtils.create(type);

        StringBuffer httpsBuffer = new StringBuffer();
        httpsBuffer.append("https://api.weixin.qq.com/cgi-bin/token?")
            .append("appid=").append(constantUtils.appId)
            .append("&secret=").append(constantUtils.secret)
            .append("&grant_type=").append("client_credential");

           String restult = HttpUtil.get(httpsBuffer.toString());
            JSONObject jsonObject = JSONObject.parseObject(restult);
            if(jsonObject.getString("errcode")!=null){
                throw new RuntimeException(jsonObject.getString("errmsg"));
            }

            token = jsonObject.getString("access_token");
            long expires = jsonObject.getLongValue("expires_in");
            if(expires>100){
                expires -=100;
            }

            redisUtil.set(key,token,expires);

            return token;

    }

测试

http://localhost:9090/english-server/wechatCheck/getURLLink?type=5&path=/linkuppages/pages/content/SegmentInfo&query=id%3De1c355af-**********

返回的结果

{"message":"ok","status":0,"page":0,"totalPage":0,"data":"\"https://wxaurl.cn/l39Qu3kzxae\"","realData":null}

分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进