mirror of
				https://github.com/ZSCNetSupportDept/WechatTicketSystem.git
				synced 2025-10-31 10:26:19 +08:00 
			
		
		
		
	using maven build tools instead
This commit is contained in:
		
							
								
								
									
										35
									
								
								src/main/java/love/sola/netsupport/Index.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/main/java/love/sola/netsupport/Index.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,35 @@ | ||||
| package love.sola.netsupport; | ||||
|  | ||||
| import javax.servlet.ServletException; | ||||
| import javax.servlet.annotation.WebServlet; | ||||
| import javax.servlet.http.HttpServlet; | ||||
| import javax.servlet.http.HttpServletRequest; | ||||
| import javax.servlet.http.HttpServletResponse; | ||||
| import java.io.IOException; | ||||
| import java.io.PrintWriter; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
|  * Created by Sola on 2014/8/4. | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| @WebServlet(name = "Index",urlPatterns = "/",loadOnStartup = 1) | ||||
| public class Index extends HttpServlet { | ||||
|  | ||||
|  | ||||
| 	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		doGet(request, response); | ||||
| 	} | ||||
|  | ||||
| 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		request.setCharacterEncoding("utf-8"); | ||||
| 		response.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("For any problem, Please contact loli@sola.love."); | ||||
| 		out.close(); | ||||
| 	} | ||||
|  | ||||
| } | ||||
							
								
								
									
										67
									
								
								src/main/java/love/sola/netsupport/api/GetUser.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								src/main/java/love/sola/netsupport/api/GetUser.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,67 @@ | ||||
| package love.sola.netsupport.api; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| import love.sola.netsupport.enums.ResponseCode; | ||||
| import love.sola.netsupport.pojo.User; | ||||
| import love.sola.netsupport.sql.TableUser; | ||||
|  | ||||
| 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; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
|  * Created by Sola on 2014/8/20. | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| @WebServlet(name = "GetUser",urlPatterns = "/api/getuser",loadOnStartup = 1) | ||||
| public class GetUser extends HttpServlet { | ||||
|  | ||||
| 	private Gson gson = null; | ||||
|  | ||||
| 	@Override | ||||
| 	public void init(ServletConfig config) throws ServletException { | ||||
| 		super.init(config); | ||||
| 		gson = new Gson(); | ||||
| 	} | ||||
|  | ||||
| 	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		doGet(request, response); | ||||
| 	} | ||||
| 	 | ||||
| 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		request.setCharacterEncoding("utf-8"); | ||||
| 		response.setCharacterEncoding("utf-8"); | ||||
| 		response.addHeader("Content-type", "text/json;charset=utf-8"); | ||||
| 		PrintWriter out = response.getWriter(); | ||||
| 		String id = request.getParameter("id"); | ||||
| 		String name = request.getParameter("name"); | ||||
| 		if ((id == null || id.isEmpty()) && (name == null || name.isEmpty())) { | ||||
| 			out.println(gson.toJson(new Response(ResponseCode.PARAMETER_REQUIRED))); | ||||
| 		} else if (id != null) { | ||||
| 			try { | ||||
| 				User u = TableUser.getUserById(Integer.parseInt(id)); | ||||
| 				if (u == null) | ||||
| 					out.println(gson.toJson(new Response(ResponseCode.USER_NOT_FOUND))); | ||||
| 				else | ||||
| 					out.println(gson.toJson(new Response(ResponseCode.OK, u))); | ||||
| 			} catch (NumberFormatException e) { | ||||
| 				out.println(gson.toJson(new Response(ResponseCode.ILLEGAL_PARAMETER))); | ||||
| 			} | ||||
| 		} else { | ||||
| 			User u = TableUser.getUserByName(name); | ||||
| 			if (u == null) | ||||
| 				out.println(gson.toJson(new Response(ResponseCode.USER_NOT_FOUND))); | ||||
| 			else | ||||
| 				out.println(gson.toJson(new Response(ResponseCode.OK, u))); | ||||
| 		} | ||||
| 		out.close(); | ||||
| 	} | ||||
|  | ||||
| } | ||||
							
								
								
									
										29
									
								
								src/main/java/love/sola/netsupport/api/Response.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/main/java/love/sola/netsupport/api/Response.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,29 @@ | ||||
