二维码

This commit is contained in:
wangw 2025-10-20 17:31:59 +08:00
parent 0b710bd391
commit 5bef4af600
4 changed files with 56 additions and 16 deletions

View File

@ -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 数据
*/

View File

@ -86,6 +86,10 @@ public class UserInfo implements Serializable {
* 登录密码
*/
private String password;
/**
* 是否关注微信公众号 0-未绑定 1-已绑定
*/
private Integer isAc;
// 微信公众号二维码有效时间
private LocalDateTime acQrcodeValidTime;
// 微信公众号二维码

View File

@ -1,11 +1,7 @@
package com.czg.service.account.service.impl;
//import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.symmetric.AES;
import com.alibaba.fastjson2.JSONObject;
import com.czg.account.dto.auth.GetPhoneDTO;
import com.czg.account.dto.auth.LoginTokenDTO;

View File

@ -61,7 +61,7 @@ public class AcAccountUtil {
bodyJson.put("action_name", "QR_SCENE");
JSONObject actionInfo = new JSONObject();
JSONObject scene = new JSONObject();
scene.put("scene_id", userId);
scene.put("scene_str", userId.toString());
actionInfo.put("scene", scene);
bodyJson.put("action_info", actionInfo);
@ -102,23 +102,26 @@ public class AcAccountUtil {
}
public static void main(String[] args) {
String accessToken = "97_Kgg5H4EvOGc67n2GoR7Kqp-NuREOMuzesG0CpbNrx1JhvM6axp9Ub6OoZzBeANuQI3vExR9onbEeBhLdgiMjLjxpiwgDmXOgvZS_rEL5S766QVJQ7p2eggksyuQFPRdAEALVX";
// String resp = HttpUtil.get(StrUtil.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}", "wx1fb600d0f5ea6279", "b4c0534c9b5e6c84a7fe5c2078dff876"));
// System.out.println(resp);
String accessToken = "97_-4x_Mt_x243Ixmcm0kEuO5t7y7RGCdCMXQyIBBi_LCX2B7kdGSZj0XZ4poHC1J4bmMWVdMI1Gengm9phoXWbirwPmTjj2mfmEBoCNe4FeZA6dg3ZLBslvCaOijECHUhAJAVAN";
JSONObject bodyJson = new JSONObject();
//二维码有效时间最大2592000仅临时二维码需要
bodyJson.put("expire_seconds", "2592000");
//二维码类型QR_SCENE(临时整型)/QR_STR_SCENE(临时字符串)/QR_LIMIT_SCENE(永久整型)/QR_LIMIT_STR_SCENE(永久字符串)
bodyJson.put("action_name", "QR_SCENE");
bodyJson.put("action_name", "QR_STR_SCENE");
JSONObject actionInfo = new JSONObject();
JSONObject scene = new JSONObject();
scene.put("scene_id", 333);
scene.put("scene_str", "43525423");
actionInfo.put("scene", scene);
bodyJson.put("action_info", actionInfo);
System.out.println(bodyJson);
//创建临时二维码失败,发送参数: {"action_info":{"scene":{"scene_id":36449}},"action_name":"QR_SCENE","expire_seconds":"2592000"}, 响应内容: {"errcode":40052,"errmsg":"invalid action name rid: 68f5fa37-20fb3162-0f87b4f2"}
//创建临时二维码失败,发送参数: {"action_info":{"scene":{"scene_id":36449}},"action_name":"QR_SCENE","expire_seconds":"2592000"}, 响应内容: {"errcode":40052,"errmsg":"invalid action name rid: 68f5f970-2a84749c-78e4d78f"}
String resps = HttpUtil.post(StrUtil.format("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={}", accessToken), JSONObject.toJSONString(bodyJson));
JSONObject respInfos = JSONObject.parseObject(resps);
System.out.println(respInfos);
//{"action_info":{"scene":{"scene_id":36434}},"action_name":"QR_SCENE","expire_seconds":"2592000"}, 响应内容: {"errcode":40052,"errmsg":"invalid action name rid: 68f5e3ac-7f703951-6a8908fa"}
//{"action_info":{"scene":{"scene_id":36449}},"action_name":"QR_SCENE","expire_seconds":"2592000"}
// {"action_info":{"scene":{"scene_id":333}},"action_name":"QR_SCENE","expire_seconds":"2592000"}
// {"ticket":"gQEZ8TwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyUFVfTnM2MXhmbUoxYTM3dGhGMXIAAgSD_vVoAwQAjScA","expire_seconds":2592000,"url":"http://weixin.qq.com/q/02PU_Ns61xfmJ1a37thF1r"}
}
}