提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
调用接口获取小程序码
一、小程序码工具类
package com.hxtt.utils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
/**
*
*
* @date 2023-04-12
*/
@Slf4j
public class ImageUtils {
/**
* @param mpData 小程序码中写入的数据
* @throws IOException
*/
// 获取特定对象对应的小程序码(保存到本地)
public static String saveMPQRCodeToServer(HashMap<String, Object> mpData) throws Exception {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
String uri = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+mpData.get("access_token");
String name = mpData.get("name").toString();
//需要跳转的小程序版本
//正式版为 "release",体验版为 "trial",开发版为 "develop"
String version = mpData.get("env_version").toString();
mpData.remove("name");
mpData.remove("access_token");
JSONObject dataJson = new JSONObject(mpData);
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<String> entity = new HttpEntity<>(dataJson.toJSONString(), headers);
log.info(entity.getBody());
ResponseEntity<byte[]> response = restTemplate.exchange(uri, HttpMethod.POST, entity, byte[].class);
return ByteArrayConvertToImage(response.getBody(),version,"小程序码_"+name+".jpg");
}
/**
* @param mpData 小程序码中写入的数据
* @throws IOException
*/
// 获取特定对象对应的小程序码(将byte数组返回到前台)
public static byte[] getMPQRCode(HashMap<String, Object> mpData) throws Exception {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
String uri = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+mpData.get("access_token");
// String name = mpData.get("name").toString();
// mpData.remove("name");
mpData.remove("access_token");
JSONObject dataJson = new JSONObject(mpData);
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<String> entity = new HttpEntity<>(dataJson.toJSONString(), headers);
log.info(dataJson.toJSONString());
ResponseEntity<byte[]> response = restTemplate.exchange(uri, HttpMethod.POST, entity, byte[].class);
return response.getBody();
}
//保存到服务器
private static String ByteArrayConvertToImage(byte[] buffer,String version,String name) throws Exception
{
String pathPre = "D:/images/qrcode/"+version+"/";
String path = pathPre+name;
File f = new File(pathPre);
if(!f.exists()) f.mkdirs();
OutputStream os = Files.newOutputStream(Paths.get(path));
os.write(buffer, 0, buffer.length);
os.flush();
os.close();
return path;
}
}
获取token的方法略
二、调用
@ApiOperation("获取二维码")
@PostMapping (value = "/getMPQRCode")
// 获取单个对象对应的小程序码
public ResponseEntity<Object> getMPQRCode(@RequestBody Map info) throws Exception {
//可以通过info传入图片大小、跳转时需要传入的参数、跳转路径、小程序版本等
HashMap<String, Object> params = new HashMap<>();
String accessToken = wXTokenUtil.getAccessToken();
boolean width = info.containsKey("width");
if(width) params.put("width",info.get("width"));
else params.put("width",800);
params.put("scene",info.get("id"));
params.put("access_token", accessToken);
params.put("env_version", info.get("version"));
params.put("page", "pages/reportToThePolice/reportToThePolice");
byte[] qrCode = ImageUtils.getMPQRCode(params);
return new ResponseEntity<>(qrCode,HttpStatus.OK);
}
注:
1、将小程序码保存到服务器本地的方式与上面类似。
2、目前微信没有提供同时生成多个小程序码的API,可以通过循环调用ImageUtils中的方法来实现。
文章介绍了使用SpringBoot和Fastjson库在Java中创建小程序码工具类,通过调用微信API获取和保存小程序码,以及在API中调用生成二维码的方法,支持传入参数动态生成不同尺寸和版本的小程序码。
1161

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



