java 获取小程序码,适用于需要的码数量极多的业务场景

##java 获取小程序码,适用于需要的码数量极多的业务场景 2019年12月9日 panlobal编写 第1版

注:必须是发布的小程序才可以! 请求地址 POST https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN 请求参数: 1 access_token string 是 接口调用凭证 2 scene string 是 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&’()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式) page string 主页 否 必须是已经发布的小程序存在的页面(否则报错),例如 pages/index/index, 根路径前不要填加 /,不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面 3 width number 430 否 二维码的宽度,单位 px,最小 280px,最大 1280px

错误码 45009 调用分钟频率受限(目前5000次/分钟,会调整),如需大量小程序码,建议预生成。 41030 所传page页面不存在,或者小程序没有发布 40001 access_token过期了

#第一步 获取 access_token https://developers.weixin.qq.com/miniprogram/dev/api/getAccessToken.html

1//获取微信小程序的access_token 2private String getAccessToken(){ 3 4 //微信接口url 5 String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?" + 6 "grant_type=client_credential" + 7 "&appid=" + APPID + 8 "&secret=" + SECRET; 9 RestTemplate restTemplate = new RestTemplate(); 10 11 ResponseEntity<String> responseEntity = restTemplate.getForEntity(accessTokenUrl,String.class); 12 String accessToken = null; 13 if(responseEntity != null && responseEntity.getStatusCode() == HttpStatus.OK){ 14 String strBody = responseEntity.getBody(); 15 JSONObject jsonObject = JSONObject.parseObject(strBody); 16 accessToken = jsonObject.getString("access_token"); 17 log.info("accessToken:{}",accessToken); 18 } 19 return accessToken; 20 21}

代码注解:APPID 在代码中定义你自己的appid,SECRET 也是一样。

#第二步 发送post请求, 并保存图片 url: https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN

1//发送post请求,并保存图片 2private String saveImage(){ 3 4 String accessToken = getAccessToken(); 5 log.info("getQrCodeByCooperationId,accessToken:{}",accessToken); 6 7 //图片文件名称 8 String fileName = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()) + 9 UUID.randomUUID().toString().substring(8)+".png"; 10 RestTemplate rest = new RestTemplate(); 11 InputStream inputStream = null; 12 OutputStream outputStream = null; 13 14 try { 15 String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken; 16 Map<String,Object> param = new HashMap<>(); 17 18 param.put("path", "pages/index/index"); 19 param.put("width", 430); 20 param.put("auto_color", true); 21 log.info("调用生成微信URL接口传参:" + param); 22 23 MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); 24 HttpEntity requestEntity = new HttpEntity(param, headers); 25 ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]); 26 log.info("调用小程序生成微信永久二维码URL接口返回结果:" + entity.getBody()); 27 28 byte[] result = entity.getBody(); 29 inputStream = new ByteArrayInputStream(result); 30 File file = new File(fileName); 31 outputStream = new FileOutputStream(file); 32 int len = 0; 33 byte[] buf = new byte[1024]; 34 while ((len = inputStream.read(buf, 0, 1024)) != -1) { 35 outputStream.write(buf, 0, len); 36 } 37 outputStream.flush(); 38 39 } catch (Exception e) { 40 log.error("调用小程序生成微信永久二维码URL接口异常",e); 41 } finally { 42 if(inputStream != null){ 43 try { 44 inputStream.close(); 45 } catch (IOException e) { 46 e.printStackTrace(); 47 } 48 } 49 if(outputStream != null){ 50 try { 51 outputStream.close(); 52 } catch (IOException e) { 53 e.printStackTrace(); 54 } 55 } 56 } 57 58 return fileName; 59 60}

因项目需要,后续会将获取的小程序二维码,通过spring cloud Feign接口调用的方式 上传到另一个微服务:文件服务器,并获取该图片的链接;若有同样功能需求的朋友,可以翻看后续文章。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

java 获取小程序码,适用于需要的码数量极多的业务场景 - HelloWorld