微信生成小程序码

微信生成小程序码扫描唤醒小程序(2)
	我做的是 _**带参数无限个数小程序码**,有什么问题或者错误,请及时指出! 
	 (有些问题我还没有搞懂!但是代码能跑,小程序码能跳转!)

微信官方文档地址 微信公众平台配置说明

  • 配置微信小程序后台(至今也不能跳转到开发版),登陆微信公众平台 ,在首页下面找到<开发>点击进去,然后在当前页面点击开发设置,滑到页面最下方
    在这里插入图片描述
  • 接下来看下具体的配置(只供参考,我也配的不是很明确,参考者自己去看下文档吧)
    在这里插入图片描述
  • 这里需要说下,调用微信接口生成小程序码需要accessToken,如何获取accessToken,这边我还是把代码贴一下吧
    @PostMapping("getToken")
    @ApiOperation(value = "获取AccessToken")
    public JSONObject getAccessToken(@ReqHeader @ApiIgnore ApiReqHeader reqHeader) throws IOException {

        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
        // 这两个字段在微信的配置文件里面找吧,
        String APPID = "你的AppId";
        String APPSECRET = "你的AppSecret";
        String request_url = url.replace("APPID", APPID).replace("APPSECRET", APPSECRET);
        
        URL conn_url =  new URL(request_url);
        HttpURLConnection conn = (HttpsURLConnection)conn_url.openConnection();
        // 这个GET请求必须是大写的, 掉进过坑里一下
        conn.setRequestMethod("GET");
        conn.setReadTimeout(5000);
        conn.setConnectTimeout(5000);
        conn.connect();
        JSONObject jsonObject = null;
        
        if(conn.getResponseCode()==200){
            InputStream stream = conn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(stream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            StringBuffer buffer = new StringBuffer();
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            bufferedReader.close();
            inputStreamReader.close();
            stream.close();
            conn.disconnect();
            jsonObject = JSONObject.parseObject(buffer.toString());
        }
        return jsonObject;
    }
  • 切记,如果AppId和AppSecret获取错了或者配置文件里的错了(scene中的参数没有的也是生成不了二维码的),你生成的二维码是没有用的
  • accessToken有了就来调用微信的接口吧,
Controller
  • 缺啥自己就注释啥吧,反正这段代码也不重要
 	/**
     * <p>App端-扫二维码唤醒小程序</p>
     */
    @PostMapping("createQRCodeMap/{accessToken}")
    @ApiOperation(value = "生成二维码",notes="路径后面带AccessToken")
    public ResultWrap<MemberInfoURLIO> createQRCodeMap(@ReqHeader ApiReqHeader reqHeader,@PathVariable("accessToken") String accessToken){

        log.info("接收{}请求报文:{}", OperationType.CREATE, LogWriteUtils.getJsonStr(reqHeader));
        String base64 = memberInfoService.createQRCodeMap(reqHeader,accessToken);
        // 上传图片
        String path = fastDFSFileUploadManager.uploadBase64(base64);
        // 配置待修改,先用着吧
        //MemberInfoBO memberInfoBO = memberInfoService.updataInvitationCodeUrl(reqHeader, getBaseUrl().concat("/").concat(path));
        String url = "http://***.115.137.33:880";
        MemberInfoBO memberInfoBO = memberInfoService.updataInvitationCodeUrl(reqHeader, url.concat("/").concat(path));
        MemberInfoURLIO memberInfoURLIO = POJOUtils.copyProperties(memberInfoBO, MemberInfoURLIO.class);
        return RestParamsUtils.toRespWrapSucess(memberInfoURLIO);

    }
Service
  • 自己要什么要的格式的数据自己转,记得把流关下!
/**
     * <p>扫描二维码唤起小程序</p>
     */
    public String createQRCodeMap(ApiReqHeader reqHeader, String accessToken) {
		// 这个是我获取到的参数放入到scene中
        MemberInfoPO memberInfoPO = memberInfoDao.findById(reqHeader.getUserId());
        MemberInfoVO memberInfoVO = POJOUtils.copyProperties(memberInfoPO, MemberInfoVO.class);
        String invitationCode = memberInfoVO.getInvitationCode();
        
        String base64String = "";
        try {
            URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");// 提交模式
            // conn.setConnectTimeout(10000);//连接超时 单位毫秒
            // conn.setReadTimeout(2000);//读取超时 单位毫秒
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
            // 发送请求参数
            JSONObject paramJson = new JSONObject();
            paramJson.put("scene", invitationCode);
            paramJson.put("path", "pages/index/index");
            paramJson.put("width", 430);
            printWriter.write(paramJson.toString());
            // flush输出流的缓冲
            printWriter.flush();
            //开始获取数据
            BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());

            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
            //buff用于存放循环读取的临时数据
            byte[] buff = new byte[1024];
            int rc = 0;
            while ((rc = bis.read(buff, 0, 100)) > 0) {
                swapStream.write(buff, 0, rc);
            }
            byte[] bytes = swapStream.toByteArray();
            // 转为base64
            BASE64Encoder encoder = new BASE64Encoder();
            String binary = encoder.encodeBuffer(bytes).trim();
            // 去除字符串中的所有空格
            String replaceAll = (binary).replaceAll(" +", "");
            base64String = "data:image/png;base64,"+replaceAll;
            System.out.println(base64String);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 自己把流关一下
        return base64String;
}
成果 在这里插入图片描述
  • 再来一个demo,这玩意挺好找的,百度一搜一大堆
@Slf4j
public class Test1 {
    public static void main(String[] args) {
    
        try {
            URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=你的accessToken");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");// 提交模式
            // conn.setConnectTimeout(10000);//连接超时 单位毫秒
            // conn.setReadTimeout(2000);//读取超时 单位毫秒
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
            // 发送请求参数
            JSONObject paramJson = new JSONObject();
            paramJson.put("scene", "12312312");
            paramJson.put("path", "pages/index/index");
            paramJson.put("width", 430);
            printWriter.write(paramJson.toString());
            // flush输出流的缓冲
            printWriter.flush();
            //开始获取数据
            BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());

            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
            //buff用于存放循环读取的临时数据
            byte[] buff = new byte[1024];
            int rc = 0;
            while ((rc = bis.read(buff, 0, 100)) > 0) {
                swapStream.write(buff, 0, rc);
            }
            swapStream.flush();
            byte[] bytes = swapStream.toByteArray();
            // 转为base64
            BASE64Encoder encoder = new BASE64Encoder();
            String binary = encoder.encodeBuffer(bytes).trim();
            String replaceAll = (binary).replaceAll(" +", "");
            String base64String = "data:image/png;base64,"+replaceAll;
            
            System.out.println(base64String);
            System.out.println("12312312");
            System.out.println("12312312");
            System.out.println("12312312");
          
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
尾记
  • 至今还搞不懂怎么跳转到开发版,因为跳转的是线上版本的,测试很不方便(我是后端我无所谓咯),在微信的开发者专区里面找了找,都说跳不了,但是微信官方的开发者说可以! :)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值