mirror of
https://github.com/ZSCNetSupportDept/WechatTicketSystem.git
synced 2025-10-28 23:55:04 +08:00
use student id as primary key instead.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package love.sola.netsupport;
|
||||
|
||||
import love.sola.netsupport.config.Settings;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
|
||||
@@ -45,7 +45,7 @@ public class GetUser extends HttpServlet {
|
||||
out.println(gson.toJson(new Response(Response.ResponseCode.PARAMETER_REQUIRED)));
|
||||
} else if (id != null) {
|
||||
try {
|
||||
User u = TableUser.getUserById(Integer.parseInt(id));
|
||||
User u = TableUser.getUserById(Long.parseLong(id));
|
||||
if (u == null)
|
||||
out.println(gson.toJson(new Response(Response.ResponseCode.USER_NOT_FOUND)));
|
||||
else
|
||||
|
||||
@@ -14,9 +14,8 @@ import love.sola.netsupport.enums.ISPType;
|
||||
@AllArgsConstructor
|
||||
public class User {
|
||||
|
||||
private final int id;
|
||||
private final long id;
|
||||
private final String name;
|
||||
private final long studentId;
|
||||
private String netAccount;
|
||||
private ISPType isp;
|
||||
private String wechatId;
|
||||
|
||||
@@ -18,7 +18,6 @@ public class TableUser extends SQLCore {
|
||||
|
||||
public static final String COLUMN_ID = "id";
|
||||
public static final String COLUMN_NAME = "name";
|
||||
public static final String COLUMN_STUDENT_ID = "studentid";
|
||||
public static final String COLUMN_NET_ACCOUNT = "netaccount";
|
||||
public static final String COLUMN_ISP = "isp";
|
||||
public static final String COLUMN_WECHAT = "wechat";
|
||||
@@ -29,32 +28,51 @@ public class TableUser extends SQLCore {
|
||||
ps.setString(1, name);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
return new User(rs.getInt(COLUMN_ID),
|
||||
return new User(rs.getLong(COLUMN_ID),
|
||||
rs.getString(COLUMN_NAME),
|
||||
rs.getLong(COLUMN_STUDENT_ID),
|
||||
rs.getString(COLUMN_NET_ACCOUNT),
|
||||
ISPType.fromId(rs.getInt(COLUMN_ISP)),
|
||||
rs.getString(COLUMN_WECHAT));
|
||||
}
|
||||
} catch (SQLException e) { }
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static User getUserById(int id) {
|
||||
public static User getUserById(long id) {
|
||||
try (Connection conn = ds.getConnection()) {
|
||||
PreparedStatement ps = conn.prepareStatement("SELECT * FROM user_info WHERE id=?");
|
||||
ps.setInt(1, id);
|
||||
ps.setLong(1, id);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
return new User(rs.getInt(COLUMN_ID),
|
||||
return new User(rs.getLong(COLUMN_ID),
|
||||
rs.getString(COLUMN_NAME),
|
||||
rs.getLong(COLUMN_STUDENT_ID),
|
||||
rs.getString(COLUMN_NET_ACCOUNT),
|
||||
ISPType.fromId(rs.getInt(COLUMN_ISP)),
|
||||
rs.getString(COLUMN_WECHAT));
|
||||
}
|
||||
} catch (SQLException e) { }
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static int updateUser(User user) {
|
||||
try (Connection conn = ds.getConnection()) {
|
||||
PreparedStatement ps = conn.prepareStatement("UPDATE user_info SET " +
|
||||
COLUMN_WECHAT + "=?," +
|
||||
COLUMN_NET_ACCOUNT + "=?," +
|
||||
COLUMN_ISP + "=? " +
|
||||
"WHERE id=?");
|
||||
ps.setString(1, user.getWechatId());
|
||||
ps.setString(2, user.getNetAccount());
|
||||
ps.setInt(3, user.getIsp().id);
|
||||
ps.setLong(4, user.getId());
|
||||
return ps.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,6 +52,7 @@ public class WxMpServlet extends HttpServlet {
|
||||
wxMpMessageRouter = new WxMpMessageRouter(wxMpService);
|
||||
wxMpMessageRouter.rule()
|
||||
.async(false)
|
||||
.msgType("text")
|
||||
.matcher(new CommandMatcher(Command.REGISTER))
|
||||
.handler(new RegisterHandler())
|
||||
.interceptor(checkSpamInterceptor)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package love.sola.netsupport.wechat.handler;
|
||||
|
||||
import love.sola.netsupport.pojo.User;
|
||||
import love.sola.netsupport.sql.TableUser;
|
||||
import love.sola.netsupport.wechat.Command;
|
||||
import love.sola.netsupport.wechat.matcher.CommandMatcher;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
@@ -22,24 +23,69 @@ import java.util.Map;
|
||||
*/
|
||||
public class RegisterHandler implements WxMpMessageHandler {
|
||||
|
||||
public static final String STUDENT_ID_REGEX = "^(2012|2013|2014|2015)[0-9]{9}";
|
||||
|
||||
Map<String, User> pre_confirm = new HashMap<>();
|
||||
Map<String, User> confirmed = 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());
|
||||
String in = wxMessage.getContent();
|
||||
String userName = wxMessage.getFromUserName();
|
||||
if (in.matches(Command.REGISTER.getRegex())) {
|
||||
out.content("Welcome, please type your student identification number.");
|
||||
} else if (pre_confirm.containsKey(userName)) {
|
||||
//TODO
|
||||
|
||||
} else {
|
||||
out.content("Illegal Operation.");
|
||||
CommandMatcher.inCmdUsers.remove(userName);
|
||||
}
|
||||
handle(wxMessage.getContent(), wxMessage.getFromUserName(), out);
|
||||
return out.build();
|
||||
}
|
||||
|
||||
private void handle(String in, String userName, TextBuilder out) {
|
||||
if (in.matches(Command.REGISTER.getRegex())) {
|
||||
out.content("Welcome, please type your student identification number.");
|
||||
return;
|
||||
}
|
||||
if (pre_confirm.containsKey(userName)) {
|
||||
User user = pre_confirm.get(userName);
|
||||
if (in.trim().equals(user.getName())) {
|
||||
pre_confirm.remove(userName);
|
||||
confirmed.put(userName, user);
|
||||
out.content("Confirmed Success.\n" +
|
||||
"Please enter your ISP.");
|
||||
} else {
|
||||
out.content("Validate failed.\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
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (in.equalsIgnoreCase("q")) {
|
||||
CommandMatcher.inCmdUsers.remove(userName);
|
||||
pre_confirm.remove(userName);
|
||||
confirmed.remove(userName);
|
||||
out.content("Operation Canceled.");
|
||||
return;
|
||||
}
|
||||
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) { }
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
26
src/test/java/love/sola/netsupport/wechat/TestGson.java
Normal file
26
src/test/java/love/sola/netsupport/wechat/TestGson.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package love.sola.netsupport.wechat;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import love.sola.netsupport.config.Settings;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* ***********************************************
|
||||
* Created by Sola on 2015/11/26.
|
||||
* Don't modify this source without my agreement
|
||||
* ***********************************************
|
||||
*/
|
||||
public class TestGson {
|
||||
|
||||
@Test
|
||||
public void testGson() {
|
||||
Gson gson = new Gson();
|
||||
Settings settings = new Settings();
|
||||
settings.Wechat_AppId = "*";
|
||||
settings.Wechat_Secret = "*";
|
||||
settings.Wechat_Token = "*";
|
||||
settings.Wechat_AesKey = "*";
|
||||
System.out.println(gson.toJson(settings));
|
||||
}
|
||||
|
||||
}
|
||||
21
src/test/java/love/sola/netsupport/wechat/TestRegex.java
Normal file
21
src/test/java/love/sola/netsupport/wechat/TestRegex.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package love.sola.netsupport.wechat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* ***********************************************
|
||||
* Created by Sola on 2015/11/26.
|
||||
* Don't modify this source without my agreement
|
||||
* ***********************************************
|
||||
*/
|
||||
public class TestRegex {
|
||||
|
||||
public static final String STUDENT_ID_REGEX = "^(2012|2013|2014|2015)[0-9]{9}";
|
||||
|
||||
@Test
|
||||
public void testStudentId() {
|
||||
System.out.println("2011130201233".matches(STUDENT_ID_REGEX));
|
||||
System.out.println("2015130201233".matches(STUDENT_ID_REGEX));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user