当前位置:   article > 正文

Java 生成微信小程序二维码_java 生成小程序码

java 生成小程序码

生成二维码图片,拿到图片路径

   @Value("${xcx.AppletAppId}")
    private String appId;
    @Value("${xcx.AppletAppSecret}")
    private String appSecret;
    @Value("${file.uploadFolder}")
    private String uploadFolder;
    @Value("${file.url}")
    private String url;

    @GetMapping("/shareQr")
    @ApiOperation("自评分享码")
    public AjaxResult shareQr( @RequestParam("id") String id  ) throws Exception {
        if (id == null ){
            return AjaxResult.Error("数据异常。");
        }
            String scene = "id=" + id  ; // 替换成自己的参数值
            String name = id;
           // 替换成自己的页面路径
            String page = "pages/choose/detail/index?"+scene; // 替换成自己的页面路径
            String now = DateUtils.format(new Date(), "yyyy-MM-dd");
            String filePath = uploadFolder +"pcQr" + File.separator + now + "-" + name+".png";
            //正式版为 "release",体验版为 "trial",开发版为 "develop"。
            String envVersion = "trial";
            Boolean checkPath = true ;


            MiniProgramQRCode.getQRCode( appId  ,   appSecret , scene , page , filePath  ,  envVersion  ,  checkPath);

        return AjaxResult.Success("success", filePath);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
package com.sipsd.util;




import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;


public class MiniProgramQRCode {

    private static final String API_GET_TOKEN = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
    private static final String API_GET_QR_CODE = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=%s";
//    private static final String API_GET_QR_CODE = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s";
//    private static final String API_GET_QR_CODE = "https://api.weixin.qq.com/wxa/getwxacode?access_token=%s";


//    wxacode.get -----A    path
//    获取小程序码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制,详见获取二维码。
//    POST https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN

//    wxacode.getUnlimited  --B   page
//    获取小程序码,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制。 更多用法详见 获取二维码。
//    POST https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
//
//    wxacode.createQRCode -- C   path
//    获取小程序二维码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制,详见获取二维码。
//    POST https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN



    public static void  getQRCode( String appId  ,  String appSecret , String scenes , String pages ,  String filePaths ,String envVersion , Boolean checkPath) throws Exception {
        String accessToken = getToken(appId, appSecret);
        String scene = scenes; // 替换成自己的参数值
        String page = pages; // 替换成自己的页面路径
        int width = 200; // 替换成自己的二维码宽度
        String filePath = filePaths; // 替换成自己的保存路径
        byte[] qrCodeBytes = getQRCode(accessToken, scene, page, width ,  envVersion ,   checkPath);
        FileOutputStream fos = new FileOutputStream(filePath);
        IOUtils.write(qrCodeBytes, fos);
        fos.flush();
        fos.close();
    }
    public static String getToken(String appId, String appSecret) throws Exception {
        String url = String.format(API_GET_TOKEN, URLEncoder.encode(appId, "UTF-8"), URLEncoder.encode(appSecret, "UTF-8"));
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        InputStream is = conn.getInputStream();
        String json = IOUtils.toString(is);
        is.close();
        conn.disconnect();
        return json.substring(json.indexOf("access_token") + "access_token".length() + 3, json.indexOf("expires_in") - 3);
    }
    public static byte[] getQRCode(String accessToken, String scene, String page, int width , String envVersion  ,Boolean checkPath) throws Exception {
        String url = String.format(API_GET_QR_CODE, accessToken);
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setDoOutput(true);
        if (StringUtils.isNotBlank(scene)) {
            page = page + "?" + scene;
        }
        if (StringUtils.isBlank(envVersion)) {
            envVersion = "release";
        }
//        正式版为 "release",体验版为 "trial",开发版为 "develop"。
//        String json = "{\"page\":\"" + page + "\",\"width\":" + width + ", \"scene\":\"" + scene + "\" ,  \"env_version\":\" "+ envVersion + "\" ,  \"check_path\":\""+ checkPath + "\"      }";
        String json = "{\"path\":\"" + page + "\",\"width\":" + width + ",   \"env_version\":\" "+ envVersion + "\" ,  \"check_path\":\""+ checkPath + "\"      }";
        conn.getOutputStream().write(json.getBytes("UTF-8"));
        InputStream is = conn.getInputStream();
        byte[] qrCodeBytes = IOUtils.toByteArray(is);
        is.close();
        conn.disconnect();
        return qrCodeBytes;
    }






}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号