2018-12-26 14:42:05 +08:00
|
|
|
package com.ljsd.controller;
|
|
|
|
|
|
|
|
|
|
import com.ljsd.redis.RedisKey;
|
|
|
|
|
import com.ljsd.util.BaseGlobal;
|
|
|
|
|
import com.mongodb.BasicDBObject;
|
|
|
|
|
import com.mongodb.DBObject;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
|
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;
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
|
|
public class GetUserController extends HttpServlet {
|
|
|
|
|
private final static String _COLLECTION_NAME = "user_info";
|
|
|
|
|
|
|
|
|
|
public GetUserController() {
|
|
|
|
|
super();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void destroy() {
|
|
|
|
|
super.destroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
|
|
|
|
throws ServletException, IOException {
|
|
|
|
|
String openId = request.getParameter("openId");
|
2018-12-27 13:31:34 +08:00
|
|
|
if (openId == null || openId.isEmpty()) {
|
|
|
|
|
response.sendError(400, "openId is empety");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-12-26 14:42:05 +08:00
|
|
|
String serverId = request.getParameter("serverId");
|
2018-12-27 13:31:34 +08:00
|
|
|
if (serverId == null || serverId.isEmpty()) {
|
|
|
|
|
response.sendError(400, "serverId is empety");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-12-26 14:42:05 +08:00
|
|
|
DBObject dbObject = new BasicDBObject();
|
|
|
|
|
dbObject.put("openId", openId);
|
|
|
|
|
dbObject.put("serverId", serverId);
|
|
|
|
|
try {
|
|
|
|
|
int uid = 0;
|
2018-12-27 10:06:44 +08:00
|
|
|
List<DBObject> userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject);
|
2018-12-26 14:42:05 +08:00
|
|
|
DBObject res = new BasicDBObject();
|
|
|
|
|
if (userInfos.size() == 0) {
|
2018-12-27 13:31:34 +08:00
|
|
|
uid = BaseGlobal.getInstance().mongoDBPool.inc("uid") + 10000000;
|
2018-12-26 14:42:05 +08:00
|
|
|
dbObject.put("uid", uid);
|
2018-12-27 13:31:34 +08:00
|
|
|
dbObject.put("_id", uid);
|
2018-12-26 14:42:05 +08:00
|
|
|
userInfos.add(dbObject);
|
2018-12-27 10:06:44 +08:00
|
|
|
BaseGlobal.getInstance().mongoDBPool.save(_COLLECTION_NAME, dbObject);
|
2018-12-26 14:42:05 +08:00
|
|
|
}
|
|
|
|
|
Random random = new Random();
|
2019-01-04 18:44:38 +08:00
|
|
|
int token = Math.abs(random.nextInt());
|
2018-12-26 14:42:05 +08:00
|
|
|
uid = (int) userInfos.get(0).get("uid");
|
|
|
|
|
res.put("uid", uid);
|
2019-01-04 18:44:38 +08:00
|
|
|
res.put("token", token);
|
2018-12-27 10:06:44 +08:00
|
|
|
BaseGlobal.getInstance().redisApp.set(RedisKey.TOKEN, String.valueOf(uid), token, -1, false);
|
2018-12-26 14:42:05 +08:00
|
|
|
PrintWriter out = response.getWriter();
|
|
|
|
|
out.print(res);
|
|
|
|
|
out.flush();
|
|
|
|
|
out.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
|
|
|
|
throws ServletException, IOException {
|
|
|
|
|
this.doGet(request, response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|