| package love.sola.netsupport.api; | ||||
|  | ||||
| import lombok.AllArgsConstructor; | ||||
| import love.sola.netsupport.enums.ResponseCode; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
|  * 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.info; | ||||
| 		this.result = result; | ||||
| 	} | ||||
|  | ||||
| } | ||||
							
								
								
									
										56
									
								
								src/main/java/love/sola/netsupport/enums/ISPType.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								src/main/java/love/sola/netsupport/enums/ISPType.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,56 @@ | ||||
| package love.sola.netsupport.enums; | ||||
|  | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
|  * Created by Sola on 2014/8/20. | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| public enum ISPType { | ||||
|  | ||||
| 	TELECOM("Telecom", 1), | ||||
| 	UNICOM("Unicom", 2), | ||||
| 	CHINAMOBILE("ChinaMobile", 3),; | ||||
|  | ||||
| 	private static final Map<String, ISPType> NAME_MAP = new HashMap<>(); | ||||
| 	private static final Map<Integer, ISPType> ID_MAP = new HashMap<>(); | ||||
|  | ||||
| 	static { | ||||
| 		for (ISPType type : values()) { | ||||
| 			if (type.name != null) { | ||||
| 				NAME_MAP.put(type.name.toLowerCase(), type); | ||||
| 			} | ||||
| 			if (type.id > 0) { | ||||
| 				ID_MAP.put(type.id, type); | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	public final String name; | ||||
| 	public final int id; | ||||
|  | ||||
| 	ISPType(String name, int id) { | ||||
| 		this.name = name; | ||||
| 		this.id = id; | ||||
| 	} | ||||
|  | ||||
| 	public static ISPType fromName(String name) { | ||||
| 		if (name == null) { | ||||
| 			return null; | ||||
| 		} | ||||
| 		return NAME_MAP.get(name.toLowerCase()); | ||||
| 	} | ||||
|  | ||||
| 	public static ISPType fromId(int id) { | ||||
| 		return ID_MAP.get(id); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		return name; | ||||
| 	} | ||||
|  | ||||
| } | ||||
							
								
								
									
										47
									
								
								src/main/java/love/sola/netsupport/enums/ResponseCode.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								src/main/java/love/sola/netsupport/enums/ResponseCode.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | ||||
| package love.sola.netsupport.enums; | ||||
|  | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
|  * Created by Sola on 2015/11/6. | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| public enum ResponseCode { | ||||
|  | ||||
| 	OK(0, "OK"), | ||||
| 	PARAMETER_REQUIRED(-1, "Parameter Required"), | ||||
| 	ILLEGAL_PARAMETER(-2, "Illegal parameter"), | ||||
| 	USER_NOT_FOUND(-11, "User not found"), | ||||
| 	; | ||||
|  | ||||
| 	private static final Map<Integer, ResponseCode> ID_MAP = new HashMap<>(); | ||||
|  | ||||
| 	static { | ||||
| 		for (ResponseCode type : values()) { | ||||
| 			if (type.id > 0) { | ||||
| 				ID_MAP.put(type.id, type); | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	public final String info; | ||||
| 	public final int id; | ||||
|  | ||||
| 	ResponseCode(int id, String info) { | ||||
| 		this.info = info; | ||||
| 		this.id = id; | ||||
| 	} | ||||
|  | ||||
| 	public static ResponseCode fromId(int id) { | ||||
| 		return ID_MAP.get(id); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		return info; | ||||
| 	} | ||||
|  | ||||
| } | ||||
							
								
								
									
										24
									
								
								src/main/java/love/sola/netsupport/pojo/User.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								src/main/java/love/sola/netsupport/pojo/User.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | ||||
| package love.sola.netsupport.pojo; | ||||
|  | ||||
| import lombok.AllArgsConstructor; | ||||
| import lombok.Data; | ||||
| import love.sola.netsupport.enums.ISPType; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
|  * Created by Sola on 2014/8/20. | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| @Data | ||||
| @AllArgsConstructor | ||||
| public class User { | ||||
|  | ||||
| 	private final int id; | ||||
| 	private final String name; | ||||
| 	private final long studentId; | ||||
| 	private String netAccount; | ||||
| 	private ISPType isp; | ||||
| 	private String wechatId; | ||||
|  | ||||
| } | ||||
							
								
								
									
										32
									
								
								src/main/java/love/sola/netsupport/sql/SQLCore.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/main/java/love/sola/netsupport/sql/SQLCore.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | ||||
| package love.sola.netsupport.sql; | ||||
|  | ||||
| import javax.naming.InitialContext; | ||||
| import javax.sql.DataSource; | ||||
| import java.sql.Connection; | ||||
| import java.sql.SQLException; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
|  * Created by Sola on 2014/8/20. | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| public class SQLCore { | ||||
|  | ||||
| 	public static DataSource ds; | ||||
|  | ||||
| 	static { | ||||
| 		try { | ||||
| 			InitialContext ic = new InitialContext(); | ||||
| 			ds = (DataSource) ic.lookup("java:comp/env/jdbc/netsupport"); | ||||
| 			ds.setLoginTimeout(3); | ||||
| 		} catch (Exception e) { | ||||
| 			e.printStackTrace(); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	public static void free(Connection conn) { | ||||
| 		if (conn != null) try { conn.close(); } catch (SQLException e) { } | ||||
| 	} | ||||
|  | ||||
| } | ||||
							
								
								
									
										14
									
								
								src/main/java/love/sola/netsupport/sql/TableConfig.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/main/java/love/sola/netsupport/sql/TableConfig.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,14 @@ | ||||
| package love.sola.netsupport.sql; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
|  * Created by Sola on 2015/11/10. | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| public class TableConfig { | ||||
|  | ||||
|  | ||||
| 	 | ||||
|  | ||||
| } | ||||
							
								
								
									
										66
									
								
								src/main/java/love/sola/netsupport/sql/TableUser.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								src/main/java/love/sola/netsupport/sql/TableUser.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,66 @@ | ||||
| package love.sola.netsupport.sql; | ||||
|  | ||||
| import love.sola.netsupport.enums.ISPType; | ||||
| import love.sola.netsupport.pojo.User; | ||||
|  | ||||
| import java.sql.Connection; | ||||
| import java.sql.PreparedStatement; | ||||
| import java.sql.ResultSet; | ||||
| import java.sql.SQLException; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
|  * Created by Sola on 2015/11/10. | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| public class TableUser extends SQLCore { | ||||
|  | ||||
| 	public static final String COLUMN_ID = "id"; | ||||
| 	public static final String COLUMN_NAME = "name"; | ||||
| 	public static final String COLUMN_STUDENT_ID = "studentid"; | ||||
| 	public static final String COLUMN_NET_ACCOUNT = "netaccount"; | ||||
| 	public static final String COLUMN_ISP = "isp"; | ||||
| 	public static final String COLUMN_WECHAT = "wechat"; | ||||
|  | ||||
| 	public static User getUserByName(String name) { | ||||
| 		Connection conn = null; | ||||
| 		try { | ||||
| 			conn = ds.getConnection(); | ||||
| 			PreparedStatement ps = conn.prepareStatement("SELECT * FROM user_info WHERE name=?"); | ||||
| 			ps.setString(1, name); | ||||
| 			ResultSet rs = ps.executeQuery(); | ||||
| 			if (rs.next()) { | ||||
| 				return new User(rs.getInt(COLUMN_ID), | ||||
| 						rs.getString(COLUMN_NAME), | ||||
| 						rs.getLong(COLUMN_STUDENT_ID), | ||||
| 						rs.getString(COLUMN_NET_ACCOUNT), | ||||
| 						ISPType.fromId(rs.getInt(COLUMN_ISP)), | ||||
| 						rs.getString(COLUMN_WECHAT)); | ||||
| 			} | ||||
| 		} catch (SQLException e) { | ||||
| 		} finally { free(conn); } | ||||
| 		return null; | ||||
| 	} | ||||
|  | ||||
| 	public static User getUserById(int id) { | ||||
| 		Connection conn = null; | ||||
| 		try { | ||||
| 			conn = ds.getConnection(); | ||||
| 			PreparedStatement ps = conn.prepareStatement("SELECT * FROM user_info WHERE id=?"); | ||||
| 			ps.setInt(1, id); | ||||
| 			ResultSet rs = ps.executeQuery(); | ||||
| 			if (rs.next()) { | ||||
| 				return new User(rs.getInt(COLUMN_ID), | ||||
| 						rs.getString(COLUMN_NAME), | ||||
| 						rs.getLong(COLUMN_STUDENT_ID), | ||||
| 						rs.getString(COLUMN_NET_ACCOUNT), | ||||
| 						ISPType.fromId(rs.getInt(COLUMN_ISP)), | ||||
| 						rs.getString(COLUMN_WECHAT)); | ||||
| 			} | ||||
| 		} catch (SQLException e) { | ||||
| 		} finally { free(conn); } | ||||
| 		return null; | ||||
| 	} | ||||
|  | ||||
| } | ||||
							
								
								
									
										100
									
								
								src/main/java/love/sola/netsupport/wechat/WxMpServlet.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										100
									
								
								src/main/java/love/sola/netsupport/wechat/WxMpServlet.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,100 @@ | ||||
| package love.sola.netsupport.wechat; | ||||
|  | ||||
| import me.chanjar.weixin.common.util.StringUtils; | ||||
| import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage; | ||||
| import me.chanjar.weixin.mp.api.WxMpMessageRouter; | ||||
| import me.chanjar.weixin.mp.api.WxMpService; | ||||
| import me.chanjar.weixin.mp.api.WxMpServiceImpl; | ||||
| import me.chanjar.weixin.mp.bean.WxMpXmlMessage; | ||||
| import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage; | ||||
|  | ||||
| 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; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
|  * Created by Sola on 2015/11/2. | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| @WebServlet(name = "WxMpServlet", urlPatterns = "/wechattest", loadOnStartup = 2) | ||||
| public class WxMpServlet extends HttpServlet { | ||||
|  | ||||
| 	public static WxMpServlet instance; | ||||
| 	protected WxMpInMemoryConfigStorage config; | ||||
| 	protected WxMpService wxMpService; | ||||
| 	protected WxMpMessageRouter wxMpMessageRouter; | ||||
|  | ||||
| 	public WxMpServlet() { | ||||
| 		instance = this; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public void init() throws ServletException { | ||||
| 		super.init(); | ||||
|  | ||||
| 		config = new WxMpInMemoryConfigStorage(); | ||||
| 		config.setAppId("*****"); | ||||
| 		config.setSecret("*****"); | ||||
| 		config.setToken("*****"); | ||||
| //		config.setAesKey("SolaAES"); | ||||
|  | ||||
| 		wxMpService = new WxMpServiceImpl(); | ||||
| 		wxMpService.setWxMpConfigStorage(config); | ||||
| 		wxMpMessageRouter = new WxMpMessageRouter(wxMpService); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	protected void doPost(HttpServletRequest request, HttpServletResponse response) | ||||
| 			throws ServletException, IOException { | ||||
|  | ||||
| 		response.setContentType("text/html;charset=utf-8"); | ||||
| 		response.setStatus(HttpServletResponse.SC_OK); | ||||
|  | ||||
| 		String signature = request.getParameter("signature"); | ||||
| 		String nonce = request.getParameter("nonce"); | ||||
| 		String timestamp = request.getParameter("timestamp"); | ||||
|  | ||||
| 		if (!wxMpService.checkSignature(timestamp, nonce, signature)) { | ||||
| 			// Signature fail | ||||
| 			response.getWriter().println("Access Denied"); | ||||
| 			return; | ||||
| 		} | ||||
|  | ||||
| 		String echostr = request.getParameter("echostr"); | ||||
| 		if (StringUtils.isNotBlank(echostr)) { | ||||
| 			// validate request | ||||
| 			response.getWriter().println(echostr); | ||||
| 			return; | ||||
| 		} | ||||
|  | ||||
| 		String encryptType = StringUtils.isBlank(request.getParameter("encrypt_type")) ? "raw" : request.getParameter("encrypt_type"); | ||||
|  | ||||
| 		if ("raw".equals(encryptType)) { | ||||
| 			WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(request.getInputStream()); | ||||
| 			WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage); | ||||
| 			response.getWriter().write(outMessage.toXml()); | ||||
| 			return; | ||||
| 		} | ||||
|  | ||||
| 		if ("aes".equals(encryptType)) { | ||||
| 			String msgSignature = request.getParameter("msg_signature"); | ||||
| 			WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(request.getInputStream(), config, timestamp, nonce, msgSignature); | ||||
| 			WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage); | ||||
| 			response.getWriter().write(outMessage.toEncryptedXml(config)); | ||||
| 			return; | ||||
| 		} | ||||
|  | ||||
| 		response.getWriter().println("Unknown encrypt-type"); | ||||
| 		return; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||||
| 		doPost(req, resp); | ||||
| 	} | ||||
| } | ||||
| @@ -0,0 +1,28 @@ | ||||
| package love.sola.netsupport.wechat.handler; | ||||
|  | ||||
| import me.chanjar.weixin.mp.api.WxMpMessageHandler; | ||||
| import me.chanjar.weixin.mp.api.WxMpMessageRouter; | ||||
|  | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
|  * Created by Sola on 2015/11/5. | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| public class HandlerList { | ||||
|  | ||||
| 	public static Map<String, Class<? extends WxMpMessageHandler>> handlers = new HashMap<>(); | ||||
|  | ||||
| 	static { | ||||
| 		handlers.put("Register", RegisterHandler.class); | ||||
| 	} | ||||
|  | ||||
| 	public void init(WxMpMessageRouter router) { | ||||
|  | ||||
| 	} | ||||
|  | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,33 @@ | ||||
| package love.sola.netsupport.wechat.handler; | ||||
|  | ||||
| import me.chanjar.weixin.common.exception.WxErrorException; | ||||
| import me.chanjar.weixin.common.session.WxSessionManager; | ||||
| import me.chanjar.weixin.mp.api.WxMpMessageHandler; | ||||
| import me.chanjar.weixin.mp.api.WxMpMessageRouter; | ||||
| import me.chanjar.weixin.mp.api.WxMpService; | ||||
| import me.chanjar.weixin.mp.bean.WxMpXmlMessage; | ||||
| import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage; | ||||
|  | ||||
| import java.util.Map; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
|  * Created by Sola on 2015/11/4. | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| public class RegisterHandler implements WxMpMessageHandler { | ||||
|  | ||||
|  | ||||
| 	public RegisterHandler(WxMpMessageRouter router) { | ||||
| 		router.rule().async(false).rContent("^").handler(this).end(); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) | ||||
| 			throws WxErrorException { | ||||
| 		 | ||||
| 		return null; | ||||
| 	} | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,45 @@ | ||||
| package love.sola.netsupport.wechat.intercepter; | ||||
|  | ||||
| import com.google.common.cache.CacheBuilder; | ||||
| import com.google.common.cache.CacheLoader; | ||||
| import com.google.common.cache.LoadingCache; | ||||
| import me.chanjar.weixin.common.exception.WxErrorException; | ||||
| import me.chanjar.weixin.common.session.WxSessionManager; | ||||
| import me.chanjar.weixin.mp.api.WxMpMessageInterceptor; | ||||
| import me.chanjar.weixin.mp.api.WxMpService; | ||||
| import me.chanjar.weixin.mp.bean.WxMpXmlMessage; | ||||
|  | ||||
| import java.util.Map; | ||||
| import java.util.concurrent.TimeUnit; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
|  * Created by Sola on 2015/11/4. | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| public class CheckSpamInterceptor implements WxMpMessageInterceptor { | ||||
|  | ||||
| 	private static class ValueLoader extends CacheLoader<String, Long> { | ||||
| 		@Override | ||||
| 		public Long load(String key) throws Exception { | ||||
| 			return System.currentTimeMillis(); //TODO: CONFIGURATION | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	private static LoadingCache<String, Long> cache = CacheBuilder.newBuilder() | ||||
| 			.concurrencyLevel(4) | ||||
| 			.weakKeys() | ||||
| 			.maximumSize(4096) | ||||
| 			.expireAfterWrite(5, TimeUnit.SECONDS) | ||||
| 			.build(new ValueLoader()); | ||||
|  | ||||
| 	@Override | ||||
| 	public boolean intercept(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) throws WxErrorException { | ||||
| 		if (cache.getIfPresent(wxMessage.getFromUserName()) != null) { | ||||
|  | ||||
| 		} | ||||
| 		return false; | ||||
| 	} | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Sola
					Sola