二维码
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package com.czg.controller;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.czg.account.entity.UserInfo;
|
||||
import com.czg.account.service.UserInfoService;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -32,7 +36,7 @@ public class NotifyController {
|
||||
|
||||
@RequestMapping(produces = MediaType.TEXT_PLAIN_VALUE)
|
||||
public String notify(HttpServletRequest request,
|
||||
@RequestParam(required = false) String signature, // GET 必传,POST 可选,设为非必选
|
||||
@RequestParam(required = false) String signature,
|
||||
@RequestParam(required = false) String timestamp,
|
||||
@RequestParam(required = false) String nonce,
|
||||
@RequestParam(required = false) String echostr) {
|
||||
@@ -50,17 +54,50 @@ public class NotifyController {
|
||||
signature, timestamp, nonce);
|
||||
// 读取 POST 请求体中的 XML 数据(微信推送的消息格式为 XML)
|
||||
String xmlData = readPostXml(request);
|
||||
JSONObject jsonObject = JSON.parseObject(xmlData);
|
||||
log.info("微信 POST 消息内容: {}", xmlData);
|
||||
|
||||
// TODO: 后续可添加消息解析、业务处理逻辑(如关注事件、文本消息回复等)
|
||||
// 获取消息类型(如 event)
|
||||
String msgType = jsonObject.getString("MsgType");
|
||||
// 获取事件类型(如 unsubscribe:用户取消关注 subscribe:用户关注)
|
||||
String event = jsonObject.getString("Event");
|
||||
// 获取用户 OpenID
|
||||
String openId = jsonObject.getString("FromUserName");
|
||||
//携带参数
|
||||
String eventKey = jsonObject.getString("EventKey");
|
||||
Long userId = null;
|
||||
log.info("解析结果 - 消息类型: {}, 事件类型: {}, 用户 OpenID: {} 携带参数: {}", msgType, event, openId, eventKey);
|
||||
if (eventKey != null && eventKey.startsWith("qrscene_")) {
|
||||
try {
|
||||
// 截取 "qrscene_" 前缀后的字符串(长度为 8),并转为 Long
|
||||
String numberStr = eventKey.substring("qrscene_".length());
|
||||
userId = Long.parseLong(numberStr);
|
||||
} catch (NumberFormatException e) {
|
||||
log.error("EventKey 后缀不是有效数字,eventKey: {}", eventKey, e);
|
||||
}
|
||||
}
|
||||
updateUserInfoIsAc(event, openId, userId);
|
||||
// 处理完成后,微信要求返回 "SUCCESS" 或空字符串(否则会重试推送)
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
// 3. 其他请求方式(如 PUT/DELETE),返回空字符串
|
||||
return "";
|
||||
}
|
||||
|
||||
//更新userInfo openId 以及 关注表示isAc
|
||||
private void updateUserInfoIsAc(String event, String openId, Long userId) {
|
||||
if ("subscribe".equals(event) && userId != null) {
|
||||
// 关注事件,更新用户关注状态为 1
|
||||
UserInfo userInfo = new UserInfo();
|
||||
userInfo.setIsAc(1);
|
||||
userInfo.setWechatAcOpenId(openId);
|
||||
userInfoService.update(userInfo, new QueryWrapper().eq(UserInfo::getId, userId));
|
||||
} else if ("unsubscribe".equals(event)) {
|
||||
// 取消关注事件,更新用户关注状态为 0
|
||||
UserInfo userInfo = new UserInfo();
|
||||
userInfo.setIsAc(0);
|
||||
userInfoService.update(userInfo, new QueryWrapper().eq(UserInfo::getWechatAcOpenId, openId));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 工具方法:读取 POST 请求体中的 XML 数据
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user