需求
前端H5页面,需要有二维码跳转小程序,实现通过二维码邀请商户入驻平台。
官方文档
微信关于小程序码有三个应用场景:
wxacode.createQRCode:适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制
wxacode.get:适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制
wxacode.getUnlimited:适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制
文档地址
服务端步骤
-
获取access_token:文档地址
照着文档来就行,有APPID和秘钥,发出Http请求就能获取的通行凭证 -
获得小程序码,我这里返回的是base64编码的字符串,交给前端浏览器解析,如果是小程序的话,需要你生成图片,把图片URL地址返回给前端就行。
代码
` public String getQRCode(String wxToken, String sceneStr) throws IOException {
RestTemplate rest = new RestTemplate();InputStream inputStream = null; OutputStream outputStream = null; String reuslt = null; try { String url = WXCODE_URL+wxToken; Map<String,Object> param = new HashMap<>(); param.put("scene", sceneStr); param.put("page", "pages/index/index"); param.put("width", 200); param.put("auto_color", false); Map<String,Object> line_color = new HashMap<>(); line_color.put("r", 0); line_color.put("g", 0); line_color.put("b", 0); param.put("line_color", line_color); log.info("调用生成微信URL接口传参:" + param); MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); HttpEntity requestEntity = new HttpEntity(param, headers); ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]); log.info("调用小程序生成微信永久小程序码URL接口返回结果:" + entity.getBody()+entity.getStatusCode()); byte[] result = entity.getBody(); log.info(Base64.encodeBase64String(result)); reuslt = Base64.encodeBase64String(result); return reuslt; } catch (Exception e) { log.error("调用小程序生成微信永久小程序码URL接口异常",e); } finally { if(inputStream != null){ try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if(outputStream != null){ try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return null;}`
总结
自己对文件字符字节流不熟悉,感觉腾讯对文档的支持也是不够完善,好在最后参考各个大佬们的作业,终于整出来了。加油
本文介绍如何在H5页面中生成小程序二维码,用于邀请商户入驻平台。详细讲解了使用微信提供的三种不同场景下的小程序码生成接口,以及服务端代码实现。
3690

被折叠的 条评论
为什么被折叠?



