2018-12-26 14:42:05 +08:00
|
|
|
package com.ljsd.controller;
|
|
|
|
|
|
2019-07-01 11:27:00 +08:00
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.ljsd.pojo.SuJieAccount;
|
2018-12-26 14:42:05 +08:00
|
|
|
import com.ljsd.redis.RedisKey;
|
|
|
|
|
import com.ljsd.util.BaseGlobal;
|
2019-07-01 11:27:00 +08:00
|
|
|
import com.ljsd.util.EncryptUtils;
|
|
|
|
|
import com.ljsd.util.HttpUtils;
|
|
|
|
|
import com.ljsd.util.KTSDKConstans;
|
2018-12-26 14:42:05 +08:00
|
|
|
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;
|
2019-07-01 11:27:00 +08:00
|
|
|
import java.util.HashMap;
|
2018-12-26 14:42:05 +08:00
|
|
|
import java.util.List;
|
2019-07-01 11:27:00 +08:00
|
|
|
import java.util.Map;
|
2018-12-26 14:42:05 +08:00
|
|
|
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;
|
|
|
|
|
}
|
2019-07-01 11:27:00 +08:00
|
|
|
String token = request.getParameter("token");
|
|
|
|
|
if (token == null || serverId.isEmpty()) {
|
|
|
|
|
response.sendError(400, "token is empety");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
String platform = request.getParameter("platform"); //平台类型
|
|
|
|
|
if (platform == null || platform.isEmpty()) {
|
|
|
|
|
response.sendError(400, "token is empety");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(!"3".equals(platform)){
|
|
|
|
|
SuJieAccount suJieAccount = loginVerfify(platform, openId, token);
|
|
|
|
|
if(suJieAccount == null){
|
|
|
|
|
response.sendError(400, "verify fail");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-26 14:42:05 +08:00
|
|
|
DBObject dbObject = new BasicDBObject();
|
|
|
|
|
dbObject.put("openId", openId);
|
|
|
|
|
dbObject.put("serverId", serverId);
|
2019-07-01 11:27:00 +08:00
|
|
|
dbObject.put("platform", platform);
|
2018-12-26 14:42:05 +08:00
|
|
|
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-07-01 11:27:00 +08:00
|
|
|
int utoken = Math.abs(random.nextInt());
|
2018-12-26 14:42:05 +08:00
|
|
|
uid = (int) userInfos.get(0).get("uid");
|
|
|
|
|
res.put("uid", uid);
|
2019-07-01 11:27:00 +08:00
|
|
|
res.put("token", utoken);
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-01 11:27:00 +08:00
|
|
|
|
|
|
|
|
public static SuJieAccount loginVerfify(String platForm,String openId,String token){
|
|
|
|
|
try{
|
|
|
|
|
String loginUrl = "";
|
|
|
|
|
if("1".equals(platForm)){
|
|
|
|
|
loginUrl = KTSDKConstans.androidVerify;
|
|
|
|
|
}else if("2".equals(platForm)){
|
|
|
|
|
loginUrl = KTSDKConstans.iosVerify;
|
|
|
|
|
}
|
|
|
|
|
Map<String, String> params = new HashMap<String, String>();
|
|
|
|
|
params.put("userID", openId);
|
|
|
|
|
params.put("token", token);
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
sb.append("userID=").append(openId)
|
|
|
|
|
.append("token=").append(token)
|
|
|
|
|
.append(KTSDKConstans.appid);
|
|
|
|
|
|
|
|
|
|
String sign = EncryptUtils.createMD5String(sb.toString());
|
|
|
|
|
params.put("sign", sign);
|
|
|
|
|
String loginResult = HttpUtils.doPost(loginUrl,params);
|
|
|
|
|
if(loginResult == null || loginResult.isEmpty()){
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return parseLoginResult(loginResult);
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static SuJieAccount parseLoginResult(String orderResult){
|
|
|
|
|
try {
|
|
|
|
|
SuJieAccount suJieAccount = (SuJieAccount)JSONObject.parse(orderResult);
|
|
|
|
|
int state = suJieAccount.getState();
|
|
|
|
|
if(state != 0){
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return suJieAccount;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-26 14:42:05 +08:00
|
|
|
}
|