小程序开发时,遇到分享的逻辑,而小程序不支持URL直接拼接的方式。
微信提供小程序码可满足扫码进入小程序指定页面(可带参),微信返回的是图片buffer,需要处理。
这里提供获取小程序码后上传本地,然后返回本地URL路径。
官方API:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html
参考:(周末找的博客,具体链接找不到了,如下获取小程序代码段80%是参考那篇博客,以后找到把链接放上)
废话不说,直接上代码:
@RequestMapping(value="/getEwCode")
@ResponseBody
public String getEwCode( String page, String scene) {
RestTemplate rest = new RestTemplate();
InputStream inputStream = null;
OutputStream outputStream = null;
PageData pd = this.getPageData();
System.out.println("page " + page);
System.out.println("scene" + scene);
try {
//注:access_token 每天有调用上限,这里处理是放入redis缓存,设置有效期一小时
//redis工具类这里就不提供了
access_token_url = access_token_url.replace("APPID", appId).replace("APPSECRET", appKey);
System.out.println("access_token_url::" + access_token_url);
String access_token = "";
boolean isYx = redisDaoImpl.hasKey("hgdxcx_access_token");
System.out.println("是否存在缓存 : " + isYx);
if(isYx){
System.out.println("存在");
String hgdxcx_access_token= redisDaoImpl.get("hgdxcx_access_token");
access_token = hgdxcx_access_token;
}else{
System.out.println("不存在");
//这里使用的工具类是微信官方util
access_token = WxUtils.getAccessToken(access_token_url);
redisDaoImpl.addString("hgdxcx_access_token", access_token);
redisDaoImpl.expire("hgdxcx_access_token", 60 * 60 );//生命周期:1H
}
System.out.println("access_token : " + access_token);
//获取小程序码调用API
String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + access_token;
Map<String,Object> param = new HashMap();
param.put("page", page);//小程序页面
param.put("width", 430);
param.put("scene", scene);//参数
param.put("auto_color", true);
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]);
byte[] result = entity.getBody();
inputStream = new ByteArrayInputStream(result);
String ffile = DateUtil.getDays(), fileName = UUID.randomUUID().toString().trim().replaceAll("-", "") + ".png";
//本地上传,路径填写自己项目路径
String filePath = PathUtil.getClasspath() +“uploadFiles/” + ffile + fileName; //文件上传路径
System.out.println("filePath : " + filePath);
File file = new File(filePath);
outputStream = new FileOutputStream(file);
int len = 0;
byte[] buf = new byte[1024];
while ((len = inputStream.read(buf, 0, 1024)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.flush();
//保存到数据表
String moiUserId = this.getWxUser().getId();
pd.put("id", this.get32UUID());
pd.put("moi_user_id", moiUserId);
pd.put("page", page);
pd.put("scene", scene);
System.out.println("pd : " + pd);
dao.save("AppletPersonCenterMapper.saveCodeInfo", pd);
//返回本地图片路径
return “http://localhost:8080/test/” + "uploadFiles/" + ffile + fileName;
} catch (Exception e) {
System.out.println("调用小程序生成微信永久二维码URL接口异常" + e);
e.printStackTrace();
} 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;
}