mirror of
https://github.com/ZSCNetSupportDept/WechatTicketSystem.git
synced 2025-10-31 02:16:18 +08:00
Use Hibernate framework to operate pojo instance, bye bye SQL strings :)
This commit is contained in:
@@ -3,6 +3,7 @@ package love.sola.netsupport.api;
|
||||
import com.google.gson.Gson;
|
||||
import love.sola.netsupport.config.Settings;
|
||||
import love.sola.netsupport.sql.SQLCore;
|
||||
import love.sola.netsupport.util.JsonP;
|
||||
import love.sola.netsupport.wechat.Command;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
@@ -15,8 +16,6 @@ import java.io.PrintWriter;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import static love.sola.netsupport.config.Lang.lang;
|
||||
|
||||
/**
|
||||
* ***********************************************
|
||||
* Created by Sola on 2015/12/2.
|
||||
@@ -32,16 +31,21 @@ public class Authorize extends HttpServlet {
|
||||
public static Map<String, Command> fetchedCommand = new ConcurrentHashMap<>();
|
||||
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
doGet(request, response);
|
||||
}
|
||||
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
request.setCharacterEncoding("utf-8");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.addHeader("Content-type", "text/json;charset=utf-8");
|
||||
PrintWriter out = response.getWriter();
|
||||
String wechat = request.getParameter("wechat");
|
||||
out.println(gson.toJson(authorize(wechat)));
|
||||
String json = gson.toJson(authorize(request));
|
||||
out.println(JsonP.parse(request, json));
|
||||
out.close();
|
||||
}
|
||||
|
||||
private Response authorize(String wechat) {
|
||||
private Response authorize(HttpServletRequest request) {
|
||||
String wechat = request.getParameter("wechat");
|
||||
if (wechat == null) {
|
||||
return new Response(Response.ResponseCode.PARAMETER_REQUIRED);
|
||||
}
|
||||
@@ -50,28 +54,20 @@ public class Authorize extends HttpServlet {
|
||||
if (l == null || c == null) {
|
||||
return new Response(Response.ResponseCode.AUTHORIZE_FAILED);
|
||||
}
|
||||
if (l < System.currentTimeMillis() - Settings.I.User_Command_Timeout) {
|
||||
return new Response(Response.ResponseCode.AUTHORIZE_FAILED);
|
||||
if (l < System.currentTimeMillis() - Settings.I.User_Command_Timeout * 1000) {
|
||||
return new Response(Response.ResponseCode.REQUEST_EXPIRED);
|
||||
}
|
||||
switch (c) {
|
||||
case REGISTER:
|
||||
Register.authorized.put(wechat, System.currentTimeMillis());
|
||||
break;
|
||||
case QUERY:
|
||||
request.getSession(true).setAttribute("wechat", wechat);
|
||||
request.getSession(true).setAttribute("wechat", wechat);
|
||||
default:
|
||||
return new Response(Response.ResponseCode.AUTHORIZE_FAILED);
|
||||
}
|
||||
return new Response(Response.ResponseCode.OK);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("Duplicates")
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
request.setCharacterEncoding("utf-8");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.addHeader("Content-type", "text/plain;charset=utf-8");
|
||||
PrintWriter out = response.getWriter();
|
||||
out.println(lang("Illegal_Request"));
|
||||
out.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.google.gson.Gson;
|
||||
import love.sola.netsupport.pojo.User;
|
||||
import love.sola.netsupport.sql.SQLCore;
|
||||
import love.sola.netsupport.sql.TableUser;
|
||||
import love.sola.netsupport.util.JsonP;
|
||||
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.ServletException;
|
||||
@@ -39,28 +40,34 @@ public class GetUser extends HttpServlet {
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.addHeader("Content-type", "text/json;charset=utf-8");
|
||||
PrintWriter out = response.getWriter();
|
||||
String json = gson.toJson(query(request));
|
||||
out.println(JsonP.parse(request, json));
|
||||
out.close();
|
||||
}
|
||||
|
||||
private Response query(HttpServletRequest request) {
|
||||
String id = request.getParameter("id");
|
||||
String name = request.getParameter("name");
|
||||
if ((id == null || id.isEmpty()) && (name == null || name.isEmpty())) {
|
||||
out.println(gson.toJson(new Response(Response.ResponseCode.PARAMETER_REQUIRED)));
|
||||
} else if (id != null) {
|
||||
return new Response(Response.ResponseCode.PARAMETER_REQUIRED);
|
||||
}
|
||||
if (id != null) {
|
||||
try {
|
||||
User u = TableUser.getUserById(Long.parseLong(id));
|
||||
if (u == null)
|
||||
out.println(gson.toJson(new Response(Response.ResponseCode.USER_NOT_FOUND)));
|
||||
return new Response(Response.ResponseCode.USER_NOT_FOUND);
|
||||
else
|
||||
out.println(gson.toJson(new Response(Response.ResponseCode.OK, u)));
|
||||
return new Response(Response.ResponseCode.OK, u);
|
||||
} catch (NumberFormatException e) {
|
||||
out.println(gson.toJson(new Response(Response.ResponseCode.ILLEGAL_PARAMETER)));
|
||||
return new Response(Response.ResponseCode.ILLEGAL_PARAMETER);
|
||||
}
|
||||
} else {
|
||||
User u = TableUser.getUserByName(name);
|
||||
if (u == null)
|
||||
out.println(gson.toJson(new Response(Response.ResponseCode.USER_NOT_FOUND)));
|
||||
return new Response(Response.ResponseCode.USER_NOT_FOUND);
|
||||
else
|
||||
out.println(gson.toJson(new Response(Response.ResponseCode.OK, u)));
|
||||
return new Response(Response.ResponseCode.OK, u);
|
||||
}
|
||||
out.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
44
src/main/java/love/sola/netsupport/api/QueryTicket.java
Normal file
44
src/main/java/love/sola/netsupport/api/QueryTicket.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package love.sola.netsupport.api;
|
||||
|
||||
import love.sola.netsupport.sql.SQLCore;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.Session;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
/**
|
||||
* ***********************************************
|
||||
* Created by Sola on 2015/12/4.
|
||||
* Don't modify this source without my agreement
|
||||
* ***********************************************
|
||||
*/
|
||||
@WebServlet(name = "QueryTicket", urlPatterns = "/api/queryticket", loadOnStartup = 23)
|
||||
public class QueryTicket extends HttpServlet {
|
||||
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
|
||||
}
|
||||
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
request.setCharacterEncoding("utf-8");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.addHeader("Content-type", "text/json;charset=utf-8");
|
||||
PrintWriter out = response.getWriter();
|
||||
}
|
||||
|
||||
private Response query(HttpServletRequest request) {
|
||||
try (Session s = SQLCore.sf.openSession()) {
|
||||
// TODO: 2015/12/5 TICKET QUERY
|
||||
} catch (HibernateException e) {
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,7 +12,6 @@ import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@@ -32,27 +31,31 @@ public class Register extends HttpServlet {
|
||||
public static final String STUDENT_ID_REGEX = "^(2010|2012|2013|2014|2015)[0-9]{9}$";
|
||||
public static final String PHONE_NUMBER_REGEX = "^1[34578][0-9]{9}$";
|
||||
|
||||
public static final String REDIRECT_PAGE = "http://topaz.sinaapp.com/nm/result.html?";
|
||||
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
request.setCharacterEncoding("utf-8");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.addHeader("Content-type", "text/plain;charset=utf-8");
|
||||
PrintWriter out = response.getWriter();
|
||||
|
||||
ISP isp = checkISP(request.getParameter("isp"));
|
||||
Block block = checkBlock(request.getParameter("block"));
|
||||
out.println(
|
||||
register(
|
||||
checkStudentId(request.getParameter("sid")),
|
||||
request.getParameter("name"),
|
||||
isp,
|
||||
checkNetAccount(request.getParameter("username"), isp),
|
||||
block,
|
||||
checkRoom(request.getParameter("room"), block),
|
||||
checkPhoneNumber(request.getParameter("phone")),
|
||||
checkWechat(request.getParameter("wechatid"))
|
||||
String result = register(
|
||||
checkStudentId(request.getParameter("sid")),
|
||||
request.getParameter("name"),
|
||||
isp,
|
||||
checkNetAccount(request.getParameter("username"), isp),
|
||||
block,
|
||||
checkRoom(request.getParameter("room"), block),
|
||||
checkPhoneNumber(request.getParameter("phone")),
|
||||
checkWechat(request.getParameter("wechatid"))
|
||||
);
|
||||
response.sendRedirect(
|
||||
response.encodeRedirectURL(REDIRECT_PAGE +
|
||||
"msg=" + result + "" +
|
||||
"&type=" + (result.equals("Register_Success") ? 1 : 0)
|
||||
)
|
||||
);
|
||||
out.close();
|
||||
}
|
||||
|
||||
@SuppressWarnings("Duplicates")
|
||||
@@ -60,31 +63,35 @@ public class Register extends HttpServlet {
|
||||
request.setCharacterEncoding("utf-8");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.addHeader("Content-type", "text/plain;charset=utf-8");
|
||||
PrintWriter out = response.getWriter();
|
||||
out.println(lang("Illegal_Request"));
|
||||
out.close();
|
||||
response.sendRedirect(
|
||||
response.encodeRedirectURL(REDIRECT_PAGE +
|
||||
"msg=" + lang("Illegal_Request") +
|
||||
"&type=-1"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private String register(long sid, String name, ISP isp, String netAccount, Block block, int room, long phone, String wechat) {
|
||||
if (wechat == null) return lang("Illegal_Request");
|
||||
if (sid == -1) return lang("Invalid_Student_Id");
|
||||
if (name == null) return lang("Invalid_Name");
|
||||
if (isp == null) return lang("Invalid_ISP");
|
||||
if (netAccount == null) return lang("Invalid_Account");
|
||||
if (block == null) return lang("Invalid_Block");
|
||||
if (room == -1) return lang("Invalid_Room");
|
||||
if (phone == -1) return lang("Invalid_Phone_Number");
|
||||
if (wechat == null) return "Illegal_Request";
|
||||
if (sid == -1) return "Invalid_Student_Id";
|
||||
if (name == null) return "Invalid_Name";
|
||||
if (isp == null) return "Invalid_ISP";
|
||||
if (netAccount == null) return "Invalid_Account";
|
||||
if (block == null) return "Invalid_Block";
|
||||
if (room == -1) return "Invalid_Room";
|
||||
if (phone == -1) return "Invalid_Phone_Number";
|
||||
User user = TableUser.getUserById(sid);
|
||||
if (user == null) return lang("Invalid_Student_Id");
|
||||
if (!user.getName().equals(name)) return lang("Invalid_Name");
|
||||
if (user.getWechatId() != null) return lang("User_Already_Registered");
|
||||
if (user == null) return "Invalid_Student_Id";
|
||||
if (!user.getName().equals(name)) return "Invalid_Name";
|
||||
if (user.getWechatId() != null) return "User_Already_Registered";
|
||||
user.setIsp(isp);
|
||||
user.setNetAccount(netAccount);
|
||||
user.setBlock(block);
|
||||
user.setRoom(room);
|
||||
user.setPhone(phone);
|
||||
user.setWechatId(wechat);
|
||||
return lang("Register_Success");
|
||||
TableUser.updateUser(user);
|
||||
return "Register_Success";
|
||||
}
|
||||
|
||||
|
||||
@@ -150,7 +157,7 @@ public class Register extends HttpServlet {
|
||||
private String checkWechat(String wechat) {
|
||||
if (wechat == null) return null;
|
||||
Long l = authorized.remove(wechat);
|
||||
return l == null ? null : l < System.currentTimeMillis() - Settings.I.User_Register_Timeout ? null : wechat;
|
||||
return l == null ? null : l < System.currentTimeMillis() - Settings.I.User_Register_Timeout * 1000 ? null : wechat;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ public class Response {
|
||||
ILLEGAL_PARAMETER(-2),
|
||||
AUTHORIZE_FAILED(-9),
|
||||
USER_NOT_FOUND(-11),
|
||||
REQUEST_EXPIRED(-21),
|
||||
;
|
||||
|
||||
private static final Map<Integer, ResponseCode> ID_MAP = new HashMap<>();
|
||||
|
||||
Reference in New Issue
Block a user