版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/w410589502/article/details/72910725
前言:最近一直在开发微信的东西,总结一下微信生成带参数的二维码。这个其实在参考文章的第一篇总结的非常详细,大家可以参考一下。这里总结一下微信生成带参数二维码的过程和主要开发代码。
注:本文使用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}}}
2.2 永久二维码请求说明
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"}}}
2.3 参数说明
参数 | 说明 |
---|---|
expire_seconds | 该二维码有效时间,以秒为单位。 最大不超过2592000(即30天),此字段如果不填,则默认有效期为30秒。 |
action_name | 二维码类型,QR_SCENE为临时,QR_LIMIT_SCENE为永久,QR_LIMIT_STR_SCENE为永久的字符串参数值 |
action_info | 二维码详细信息 |
scene_id | 场景值ID,临时二维码时为32位非0整型,永久二维码时最大值为100000(目前参数只支持1–100000) |
scene_str | 场景值ID(字符串形式的ID),字符串类型,长度限制为1到64,仅永久二维码支持此字段 |
3:java接口开发
3.1 常量
// 临时二维码
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";
3.2 创建临时带参数二维码
/**
* 创建临时带参数二维码
* @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<String,Integer> intMap = new HashMap<String,Integer>();
intMap.put("scene_id",sceneId);
Map<String,Map<String,Integer>> mapMap = new HashMap<String,Map<String,Integer>>();
mapMap.put("scene", intMap);
Map<String,Object> paramsMap = new HashMap<String,Object>();
paramsMap.put("expire_seconds", expireSeconds);
paramsMap.put("action_name", QR_SCENE);
paramsMap.put("action_info", mapMap);
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
HttpEntity requestEntity = new HttpEntity(paramsMap, headers);
Map result = null;
try {
ResponseEntity<Map> 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;
}
3.3 创建永久二维码(数字)
/**
* 创建永久二维码(字符串)
* @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<String,Object> param = new HashMap<>();
param.put("action_name", "QR_LIMIT_STR_SCENE");
Map<String,Object> action = new HashMap<>();
Map<String,Object> scene = new HashMap<>();
scene.put("scene_id", sceneId);
action.put("scene", scene);
param.put("action_info", action);
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
HttpEntity requestEntity = new HttpEntity(param, headers);
Map result = null;
try {
ResponseEntity<Map> 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;
}
3.4 创建永久二维码(字符串)
/**
* 创建永久二维码(字符串)
* @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<String,Object> param = new HashMap<>();
param.put("action_name", "QR_LIMIT_STR_SCENE");
Map<String,Object> action = new HashMap<>();
Map<String,Object> scene = new HashMap<>();
scene.put("scene_str", sceneStr);
action.put("scene", scene);
param.put("action_info", action);
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
HttpEntity requestEntity = new HttpEntity(param, headers);
Map result = null;
try {
ResponseEntity<Map> 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;
}
4:返回参数
4.1 返回说明
正确的Json返回结果:
{"ticket":"gQH47joAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL2taZ2Z3TVRtNzJXV1Brb3ZhYmJJAAIEZ23sUwMEmm3sUw==","expire_seconds":60,"url":"http:\/\/weixin.qq.com\/q\/kZgfwMTm72WWPkovabbI"}
4.2 参数说明
参数 | 说明 |
---|---|
ticket | 获取的二维码ticket,凭借此ticket可以在有效时间内换取二维码。 |
expire_seconds | 该二维码有效时间,以秒为单位。 最大不超过2592000(即30天)。 |
url | 二维码图片解析后的地址,开发者可根据该地址自行生成需要的二维码图片 |
三、获取二维码图片
1:通过ticket换取二维码
获取二维码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创建二维码图片。