mirror of
https://github.com/ZSCNetSupportDept/WechatTicketSystem.git
synced 2025-10-31 02:16:18 +08:00
using maven build tools instead
This commit is contained in:
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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user