|
|
@@ -9,6 +9,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.net.URLEncoder;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
@@ -18,7 +20,8 @@ public class WechatCallbackController {
|
|
|
|
|
|
@Autowired
|
|
|
private WechatService wechatService;
|
|
|
- private static final String TOKEN = "PPKOdAlCoMO"; // 与微信后台保持一致
|
|
|
+
|
|
|
+ private static final String TOKEN = "PPKOdAlCoMO";
|
|
|
|
|
|
/** 微信服务器验证 (GET) */
|
|
|
@GetMapping
|
|
|
@@ -33,7 +36,6 @@ public class WechatCallbackController {
|
|
|
String content = String.join("", arr);
|
|
|
|
|
|
String hash = DigestUtils.sha1Hex(content);
|
|
|
-
|
|
|
return hash.equals(signature) ? echostr : "error";
|
|
|
}
|
|
|
|
|
|
@@ -47,12 +49,61 @@ public class WechatCallbackController {
|
|
|
("subscribe".equalsIgnoreCase(event.getEvent()) || "SCAN".equalsIgnoreCase(event.getEvent()))) {
|
|
|
|
|
|
String openid = event.getFromUserName();
|
|
|
+ String fromUser = event.getToUserName();
|
|
|
String eventKey = event.getEventKey();
|
|
|
+
|
|
|
+ // 绑定二维码场景参数
|
|
|
if (eventKey != null && !eventKey.isEmpty()) {
|
|
|
String sceneKey = eventKey.replace("qrscene_", "");
|
|
|
wechatService.bindSellerByScene(sceneKey, openid);
|
|
|
}
|
|
|
+
|
|
|
+ // 自动回复带订阅授权链接
|
|
|
+ String authUrl = generateAuthUrl();
|
|
|
+ String msgContent = "欢迎关注!点击下方链接开启消息提醒:" + authUrl;
|
|
|
+ return buildTextMessage(openid, fromUser, msgContent);
|
|
|
}
|
|
|
+
|
|
|
return "success";
|
|
|
}
|
|
|
+
|
|
|
+ /** 生成订阅消息授权链接 */
|
|
|
+ private String generateAuthUrl() throws UnsupportedEncodingException {
|
|
|
+ String appid = "wx7670b3b1b1cfcd47"; // 服务号appid
|
|
|
+ String templateId = "CHyzPCokptj4Z9qTpo-UJQzd_OAN4cNa6zpZo9LZ_YY"; // 长期订阅模板ID
|
|
|
+ String redirectUrl = URLEncoder.encode("https://userapp.hbhdt.top//app/common/submsgCallback", StandardCharsets.UTF_8.name());
|
|
|
+
|
|
|
+ return "https://mp.weixin.qq.com/mp/subscribemsg?action=get_confirm"
|
|
|
+ + "&appid=" + appid
|
|
|
+ + "&scene=1000"
|
|
|
+ + "&template_id=" + templateId
|
|
|
+ + "&redirect_url=" + redirectUrl
|
|
|
+ + "#wechat_redirect";
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 构建文本消息XML */
|
|
|
+ private String buildTextMessage(String toUser, String fromUser, String content) {
|
|
|
+ return "<xml>" +
|
|
|
+ "<ToUserName><![CDATA[" + toUser + "]]></ToUserName>" +
|
|
|
+ "<FromUserName><![CDATA[" + fromUser + "]]></FromUserName>" +
|
|
|
+ "<CreateTime>" + System.currentTimeMillis() / 1000 + "</CreateTime>" +
|
|
|
+ "<MsgType><![CDATA[text]]></MsgType>" +
|
|
|
+ "<Content><![CDATA[" + content + "]]></Content>" +
|
|
|
+ "</xml>";
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 授权回调接口 (用户点击“允许订阅”后微信回调) */
|
|
|
+ @GetMapping("/submsgCallback")
|
|
|
+ public String submsgCallback(@RequestParam String action,
|
|
|
+ @RequestParam String openid,
|
|
|
+ @RequestParam String template_id) {
|
|
|
+ if ("confirm".equals(action)) {
|
|
|
+ System.out.println("用户已授权订阅,openid:" + openid + ", template_id:" + template_id);
|
|
|
+ // 保存用户订阅信息,可保存到数据库或Redis
|
|
|
+ //wechatService.saveUserSubscription(openid, template_id);
|
|
|
+ return "您已成功开启订阅提醒!";
|
|
|
+ } else {
|
|
|
+ return "您已取消订阅授权";
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|