mirror of
				https://github.com/ZSCNetSupportDept/WechatTicketSystem.git
				synced 2025-10-31 10:26:19 +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.setCharacterEncoding("utf-8"); | ||||
| 		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.close(); | ||||
| 	} | ||||
|   | ||||
| @@ -2,7 +2,9 @@ package love.sola.netsupport.api; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| import love.sola.netsupport.config.Settings; | ||||
| 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 love.sola.netsupport.wechat.Command; | ||||
|  | ||||
| @@ -57,13 +59,16 @@ public class Authorize extends HttpServlet { | ||||
| 		if (l < System.currentTimeMillis() - Settings.I.User_Command_Timeout * 1000) { | ||||
| 			return new Response(Response.ResponseCode.REQUEST_EXPIRED); | ||||
| 		} | ||||
| 		request.getSession(true).setAttribute("authorized", c); | ||||
| 		request.getSession(true).setAttribute("wechat", wechat); | ||||
| 		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); | ||||
| 				User u = TableUser.getUserByWechat(wechat); | ||||
| 				if (u == null) return new Response(Response.ResponseCode.AUTHORIZE_FAILED); | ||||
| 				request.getSession(true).setAttribute("user", u); | ||||
| 				break; | ||||
| 			default: | ||||
| 				return new Response(Response.ResponseCode.AUTHORIZE_FAILED); | ||||
| 		} | ||||
|   | ||||
| @@ -1,8 +1,15 @@ | ||||
| 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.util.JsonP; | ||||
| import love.sola.netsupport.wechat.Command; | ||||
| import org.hibernate.Criteria; | ||||
| import org.hibernate.HibernateException; | ||||
| import org.hibernate.Session; | ||||
| import org.hibernate.criterion.Restrictions; | ||||
|  | ||||
| import javax.servlet.ServletException; | ||||
| import javax.servlet.annotation.WebServlet; | ||||
| @@ -18,11 +25,13 @@ import java.io.PrintWriter; | ||||
|  * 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 { | ||||
|  | ||||
| 	private Gson gson = SQLCore.gson; | ||||
|  | ||||
| 	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		 | ||||
| 		doGet(request, response); | ||||
| 	} | ||||
| 	 | ||||
| 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| @@ -30,15 +39,47 @@ public class QueryTicket 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) { | ||||
| 		try (Session s = SQLCore.sf.openSession()) { | ||||
| 			// TODO: 2015/12/5 TICKET QUERY  | ||||
| 		} catch (HibernateException e) { | ||||
| //			if (request.getParameter("id") != null) { | ||||
| //				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; | ||||
|  | ||||
| import love.sola.netsupport.config.Settings; | ||||
| import love.sola.netsupport.enums.Block; | ||||
| import love.sola.netsupport.enums.ISP; | ||||
| import love.sola.netsupport.pojo.User; | ||||
| @@ -12,8 +11,6 @@ import javax.servlet.http.HttpServlet; | ||||
| import javax.servlet.http.HttpServletRequest; | ||||
| import javax.servlet.http.HttpServletResponse; | ||||
| import java.io.IOException; | ||||
| import java.util.Map; | ||||
| import java.util.concurrent.ConcurrentHashMap; | ||||
|  | ||||
| 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) | ||||
| 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 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 { | ||||
| 		request.setCharacterEncoding("utf-8"); | ||||
| 		response.setCharacterEncoding("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")); | ||||
| 		Block block = checkBlock(request.getParameter("block")); | ||||
| 		String result = register( | ||||
| @@ -48,11 +49,11 @@ public class Register extends HttpServlet { | ||||
| 				block, | ||||
| 				checkRoom(request.getParameter("room"), block), | ||||
| 				checkPhoneNumber(request.getParameter("phone")), | ||||
| 				checkWechat(request.getParameter("wechatid")) | ||||
| 				wechat | ||||
| 		); | ||||
| 		response.sendRedirect( | ||||
| 				response.encodeRedirectURL(REDIRECT_PAGE + | ||||
| 						"msg=" + result + "" + | ||||
| 						"?msg=" + result + "" + | ||||
| 						"&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.sendRedirect( | ||||
| 				response.encodeRedirectURL(REDIRECT_PAGE + | ||||
| 						"msg=" + lang("Illegal_Request") + | ||||
| 						"?msg=" + lang("Illegal_Request") + | ||||
| 						"&type=-1" | ||||
| 				) | ||||
| 		); | ||||
| @@ -154,10 +155,12 @@ public class Register extends HttpServlet { | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	private String checkWechat(String wechat) { | ||||
| 	private String checkWechat(String wechat, HttpServletRequest request) { | ||||
| 		if (wechat == null) return null; | ||||
| 		Long l = authorized.remove(wechat); | ||||
| 		return l == null ? null : l < System.currentTimeMillis() - Settings.I.User_Register_Timeout * 1000 ? null : wechat; | ||||
| 		if (request.getSession() == null) return null; | ||||
| 		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), | ||||
| 		AUTHORIZE_FAILED(-9), | ||||
| 		USER_NOT_FOUND(-11), | ||||
| 		TICKET_NOT_FOUND(-12), | ||||
| 		UNAUTHORIZED(-20), | ||||
| 		REQUEST_EXPIRED(-21), | ||||
| 		INTERNAL_ERROR(-90), | ||||
| 		DATABASE_ERROR(-91), | ||||
| 		; | ||||
|  | ||||
| 		private static final Map<Integer, ResponseCode> ID_MAP = new HashMap<>(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Sola
					Sola