mirror of
https://github.com/ZSCNetSupportDept/WechatTicketSystem.git
synced 2025-10-29 08:05:04 +08:00
ticket query api
This commit is contained in:
@@ -26,7 +26,7 @@ public class Index extends HttpServlet {
|
|||||||
response.addHeader("Content-type", "text/plain;charset=utf-8");
|
response.addHeader("Content-type", "text/plain;charset=utf-8");
|
||||||
response.setCharacterEncoding("utf-8");
|
response.setCharacterEncoding("utf-8");
|
||||||
PrintWriter out = response.getWriter();
|
PrintWriter out = response.getWriter();
|
||||||
out.println("Wechat Ticket System (WTS) 0.1 Copyright 2015 Sola all rights reserved. | Commercial license for ZSC Network Support Department (ZSCNSD).");
|
out.println("Wechat Ticket System (WTS) 0.1 Copyright 2015 Sola & LiuYue all rights reserved. | Commercial license for ZSC Network Support Department (ZSCNSD).");
|
||||||
out.println("For any problem, Please contact loli@sola.love.");
|
out.println("For any problem, Please contact loli@sola.love.");
|
||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package love.sola.netsupport.api;
|
|||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import love.sola.netsupport.config.Settings;
|
import love.sola.netsupport.config.Settings;
|
||||||
|
import love.sola.netsupport.pojo.User;
|
||||||
import love.sola.netsupport.sql.SQLCore;
|
import love.sola.netsupport.sql.SQLCore;
|
||||||
|
import love.sola.netsupport.sql.TableUser;
|
||||||
import love.sola.netsupport.util.JsonP;
|
import love.sola.netsupport.util.JsonP;
|
||||||
import love.sola.netsupport.wechat.Command;
|
import love.sola.netsupport.wechat.Command;
|
||||||
|
|
||||||
@@ -57,13 +59,16 @@ public class Authorize extends HttpServlet {
|
|||||||
if (l < System.currentTimeMillis() - Settings.I.User_Command_Timeout * 1000) {
|
if (l < System.currentTimeMillis() - Settings.I.User_Command_Timeout * 1000) {
|
||||||
return new Response(Response.ResponseCode.REQUEST_EXPIRED);
|
return new Response(Response.ResponseCode.REQUEST_EXPIRED);
|
||||||
}
|
}
|
||||||
|
request.getSession(true).setAttribute("authorized", c);
|
||||||
|
request.getSession(true).setAttribute("wechat", wechat);
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case REGISTER:
|
case REGISTER:
|
||||||
Register.authorized.put(wechat, System.currentTimeMillis());
|
|
||||||
break;
|
break;
|
||||||
case QUERY:
|
case QUERY:
|
||||||
request.getSession(true).setAttribute("wechat", wechat);
|
User u = TableUser.getUserByWechat(wechat);
|
||||||
request.getSession(true).setAttribute("wechat", wechat);
|
if (u == null) return new Response(Response.ResponseCode.AUTHORIZE_FAILED);
|
||||||
|
request.getSession(true).setAttribute("user", u);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return new Response(Response.ResponseCode.AUTHORIZE_FAILED);
|
return new Response(Response.ResponseCode.AUTHORIZE_FAILED);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,15 @@
|
|||||||
package love.sola.netsupport.api;
|
package love.sola.netsupport.api;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import love.sola.netsupport.pojo.Ticket;
|
||||||
|
import love.sola.netsupport.pojo.User;
|
||||||
import love.sola.netsupport.sql.SQLCore;
|
import love.sola.netsupport.sql.SQLCore;
|
||||||
|
import love.sola.netsupport.util.JsonP;
|
||||||
|
import love.sola.netsupport.wechat.Command;
|
||||||
|
import org.hibernate.Criteria;
|
||||||
import org.hibernate.HibernateException;
|
import org.hibernate.HibernateException;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.criterion.Restrictions;
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.annotation.WebServlet;
|
import javax.servlet.annotation.WebServlet;
|
||||||
@@ -18,11 +25,13 @@ import java.io.PrintWriter;
|
|||||||
* Don't modify this source without my agreement
|
* Don't modify this source without my agreement
|
||||||
* ***********************************************
|
* ***********************************************
|
||||||
*/
|
*/
|
||||||
@WebServlet(name = "QueryTicket", urlPatterns = "/api/queryticket", loadOnStartup = 23)
|
@WebServlet(name = "QueryTicket", urlPatterns = "/api/querytickets", loadOnStartup = 23)
|
||||||
public class QueryTicket extends HttpServlet {
|
public class QueryTicket extends HttpServlet {
|
||||||
|
|
||||||
|
private Gson gson = SQLCore.gson;
|
||||||
|
|
||||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
|
doGet(request, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
@@ -30,15 +39,47 @@ public class QueryTicket extends HttpServlet {
|
|||||||
response.setCharacterEncoding("utf-8");
|
response.setCharacterEncoding("utf-8");
|
||||||
response.addHeader("Content-type", "text/json;charset=utf-8");
|
response.addHeader("Content-type", "text/json;charset=utf-8");
|
||||||
PrintWriter out = response.getWriter();
|
PrintWriter out = response.getWriter();
|
||||||
|
String json = gson.toJson(query(request));
|
||||||
|
out.println(JsonP.parse(request, json));
|
||||||
|
out.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Response query(HttpServletRequest request) {
|
private Response query(HttpServletRequest request) {
|
||||||
try (Session s = SQLCore.sf.openSession()) {
|
try (Session s = SQLCore.sf.openSession()) {
|
||||||
// TODO: 2015/12/5 TICKET QUERY
|
// if (request.getParameter("id") != null) {
|
||||||
} catch (HibernateException e) {
|
// Ticket t = s.get(Ticket.class, Integer.parseInt(request.getParameter("id")));
|
||||||
|
// if (t == null) return new Response(Response.ResponseCode.TICKET_NOT_FOUND);
|
||||||
|
// else return new Response(Response.ResponseCode.OK, t);
|
||||||
|
// }
|
||||||
|
if (request.getSession() == null || request.getSession().getAttribute("authorized") != Command.QUERY) {
|
||||||
|
return new Response(Response.ResponseCode.UNAUTHORIZED);
|
||||||
|
}
|
||||||
|
|
||||||
|
User u = (User) request.getSession().getAttribute("user");
|
||||||
|
if (u == null) return new Response(Response.ResponseCode.UNAUTHORIZED);
|
||||||
|
|
||||||
|
Criteria c = s.createCriteria(Ticket.class);
|
||||||
|
int first = request.getParameter("offset") == null ? 0 : Integer.parseInt(request.getParameter("offset"));
|
||||||
|
int limit = request.getParameter("limit") == null ? 5 : Integer.parseInt(request.getParameter("limit"));
|
||||||
|
c.setFirstResult(first);
|
||||||
|
c.setMaxResults(limit);
|
||||||
|
c.add(Restrictions.eq("sid", u.getId()));
|
||||||
|
if (request.getParameter("status") != null) {
|
||||||
|
c.add(Restrictions.eq("status", Integer.parseInt(request.getParameter("status"))));
|
||||||
|
} else if (request.getParameter("statusl") != null && request.getParameter("statush") != null) {
|
||||||
|
c.add(Restrictions.between("status",
|
||||||
|
Integer.parseInt(request.getParameter("statusl")),
|
||||||
|
Integer.parseInt(request.getParameter("statush"))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
return new Response(Response.ResponseCode.OK, c.list());
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return new Response(Response.ResponseCode.ILLEGAL_PARAMETER);
|
||||||
|
} catch (HibernateException e) {
|
||||||
|
return new Response(Response.ResponseCode.DATABASE_ERROR);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return new Response(Response.ResponseCode.INTERNAL_ERROR);
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package love.sola.netsupport.api;
|
package love.sola.netsupport.api;
|
||||||
|
|
||||||
import love.sola.netsupport.config.Settings;
|
|
||||||
import love.sola.netsupport.enums.Block;
|
import love.sola.netsupport.enums.Block;
|
||||||
import love.sola.netsupport.enums.ISP;
|
import love.sola.netsupport.enums.ISP;
|
||||||
import love.sola.netsupport.pojo.User;
|
import love.sola.netsupport.pojo.User;
|
||||||
@@ -12,8 +11,6 @@ import javax.servlet.http.HttpServlet;
|
|||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
import static love.sola.netsupport.config.Lang.lang;
|
import static love.sola.netsupport.config.Lang.lang;
|
||||||
|
|
||||||
@@ -26,18 +23,22 @@ import static love.sola.netsupport.config.Lang.lang;
|
|||||||
@WebServlet(name = "Register", urlPatterns = "/api/register", loadOnStartup = 22)
|
@WebServlet(name = "Register", urlPatterns = "/api/register", loadOnStartup = 22)
|
||||||
public class Register extends HttpServlet {
|
public class Register extends HttpServlet {
|
||||||
|
|
||||||
public static Map<String, Long> authorized = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
public static final String STUDENT_ID_REGEX = "^(2010|2012|2013|2014|2015)[0-9]{9}$";
|
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 PHONE_NUMBER_REGEX = "^1[34578][0-9]{9}$";
|
||||||
|
|
||||||
public static final String REDIRECT_PAGE = "http://topaz.sinaapp.com/nm/result.html?";
|
public static final String REDIRECT_PAGE = "http://topaz.sinaapp.com/nm/result.html";
|
||||||
|
|
||||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
request.setCharacterEncoding("utf-8");
|
request.setCharacterEncoding("utf-8");
|
||||||
response.setCharacterEncoding("utf-8");
|
response.setCharacterEncoding("utf-8");
|
||||||
response.addHeader("Content-type", "text/plain;charset=utf-8");
|
response.addHeader("Content-type", "text/plain;charset=utf-8");
|
||||||
|
|
||||||
|
String wechat = checkWechat(request.getParameter("wechatid"), request);
|
||||||
|
if (wechat == null) {
|
||||||
|
response.sendRedirect(response.encodeRedirectURL(REDIRECT_PAGE + "?msg=Illegal_Request&type=1"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
ISP isp = checkISP(request.getParameter("isp"));
|
ISP isp = checkISP(request.getParameter("isp"));
|
||||||
Block block = checkBlock(request.getParameter("block"));
|
Block block = checkBlock(request.getParameter("block"));
|
||||||
String result = register(
|
String result = register(
|
||||||
@@ -48,11 +49,11 @@ public class Register extends HttpServlet {
|
|||||||
block,
|
block,
|
||||||
checkRoom(request.getParameter("room"), block),
|
checkRoom(request.getParameter("room"), block),
|
||||||
checkPhoneNumber(request.getParameter("phone")),
|
checkPhoneNumber(request.getParameter("phone")),
|
||||||
checkWechat(request.getParameter("wechatid"))
|
wechat
|
||||||
);
|
);
|
||||||
response.sendRedirect(
|
response.sendRedirect(
|
||||||
response.encodeRedirectURL(REDIRECT_PAGE +
|
response.encodeRedirectURL(REDIRECT_PAGE +
|
||||||
"msg=" + result + "" +
|
"?msg=" + result + "" +
|
||||||
"&type=" + (result.equals("Register_Success") ? 1 : 0)
|
"&type=" + (result.equals("Register_Success") ? 1 : 0)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@@ -65,7 +66,7 @@ public class Register extends HttpServlet {
|
|||||||
response.addHeader("Content-type", "text/plain;charset=utf-8");
|
response.addHeader("Content-type", "text/plain;charset=utf-8");
|
||||||
response.sendRedirect(
|
response.sendRedirect(
|
||||||
response.encodeRedirectURL(REDIRECT_PAGE +
|
response.encodeRedirectURL(REDIRECT_PAGE +
|
||||||
"msg=" + lang("Illegal_Request") +
|
"?msg=" + lang("Illegal_Request") +
|
||||||
"&type=-1"
|
"&type=-1"
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@@ -154,10 +155,12 @@ public class Register extends HttpServlet {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String checkWechat(String wechat) {
|
private String checkWechat(String wechat, HttpServletRequest request) {
|
||||||
if (wechat == null) return null;
|
if (wechat == null) return null;
|
||||||
Long l = authorized.remove(wechat);
|
if (request.getSession() == null) return null;
|
||||||
return l == null ? null : l < System.currentTimeMillis() - Settings.I.User_Register_Timeout * 1000 ? null : wechat;
|
String reqWechat = (String) request.getSession().getAttribute("wechat");
|
||||||
|
if (reqWechat != null && reqWechat.equals(wechat)) return reqWechat;
|
||||||
|
else return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,11 @@ public class Response {
|
|||||||
ILLEGAL_PARAMETER(-2),
|
ILLEGAL_PARAMETER(-2),
|
||||||
AUTHORIZE_FAILED(-9),
|
AUTHORIZE_FAILED(-9),
|
||||||
USER_NOT_FOUND(-11),
|
USER_NOT_FOUND(-11),
|
||||||
|
TICKET_NOT_FOUND(-12),
|
||||||
|
UNAUTHORIZED(-20),
|
||||||
REQUEST_EXPIRED(-21),
|
REQUEST_EXPIRED(-21),
|
||||||
|
INTERNAL_ERROR(-90),
|
||||||
|
DATABASE_ERROR(-91),
|
||||||
;
|
;
|
||||||
|
|
||||||
private static final Map<Integer, ResponseCode> ID_MAP = new HashMap<>();
|
private static final Map<Integer, ResponseCode> ID_MAP = new HashMap<>();
|
||||||
|
|||||||
Reference in New Issue
Block a user