mirror of
				https://github.com/ZSCNetSupportDept/WechatTicketSystem.git
				synced 2025-10-31 02:16:18 +08:00 
			
		
		
		
	use java reflection to make a lite-restful handler
This commit is contained in:
		
							
								
								
									
										32
									
								
								src/main/java/love/sola/netsupport/api/API.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/main/java/love/sola/netsupport/api/API.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | ||||
| package love.sola.netsupport.api; | ||||
|  | ||||
| import love.sola.netsupport.enums.Access; | ||||
| import love.sola.netsupport.wechat.Command; | ||||
| import me.chanjar.weixin.common.session.WxSession; | ||||
|  | ||||
| import javax.servlet.http.HttpServletRequest; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
|  * Created by Sola on 2016/2/27. | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| public abstract class API { | ||||
|  | ||||
| 	public String url = null; //url | ||||
| 	public int access = Access.GOD_MODE; //operator's permission | ||||
| 	public Command authorize = null; //session check | ||||
|  | ||||
| 	protected abstract Object process(HttpServletRequest req, WxSession session) throws Exception; | ||||
|  | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		return getClass().getSimpleName() + "{" + | ||||
| 				"url='" + url + '\'' + | ||||
| 				", access=" + Access.inverseMap.get(access) + | ||||
| 				", authorize=" + authorize + | ||||
| 				'}'; | ||||
| 	} | ||||
|  | ||||
| } | ||||
							
								
								
									
										127
									
								
								src/main/java/love/sola/netsupport/api/APIRouter.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										127
									
								
								src/main/java/love/sola/netsupport/api/APIRouter.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,127 @@ | ||||
