【微信公众号开发】【9】生成带参数的二维码
前言:
1,目前有2种类型的二维码:
(1)临时二维码,是有过期时间的,最长可以设置为在二维码生成后的30天(即2592000秒)后过期,但能够生成较多数量。临时二维码主要用于帐号绑定等不要求二维码永久保存的业务场景
(2)永久二维码,是无过期时间的,但数量较少(目前为最多10万个)。永久二维码主要用于适用于帐号绑定、用户来源统计等场景。
2,二维码参数是分类型的,场景值ID(scene_id:数字,scene_str:字符串),且限制了大小
3,扫描二维码后,对事件的处理(见第四章)
4,公众号迁移到新公众号,带参数二维码会失效,普通二维码会跳转到新公众号(解决方案:重新生成数据,已打印或截图的就没办法了)
正文:
accessToken 见第二章;最终获取的是一个链接,要转为图片见第七章
public static String QRCODE_URL = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s";
//生成带参数的永久二维码 public static String createQrcode(String param, String appName) { logger.info("createQrcode begin"); String access_token = getAccessToken(appName); String ticket = QrcodeUtil.foreverStrQrcodeTicket(access_token, param); if (StringUtils.isEmpty(ticket)) { //生成ticket为null时,可能是access_token失效或为空,也可以重新获取一次 logger.info(String.format("createQrcode ticket is null. appName:%s, param1:%s", appName, param)); return ""; } String qrcodePath = String.format(WxConfig.QRCODE_URL, ticket); return qrcodePath; }
QrcodeUtil
public class QrcodeUtil { public static Logger logger = LoggerFactory.getLogger(QrcodeUtil.class.getName()); //获取永久数字参数二维码的ticket public static String foreverIntQrcodeTicket(String accessToken, int sceneId) { String requestUrl = String.format(WxConfig.QRCODE_TICKET_URL, accessToken); String outputStr = "{\"action_name\": \"QR_LIMIT_SCENE\", \"action_info\":{\"scene\": {\"scene_id\":\"%s\"}}}"; JSONObject jsonObject = HttpRequest.httpRequest(requestUrl, "POST", String.format(outputStr, sceneId)); String ticket = dealQrcodeTicketJson(accessToken, jsonObject); return ticket; } //获取永久字符参数二维码的ticket public static String foreverStrQrcodeTicket(String accessToken, String sceneStr) { String requestUrl = String.format(WxConfig.QRCODE_TICKET_URL, accessToken); String outputStr = "{\"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\":{\"scene\": {\"scene_str\":\"%s\"}}}"; JSONObject jsonObject = HttpRequest.httpRequest(requestUrl, "POST", String.format(outputStr, sceneStr)); String ticket = dealQrcodeTicketJson(accessToken, jsonObject); return ticket; } //获取临时数字参数二维码的ticket public static String tempIntQrcodeTicket(String accessToken, int sceneId) { String requestUrl = String.format(WxConfig.QRCODE_TICKET_URL, accessToken); String outputStr = "{\"expire_seconds\": 300, \"action_name\": \"QR_SCENE\", \"action_info\":{\"scene\": {\"scene_id\":\"%s\"}}}"; JSONObject jsonObject = HttpRequest.httpRequest(requestUrl, "POST", String.format(outputStr, sceneId)); String ticket = dealQrcodeTicketJson(accessToken, jsonObject); return ticket; } //获取临时字符参数二维码的ticket public static String tempStrQrcodeTicket(String accessToken, String sceneStr) { String requestUrl = String.format(WxConfig.QRCODE_TICKET_URL, accessToken); String outputStr = "{\"expire_seconds\": 300, \"action_name\": \"QR_STR_SCENE\", \"action_info\":{\"scene\": {\"scene_str\":\"%s\"}}}"; JSONObject jsonObject = HttpRequest.httpRequest(requestUrl, "POST", String.format(outputStr, sceneStr)); String ticket = dealQrcodeTicketJson(accessToken, jsonObject); return ticket; } //共用方法 public static String dealQrcodeTicketJson(String accessToken, JSONObject jsonObject) { String ticket = null; if (null != jsonObject) { try { ticket = jsonObject.getString("ticket"); }catch (Exception e) { logger.info(String.format("errorCode:%s;errmsg:%s", jsonObject.getString("errcode"), jsonObject.getString("errmsg"))); } } return ticket; } }