生成带参数的二维码
文档中有这么一句话,“永久二维码,是无过期时间的,但数量较少(目前为最多10万个)。”
为了减少永久二维码的生成,在实际开发中应该灵活地重复使用被废弃掉的永久二维码。所谓的“被废弃掉的”,举例说明,一个会议报名的永久二维码,当且仅当该会议结束了,该会议报名的永久二维码就可以被其他有有效的会议再次使用。
2、开发实例说明 2.1 表结构
-- Create table
create table
(
id number(10) not null,
meeting_id number(10),
type number(2),
ticket varchar2(200),
scene_id number(10),
create_time number
)
;
-- Add comments to the columns
comment on column .id
is 'ID';
comment on column .meeting_id
is '会议ID';
comment on column .type
is '类型1.报名;2.签到';
comment on column .ticket
is '微信ticket';
comment on column .scene_id
is '场景id';
comment on column .create_time
is '创建时间';
2.2 SQL编写
-- 查询一条最新会议的签到结束的永久二维码记录
select *
from (select a.*
from (select * from meeting_permanent_qrcode where type = 1) a
inner join (select id from meeting where status = 3) b
on a.meeting_id = b.id
order by a.create_time desc)
where rownum < 2
2.3 Java 编写
/**
* 查看报名页二维码
*
* @param meeting_id
*/
public void viewRegistrationQrcode(Model model, String meeting_id) {
try {
Map filter = new HashMap();
filter.put("meeting_id", meeting_id);
filter.put("type", 1L);
MeetingPermanentQrcodeFormMap meetingPermanentQrcodeFormMap = meetingPermanentQrcodeMapper.selectMeetingPermanentQrcodeBy(filter);
String ticket = null;
if (meetingPermanentQrcodeFormMap != null) {
ticket = meetingPermanentQrcodeFormMap.getStr("ticket");
}
MeetingFormMap meetingFormMap = new MeetingFormMap();
meetingFormMap.put("id", meeting_id);
meetingFormMap = meetingMapper.findById(meetingFormMap);
if (!"3".equals(meetingFormMap.getStr("status"))) {// 会议未结束
if (StringUtils.isBlank(ticket)) {// 二维码未生成过
MeetingPermanentQrcodeFormMap lastestOutOfDataMeetingPermanentQrcode = meetingPermanentQrcodeMapper.selectLastestOutOfDataMeetingPermanentQrcode(1L);
if (lastestOutOfDataMeetingPermanentQrcode == null) {// 没有待被占用的永久二维码记录
String sceneId = "14" + meeting_id;// 14开头+会议id,二维码参数唯一
Map map5 = WXService.createQrcodeQrLimitScene(sceneId);
String ecode = URLEncoder.encode(map5.get("ticket").toString());
MeetingPermanentQrcodeFormMap saveMap = new MeetingPermanentQrcodeFormMap();
// type + sceneId 确定一条永久二维码记录
saveMap.put("meeting_id", meeting_id);
saveMap.put("type", 1L);
saveMap.put("ticket", ecode);
saveMap.put("scene_id", sceneId);
saveMap.put("create_time", new Date().getTime());
baseComponentService.addEntity(saveMap);
model.addAttribute("ticket", ecode);
} else {// 有待被占用的永久二维码记录,更新meeting_id
lastestOutOfDataMeetingPermanentQrcode.put("meeting_id", meeting_id);
model.addAttribute("ticket", lastestOutOfDataMeetingPermanentQrcode.getStr("ticket"));
baseComponentService.editEntity(lastestOutOfDataMeetingPermanentQrcode);
}
} else {// 二维码生成过,不再重新生成
model.addAttribute("ticket", ticket);
}
} else {// 会议结束
if (StringUtils.isBlank(ticket)) {// 二维码被占用
model.addAttribute("ticket", null);
} else {// 二维码未被占用
model.addAttribute("ticket", ticket);
}
}
} catch (Exception e) {
log.error(e.getMessage());
}
}
2.4 JSP
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<img height="400" alt="会议已结束,二维码已被其他会议占用" src="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=${ticket}">
2.5 扫码后台响应事件
if ("14".equals(top)) {// 会议报名
MeetingPermanentQrcodeFormMap meetingPermanentQrcodeFormMap = new MeetingPermanentQrcodeFormMap();
// type + sceneId 确定一条永久二维码记录
meetingPermanentQrcodeFormMap.put("scene_id", sceneId);
meetingPermanentQrcodeFormMap.put("type", 1L);
meetingPermanentQrcodeFormMap = meetingPermanentQrcodeMapper
.selectMeetingPermanentQrcodeBy(meetingPermanentQrcodeFormMap);
String meeting_id = meetingPermanentQrcodeFormMap.getStr("meeting_id");
String url = "http://" + propertiesUtil.getProperty("wx.domain")
+ "/meetingregistration/wxRegistrationPage.shtml?meeting_id=" + meeting_id;
String content = "您已成功获取报名链接,您可以点击<a href='" + url + "'>会议报名进行会议报名,谢谢!";
String str = MessageUtil.constructTextMessageXml(toUserName, fromUserName, content);
response.getWriter().println(str == null ? "" : str.toString());
System.out.println(str);
return;
}