返回
顶部

修改密码

微信小程序——服务端获取小程序二维码 永久有效 数量无限制 ...

+1

-1

收藏

+1

-1

点赞0

评论0

因为现在做的小程序,想要分享小程序中的页面给微信好友,那就可以使用二维码,很方便。而且通过后台接口可以获取小程序任意页面的小程序码扫描该小程序码可以直接进入小程序对应的页面,所有生成的小程序码永久有效请求地址POST方法https://api.weixin.qq.com/wxa/getwxaco…

因为现在做的小程序,想要分享小程序中的页面给微信好友,那就可以使用二维码,很方便。

而且通过后台接口可以获取小程序任意页面的小程序码

扫描该小程序码可以直接进入小程序对应的页面,所有生成的小程序码永久有效

请求地址

  1. POST方法
  2. https://api.weixin.qq.com/wxa/getwxacodeunlimit?
  3. access_token=ACCESS_TOKEN

后台代码

  1. import org.springframework.http.HttpEntity;
  2. import org.springframework.http.HttpMethod;
  3. import org.springframework.http.ResponseEntity;
  4. import org.springframework.util.LinkedMultiValueMap;
  5. import org.springframework.util.MultiValueMap;
  6. import org.springframework.web.client.RestTemplate;
  7. import java.io.*;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. public class QRCodeHelp {
  11. //生成活动二维码
  12. public void createActQRCode()
  13. {
  14. String access_token = "你获取到的access_token" ;
  15. String path = "pages/index/huodong/huodong";//你小程序中页面的路径
  16. RestTemplate rest = new RestTemplate();
  17. InputStream inputStream = null;
  18. OutputStream outputStream = null;
  19. try {
  20. String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?" +
  21. "access_token="+access_token;
  22. Map<String,Object> param = new HashMap<>();
  23. param.put("scene", 参数值);
  24. param.put("page", path);
  25. param.put("width", 430);
  26. param.put("auto_color", false);
  27. Map<String,Object> line_color = new HashMap<>();
  28. line_color.put("r", 0);
  29. line_color.put("g", 0);
  30. line_color.put("b", 0);
  31. param.put("line_color", line_color);
  32. param.put("is_hyaline",false);
  33. MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
  34. HttpEntity requestEntity = new HttpEntity(param, headers);
  35. ResponseEntity<byte[]> entity =
  36. rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
  37. byte[] result = entity.getBody();
  38. inputStream = new ByteArrayInputStream(result);
  39. //我将图片存储到了服务器中的ActQRImage文件夹下
  40. File file = new File("/root/application/ActQRImage/1.png");
  41. if (!file.exists()){
  42. file.createNewFile();
  43. }
  44. outputStream = new FileOutputStream(file);
  45. outputStream.write(result);
  46. outputStream.flush();
  47. } catch (Exception e) {
  48. System.out.println("调用小程序生成微信永久小程序码URL接口异常"+e);
  49. } finally {
  50. if(inputStream != null){
  51. try {
  52. inputStream.close();
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. if(outputStream != null){
  58. try {
  59. outputStream.close();
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. }
  65. }
  66. }

scene参数存放与获取: 

后台生成了二维码之后,前端小程序也要进行操作来获取到scene中存放的参数。

1.如果只需要传一个参数 可以直接scene=参数值  例如:scene=123

  1. onLoad:function(options){
  2. if(options.scene){
  3. // options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
  4. var scene = decodeURIComponent(options.scene)
  5. }
  6. }

2.如果两个及两个以上的参数  可以是scene = "id/"+id+"*userId/"+userId;  例如:scene=id/1*userId/2

    这里自定义了scene的参数格式,以 / 代替 = ,以 * 代替 & 。

    因为如果用正常的格式(例如:id=1&userId=2)会导致参数识别一部分,因为小程序在识别二维码页面参数时,拿到的参数列表是这样的scene=id=1&userId=2,这样就会导致只能识别id,id后的等号后面的参数全都无效了。所以这里用特      殊字符代替常规字符,然后获取之后再用js去解析,从而拿到参数列表。 

  1. onLoad:function getOptions(options){
  2. // options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
  3. var scene = decodeURIComponent(options.scene);
  4. var obj = {};
  5. for (var i = 0; i < scene.split('*').length;i++){
  6. var arr = scene.split('*')[i].split('/');
  7. obj[arr[0]] = arr[1];
  8. }
  9. return obj
  10. }

 

请求参数

属性类型默认值必填说明
access_tokenstring 

接口调用凭证(获取方式见另一博客获取access_token)

scenestring 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式)
pagestring主页必须是已经发布的小程序存在的页面(否则报错),例如 pages/index/index, 根路径前不要填加 /,不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面
widthnumber430二维码的宽度,单位 px,最小 280px,最大 1280px
auto_colorbooleanfalse自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调,默认 false
line_colorObject{"r":0,"g":0,"b":0}auto_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} 十进制表示
is_hyalinebooleanfalse是否需要透明底色,为 true 时,生成透明底色的小程序

返回值说明

如果调用成功,会直接返回图片二进制内容,如果请求失败,会返回 JSON 格式的数据。

返回值

Buffer

返回的图片 Buffer

异常返回Object

JSON

属性类型说明
errcodenumber错误码
errmsgstring错误信息

errcode 的合法值

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

注意

  • POST 参数需要转成 JSON 字符串,不支持 form 表单提交。
  • 接口只能生成已发布的小程序的二维码
  • 调用分钟频率受限(5000次/分钟),如需大量小程序码,建议预生成

扫一扫在手机打开

评论
已有0条评论
0/150
提交
热门评论
相关推荐
微信小程序开发中常见的设计败笔
  • 开发资料
  • 2022-05-20 18:35
  • 14 0 0
+1
从王者荣耀里我学会的前端新手指引
  • 开发资料
  • 2022-05-20 18:35
  • 46 0 0
+1
微信小程序登录鉴权与获取用户信息
  • 开发资料
  • 2022-05-20 18:35
  • 3 0 0
+1
小程序录音功能实现
  • 开发资料
  • 2022-05-20 18:35
  • 26 0 0
+1
今日要闻
换一批
热点排行