mirror of
https://github.com/ZSCNetSupportDept/WechatTicketSystem.git
synced 2025-10-28 23:55: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.matcher.CommandMatcher;
|
||||
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.WxMpXmlOutMessage;
|
||||
|
||||
@@ -85,17 +88,31 @@ public class WxMpServlet extends HttpServlet {
|
||||
|
||||
String encryptType = StringUtils.isBlank(request.getParameter("encrypt_type")) ? "raw" : request.getParameter("encrypt_type");
|
||||
|
||||
// if ("raw".equals(encryptType)) {
|
||||
// WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(request.getInputStream());
|
||||
// WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage);
|
||||
// response.getWriter().write(outMessage.toXml());
|
||||
// return;
|
||||
// }
|
||||
if ("raw".equals(encryptType)) {
|
||||
WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(request.getInputStream());
|
||||
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.toXml());
|
||||
return;
|
||||
}
|
||||
|
||||
if ("aes".equals(encryptType)) {
|
||||
String msgSignature = request.getParameter("msg_signature");
|
||||
WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(request.getInputStream(), config, timestamp, nonce, msgSignature);
|
||||
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));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package love.sola.netsupport.wechat.handler;
|
||||
|
||||
import love.sola.netsupport.enums.ISPType;
|
||||
import love.sola.netsupport.pojo.User;
|
||||
import love.sola.netsupport.sql.TableUser;
|
||||
import love.sola.netsupport.wechat.Command;
|
||||
@@ -23,12 +24,13 @@ import java.util.Map;
|
||||
*/
|
||||
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, RegisterStep> steps = new HashMap<>();
|
||||
|
||||
@Override
|
||||
|
||||
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager)
|
||||
throws WxErrorException {
|
||||
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) {
|
||||
if (in.matches(Command.REGISTER.getRegex())) {
|
||||
out.content("Welcome, please type your student identification number.");
|
||||
if (in.equalsIgnoreCase("q")) {
|
||||
CommandMatcher.inCmdUsers.remove(userName);
|
||||
confirmed.remove(userName);
|
||||
steps.remove(userName);
|
||||
out.content("Operation canceled.");
|
||||
return;
|
||||
}
|
||||
if (pre_confirm.containsKey(userName)) {
|
||||
User user = pre_confirm.get(userName);
|
||||
if (!steps.containsKey(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())) {
|
||||
pre_confirm.remove(userName);
|
||||
confirmed.put(userName, user);
|
||||
out.content("Confirmed Success.\n" +
|
||||
"Please enter your ISP.");
|
||||
out.content("Confirmed success.\n" +
|
||||
"Please enter your ISP.\n" +
|
||||
"1-Telecom 2-Unicom 3-ChinaMobile");
|
||||
steps.put(userName, RegisterStep.ISP);
|
||||
} else {
|
||||
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.");
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (confirmed.containsKey(userName)) {
|
||||
User user = pre_confirm.get(userName);
|
||||
if (user.getIsp() == null) {
|
||||
//TODO
|
||||
if (step == RegisterStep.ISP) {
|
||||
ISPType type = checkISP(in);
|
||||
if (type == null) {
|
||||
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;
|
||||
}
|
||||
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);
|
||||
pre_confirm.remove(userName);
|
||||
confirmed.remove(userName);
|
||||
out.content("Operation Canceled.");
|
||||
return;
|
||||
steps.remove(userName);
|
||||
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) {
|
||||
if (studentId.matches(STUDENT_ID_REGEX)) {
|
||||
try {
|
||||
return Long.parseLong(studentId);
|
||||
} catch (NumberFormatException ignored) { }
|
||||
} catch (NumberFormatException ignored) {
|
||||
}
|
||||
}
|
||||
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 love.sola.netsupport.config.Settings;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* ***********************************************
|
||||
@@ -12,7 +11,7 @@ import org.junit.Test;
|
||||
*/
|
||||
public class TestGson {
|
||||
|
||||
@Test
|
||||
// @Test
|
||||
public void testGson() {
|
||||
Gson gson = new Gson();
|
||||
Settings settings = new Settings();
|
||||
|
||||
Reference in New Issue
Block a user