biubiuchen
2019-09-19 16:03:28
微信小程序码的获取
最近在做一个小程序,期间发现一个有趣儿的功能,生成小程序码,官方api地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.createQRCode.html,现将实现过程贴出来:
1:获取token
//授权(必填)
String grant_type = "client_credential";
//URL
String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?";
String params = "appid=" + Const.MINAPPID + "&secret=" + Const.MINSECRET + "&grant_type=" + grant_type;
//发送请求
JSONObject data = NetUtil.get(requestUrl + params);
return data.getString("access_token");
2:将获取到的图片buffer转为base64
public static String imgBase64(String token, String path, Integer width, String scene, Integer type) {
RestTemplate rest = new RestTemplate();
InputStream inputStream = null;
OutputStream outputStream = null;
try {
String url = null;
if (1 == type) {
//获取小程序二维码,适用于需要的码数量较少的业务场景
url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" + token;
}
if (2 == type) {
//获取小程序码,适用于需要的码数量较少的业务场景
url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + token;
}
if (3 == type) {
//获取小程序码,适用于需要的码数量极多的业务场景。
url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + token;
}
Map<String, Object> param = new HashMap<>();
param.put("path", path);
param.put("width", width);
param.put("auto_color", false);
param.put("scene", scene);
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);
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
org.springframework.http.HttpEntity requestEntity = new HttpEntity(param, headers);
ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
byte[] result1 = entity.getBody();
//切记加上"data:image/png;base64,不然会报错"
return "data:image/png;base64," + Base64.encodeBase64String(result1);
} 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;
}
3:将获取到的base64转为 MultipartFile
public static MultipartFile base64ToMultipart(String base64) {
try {
String[] baseStrs = base64.split(",");
BASE64Decoder decoder = new BASE64Decoder();
byte[] b = new byte[0];
b = decoder.decodeBuffer(baseStrs[1]);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
return new BASE64DecodedMultipartFile(b, baseStrs[0]);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
4:将图片传到服务器,并返回带有https的图片路径
5:结果
{
"success": true,
"message": "操作成功!",
"code": 0,
"result": "https://**/1568880379942.png",
"timestamp": 1568880230848
}
评论
最近浏览
Adguard LV3
2024年6月28日
豆子小兔子 LV9
2022年10月13日
百年人生
2022年5月18日
暂无贡献等级
噜噜噜路口 LV2
2021年6月16日
Ambition_ning
2020年9月12日
暂无贡献等级
baizhongcai LV24
2020年8月21日
随便取个名字_哈哈 LV27
2020年6月7日
qaz7225277
2020年4月26日
暂无贡献等级
Enjack
2020年2月24日
暂无贡献等级
kww999
2020年1月9日
暂无贡献等级


