mirror of
https://github.com/ZSCNetSupportDept/WechatTicketSystem.git
synced 2025-10-29 08:05:04 +08:00
complete register process
This commit is contained in:
@@ -5,7 +5,10 @@ import love.sola.netsupport.wechat.handler.RegisterHandler;
|
|||||||
import love.sola.netsupport.wechat.intercepter.CheckSpamInterceptor;
|
import love.sola.netsupport.wechat.intercepter.CheckSpamInterceptor;
|
||||||
import love.sola.netsupport.wechat.matcher.CommandMatcher;
|
import love.sola.netsupport.wechat.matcher.CommandMatcher;
|
||||||
import me.chanjar.weixin.common.util.StringUtils;
|
import me.chanjar.weixin.common.util.StringUtils;
|
||||||
import me.chanjar.weixin.mp.api.*;
|
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
|
||||||
|
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
|
||||||
|
import me.chanjar.weixin.mp.api.WxMpService;
|
||||||
|
import me.chanjar.weixin.mp.api.WxMpServiceImpl;
|
||||||
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
|
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
|
||||||
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
|
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
|
||||||
|
|
||||||
@@ -85,17 +88,31 @@ public class WxMpServlet extends HttpServlet {
|
|||||||
|
|
||||||
String encryptType = StringUtils.isBlank(request.getParameter("encrypt_type")) ? "raw" : request.getParameter("encrypt_type");
|
String encryptType = StringUtils.isBlank(request.getParameter("encrypt_type")) ? "raw" : request.getParameter("encrypt_type");
|
||||||
|
|
||||||
// if ("raw".equals(encryptType)) {
|
if ("raw".equals(encryptType)) {
|
||||||
// WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(request.getInputStream());
|
WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(request.getInputStream());
|
||||||
// WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage);
|
WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage);
|
||||||
// response.getWriter().write(outMessage.toXml());
|
if (outMessage == null) {
|
||||||
// return;
|
outMessage = WxMpXmlOutMessage.TEXT()
|
||||||
// }
|
.fromUser(inMessage.getToUserName())
|
||||||
|
.toUser(inMessage.getFromUserName())
|
||||||
|
.content("Invalid Operation.")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
response.getWriter().write(outMessage.toXml());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if ("aes".equals(encryptType)) {
|
if ("aes".equals(encryptType)) {
|
||||||
String msgSignature = request.getParameter("msg_signature");
|
String msgSignature = request.getParameter("msg_signature");
|
||||||
WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(request.getInputStream(), config, timestamp, nonce, msgSignature);
|
WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(request.getInputStream(), config, timestamp, nonce, msgSignature);
|
||||||
WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage);
|
WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage);
|
||||||
|
if (outMessage == null) {
|
||||||
|
outMessage = WxMpXmlOutMessage.TEXT()
|
||||||
|
.fromUser(inMessage.getToUserName())
|
||||||
|
.toUser(inMessage.getFromUserName())
|
||||||
|
.content("Invalid Operation.")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
response.getWriter().write(outMessage.toEncryptedXml(config));
|
response.getWriter().write(outMessage.toEncryptedXml(config));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package love.sola.netsupport.wechat.handler;
|
package love.sola.netsupport.wechat.handler;
|
||||||
|
|
||||||
|
import love.sola.netsupport.enums.ISPType;
|
||||||
import love.sola.netsupport.pojo.User;
|
import love.sola.netsupport.pojo.User;
|
||||||
import love.sola.netsupport.sql.TableUser;
|
import love.sola.netsupport.sql.TableUser;
|
||||||
import love.sola.netsupport.wechat.Command;
|
import love.sola.netsupport.wechat.Command;
|
||||||
@@ -23,12 +24,13 @@ import java.util.Map;
|
|||||||
*/
|
*/
|
||||||
public class RegisterHandler implements WxMpMessageHandler {
|
public class RegisterHandler implements WxMpMessageHandler {
|
||||||
|
|
||||||
public static final String STUDENT_ID_REGEX = "^(2012|2013|2014|2015)[0-9]{9}";
|
public static final String STUDENT_ID_REGEX = "^(2010|2012|2013|2014|2015)[0-9]{9}";
|
||||||
|
|
||||||
Map<String, User> pre_confirm = new HashMap<>();
|
|
||||||
Map<String, User> confirmed = new HashMap<>();
|
Map<String, User> confirmed = new HashMap<>();
|
||||||
|
Map<String, RegisterStep> steps = new HashMap<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
||||||
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager)
|
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager)
|
||||||
throws WxErrorException {
|
throws WxErrorException {
|
||||||
TextBuilder out = WxMpXmlOutMessage.TEXT().fromUser(wxMessage.getToUserName()).toUser(wxMessage.getFromUserName());
|
TextBuilder out = WxMpXmlOutMessage.TEXT().fromUser(wxMessage.getToUserName()).toUser(wxMessage.getFromUserName());
|
||||||
@@ -37,55 +39,108 @@ public class RegisterHandler implements WxMpMessageHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void handle(String in, String userName, TextBuilder out) {
|
private void handle(String in, String userName, TextBuilder out) {
|
||||||
if (in.matches(Command.REGISTER.getRegex())) {
|
if (in.equalsIgnoreCase("q")) {
|
||||||
out.content("Welcome, please type your student identification number.");
|
CommandMatcher.inCmdUsers.remove(userName);
|
||||||
|
confirmed.remove(userName);
|
||||||
|
steps.remove(userName);
|
||||||
|
out.content("Operation canceled.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (pre_confirm.containsKey(userName)) {
|
if (!steps.containsKey(userName)) {
|
||||||
User user = pre_confirm.get(userName);
|
out.content("Welcome, please type your student identification number.");
|
||||||
|
CommandMatcher.inCmdUsers.put(userName, Command.REGISTER);
|
||||||
|
steps.put(userName, RegisterStep.STUDENT_ID);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
RegisterStep step = steps.get(userName);
|
||||||
|
if (step == RegisterStep.STUDENT_ID) {
|
||||||
|
Long sid = checkStudentId(in);
|
||||||
|
User user;
|
||||||
|
if (sid == null || (user = TableUser.getUserById(sid)) == null) {
|
||||||
|
out.content("Invalid student identification number. Type 'q' to cancel this operation.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
out.content("Please type your real name to validate.");
|
||||||
|
confirmed.put(userName, user);
|
||||||
|
steps.put(userName, RegisterStep.NAME);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
User user = confirmed.get(userName);
|
||||||
|
if (step == RegisterStep.NAME) {
|
||||||
if (in.trim().equals(user.getName())) {
|
if (in.trim().equals(user.getName())) {
|
||||||
pre_confirm.remove(userName);
|
out.content("Confirmed success.\n" +
|
||||||
confirmed.put(userName, user);
|
"Please enter your ISP.\n" +
|
||||||
out.content("Confirmed Success.\n" +
|
"1-Telecom 2-Unicom 3-ChinaMobile");
|
||||||
"Please enter your ISP.");
|
steps.put(userName, RegisterStep.ISP);
|
||||||
} else {
|
} else {
|
||||||
out.content("Validate failed.\n" +
|
out.content("Validate failed.\n" +
|
||||||
"if you have any issue, please contact administrator.\n" +
|
"If you have any issue, please contact administrator.\n" +
|
||||||
"Type 'q' to cancel this operation.");
|
"Type 'q' to cancel this operation.");
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (confirmed.containsKey(userName)) {
|
if (step == RegisterStep.ISP) {
|
||||||
User user = pre_confirm.get(userName);
|
ISPType type = checkISP(in);
|
||||||
if (user.getIsp() == null) {
|
if (type == null) {
|
||||||
//TODO
|
out.content("Invalid ISP. Please retype your ISP.");
|
||||||
|
} else {
|
||||||
|
user.setIsp(type);
|
||||||
|
out.content("Success.\n" +
|
||||||
|
"Please enter your net account.");
|
||||||
|
steps.put(userName, RegisterStep.NET_ACCOUNT);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (in.equalsIgnoreCase("q")) {
|
if (step == RegisterStep.NET_ACCOUNT) {
|
||||||
|
String account = checkNetAccount(in);
|
||||||
|
if (account == null) {
|
||||||
|
out.content("Invalid account. Please retype your net account.");
|
||||||
|
} else {
|
||||||
|
user.setNetAccount(account);
|
||||||
|
steps.put(userName, RegisterStep.COMPLETE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (step == RegisterStep.COMPLETE) {
|
||||||
|
user.setWechatId(userName);
|
||||||
|
TableUser.updateUser(user);
|
||||||
CommandMatcher.inCmdUsers.remove(userName);
|
CommandMatcher.inCmdUsers.remove(userName);
|
||||||
pre_confirm.remove(userName);
|
|
||||||
confirmed.remove(userName);
|
confirmed.remove(userName);
|
||||||
out.content("Operation Canceled.");
|
steps.remove(userName);
|
||||||
return;
|
out.content("Congratulations!\n" +
|
||||||
|
"You has successful registered!\n" +
|
||||||
|
"Please enjoy our service.");
|
||||||
}
|
}
|
||||||
Long sid = checkStudentId(in);
|
|
||||||
User user;
|
|
||||||
if (sid == null || (user = TableUser.getUserById(sid)) == null) {
|
|
||||||
out.content("Invalid student identification number. Type 'q' to cancel this operation.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
pre_confirm.put(userName, user);
|
|
||||||
out.content("Please type your real name to validate.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Long checkStudentId(String studentId) {
|
private Long checkStudentId(String studentId) {
|
||||||
if (studentId.matches(STUDENT_ID_REGEX)) {
|
if (studentId.matches(STUDENT_ID_REGEX)) {
|
||||||
try {
|
try {
|
||||||
return Long.parseLong(studentId);
|
return Long.parseLong(studentId);
|
||||||
} catch (NumberFormatException ignored) { }
|
} catch (NumberFormatException ignored) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ISPType checkISP(String isp) {
|
||||||
|
try {
|
||||||
|
return ISPType.fromId(Integer.parseInt(isp));
|
||||||
|
} catch (NumberFormatException ignored) {
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String checkNetAccount(String account) {
|
||||||
|
return account;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum RegisterStep {
|
||||||
|
STUDENT_ID,
|
||||||
|
NAME,
|
||||||
|
ISP,
|
||||||
|
NET_ACCOUNT,
|
||||||
|
COMPLETE,
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package love.sola.netsupport.wechat;
|
|||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import love.sola.netsupport.config.Settings;
|
import love.sola.netsupport.config.Settings;
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ***********************************************
|
* ***********************************************
|
||||||
@@ -12,7 +11,7 @@ import org.junit.Test;
|
|||||||
*/
|
*/
|
||||||
public class TestGson {
|
public class TestGson {
|
||||||
|
|
||||||
@Test
|
// @Test
|
||||||
public void testGson() {
|
public void testGson() {
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
Settings settings = new Settings();
|
Settings settings = new Settings();
|
||||||
|
|||||||
Reference in New Issue
Block a user