| package love.sola.netsupport.api; | ||||
|  | ||||
| import com.google.common.reflect.ClassPath; | ||||
| import com.google.gson.Gson; | ||||
| import love.sola.netsupport.enums.Access; | ||||
| import love.sola.netsupport.enums.Attribute; | ||||
| import love.sola.netsupport.pojo.Operator; | ||||
| import love.sola.netsupport.pojo.User; | ||||
| import love.sola.netsupport.sql.SQLCore; | ||||
| import love.sola.netsupport.wechat.WechatSession; | ||||
| import me.chanjar.weixin.common.session.WxSession; | ||||
| import org.hibernate.HibernateException; | ||||
|  | ||||
| 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; | ||||
| import java.text.ParseException; | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| import java.util.Set; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
|  * Created by Sola on 2016/2/27. | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| @WebServlet(name = "APIRouter", urlPatterns = "/api/*", loadOnStartup = 11) | ||||
| public class APIRouter extends HttpServlet { | ||||
|  | ||||
| 	protected static Gson gson = SQLCore.gson; | ||||
| 	private Map<String, API> nodes = new HashMap<>(); | ||||
|  | ||||
| 	public APIRouter() { | ||||
| 		try { | ||||
| 			ClassPath path = ClassPath.from(getClass().getClassLoader()); | ||||
| 			Set<ClassPath.ClassInfo> classes = path.getTopLevelClassesRecursive(getClass().getPackage().getName()); | ||||
| 			for (ClassPath.ClassInfo info : classes) { | ||||
| 				Class<?> clz = info.load(); | ||||
| 				if (!API.class.equals(clz) && API.class.isAssignableFrom(clz)) { | ||||
| 					try { | ||||
| 						System.out.print("Loading API: " + clz.getName()); | ||||
| 						API obj = (API) clz.newInstance(); | ||||
| 						System.out.println("Registered API: " + obj); | ||||
| 						nodes.put(obj.url, obj); | ||||
| 					} catch (InstantiationException | IllegalAccessException e) { | ||||
| 						e.printStackTrace(); | ||||
| 					} | ||||
| 				} | ||||
| 			} | ||||
| 		} catch (Exception e) { | ||||
| 			e.printStackTrace(); | ||||
| 		} | ||||
| 		System.out.println("Total " + nodes.size() + " API(s) loaded."); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||||
| 		req.setCharacterEncoding("utf-8"); | ||||
| 		resp.setCharacterEncoding("utf-8"); | ||||
| 		resp.addHeader("Content-type", "application/json;charset=utf-8"); | ||||
| 		resp.addHeader("Access-Control-Allow-Origin", "*"); | ||||
| 		Object obj = null; | ||||
| 		try { | ||||
| 			API api = nodes.get(req.getRequestURI()); | ||||
| 			if (api == null) { | ||||
| 				resp.sendError(HttpServletResponse.SC_FORBIDDEN); | ||||
| 				return; | ||||
| 			} | ||||
| 			WxSession session = getSession(req); | ||||
| 			if (session == null) { | ||||
| 				obj = Error.UNAUTHORIZED; | ||||
| 				return; | ||||
| 			} | ||||
| 			if (api.authorize != null) { | ||||
| 				if (session.getAttribute(Attribute.AUTHORIZED) != api.authorize) { | ||||
| 					obj = Error.UNAUTHORIZED; | ||||
| 					return; | ||||
| 				} | ||||
| 				if (api.access == Access.USER) { | ||||
| 					User u = (User) session.getAttribute(Attribute.USER); | ||||
| 					if (u == null) { | ||||
| 						obj = Error.UNAUTHORIZED; | ||||
| 						return; | ||||
| 					} | ||||
| 				} | ||||
| 				if (api.access < Access.USER) { | ||||
| 					Operator op = (Operator) session.getAttribute(Attribute.OPERATOR); | ||||
| 					if (op == null) { | ||||
| 						obj = Error.UNAUTHORIZED; | ||||
| 						return; | ||||
| 					} | ||||
| 					if (op.getAccess() > api.access) { | ||||
| 						obj = Error.PERMISSION_DENIED; | ||||
| 						return; | ||||
| 					} | ||||
| 				} | ||||
| 			} | ||||
| 			obj = api.process(req, session); | ||||
| 		} catch (ParseException | NumberFormatException e) { | ||||
| 			obj = Error.ILLEGAL_PARAMETER; | ||||
| 		} catch (HibernateException e) { | ||||
| 			e.printStackTrace(); | ||||
| 			obj = Error.DATABASE_ERROR; | ||||
| 		} catch (Exception e) { | ||||
| 			e.printStackTrace(); | ||||
| 			obj = Error.INTERNAL_ERROR; | ||||
| 		} finally { | ||||
| 			if (!resp.isCommitted()) { | ||||
| 				try (PrintWriter out = resp.getWriter()) { | ||||
| 					out.println(gson.toJson(obj)); | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	private static WxSession getSession(HttpServletRequest req) { | ||||
| 		String t = req.getParameter("token"); | ||||
| 		if (t == null || t.isEmpty()) return null; | ||||
| 		return WechatSession.get(t, false); | ||||
| 	} | ||||
|  | ||||
| } | ||||
| @@ -1,19 +1,10 @@ | ||||
| package love.sola.netsupport.api; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| import love.sola.netsupport.enums.Access; | ||||
| import love.sola.netsupport.enums.Attribute; | ||||
| import love.sola.netsupport.sql.SQLCore; | ||||
| import love.sola.netsupport.util.ParseUtil; | ||||
| import love.sola.netsupport.wechat.WechatSession; | ||||
| import me.chanjar.weixin.common.session.WxSession; | ||||
|  | ||||
| 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; | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
|  | ||||
| @@ -23,44 +14,28 @@ import java.util.Map; | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| @WebServlet(name = "CheckSession", urlPatterns = "/api/checksession", loadOnStartup = 11) | ||||
| public class CheckSession extends HttpServlet { | ||||
| public class CheckSession extends API { | ||||
|  | ||||
| 	private Gson gson = SQLCore.gson; | ||||
|  | ||||
| 	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		doGet(request, response); | ||||
| 	public CheckSession() { | ||||
| 		url = "/api/checksession"; | ||||
| 		access = Access.GUEST; | ||||
| 		authorize = null; | ||||
| 	} | ||||
|  | ||||
| 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		request.setCharacterEncoding("utf-8"); | ||||
| 		response.setCharacterEncoding("utf-8"); | ||||
| 		response.addHeader("Content-type", "application/json;charset=utf-8"); | ||||
| 		PrintWriter out = response.getWriter(); | ||||
| 		String json = gson.toJson(check(request)); | ||||
| 		out.println(ParseUtil.parseJsonP(request, json)); | ||||
| 		out.close(); | ||||
| 	} | ||||
|  | ||||
| 	private Response check(HttpServletRequest request) { | ||||
| 		String t = request.getParameter("token"); | ||||
| 		if (t == null || t.isEmpty()) return new Response(Response.ResponseCode.UNAUTHORIZED); | ||||
| 		WxSession s = WechatSession.get(t, false); | ||||
| 		if (s == null) return new Response(Response.ResponseCode.UNAUTHORIZED); | ||||
| 		String more = request.getParameter("more"); | ||||
| 	@Override | ||||
| 	protected Object process(HttpServletRequest req, WxSession session) throws Exception { | ||||
| 		String more = req.getParameter("more"); | ||||
| 		Map<String, Object> result = new HashMap<>(); | ||||
| 		result.put(Attribute.AUTHORIZED, s.getAttribute(Attribute.AUTHORIZED)); | ||||
| 		if (more != null){ | ||||
| 		result.put(Attribute.AUTHORIZED, session.getAttribute(Attribute.AUTHORIZED)); | ||||
| 		if (more != null) { | ||||
| 			switch (more) { | ||||
| 				case "1": | ||||
| 					result.put(Attribute.USER, s.getAttribute(Attribute.USER)); | ||||
| 					result.put(Attribute.OPERATOR, s.getAttribute(Attribute.OPERATOR)); | ||||
| 					result.put(Attribute.USER, session.getAttribute(Attribute.USER)); | ||||
| 					result.put(Attribute.OPERATOR, session.getAttribute(Attribute.OPERATOR)); | ||||
| 					break; | ||||
| 			} | ||||
| 		} | ||||
| 		return new Response(Response.ResponseCode.OK, result); | ||||
| 		return result; | ||||
| 	} | ||||
|  | ||||
|  | ||||
|  | ||||
| } | ||||
|   | ||||
							
								
								
									
										45
									
								
								src/main/java/love/sola/netsupport/api/Error.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								src/main/java/love/sola/netsupport/api/Error.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,45 @@ | ||||
| package love.sola.netsupport.api; | ||||
|  | ||||
| import static love.sola.netsupport.config.Lang.lang; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
|  * Created by Sola on 2015/11/5. | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| public class Error { | ||||
|  | ||||
| 	public static final Error ALREADY_SUBMITTED = new Error(1); | ||||
| 	public static final Error OK = new Error(0); | ||||
| 	public static final Error PARAMETER_REQUIRED = new Error(-1); | ||||
| 	public static final Error ILLEGAL_PARAMETER = new Error(-2); | ||||
| 	public static final Error REQUEST_FAILED = new Error(-3); | ||||
| 	public static final Error LENGTH_LIMIT_EXCEEDED = new Error(-4); | ||||
| 	public static final Error INVALID_PARAMETER = new Error(-5); | ||||
| 	public static final Error USER_NOT_FOUND = new Error(-11); | ||||
| 	public static final Error TICKET_NOT_FOUND = new Error(-12); | ||||
| 	public static final Error OPERATOR_NOT_FOUND = new Error(-13); | ||||
| 	public static final Error UNAUTHORIZED = new Error(-20); | ||||
| 	public static final Error WRONG_PASSWORD = new Error(-22); | ||||
| 	public static final Error PERMISSION_DENIED = new Error(-24); | ||||
| 	public static final Error INTERNAL_ERROR = new Error(-90); | ||||
| 	public static final Error DATABASE_ERROR = new Error(-91); | ||||
|  | ||||
| 	public int errCode; | ||||
| 	public String errMsg; | ||||
|  | ||||
| 	private Error(int code) { | ||||
| 		this(code, lang("ERR_" + code)); | ||||
| 	} | ||||
|  | ||||
| 	public Error(int errCode, String errMsg) { | ||||
| 		this.errCode = errCode; | ||||
| 		this.errMsg = errMsg; | ||||
| 	} | ||||
|  | ||||
| 	public Error withMsg(String msg) { | ||||
| 		return new Error(errCode, msg); | ||||
| 	} | ||||
|  | ||||
| } | ||||
| @@ -49,19 +49,19 @@ public class Login extends HttpServlet { | ||||
| 		out.close(); | ||||
| 	} | ||||
|  | ||||
| 	private Response login(HttpServletRequest request) { | ||||
| 	private Object login(HttpServletRequest request) { | ||||
| 		try { | ||||
| 			int oid = Integer.parseInt(request.getParameter("id")); | ||||
| 			String password = request.getParameter("pass"); | ||||
| 			boolean bypass = request.getParameter("bypass") != null; | ||||
| 			Operator op = TableOperator.get(oid); | ||||
| 			if (op == null) | ||||
| 				return new Response(Response.ResponseCode.OPERATOR_NOT_FOUND); | ||||
| 			else if (op.getAccess() == Access.NOLOGIN) | ||||
| 				return new Response(Response.ResponseCode.PERMISSION_DENIED); | ||||
| 				return Error.OPERATOR_NOT_FOUND; | ||||
| 			else if (op.getAccess() >= Access.NO_LOGIN) | ||||
| 				return Error.PERMISSION_DENIED; | ||||
|  | ||||
| 			if (!Crypto.check(bypass ? password : RSAUtil.decrypt(password), op.getPassword())) { | ||||
| 				return new Response(Response.ResponseCode.WRONG_PASSWORD); | ||||
| 				return Error.WRONG_PASSWORD; | ||||
| 			} | ||||
|  | ||||
| 			String sid = WechatSession.genId(); | ||||
| @@ -83,10 +83,10 @@ public class Login extends HttpServlet { | ||||
| 			if (request.getParameter("bypasswechat") != null) { | ||||
| 				session.setAttribute(Attribute.WECHAT, request.getParameter("bypasswechat")); | ||||
| 			} | ||||
|  | ||||
| 			return new Response(Response.ResponseCode.OK, sid); | ||||
| 			return sid; | ||||
| 		} catch (Exception e) { | ||||
| 			return new Response(Response.ResponseCode.REQUEST_FAILED, e); | ||||
| 			e.printStackTrace(); | ||||
| 			return Error.REQUEST_FAILED; | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| @@ -1,75 +0,0 @@ | ||||
| package love.sola.netsupport.api; | ||||
|  | ||||
| import lombok.AllArgsConstructor; | ||||
|  | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
|  * Created by Sola on 2015/11/5. | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| @AllArgsConstructor | ||||
| public class Response { | ||||
|  | ||||
| 	public int code; | ||||
| 	public String info; | ||||
| 	public Object result; | ||||
|  | ||||
| 	public Response(ResponseCode code) { | ||||
| 		this(code, null); | ||||
| 	} | ||||
|  | ||||
| 	public Response(ResponseCode code, Object result) { | ||||
| 		this.code = code.id; | ||||
| 		this.info = code.name(); | ||||
| 		this.result = result; | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	public enum ResponseCode { | ||||
|  | ||||
| 		ALREADY_SUBMITTED(1), | ||||
| 		OK(0), | ||||
| 		PARAMETER_REQUIRED(-1), | ||||
| 		ILLEGAL_PARAMETER(-2), | ||||
| 		REQUEST_FAILED(-3), | ||||
| 		LENGTH_LIMIT_EXCEEDED(-4), | ||||
| 		USER_NOT_FOUND(-11), | ||||
| 		TICKET_NOT_FOUND(-12), | ||||
| 		OPERATOR_NOT_FOUND(-13), | ||||
| 		UNAUTHORIZED(-20), | ||||
| 		WRONG_PASSWORD(-22), | ||||
| 		PERMISSION_DENIED(-24), | ||||
| 		INTERNAL_ERROR(-90), | ||||
| 		DATABASE_ERROR(-91), | ||||
| 		; | ||||
|  | ||||
| 		private static final Map<Integer, ResponseCode> ID_MAP = new HashMap<>(); | ||||
|  | ||||
| 		static { | ||||
| 			for (ResponseCode type : values()) { | ||||
| 				ID_MAP.put(type.id, type); | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		public final int id; | ||||
|  | ||||
| 		ResponseCode(int id) { | ||||
| 			this.id = id; | ||||
| 		} | ||||
|  | ||||
| 		public static ResponseCode fromId(int id) { | ||||
| 			return ID_MAP.get(id); | ||||
| 		} | ||||
|  | ||||
| 		@Override | ||||
| 		public String toString() { | ||||
| 			return name(); | ||||
| 		} | ||||
|  | ||||
| 	} | ||||
|  | ||||
| } | ||||
| @@ -1,26 +1,14 @@ | ||||
| package love.sola.netsupport.api.manager; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| import love.sola.netsupport.api.Response; | ||||
| import love.sola.netsupport.api.API; | ||||
| import love.sola.netsupport.api.Error; | ||||
| import love.sola.netsupport.enums.Access; | ||||
| import love.sola.netsupport.enums.Attribute; | ||||
| import love.sola.netsupport.pojo.Operator; | ||||
| import love.sola.netsupport.pojo.User; | ||||
| import love.sola.netsupport.sql.SQLCore; | ||||
| import love.sola.netsupport.sql.TableUser; | ||||
| import love.sola.netsupport.util.Checker; | ||||
| import love.sola.netsupport.util.ParseUtil; | ||||
| import love.sola.netsupport.wechat.Command; | ||||
| import me.chanjar.weixin.common.session.WxSession; | ||||
|  | ||||
| import javax.servlet.ServletConfig; | ||||
| 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; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
| @@ -28,62 +16,37 @@ import java.io.PrintWriter; | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| @WebServlet(name = "GetUser",urlPatterns = "/api/admin/getuser",loadOnStartup = 41) | ||||
| public class GetUser extends HttpServlet { | ||||
| public class GetUser extends API { | ||||
|  | ||||
| 	private Gson gson = SQLCore.gson; | ||||
| 	public GetUser() { | ||||
| 		url = "/api/admin/getuser"; | ||||
| 		access = Access.LEADER; | ||||
| 		authorize = Command.LOGIN; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public void init(ServletConfig config) throws ServletException { | ||||
| 		super.init(config); | ||||
| 	} | ||||
|  | ||||
| 	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		doGet(request, response); | ||||
| 	} | ||||
| 	 | ||||
| 	@SuppressWarnings("Duplicates") | ||||
| 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		request.setCharacterEncoding("utf-8"); | ||||
| 		response.setCharacterEncoding("utf-8"); | ||||
| 		response.addHeader("Content-type", "application/json;charset=utf-8"); | ||||
| 		PrintWriter out = response.getWriter(); | ||||
| 		String json = gson.toJson(query(request)); | ||||
| 		out.println(ParseUtil.parseJsonP(request, json)); | ||||
| 		out.close(); | ||||
| 	} | ||||
|  | ||||
| 	private Response query(HttpServletRequest request) { | ||||
| 		WxSession session = Checker.isAuthorized(request, Command.LOGIN); | ||||
| 		if (session == null) { | ||||
| 			return new Response(Response.ResponseCode.UNAUTHORIZED); | ||||
| 		} | ||||
| 		Operator op = (Operator) session.getAttribute(Attribute.OPERATOR); | ||||
| 		if (op.getAccess() > Access.LEADER) { | ||||
| 			return new Response(Response.ResponseCode.PERMISSION_DENIED); | ||||
| 		} | ||||
|  | ||||
| 		String id = request.getParameter("id"); | ||||
| 		String name = request.getParameter("name"); | ||||
| 	protected Object process(HttpServletRequest req, WxSession session) throws Exception { | ||||
| 		String id = req.getParameter("id"); | ||||
| 		String name = req.getParameter("name"); | ||||
| 		if ((id == null || id.isEmpty()) && (name == null || name.isEmpty())) { | ||||
| 			return new Response(Response.ResponseCode.PARAMETER_REQUIRED); | ||||
| 			return Error.PARAMETER_REQUIRED; | ||||
| 		} | ||||
| 		if (id != null) { | ||||
| 			try { | ||||
| 				User u = TableUser.getById(Long.parseLong(id)); | ||||
| 				if (u == null) | ||||
| 					return new Response(Response.ResponseCode.USER_NOT_FOUND); | ||||
| 					return Error.USER_NOT_FOUND; | ||||
| 				else | ||||
| 					return new Response(Response.ResponseCode.OK, u); | ||||
| 					return u; | ||||
| 			} catch (NumberFormatException e) { | ||||
| 				return new Response(Response.ResponseCode.ILLEGAL_PARAMETER); | ||||
| 				return Error.ILLEGAL_PARAMETER; | ||||
| 			} | ||||
| 		} else { | ||||
| 			User u = TableUser.getByName(name); | ||||
| 			if (u == null) | ||||
| 				return new Response(Response.ResponseCode.USER_NOT_FOUND); | ||||
| 				return Error.USER_NOT_FOUND; | ||||
| 			else | ||||
| 				return new Response(Response.ResponseCode.OK, u); | ||||
| 				return u; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -1,7 +1,7 @@ | ||||
| package love.sola.netsupport.api.manager; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| import love.sola.netsupport.api.Response; | ||||
| import love.sola.netsupport.api.API; | ||||
| import love.sola.netsupport.api.Error; | ||||
| import love.sola.netsupport.config.Settings; | ||||
| import love.sola.netsupport.enums.Access; | ||||
| import love.sola.netsupport.enums.Attribute; | ||||
| @@ -11,19 +11,11 @@ import love.sola.netsupport.pojo.Ticket; | ||||
| import love.sola.netsupport.pojo.User; | ||||
| import love.sola.netsupport.sql.SQLCore; | ||||
| import love.sola.netsupport.util.Checker; | ||||
| import love.sola.netsupport.util.ParseUtil; | ||||
| import love.sola.netsupport.wechat.Command; | ||||
| import me.chanjar.weixin.common.session.WxSession; | ||||
| 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; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
| @@ -31,62 +23,35 @@ import java.io.PrintWriter; | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| @WebServlet(name = "TicketPush",urlPatterns = "/api/admin/ticketpush",loadOnStartup = 42) | ||||
| public class TicketPush extends HttpServlet{ | ||||
| public class TicketPush extends API { | ||||
|  | ||||
| 	private Gson gson = SQLCore.gson; | ||||
|  | ||||
| 	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		doGet(request, response); | ||||
| 	public TicketPush() { | ||||
| 		url = "/api/admin/ticketpush"; | ||||
| 		access = Access.LEADER; | ||||
| 		authorize = Command.LOGIN; | ||||
| 	} | ||||
|  | ||||
| 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		request.setCharacterEncoding("utf-8"); | ||||
| 		response.setCharacterEncoding("utf-8"); | ||||
| 		response.addHeader("Content-type", "application/json;charset=utf-8"); | ||||
| 		PrintWriter out = response.getWriter(); | ||||
| 		String json = gson.toJson(push(request)); | ||||
| 		out.println(ParseUtil.parseJsonP(request, json)); | ||||
| 		out.close(); | ||||
| 	} | ||||
|  | ||||
| 	private Response push(HttpServletRequest request) { | ||||
| 		String uid = request.getParameter("uid"); | ||||
| 		String desc = request.getParameter("desc"); | ||||
| 	@Override | ||||
| 	protected Object process(HttpServletRequest req, WxSession session) throws Exception { | ||||
| 		String uid = req.getParameter("uid"); | ||||
| 		String desc = req.getParameter("desc"); | ||||
| 		if (Checker.hasNull(uid, desc)) { | ||||
| 			return new Response(Response.ResponseCode.PARAMETER_REQUIRED); | ||||
| 			return Error.PARAMETER_REQUIRED; | ||||
| 		} | ||||
| 		if (desc.length() > Settings.MAX_DESC_LENGTH) { | ||||
| 			return new Response(Response.ResponseCode.LENGTH_LIMIT_EXCEEDED); | ||||
| 		} | ||||
|  | ||||
| 		WxSession session = Checker.isAuthorized(request, Command.LOGIN); | ||||
| 		if (session == null) { | ||||
| 			return new Response(Response.ResponseCode.UNAUTHORIZED); | ||||
| 			return Error.LENGTH_LIMIT_EXCEEDED; | ||||
| 		} | ||||
| 		Operator op = (Operator) session.getAttribute(Attribute.OPERATOR); | ||||
| 		if (op.getAccess() > Access.LEADER) { | ||||
| 			return new Response(Response.ResponseCode.PERMISSION_DENIED); | ||||
| 		} | ||||
|  | ||||
| 		try (Session s = SQLCore.sf.openSession()) { | ||||
| 			s.beginTransaction(); | ||||
| 			User u = s.get(User.class, Long.parseLong(uid)); | ||||
| 			if (u == null) { | ||||
| 				return new Response(Response.ResponseCode.USER_NOT_FOUND); | ||||
| 				return Error.USER_NOT_FOUND; | ||||
| 			} | ||||
| 			Ticket t = new Ticket(null, u, desc, null, "Pushed By Admin", null, op, Status.UNCHECKED); | ||||
| 			s.save(t); | ||||
| 			s.getTransaction().commit(); | ||||
| 			return new Response(Response.ResponseCode.OK, t); | ||||
| 		} catch (NumberFormatException e) { | ||||
| 			return new Response(Response.ResponseCode.ILLEGAL_PARAMETER); | ||||
| 		} catch (HibernateException e) { | ||||
| 			e.printStackTrace(); | ||||
| 			return new Response(Response.ResponseCode.DATABASE_ERROR, e.getMessage()); | ||||
| 		} catch (Exception e) { | ||||
| 			e.printStackTrace(); | ||||
| 			return new Response(Response.ResponseCode.INTERNAL_ERROR, e.getMessage()); | ||||
| 			return t; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -1,21 +1,13 @@ | ||||
| package love.sola.netsupport.api.root; | ||||
|  | ||||
| import love.sola.netsupport.api.API; | ||||
| import love.sola.netsupport.enums.Access; | ||||
| import love.sola.netsupport.enums.Attribute; | ||||
| import love.sola.netsupport.pojo.Operator; | ||||
| import love.sola.netsupport.util.Checker; | ||||
| import love.sola.netsupport.wechat.Command; | ||||
| import love.sola.netsupport.wechat.WechatSession; | ||||
| import me.chanjar.weixin.common.session.InternalSession; | ||||
| import me.chanjar.weixin.common.session.WxSession; | ||||
|  | ||||
| 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; | ||||
| import java.util.Enumeration; | ||||
|  | ||||
| /** | ||||
| @@ -24,46 +16,27 @@ import java.util.Enumeration; | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| public class DashBoard extends API { | ||||
|  | ||||
| @WebServlet(name = "Dashboard", urlPatterns = "/api/root/dashboard", loadOnStartup = 51) | ||||
| public class DashBoard extends HttpServlet { | ||||
|  | ||||
| 	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		doGet(request, response); | ||||
| 	public DashBoard() { | ||||
| 		url = "/api/root/dashboard"; | ||||
| 		access = Access.ROOT; | ||||
| 		authorize = Command.LOGIN; | ||||
| 	} | ||||
|  | ||||
| 	@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(); | ||||
| 		process(request, out); | ||||
| 		out.close(); | ||||
| 	} | ||||
|  | ||||
| 	private void process(HttpServletRequest request, PrintWriter out) { | ||||
| 		WxSession session = Checker.isAuthorized(request, Command.LOGIN); | ||||
| 		if (session == null) { | ||||
| 			out.println("Unauthorized"); | ||||
| 			return; | ||||
| 		} | ||||
|  | ||||
| 		Operator op = (Operator) session.getAttribute(Attribute.OPERATOR); | ||||
| 		if (op.getAccess() != Access.ROOT) { | ||||
| 			out.println("Unauthorized"); | ||||
| 			return; | ||||
| 		} | ||||
|  | ||||
| 	@Override | ||||
| 	protected Object process(HttpServletRequest req, WxSession session) throws Exception { | ||||
| 		StringBuilder sb = new StringBuilder(); | ||||
| 		for (InternalSession s : WechatSession.list()) { | ||||
| 			out.println("=====" + s.getIdInternal() + "====="); | ||||
| 			sb.append("=====").append(s.getIdInternal()).append("=====\n"); | ||||
| 			WxSession ws = s.getSession(); | ||||
| 			Enumeration<String> e = ws.getAttributeNames(); | ||||
| 			while (e.hasMoreElements()) { | ||||
| 				String key = e.nextElement(); | ||||
| 				out.println(key + ": " + ws.getAttribute(key)); | ||||
| 				sb.append(key).append(": ").append(ws.getAttribute(key)).append("\n"); | ||||
| 			} | ||||
| 		} | ||||
| 		return sb.toString(); | ||||
| 	} | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -1,20 +1,13 @@ | ||||
| package love.sola.netsupport.api.root; | ||||
|  | ||||
| import love.sola.netsupport.api.API; | ||||
| import love.sola.netsupport.api.Error; | ||||
| import love.sola.netsupport.enums.Access; | ||||
| import love.sola.netsupport.enums.Attribute; | ||||
| import love.sola.netsupport.pojo.Operator; | ||||
| import love.sola.netsupport.sql.TableUser; | ||||
| import love.sola.netsupport.util.Checker; | ||||
| import love.sola.netsupport.wechat.Command; | ||||
| import me.chanjar.weixin.common.session.WxSession; | ||||
|  | ||||
| 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; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
| @@ -22,38 +15,18 @@ import java.io.PrintWriter; | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| public class FlushCache extends API { | ||||
|  | ||||
| @WebServlet(name = "FlushCache", urlPatterns = "/api/root/flushcache", loadOnStartup = 52) | ||||
| public class FlushCache extends HttpServlet { | ||||
|  | ||||
| 	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		doGet(request, response); | ||||
| 	public FlushCache() { | ||||
| 		url = "/api/root/flushcache"; | ||||
| 		access = Access.ROOT; | ||||
| 		authorize = Command.LOGIN; | ||||
| 	} | ||||
|  | ||||
| 	@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(); | ||||
| 		process(request, out); | ||||
| 		out.close(); | ||||
| 	} | ||||
|  | ||||
| 	private void process(HttpServletRequest request, PrintWriter out) { | ||||
| 		WxSession session = Checker.isAuthorized(request, Command.LOGIN); | ||||
| 		if (session == null) { | ||||
| 			out.println("Unauthorized"); | ||||
| 			return; | ||||
| 		} | ||||
|  | ||||
| 		Operator op = (Operator) session.getAttribute(Attribute.OPERATOR); | ||||
| 		if (op.getAccess() != Access.ROOT) { | ||||
| 			out.println("Unauthorized"); | ||||
| 			return; | ||||
| 		} | ||||
| 	@Override | ||||
| 	protected Object process(HttpServletRequest req, WxSession session) throws Exception { | ||||
| 		TableUser.flushCache(); | ||||
| 		out.println("Flushed wechat cache"); | ||||
| 		return Error.OK; | ||||
| 	} | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -1,22 +1,16 @@ | ||||
| package love.sola.netsupport.api.root; | ||||
|  | ||||
| import love.sola.netsupport.api.API; | ||||
| import love.sola.netsupport.api.Error; | ||||
| import love.sola.netsupport.enums.Access; | ||||
| import love.sola.netsupport.enums.Attribute; | ||||
| import love.sola.netsupport.pojo.Operator; | ||||
| import love.sola.netsupport.sql.SQLCore; | ||||
| import love.sola.netsupport.util.Checker; | ||||
| import love.sola.netsupport.util.Crypto; | ||||
| import love.sola.netsupport.wechat.Command; | ||||
| import me.chanjar.weixin.common.session.WxSession; | ||||
| 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; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
| @@ -24,56 +18,31 @@ import java.io.PrintWriter; | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| @WebServlet(name = "SetPassword",urlPatterns = "/api/root/setpass",loadOnStartup = 53) | ||||
| public class SetPassword extends HttpServlet{ | ||||
| public class SetPassword extends API { | ||||
|  | ||||
| 	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		doGet(request, response); | ||||
| 	public SetPassword() { | ||||
| 		url = "/api/root/setpass"; | ||||
| 		access = Access.ROOT; | ||||
| 		authorize = Command.LOGIN; | ||||
| 	} | ||||
|  | ||||
| 	@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(); | ||||
| 		process(request, out); | ||||
| 		out.close(); | ||||
| 	} | ||||
|  | ||||
| 	private void process(HttpServletRequest request, PrintWriter out) { | ||||
| 		WxSession session = Checker.isAuthorized(request, Command.LOGIN); | ||||
| 		if (session == null) { | ||||
| 			out.println("Unauthorized"); | ||||
| 			return; | ||||
| 		} | ||||
|  | ||||
| 		Operator op = (Operator) session.getAttribute(Attribute.OPERATOR); | ||||
| 		if (op.getAccess() != Access.ROOT) { | ||||
| 			out.println("Unauthorized"); | ||||
| 			return; | ||||
| 		} | ||||
|  | ||||
| 		String id = request.getParameter("id"); | ||||
| 		String pass = request.getParameter("pass"); | ||||
| 	@Override | ||||
| 	protected Object process(HttpServletRequest req, WxSession session) throws Exception { | ||||
| 		String id = req.getParameter("id"); | ||||
| 		String pass = req.getParameter("pass"); | ||||
| 		if (pass == null || pass.length() < 8) { | ||||
| 			out.println("Invalid pass"); | ||||
| 			return; | ||||
| 			return Error.INVALID_PARAMETER; | ||||
| 		} | ||||
| 		try (Session s = SQLCore.sf.openSession()) { | ||||
| 			s.beginTransaction(); | ||||
| 			op = s.get(Operator.class, Integer.parseInt(id)); | ||||
| 			Operator op = s.get(Operator.class, Integer.parseInt(id)); | ||||
| 			if (op == null) { | ||||
| 				out.println("Invalid user"); | ||||
| 				return; | ||||
| 				return Error.OPERATOR_NOT_FOUND; | ||||
| 			} | ||||
| 			op.setPassword(Crypto.hash(pass)); | ||||
| 			s.update(op); | ||||
| 			s.getTransaction().commit(); | ||||
| 			out.println("Operation success"); | ||||
| 		} catch (NumberFormatException e) { | ||||
| 			out.println("Invalid id"); | ||||
| 			return; | ||||
| 			return Error.OK; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -1,28 +1,18 @@ | ||||
| package love.sola.netsupport.api.stuff; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| import love.sola.netsupport.api.Response; | ||||
| import love.sola.netsupport.api.API; | ||||
| import love.sola.netsupport.enums.Access; | ||||
| import love.sola.netsupport.pojo.Ticket; | ||||
| import love.sola.netsupport.sql.SQLCore; | ||||
| import love.sola.netsupport.sql.TableTicket; | ||||
| import love.sola.netsupport.util.Checker; | ||||
| import love.sola.netsupport.util.ParseUtil; | ||||
| import love.sola.netsupport.wechat.Command; | ||||
| import me.chanjar.weixin.common.session.WxSession; | ||||
| import org.apache.commons.lang3.time.DateUtils; | ||||
| import org.hibernate.HibernateException; | ||||
| import org.hibernate.Session; | ||||
| import org.hibernate.envers.AuditReader; | ||||
| import org.hibernate.envers.query.AuditEntity; | ||||
|  | ||||
| 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; | ||||
| import java.text.ParseException; | ||||
| import java.text.SimpleDateFormat; | ||||
| import java.util.Calendar; | ||||
| import java.util.Date; | ||||
| @@ -33,61 +23,36 @@ import java.util.Date; | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| @WebServlet(name = "TicketLog", urlPatterns = "/api/admin/ticketlog", loadOnStartup = 35) | ||||
| public class TicketLog extends HttpServlet { | ||||
| public class TicketLog extends API { | ||||
|  | ||||
| 	public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); | ||||
| 	private Gson gson = SQLCore.gson; | ||||
|  | ||||
| 	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		doGet(request, response); | ||||
| 	public TicketLog() { | ||||
| 		url = "/api/admin/ticketlog"; | ||||
| 		access = Access.MEMBER; | ||||
| 		authorize = Command.LOGIN; | ||||
| 	} | ||||
|  | ||||
| 	@SuppressWarnings("Duplicates") | ||||
| 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		request.setCharacterEncoding("utf-8"); | ||||
| 		response.setCharacterEncoding("utf-8"); | ||||
| 		response.addHeader("Content-type", "application/json;charset=utf-8"); | ||||
| 		PrintWriter out = response.getWriter(); | ||||
| 		String json = gson.toJson(query(request)); | ||||
| 		out.println(ParseUtil.parseJsonP(request, json)); | ||||
| 		out.close(); | ||||
| 	} | ||||
|  | ||||
| 	private Response query(HttpServletRequest request) { | ||||
| 		WxSession session = Checker.isAuthorized(request, Command.LOGIN); | ||||
| 		if (session == null) { | ||||
| 			return new Response(Response.ResponseCode.UNAUTHORIZED); | ||||
| 		} | ||||
| 	@Override | ||||
| 	protected Object process(HttpServletRequest req, WxSession session) throws Exception { | ||||
| 		int first; | ||||
| 		int limit; | ||||
| 		Date start; | ||||
| 		Date end; | ||||
| 		try { | ||||
| 			first = request.getParameter("first") == null ? 0 : Integer.parseInt(request.getParameter("first")); | ||||
| 			limit = request.getParameter("limit") == null ? 20 : Integer.parseInt(request.getParameter("limit")); | ||||
| 			start = request.getParameter("start") == null ? getToday() : dateFormat.parse(request.getParameter("start")); | ||||
| 			end = request.getParameter("end") == null ? getToday() : dateFormat.parse(request.getParameter("end")); | ||||
| 			end = DateUtils.addDays(end, 1); | ||||
| 		} catch (ParseException | NumberFormatException e) { | ||||
| 			return new Response(Response.ResponseCode.ILLEGAL_PARAMETER); | ||||
| 		} | ||||
| 		first = req.getParameter("first") == null ? 0 : Integer.parseInt(req.getParameter("first")); | ||||
| 		limit = req.getParameter("limit") == null ? 20 : Integer.parseInt(req.getParameter("limit")); | ||||
| 		start = req.getParameter("start") == null ? getToday() : dateFormat.parse(req.getParameter("start")); | ||||
| 		end = req.getParameter("end") == null ? getToday() : dateFormat.parse(req.getParameter("end")); | ||||
| 		end = DateUtils.addDays(end, 1); | ||||
| 		try (Session s = SQLCore.sf.openSession()) { | ||||
| 			AuditReader reader = TableTicket.getAuditReader(s); | ||||
| 			Object obj = reader.createQuery() | ||||
| 			return reader.createQuery() | ||||
| 					.forRevisionsOfEntity(Ticket.class, false, true) | ||||
| 					.addOrder(AuditEntity.revisionNumber().desc()) | ||||
| 					.add(AuditEntity.revisionProperty("timestamp").between(start.getTime(), end.getTime())) | ||||
| 					.setFirstResult(first) | ||||
| 					.setMaxResults(limit) | ||||
| 					.getResultList(); | ||||
| 			return new Response(Response.ResponseCode.OK, obj); | ||||
| 		} catch (HibernateException e) { | ||||
| 			e.printStackTrace(); | ||||
| 			return new Response(Response.ResponseCode.DATABASE_ERROR, e.getMessage()); | ||||
| 		} catch (Exception e) { | ||||
| 			e.printStackTrace(); | ||||
| 			return new Response(Response.ResponseCode.INTERNAL_ERROR, e.getMessage()); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -1,27 +1,14 @@ | ||||
| package love.sola.netsupport.api.stuff; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| import love.sola.netsupport.api.Response; | ||||
| import love.sola.netsupport.api.API; | ||||
| import love.sola.netsupport.enums.Access; | ||||
| import love.sola.netsupport.enums.Attribute; | ||||
| import love.sola.netsupport.pojo.Operator; | ||||
| import love.sola.netsupport.pojo.Ticket; | ||||
| import love.sola.netsupport.sql.SQLCore; | ||||
| import love.sola.netsupport.sql.TableTicket; | ||||
| import love.sola.netsupport.util.Checker; | ||||
| import love.sola.netsupport.util.ParseUtil; | ||||
| import love.sola.netsupport.wechat.Command; | ||||
| import me.chanjar.weixin.common.session.WxSession; | ||||
| import org.hibernate.HibernateException; | ||||
|  | ||||
| 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; | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
| @@ -29,52 +16,24 @@ import java.util.List; | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| @WebServlet(name = "TicketLookup", urlPatterns = "/api/admin/ticketlookup", loadOnStartup = 33) | ||||
| public class TicketLookup extends HttpServlet { | ||||
| public class TicketLookup extends API { | ||||
|  | ||||
| 	private Gson gson = SQLCore.gson; | ||||
|  | ||||
| 	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		doGet(request, response); | ||||
| 	public TicketLookup() { | ||||
| 		url = "/api/admin/ticketlookup"; | ||||
| 		access = Access.MEMBER; | ||||
| 		authorize = Command.LOGIN; | ||||
| 	} | ||||
|  | ||||
| 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		request.setCharacterEncoding("utf-8"); | ||||
| 		response.setCharacterEncoding("utf-8"); | ||||
| 		response.addHeader("Content-type", "application/json;charset=utf-8"); | ||||
| 		PrintWriter out = response.getWriter(); | ||||
| 		String json = gson.toJson(lookup(request)); | ||||
| 		out.println(ParseUtil.parseJsonP(request, json)); | ||||
| 		out.close(); | ||||
| 	} | ||||
|  | ||||
| 	private Response lookup(HttpServletRequest request) { | ||||
| 		WxSession session = Checker.isAuthorized(request, Command.LOGIN); | ||||
| 		if (session == null) { | ||||
| 			return new Response(Response.ResponseCode.UNAUTHORIZED); | ||||
| 		} | ||||
| 		try { | ||||
| 			Operator op = (Operator) session.getAttribute(Attribute.OPERATOR); | ||||
| 			int block; | ||||
| 			if (request.getParameter("block") != null) { | ||||
| 				block = Integer.parseInt(request.getParameter("block")); | ||||
| 			} else { | ||||
| 				block = op.getBlock(); | ||||
| 			} | ||||
| 			if (block == 0 && op.getAccess() > Access.MEMBER) { | ||||
| 				return new Response(Response.ResponseCode.PERMISSION_DENIED); | ||||
| 			} | ||||
| 			List<Ticket> list = TableTicket.unsolvedByBlock(block); | ||||
| 			return new Response(Response.ResponseCode.OK, list); | ||||
| 		} catch (NumberFormatException e) { | ||||
| 			return new Response(Response.ResponseCode.ILLEGAL_PARAMETER); | ||||
| 		} catch (HibernateException e) { | ||||
| 			e.printStackTrace(); | ||||
| 			return new Response(Response.ResponseCode.DATABASE_ERROR, e.getMessage()); | ||||
| 		} catch (Exception e) { | ||||
| 			e.printStackTrace(); | ||||
| 			return new Response(Response.ResponseCode.INTERNAL_ERROR, e.getMessage()); | ||||
| 	@Override | ||||
| 	protected Object process(HttpServletRequest req, WxSession session) throws Exception { | ||||
| 		Operator op = (Operator) session.getAttribute(Attribute.OPERATOR); | ||||
| 		int block; | ||||
| 		if (req.getParameter("block") != null) { | ||||
| 			block = Integer.parseInt(req.getParameter("block")); | ||||
| 		} else { | ||||
| 			block = op.getBlock(); | ||||
| 		} | ||||
| 		return TableTicket.unsolvedByBlock(block); | ||||
| 	} | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -1,22 +1,13 @@ | ||||
| package love.sola.netsupport.api.stuff; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| import love.sola.netsupport.api.Response; | ||||
| import love.sola.netsupport.sql.SQLCore; | ||||
| import love.sola.netsupport.api.API; | ||||
| import love.sola.netsupport.api.Error; | ||||
| import love.sola.netsupport.enums.Access; | ||||
| import love.sola.netsupport.sql.TableTicket; | ||||
| import love.sola.netsupport.util.Checker; | ||||
| import love.sola.netsupport.util.ParseUtil; | ||||
| import love.sola.netsupport.wechat.Command; | ||||
| import me.chanjar.weixin.common.session.WxSession; | ||||
| import org.hibernate.HibernateException; | ||||
|  | ||||
| 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; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
| @@ -24,47 +15,21 @@ import java.io.PrintWriter; | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| public class TicketTrack extends API { | ||||
|  | ||||
| @WebServlet(name = "TicketTrack", urlPatterns = "/api/admin/tickettrack", loadOnStartup = 34) | ||||
| public class TicketTrack extends HttpServlet{ | ||||
|  | ||||
| 	private Gson gson = SQLCore.gson; | ||||
|  | ||||
| 	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		doGet(request, response); | ||||
| 	public TicketTrack() { | ||||
| 		url = "/api/admin/tickettrack"; | ||||
| 		access = Access.MEMBER; | ||||
| 		authorize = Command.LOGIN; | ||||
| 	} | ||||
|  | ||||
| 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		request.setCharacterEncoding("utf-8"); | ||||
| 		response.setCharacterEncoding("utf-8"); | ||||
| 		response.addHeader("Content-type", "application/json;charset=utf-8"); | ||||
| 		PrintWriter out = response.getWriter(); | ||||
| 		String json = gson.toJson(track(request)); | ||||
| 		out.println(ParseUtil.parseJsonP(request, json)); | ||||
| 		out.close(); | ||||
| 	} | ||||
|  | ||||
| 	private Response track(HttpServletRequest request) { | ||||
| 		String tid = request.getParameter("id"); | ||||
| 	@Override | ||||
| 	protected Object process(HttpServletRequest req, WxSession session) throws Exception { | ||||
| 		String tid = req.getParameter("id"); | ||||
| 		if (tid == null) { | ||||
| 			return new Response(Response.ResponseCode.PARAMETER_REQUIRED); | ||||
| 		} | ||||
| 		WxSession session = Checker.isAuthorized(request, Command.LOGIN); | ||||
| 		if (session == null) { | ||||
| 			return new Response(Response.ResponseCode.UNAUTHORIZED); | ||||
| 		} | ||||
| 		try { | ||||
| 			return new Response(Response.ResponseCode.OK, TableTicket.track(Integer.parseInt(tid))); | ||||
| 		} catch (NumberFormatException e) { | ||||
| 			return new Response(Response.ResponseCode.ILLEGAL_PARAMETER); | ||||
| 		} catch (HibernateException e) { | ||||
| 			e.printStackTrace(); | ||||
| 			return new Response(Response.ResponseCode.DATABASE_ERROR, e.getMessage()); | ||||
| 		} catch (Exception e) { | ||||
| 			e.printStackTrace(); | ||||
| 			return new Response(Response.ResponseCode.INTERNAL_ERROR, e.getMessage()); | ||||
| 			return Error.PARAMETER_REQUIRED; | ||||
| 		} | ||||
| 		return TableTicket.track(Integer.parseInt(tid)); | ||||
| 	} | ||||
|  | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -1,25 +1,18 @@ | ||||
| package love.sola.netsupport.api.stuff; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| import love.sola.netsupport.api.Response; | ||||
| import love.sola.netsupport.api.API; | ||||
| import love.sola.netsupport.api.Error; | ||||
| import love.sola.netsupport.enums.Access; | ||||
| import love.sola.netsupport.enums.Attribute; | ||||
| import love.sola.netsupport.pojo.Operator; | ||||
| import love.sola.netsupport.pojo.Ticket; | ||||
| import love.sola.netsupport.sql.SQLCore; | ||||
| import love.sola.netsupport.util.Checker; | ||||
| import love.sola.netsupport.util.ParseUtil; | ||||
| import love.sola.netsupport.wechat.Command; | ||||
| import me.chanjar.weixin.common.session.WxSession; | ||||
| 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; | ||||
| import java.util.Date; | ||||
|  | ||||
| /** | ||||
| @@ -28,39 +21,25 @@ import java.util.Date; | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| @WebServlet(name = "TicketUpdate", urlPatterns = "/api/admin/ticketupdate", loadOnStartup = 32) | ||||
| public class TicketUpdate extends HttpServlet { | ||||
| public class TicketUpdate extends API { | ||||
|  | ||||
| 	private Gson gson = SQLCore.gson; | ||||
|  | ||||
| 	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		doGet(request, response); | ||||
| 	public TicketUpdate() { | ||||
| 		url = "/api/admin/ticketupdate"; | ||||
| 		access = Access.MEMBER; | ||||
| 		authorize = Command.LOGIN; | ||||
| 	} | ||||
|  | ||||
| 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		request.setCharacterEncoding("utf-8"); | ||||
| 		response.setCharacterEncoding("utf-8"); | ||||
| 		response.addHeader("Content-type", "application/json;charset=utf-8"); | ||||
| 		PrintWriter out = response.getWriter(); | ||||
| 		String json = gson.toJson(update(request)); | ||||
| 		out.println(ParseUtil.parseJsonP(request, json)); | ||||
| 		out.close(); | ||||
| 	} | ||||
|  | ||||
| 	private Response update(HttpServletRequest request) { | ||||
| 		String ticket = request.getParameter("ticket"); | ||||
| 		String remark = request.getParameter("remark"); | ||||
| 		String status = request.getParameter("status"); | ||||
| 		if (Checker.hasNull(ticket, remark, status)) return new Response(Response.ResponseCode.PARAMETER_REQUIRED); | ||||
| 		WxSession session = Checker.isAuthorized(request, Command.LOGIN); | ||||
| 		if (session == null) { | ||||
| 			return new Response(Response.ResponseCode.UNAUTHORIZED); | ||||
| 		} | ||||
| 	@Override | ||||
| 	protected Object process(HttpServletRequest req, WxSession session) throws Exception { | ||||
| 		String ticket = req.getParameter("ticket"); | ||||
| 		String remark = req.getParameter("remark"); | ||||
| 		String status = req.getParameter("status"); | ||||
| 		if (Checker.hasNull(ticket, remark, status)) return Error.PARAMETER_REQUIRED; | ||||
| 		try (Session s = SQLCore.sf.openSession()) { | ||||
| 			Operator op = (Operator) session.getAttribute(Attribute.OPERATOR); | ||||
| 			Ticket t = s.get(Ticket.class, Integer.parseInt(ticket)); | ||||
| 			if (t == null) { | ||||
| 				return new Response(Response.ResponseCode.TICKET_NOT_FOUND); | ||||
| 				return Error.TICKET_NOT_FOUND; | ||||
| 			} | ||||
| 			t.setOperator(op); | ||||
| 			t.setRemark(remark); | ||||
| @@ -69,16 +48,7 @@ public class TicketUpdate extends HttpServlet { | ||||
| 			s.beginTransaction(); | ||||
| 			s.update(t); | ||||
| 			s.getTransaction().commit(); | ||||
| 			return new Response(Response.ResponseCode.OK, t); | ||||
| 		} catch (NumberFormatException e) { | ||||
| 			return new Response(Response.ResponseCode.ILLEGAL_PARAMETER); | ||||
| 		} catch (HibernateException e) { | ||||
| 			e.printStackTrace(); | ||||
| 			return new Response(Response.ResponseCode.DATABASE_ERROR, e.getMessage()); | ||||
| 		} catch (Exception e) { | ||||
| 			e.printStackTrace(); | ||||
| 			return new Response(Response.ResponseCode.INTERNAL_ERROR, e.getMessage()); | ||||
| 			return t; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -1,25 +1,17 @@ | ||||
| package love.sola.netsupport.api.user; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| import love.sola.netsupport.api.Response; | ||||
| import love.sola.netsupport.api.API; | ||||
| import love.sola.netsupport.api.Error; | ||||
| import love.sola.netsupport.enums.Access; | ||||
| import love.sola.netsupport.enums.Attribute; | ||||
| import love.sola.netsupport.enums.ISP; | ||||
| import love.sola.netsupport.pojo.User; | ||||
| import love.sola.netsupport.sql.SQLCore; | ||||
| import love.sola.netsupport.sql.TableUser; | ||||
| import love.sola.netsupport.util.Checker; | ||||
| import love.sola.netsupport.util.ParseUtil; | ||||
| import love.sola.netsupport.wechat.Command; | ||||
| import me.chanjar.weixin.common.session.WxSession; | ||||
| import org.hibernate.exception.ConstraintViolationException; | ||||
|  | ||||
| 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; | ||||
|  | ||||
| import static love.sola.netsupport.util.Checker.*; | ||||
|  | ||||
| @@ -29,44 +21,28 @@ import static love.sola.netsupport.util.Checker.*; | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| @WebServlet(name = "ProfileModify", urlPatterns = "/api/profilemodify", loadOnStartup = 22) | ||||
| public class ProfileModify extends HttpServlet { | ||||
| public class ProfileModify extends API { | ||||
|  | ||||
| 	private Gson gson = SQLCore.gson; | ||||
|  | ||||
| 	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		doGet(request, response); | ||||
| 	public ProfileModify() { | ||||
| 		url = "/api/profilemodify"; | ||||
| 		access = Access.USER; | ||||
| 		authorize = Command.PROFILE; | ||||
| 	} | ||||
|  | ||||
| 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		request.setCharacterEncoding("utf-8"); | ||||
| 		response.setCharacterEncoding("utf-8"); | ||||
| 		response.addHeader("Content-type", "application/json;charset=utf-8"); | ||||
| 		PrintWriter out = response.getWriter(); | ||||
| 		String json = gson.toJson(process(request)); | ||||
| 		out.println(ParseUtil.parseJsonP(request, json)); | ||||
| 		out.close(); | ||||
| 	} | ||||
|  | ||||
| 	private Response process(HttpServletRequest request) { | ||||
| 		WxSession session = Checker.isAuthorized(request, Command.PROFILE); | ||||
| 		if (session == null) { | ||||
| 			return new Response(Response.ResponseCode.UNAUTHORIZED); | ||||
| 		} | ||||
| 	@Override | ||||
| 	protected Object process(HttpServletRequest req, WxSession session) throws Exception { | ||||
| 		User u = (User) session.getAttribute(Attribute.USER); | ||||
| 		if (u == null) return new Response(Response.ResponseCode.UNAUTHORIZED); | ||||
|  | ||||
| 		ISP isp = checkISP(request.getParameter("isp")); | ||||
| 		String netAccount = checkNetAccount(request.getParameter("username"), isp); | ||||
| 		int block = checkBlock(request.getParameter("block")); | ||||
| 		int room = checkRoom(request.getParameter("room"), block); | ||||
| 		long phone = checkPhoneNumber(request.getParameter("phone")); | ||||
| 		ISP isp = checkISP(req.getParameter("isp")); | ||||
| 		String netAccount = checkNetAccount(req.getParameter("username"), isp); | ||||
| 		int block = checkBlock(req.getParameter("block")); | ||||
| 		int room = checkRoom(req.getParameter("room"), block); | ||||
| 		long phone = checkPhoneNumber(req.getParameter("phone")); | ||||
| 		if (room == -1) | ||||
| 			return new Response(Response.ResponseCode.REQUEST_FAILED, "Invalid_Room"); | ||||
| 			return Error.INVALID_PARAMETER.withMsg("Invalid_Room"); | ||||
| 		if (phone == -1) | ||||
| 			return new Response(Response.ResponseCode.REQUEST_FAILED, "Invalid_Phone_Number"); | ||||
| 			return Error.INVALID_PARAMETER.withMsg("Invalid_Phone_Number"); | ||||
| 		if (netAccount == null) | ||||
| 			return new Response(Response.ResponseCode.REQUEST_FAILED, "Invalid_Account"); | ||||
| 			return Error.INVALID_PARAMETER.withMsg("Invalid_Account"); | ||||
|  | ||||
| 		u.setIsp(isp); | ||||
| 		u.setNetAccount(netAccount); | ||||
| @@ -77,12 +53,9 @@ public class ProfileModify extends HttpServlet { | ||||
| 			TableUser.update(u); | ||||
| 		} catch (ConstraintViolationException e) { | ||||
| 			String dupKey = e.getConstraintName(); | ||||
| 			return new Response(Response.ResponseCode.REQUEST_FAILED, "Duplicated_" + dupKey.toUpperCase()); | ||||
| 		} catch (Exception e) { | ||||
| 			e.printStackTrace(); | ||||
| 			return new Response(Response.ResponseCode.INTERNAL_ERROR, e.getMessage()); | ||||
| 			return Error.INVALID_PARAMETER.withMsg("Duplicated_" + dupKey.toUpperCase()); | ||||
| 		} | ||||
| 		session.invalidate(); | ||||
| 		return new Response(Response.ResponseCode.OK); | ||||
| 		return Error.OK; | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -1,14 +1,13 @@ | ||||
| package love.sola.netsupport.api.user; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| import love.sola.netsupport.api.Response; | ||||
| import love.sola.netsupport.api.API; | ||||
| import love.sola.netsupport.api.Error; | ||||
| import love.sola.netsupport.enums.Access; | ||||
| import love.sola.netsupport.enums.Attribute; | ||||
| import love.sola.netsupport.enums.ISP; | ||||
| import love.sola.netsupport.pojo.User; | ||||
| import love.sola.netsupport.sql.SQLCore; | ||||
| import love.sola.netsupport.sql.TableUser; | ||||
| import love.sola.netsupport.util.Checker; | ||||
| import love.sola.netsupport.util.ParseUtil; | ||||
| import love.sola.netsupport.wechat.Command; | ||||
| import love.sola.netsupport.wechat.WxMpServlet; | ||||
| import me.chanjar.weixin.common.exception.WxErrorException; | ||||
| @@ -16,13 +15,7 @@ import me.chanjar.weixin.common.session.WxSession; | ||||
| import me.chanjar.weixin.mp.bean.WxMpCustomMessage; | ||||
| import org.hibernate.exception.ConstraintViolationException; | ||||
|  | ||||
| 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; | ||||
| import java.sql.Connection; | ||||
| import java.sql.PreparedStatement; | ||||
| import java.sql.ResultSet; | ||||
| @@ -36,66 +29,45 @@ import static love.sola.netsupport.util.Checker.*; | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| @WebServlet(name = "Register", urlPatterns = "/api/register", loadOnStartup = 21) | ||||
| public class Register extends HttpServlet { | ||||
| public class Register extends API { | ||||
|  | ||||
| 	private Gson gson = SQLCore.gson; | ||||
| 	public Register() { | ||||
| 		url = "/api/register"; | ||||
| 		access = Access.GUEST; | ||||
| 		authorize = Command.REGISTER; | ||||
| 	} | ||||
|  | ||||
| 	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		request.setCharacterEncoding("utf-8"); | ||||
| 		response.setCharacterEncoding("utf-8"); | ||||
| 		response.addHeader("Content-type", "application/json;charset=utf-8"); | ||||
| 		PrintWriter out = response.getWriter(); | ||||
|  | ||||
| 		WxSession session = Checker.isAuthorized(request, Command.REGISTER); | ||||
| 		if (session == null) { | ||||
| 			printAuthorizeFailed(request, out); | ||||
| 			return; | ||||
| 		} | ||||
| 	@Override | ||||
| 	protected Object process(HttpServletRequest req, WxSession session) throws Exception { | ||||
| 		String wechat = (String) session.getAttribute(Attribute.WECHAT); | ||||
| 		if (wechat == null) { | ||||
| 			printAuthorizeFailed(request, out); | ||||
| 			return; | ||||
| 			return Error.UNAUTHORIZED; | ||||
| 		} | ||||
|  | ||||
| 		ISP isp = checkISP(request.getParameter("isp")); | ||||
| 		int block = checkBlock(request.getParameter("block")); | ||||
| 		String result = register( | ||||
| 				checkStudentId(request.getParameter("sid")), | ||||
| 				request.getParameter("name"), | ||||
| 		ISP isp = checkISP(req.getParameter("isp")); | ||||
| 		int block = checkBlock(req.getParameter("block")); | ||||
| 		return register( | ||||
| 				checkStudentId(req.getParameter("sid")), | ||||
| 				req.getParameter("name"), | ||||
| 				isp, | ||||
| 				checkNetAccount(request.getParameter("username"), isp), | ||||
| 				checkNetAccount(req.getParameter("username"), isp), | ||||
| 				block, | ||||
| 				checkRoom(request.getParameter("room"), block), | ||||
| 				checkPhoneNumber(request.getParameter("phone")), | ||||
| 				wechat | ||||
| 		); | ||||
| 		boolean isSuccess = result.equals("Register_Success"); | ||||
| 		if (isSuccess) { | ||||
| 			session.invalidate(); | ||||
| 			out.println(ParseUtil.parseJsonP(request, gson.toJson(new Response(Response.ResponseCode.OK, result)))); | ||||
| 		} else { | ||||
| 			out.println(ParseUtil.parseJsonP(request, gson.toJson(new Response(Response.ResponseCode.REQUEST_FAILED, result)))); | ||||
| 		} | ||||
| 		out.close(); | ||||
| 				checkRoom(req.getParameter("room"), block), | ||||
| 				checkPhoneNumber(req.getParameter("phone")), | ||||
| 				wechat); | ||||
| 	} | ||||
|  | ||||
| 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		doPost(request, response); | ||||
| 	} | ||||
|  | ||||
| 	private String register(long sid, String name, ISP isp, String netAccount, int block, int room, long phone, String wechat) { | ||||
| 		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 == -1) return "Invalid_Block"; | ||||
| 		if (room == -1) return "Invalid_Room"; | ||||
| 		if (phone == -1) return "Invalid_Phone_Number"; | ||||
| 	private Object register(long sid, String name, ISP isp, String netAccount, int block, int room, long phone, String wechat) { | ||||
| 		if (sid == -1) return Error.INVALID_PARAMETER.withMsg("Invalid_Student_Id"); | ||||
| 		if (name == null) return Error.INVALID_PARAMETER.withMsg("Invalid_Name"); | ||||
| 		if (isp == null) return Error.INVALID_PARAMETER.withMsg("Invalid_ISP"); | ||||
| 		if (netAccount == null) return Error.INVALID_PARAMETER.withMsg("Invalid_Account"); | ||||
| 		if (block == -1) return Error.INVALID_PARAMETER.withMsg("Invalid_Block"); | ||||
| 		if (room == -1) return Error.INVALID_PARAMETER.withMsg("Invalid_Room"); | ||||
| 		if (phone == -1) return Error.INVALID_PARAMETER.withMsg("Invalid_Phone_Number"); | ||||
| 		User user = TableUser.getById(sid); | ||||
| 		if (user == null) return "Invalid_Student_Id"; | ||||
| 		if (!user.getName().equals(name)) return "Invalid_Name"; | ||||
| 		if (user.getWechatId() != null) return "User_Already_Registered"; | ||||
| 		if (user == null) return Error.INVALID_PARAMETER.withMsg("Invalid_Student_Id"); | ||||
| 		if (!user.getName().equals(name)) return Error.INVALID_PARAMETER.withMsg("Invalid_Name"); | ||||
| 		if (user.getWechatId() != null) return Error.INVALID_PARAMETER.withMsg("User_Already_Registered"); | ||||
| 		user.setIsp(isp); | ||||
| 		user.setNetAccount(netAccount); | ||||
| 		user.setBlock(block); | ||||
| @@ -106,17 +78,11 @@ public class Register extends HttpServlet { | ||||
| 			TableUser.update(user); | ||||
| 		} catch (ConstraintViolationException e) { | ||||
| 			String dupKey = e.getConstraintName(); | ||||
| 			return "Duplicated_" + dupKey.toUpperCase(); // PHONE ACCOUNT WECHAT | ||||
| 			return Error.INVALID_PARAMETER.withMsg("Duplicated_" + dupKey.toUpperCase()); // PHONE ACCOUNT WECHAT | ||||
| 		} | ||||
| 		// FIXME: 2015/12/30 Temporary converter | ||||
| 		converterWithRetry(user); | ||||
| 		return "Register_Success"; | ||||
| 	} | ||||
|  | ||||
| 	private void printAuthorizeFailed(HttpServletRequest request, PrintWriter out) { | ||||
| 		out.println(ParseUtil.parseJsonP(request, gson.toJson(new Response(Response.ResponseCode.UNAUTHORIZED)))); | ||||
| 		out.close(); | ||||
| 		return; | ||||
| 		return Error.OK; | ||||
| 	} | ||||
|  | ||||
| 	public static void converterWithRetry(User u) { | ||||
| @@ -155,8 +121,6 @@ public class Register extends HttpServlet { | ||||
| 					WxMpServlet.instance.wxMpService.customMessageSend(WxMpCustomMessage.TEXT().toUser(u.getWechatId()).content("已进行过数据转换").build()); | ||||
| 				} | ||||
| 			} | ||||
| 		} catch (SQLException | WxErrorException e) { | ||||
| 			throw e; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -1,28 +1,19 @@ | ||||
| package love.sola.netsupport.api.user; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| import love.sola.netsupport.api.Response; | ||||
| import love.sola.netsupport.api.API; | ||||
| import love.sola.netsupport.enums.Access; | ||||
| import love.sola.netsupport.enums.Attribute; | ||||
| import love.sola.netsupport.pojo.Ticket; | ||||
| import love.sola.netsupport.pojo.User; | ||||
| import love.sola.netsupport.sql.SQLCore; | ||||
| import love.sola.netsupport.util.Checker; | ||||
| import love.sola.netsupport.util.ParseUtil; | ||||
| import love.sola.netsupport.wechat.Command; | ||||
| import me.chanjar.weixin.common.session.WxSession; | ||||
| import org.hibernate.Criteria; | ||||
| import org.hibernate.HibernateException; | ||||
| import org.hibernate.Session; | ||||
| import org.hibernate.criterion.Order; | ||||
| import org.hibernate.criterion.Restrictions; | ||||
|  | ||||
| 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; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
| @@ -30,60 +21,34 @@ import java.io.PrintWriter; | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| @WebServlet(name = "TicketQuery", urlPatterns = "/api/ticketquery", loadOnStartup = 24) | ||||
| public class TicketQuery extends HttpServlet { | ||||
| public class TicketQuery extends API { | ||||
|  | ||||
| 	private Gson gson = SQLCore.gson; | ||||
|  | ||||
| 	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		doGet(request, response); | ||||
| 	} | ||||
| 	 | ||||
| 	@SuppressWarnings("Duplicates") | ||||
| 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		request.setCharacterEncoding("utf-8"); | ||||
| 		response.setCharacterEncoding("utf-8"); | ||||
| 		response.addHeader("Content-type", "application/json;charset=utf-8"); | ||||
| 		PrintWriter out = response.getWriter(); | ||||
| 		String json = gson.toJson(query(request)); | ||||
| 		out.println(ParseUtil.parseJsonP(request, json)); | ||||
| 		out.close(); | ||||
| 	public TicketQuery() { | ||||
| 		url = "/api/ticketquery"; | ||||
| 		access = Access.USER; | ||||
| 		authorize = Command.QUERY; | ||||
| 	} | ||||
|  | ||||
| 	private Response query(HttpServletRequest request) { | ||||
| 	@Override | ||||
| 	protected Object process(HttpServletRequest req, WxSession session) throws Exception { | ||||
| 		try (Session s = SQLCore.sf.openSession()) { | ||||
|  | ||||
| 			WxSession session = Checker.isAuthorized(request, Command.QUERY); | ||||
| 			if (session == null) { | ||||
| 				return new Response(Response.ResponseCode.UNAUTHORIZED); | ||||
| 			} | ||||
| 			User u = (User) session.getAttribute(Attribute.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")); | ||||
| 			int first = req.getParameter("offset") == null ? 0 : Integer.parseInt(req.getParameter("offset")); | ||||
| 			int limit = req.getParameter("limit") == null ? 5 : Integer.parseInt(req.getParameter("limit")); | ||||
| 			c.setFirstResult(first); | ||||
| 			c.setMaxResults(limit); | ||||
| 			c.addOrder(Order.desc(Ticket.PROPERTY_SUBMIT_TIME)); | ||||
| 			c.add(Restrictions.eq(Ticket.PROPERTY_USER, u)); | ||||
| 			if (request.getParameter("status") != null) { | ||||
| 				c.add(Restrictions.eq(Ticket.PROPERTY_STATUS, Integer.parseInt(request.getParameter("status")))); | ||||
| 			} else if (request.getParameter("statusl") != null && request.getParameter("statush") != null) { | ||||
| 			if (req.getParameter("status") != null) { | ||||
| 				c.add(Restrictions.eq(Ticket.PROPERTY_STATUS, Integer.parseInt(req.getParameter("status")))); | ||||
| 			} else if (req.getParameter("statusl") != null && req.getParameter("statush") != null) { | ||||
| 				c.add(Restrictions.between(Ticket.PROPERTY_STATUS, | ||||
| 						Integer.parseInt(request.getParameter("statusl")), | ||||
| 						Integer.parseInt(request.getParameter("statush")) | ||||
| 						Integer.parseInt(req.getParameter("statusl")), | ||||
| 						Integer.parseInt(req.getParameter("statush")) | ||||
| 				)); | ||||
| 			} | ||||
| 			return new Response(Response.ResponseCode.OK, c.list()); | ||||
| 		} catch (NumberFormatException e) { | ||||
| 			return new Response(Response.ResponseCode.ILLEGAL_PARAMETER); | ||||
| 		} catch (HibernateException e) { | ||||
| 			e.printStackTrace(); | ||||
| 			return new Response(Response.ResponseCode.DATABASE_ERROR, e.getMessage()); | ||||
| 		} catch (Exception e) { | ||||
| 			e.printStackTrace(); | ||||
| 			return new Response(Response.ResponseCode.INTERNAL_ERROR, e.getMessage()); | ||||
| 			return c.list(); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -1,27 +1,19 @@ | ||||
| package love.sola.netsupport.api.user; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| import love.sola.netsupport.api.Response; | ||||
| import love.sola.netsupport.api.API; | ||||
| import love.sola.netsupport.api.Error; | ||||
| import love.sola.netsupport.config.Settings; | ||||
| import love.sola.netsupport.enums.Access; | ||||
| import love.sola.netsupport.enums.Attribute; | ||||
| import love.sola.netsupport.pojo.Ticket; | ||||
| import love.sola.netsupport.pojo.User; | ||||
| import love.sola.netsupport.sql.SQLCore; | ||||
| import love.sola.netsupport.sql.TableTicket; | ||||
| import love.sola.netsupport.util.Checker; | ||||
| import love.sola.netsupport.util.ParseUtil; | ||||
| import love.sola.netsupport.wechat.Command; | ||||
| import me.chanjar.weixin.common.session.WxSession; | ||||
| 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; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
| @@ -29,48 +21,30 @@ import java.io.PrintWriter; | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| @WebServlet(name = "TicketSubmit", urlPatterns = "/api/ticketsubmit", loadOnStartup = 23) | ||||
| public class TicketSubmit extends HttpServlet { | ||||
| public class TicketSubmit extends API { | ||||
|  | ||||
| 	private Gson gson = SQLCore.gson; | ||||
|  | ||||
| 	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		doGet(request, response); | ||||
| 	public TicketSubmit() { | ||||
| 		url = "/api/ticketsubmit"; | ||||
| 		access = Access.USER; | ||||
| 		authorize = Command.SUBMIT; | ||||
| 	} | ||||
|  | ||||
| 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		request.setCharacterEncoding("utf-8"); | ||||
| 		response.setCharacterEncoding("utf-8"); | ||||
| 		response.addHeader("Content-type", "application/json;charset=utf-8"); | ||||
| 		PrintWriter out = response.getWriter(); | ||||
| 		String json = gson.toJson(submit(request)); | ||||
| 		out.println(ParseUtil.parseJsonP(request, json)); | ||||
| 		out.close(); | ||||
| 	} | ||||
|  | ||||
| 	private Response submit(HttpServletRequest request) { | ||||
| 		String desc = request.getParameter("desc"); | ||||
| 	@Override | ||||
| 	protected Object process(HttpServletRequest req, WxSession session) throws Exception { | ||||
| 		String desc = req.getParameter("desc"); | ||||
| 		if (desc == null || desc.isEmpty()) { | ||||
| 			return new Response(Response.ResponseCode.PARAMETER_REQUIRED); | ||||
| 			return Error.PARAMETER_REQUIRED; | ||||
| 		} | ||||
| 		if (desc.length() > Settings.MAX_DESC_LENGTH) { | ||||
| 			return new Response(Response.ResponseCode.LENGTH_LIMIT_EXCEEDED); | ||||
| 			return Error.LENGTH_LIMIT_EXCEEDED; | ||||
| 		} | ||||
|  | ||||
| 		try (Session s = SQLCore.sf.openSession()) { | ||||
|  | ||||
| 			WxSession session = Checker.isAuthorized(request, Command.SUBMIT); | ||||
| 			if (session == null) { | ||||
| 				return new Response(Response.ResponseCode.UNAUTHORIZED); | ||||
| 			} | ||||
| 			User u = (User) session.getAttribute(Attribute.USER); | ||||
| 			if (u == null) return new Response(Response.ResponseCode.UNAUTHORIZED); | ||||
|  | ||||
| 			if (TableTicket.hasOpen(u)) { | ||||
| 				session.invalidate(); | ||||
| 				return new Response(Response.ResponseCode.ALREADY_SUBMITTED); | ||||
| 				return Error.ALREADY_SUBMITTED; | ||||
| 			} | ||||
|  | ||||
| 			Ticket t = new Ticket(); | ||||
| 			t.setUser(u); | ||||
| 			t.setDescription(desc); | ||||
| @@ -79,17 +53,7 @@ public class TicketSubmit extends HttpServlet { | ||||
| 			s.save(t); | ||||
| 			s.getTransaction().commit(); | ||||
| 			session.invalidate(); | ||||
| 			return new Response(Response.ResponseCode.OK, t); | ||||
| 		} catch (NumberFormatException e) { | ||||
| 			return new Response(Response.ResponseCode.ILLEGAL_PARAMETER); | ||||
| 		} catch (HibernateException e) { | ||||
| 			e.printStackTrace(); | ||||
| 			return new Response(Response.ResponseCode.DATABASE_ERROR, e.getMessage()); | ||||
| 		} catch (Exception e) { | ||||
| 			e.printStackTrace(); | ||||
| 			return new Response(Response.ResponseCode.INTERNAL_ERROR, e.getMessage()); | ||||
| 			return Error.OK; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -14,6 +14,7 @@ import static love.sola.netsupport.config.Lang.lang; | ||||
|  */ | ||||
| public class Access { | ||||
|  | ||||
| 	public static final int GOD_MODE = -1; | ||||
| 	public static final int ROOT = 0; | ||||
| 	public static final int MANAGER = 1; | ||||
| 	public static final int CO_MANAGER = 2; | ||||
| @@ -23,7 +24,9 @@ public class Access { | ||||
| 	public static final int ELDER = 6; | ||||
| 	public static final int MEMBER = 7; | ||||
| 	public static final int PRE_MEMBER = 8; | ||||
| 	public static final int NOLOGIN = 9; | ||||
| 	public static final int NO_LOGIN = 9; | ||||
| 	public static final int USER = 10; | ||||
| 	public static final int GUEST = 11; | ||||
|  | ||||
| 	public static final Map<Integer, String> inverseMap = new HashMap<>(); | ||||
|  | ||||
|   | ||||
| @@ -35,7 +35,7 @@ public class LoginHandler implements WxMpMessageHandler { | ||||
| 			Operator operator = TableOperator.get(wxMessage.getFromUserName()); | ||||
| 			if (operator == null) | ||||
| 				out.content(lang("Not_Operator")); | ||||
| 			else if (operator.getAccess() == Access.NOLOGIN) { | ||||
| 			else if (operator.getAccess() >= Access.NO_LOGIN) { | ||||
| 				out.content(lang("No_Login")); | ||||
| 			} else { | ||||
| 				String id = WechatSession.genId(); | ||||
|   | ||||
| @@ -31,7 +31,7 @@ public class OperatorInfoHandler implements WxMpMessageHandler { | ||||
| 			Operator op = TableOperator.get(wxMessage.getFromUserName()); | ||||
| 			if (op == null) | ||||
| 				out.content(lang("Not_Operator")); | ||||
| 			else if (op.getAccess() == Access.NOLOGIN) { | ||||
| 			else if (op.getAccess() >= Access.NO_LOGIN) { | ||||
| 				out.content(lang("No_Login")); | ||||
| 			} else { | ||||
| 				out.content(format("Operator_Info", op.getId(), op.getName(), op.getAccess(), op.getBlock(), op.getWeek())); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Sola
					Sola