生成带参数的二维码:
https://developers.weixin.qq.com/doc/offiaccount/Account_Management/Generating_a_Parametric_QR_Code.html
示例代码:
/**
* 生成带参数的二维码
*
* @param userId 用户id
* @return
*/
@GetMapping
@RequestMapping("/createTicket/{userId}")
public AjaxResult createTicket(@PathVariable Long userId){
String ossUrl = ticketService.createTicket(userId);
Map<String, String> map = new HashMap<>();
map.put("ossUrl", ossUrl);
return AjaxResult.success("操作成功", map);
}
/**
* 生成带参数的二维码
*
* @param userId 用户id
* @return
*/
@Override
public String createTicket(Long userId) {
if (userId == null) {
userId = 0L;
}
String url = String.format("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s",
SpringUtils.getBean(AccessTokenUtils.class).getAccessToken());
// 生成永久二维码
String data = String.format("{\"action_name\": \"QR_LIMIT_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": %d}}}", userId);
// 发送POST请求获取ticket
RestTemplate restTemplate = new RestTemplate();
// {"ticket":"gQGi8TwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyNGN1TzRPaU5jSkgxMDAwMHcwMzUAAgRoga9lAwQAAAAA","url":"http:\/\/weixin.qq.com\/q\/024cuO4OiNcJH10000w035"}
String ticketResponse = restTemplate.postForObject(url, data, String.class);
// 解析ticket
JSONObject jsonObject = JSONObject.parseObject(ticketResponse);
String ticket = jsonObject.getString("ticket");
// 根据ticket获取二维码图片URL
String ticketUrl = String.format("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s", ticket);
// 上传oss
String ossUrl = ossUtils.strToInputStreamUpload(ticketUrl);
log.info("生成的带参数二维码URL:" + ossUrl);
return ossUrl;
}
扫码接收事件推送:
https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_event_pushes.html
示例代码:
/**
* 接收用户消息并回复消息
*
* @param request xml内容
* @return xml格式的字符串
*/
@PostMapping("/validateWeChatServer")
public String receiveMessage(HttpServletRequest request) {
return weChatService.receiveMessage(request);
}
/**
* 接收用户消息并回复消息
*
* @param request xml内容
* @return xml格式的字符串
*/
@Override
public String receiveMessage(HttpServletRequest request) {
ServletInputStream inputStream = null;
try {
inputStream = request.getInputStream();
HashMap<String, String> map = new HashMap<>();
SAXReader reader = new SAXReader();
try {
// 读取request输入流, 获取Document对象
Document document = reader.read(inputStream);
// 获取root节点
Element root = document.getRootElement();
// 获取所有的子节点
List<Element> elements = root.elements();
for (Element element : elements) {
map.put(element.getName(), element.getText());
}
log.info("接收到的用户消息: {}", map);
} catch (DocumentException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 关注用户, 保存到数据库
ResUser user = SpringUtils.getBean(IResUserService.class).attentionMedia(map.get("FromUserName"));
// 回复消息
String message = getReplyTextMessage(map, user);
log.info("回复消息: {}", message);
return message;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private String getReplyTextMessage(HashMap<String, String> map, ResUser user) {
// 未关注用户时Event=subscribe, EventKey=qrscene_4(事件KEY值,qrscene_为前缀,后面是自定义的用户id)
// 获取 "qrscene_" 后面的值
Long userId = Long.valueOf(map.get("EventKey").substring("qrscene_".length()));
// TODO
}
接收到的用户消息: {Ticket=gQFC8TwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyaWplQzQzaU5jSkgxMDAwME0wM3oAAgR6r7BlAwQAAAAA, CreateTime=1706078168, EventKey=qrscene_4, Event=subscribe, ToUserName=gh_da9c697c8555, FromUserName=oUjEt6xFAs2ABHsmLyJeSUe-WVXI, MsgType=event}