微信小程序用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}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值