前言:最近一直在开发微信的东西,总结一下微信生成带参数的二维码。这个其实在参考文章的第一篇总结的非常详细,大家可以参考一下。这里总结一下微信生成带参数二维码的过程和主要开发代码。
注:本文使用RestTemplate提供远程请求,RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法。 参考文章:Spring提供的用于访问Rest服务的客户端:RestTemplate实践
一、微信二维码介绍目前有2种类型的二维码: 1、临时二维码:是有过期时间的,最长可以设置为在二维码生成后的30天(即2592000秒)后过期,但能够生成较多数量。临时二维码主要用于帐号绑定等不要求二维码永久保存的业务场景 2、永久二维码:是无过期时间的,但数量较少(目前为最多10万个)。永久二维码主要用于适用于帐号绑定、用户来源统计等场景。
参考文章:生成带参数的二维码
二、创建二维码创建二维码我们需要先获取access_token,再传入参数,获取ticket或者url得到二维码。
1:获取access_token 2:创建二维码请求说明 2.1 临时二维码请求说明http请求方式: POST
URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKENPOST数据格式:json
POST数据例子:{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}}
http请求方式: POST
URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKENPOST数据格式:json
POST数据例子:{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123}}}
或者也可以使用以下POST数据创建字符串形式的二维码参数:
{"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": "123"}}}
// 临时二维码
private final static String QR_SCENE = "QR_SCENE";
// 永久二维码
private final static String QR_LIMIT_SCENE = "QR_LIMIT_SCENE";
// 永久二维码(字符串)
private final static String QR_LIMIT_STR_SCENE = "QR_LIMIT_STR_SCENE";
// 创建二维码
private String create_ticket_path = "https://api.weixin.qq.com/cgi-bin/qrcode/create";
// 通过ticket换取二维码
private String showqrcode_path = "https://mp.weixin.qq.com/cgi-bin/showqrcode";
/**
* 创建临时带参数二维码
* @param accessToken
* @expireSeconds 该二维码有效时间,以秒为单位。 最大不超过2592000(即30天),此字段如果不填,则默认有效期为30秒。
* @param sceneId 场景Id
* @return
*/
public String createTempQr(String accessToken, String expireSeconds, int sceneId) {
RestTemplate rest = new RestTemplate();
String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+accessToken ;
// 参数:{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}}
Map intMap = new HashMap();
intMap.put("scene_id",sceneId);
Map mapMap = new HashMap();
mapMap.put("scene", intMap);
Map paramsMap = new HashMap();
paramsMap.put("expire_seconds", expireSeconds);
paramsMap.put("action_name", QR_SCENE);
paramsMap.put("action_info", mapMap);
MultiValueMap headers = new LinkedMultiValueMap();
HttpEntity requestEntity = new HttpEntity(paramsMap, headers);
Map result = null;
try {
ResponseEntity entity = rest.exchange(url, HttpMethod.POST, requestEntity,Map.class, new Object[0]);
LOG.info("调用生成微信临时二维码URL接口返回结果:" + entity.getBody());
result = (Map) entity.getBody();
} catch (Exception e) {
LOG.error("调用生成微信临时二维码URL接口异常",e);
}
if(!BlankUtil.isBlank(result)){
return result;
}
return null;
}
/**
* 创建永久二维码(字符串)
* @param sceneId 场景id
* @param accessToken
* @return
*/
public Map createForeverQr(Integer sceneId,String accessToken ) {
RestTemplate rest = new RestTemplate();
String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+accessToken ;
// 参数:{"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": sceneStr}}}
Map param = new HashMap();
param.put("action_name", "QR_LIMIT_STR_SCENE");
Map action = new HashMap();
Map scene = new HashMap();
scene.put("scene_id", sceneId);
action.put("scene", scene);
param.put("action_info", action);
MultiValueMap headers = new LinkedMultiValueMap();
HttpEntity requestEntity = new HttpEntity(param, headers);
Map result = null;
try {
ResponseEntity entity = rest.exchange(url, HttpMethod.POST, requestEntity,Map.class, new Object[0]);
LOG.info("调用生成微信永久二维码URL接口返回结果:" + entity.getBody());
result = (Map) entity.getBody();
} catch (Exception e) {
LOG.error("调用生成微信永久二维码URL接口异常",e);
}
if(!BlankUtil.isBlank(result)){
return result;
}
return null;
}
/**
* 创建永久二维码(字符串)
* @param sceneStr 场景值
* @param accessToken
* @return
*/
public Map createForeverStrQr(String sceneStr,String accessToken ) {
RestTemplate rest = new RestTemplate();
String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+accessToken ;
// 参数:{"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": sceneStr}}}
Map param = new HashMap();
param.put("action_name", "QR_LIMIT_STR_SCENE");
Map action = new HashMap();
Map scene = new HashMap();
scene.put("scene_str", sceneStr);
action.put("scene", scene);
param.put("action_info", action);
MultiValueMap headers = new LinkedMultiValueMap();
HttpEntity requestEntity = new HttpEntity(param, headers);
Map result = null;
try {
ResponseEntity entity = rest.exchange(url, HttpMethod.POST, requestEntity,Map.class, new Object[0]);
LOG.info("调用生成微信永久二维码URL接口返回结果:" + entity.getBody());
result = (Map) entity.getBody();
} catch (Exception e) {
LOG.error("调用生成微信永久二维码URL接口异常",e);
}
if(!BlankUtil.isBlank(result)){
return result;
}
return null;
}
正确的Json返回结果:
{"ticket":"gQH47joAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL2taZ2Z3TVRtNzJXV1Brb3ZhYmJJAAIEZ23sUwMEmm3sUw==","expire_seconds":60,"url":"http:\/\/weixin.qq.com\/q\/kZgfwMTm72WWPkovabbI"}
- 1
- 2
获取二维码ticket后,开发者可用ticket换取二维码图片。请注意,本接口无须登录态即可调用。
1.1 请求说明HTTP GET请求(请使用https协议)
https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET
提醒:TICKET记得进行UrlEncode
2:通过url换取二维码生成的微信二维码还包含一个图片解析后的地址url。
实例:
http://weixin.qq.com/q/02gxxGBN2HbaT10000w07E
我们开始使用草料二维码生成工具,或者根据url创建二维码图片。
参考文章:Java微信公众平台开发之生成带参二维码 微信官方技术文档—生成带参数的二维码