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;
|
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;
|
2019-08-16 12:03:41 +08:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-12-26 14:42:05 +08:00
|
|
|
|
|
|
|
|
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-08-19 10:06:30 +08:00
|
|
|
import java.util.*;
|
2018-12-26 14:42:05 +08:00
|
|
|
|
|
|
|
|
public class GetUserController extends HttpServlet {
|
|
|
|
|
private final static String _COLLECTION_NAME = "user_info";
|
2019-08-16 12:03:41 +08:00
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(GetUserController.class);
|
2019-08-19 10:06:30 +08:00
|
|
|
public static int isTestLan=0;
|
|
|
|
|
|
|
|
|
|
public static void initLanState(){
|
|
|
|
|
Properties properties = BaseGlobal.getInstance().properties;
|
|
|
|
|
isTestLan = Integer.parseInt(properties.getProperty("isTestLan"));
|
|
|
|
|
}
|
2018-12-26 14:42:05 +08:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2019-08-16 12:03:41 +08:00
|
|
|
LOGGER.info("the opendId = {}",openId);
|
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()) {
|
2019-07-01 14:09:36 +08:00
|
|
|
response.sendError(400, "platform is empety");
|
2019-07-01 11:27:00 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2018-12-26 14:42:05 +08:00
|
|
|
try {
|
2019-08-19 10:06:30 +08:00
|
|
|
if(isTestLan==1){
|
|
|
|
|
boolean result = loginVerfifyByTestLan(openId, token);
|
|
|
|
|
if(!result){
|
|
|
|
|
response.sendError(400, "verify fail");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}else if(!"3".equals(platform)){
|
|
|
|
|
boolean result = loginVerfify(platform, openId, token);
|
|
|
|
|
if(!result){
|
|
|
|
|
response.sendError(400, "verify fail");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
response.setCharacterEncoding("UTF-8");
|
|
|
|
|
response.setContentType("application/json; charset=utf-8");
|
|
|
|
|
DBObject dbObject = new BasicDBObject();
|
|
|
|
|
dbObject.put("openId", openId);
|
|
|
|
|
dbObject.put("serverId", serverId);
|
|
|
|
|
dbObject.put("platform", platform);
|
|
|
|
|
|
2018-12-26 14:42:05 +08:00
|
|
|
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
|
|
|
|
2019-07-01 16:57:23 +08:00
|
|
|
public static boolean loginVerfify(String platForm,String openId,String token){
|
2019-07-01 11:27:00 +08:00
|
|
|
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>();
|
2019-07-01 16:57:23 +08:00
|
|
|
params.put("userid", openId);
|
2019-07-01 11:27:00 +08:00
|
|
|
params.put("token", token);
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2019-07-01 16:57:23 +08:00
|
|
|
sb.append("userid=").append(openId)
|
|
|
|
|
.append("token=").append(token).append(KTSDKConstans.appid).append(KTSDKConstans.appkey);
|
2019-07-01 11:27:00 +08:00
|
|
|
|
|
|
|
|
String sign = EncryptUtils.createMD5String(sb.toString());
|
|
|
|
|
params.put("sign", sign);
|
2019-07-01 16:57:23 +08:00
|
|
|
params.put("appid", KTSDKConstans.appid);
|
2019-07-01 11:27:00 +08:00
|
|
|
String loginResult = HttpUtils.doPost(loginUrl,params);
|
|
|
|
|
if(loginResult == null || loginResult.isEmpty()){
|
2019-07-01 16:57:23 +08:00
|
|
|
return false;
|
2019-07-01 11:27:00 +08:00
|
|
|
}
|
|
|
|
|
return parseLoginResult(loginResult);
|
|
|
|
|
}catch(Exception e){
|
2019-07-01 16:57:23 +08:00
|
|
|
return false;
|
2019-07-01 11:27:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-19 10:06:30 +08:00
|
|
|
public static boolean loginVerfifyByTestLan(String openId,String token) throws Exception {
|
|
|
|
|
String tokenInRedis = BaseGlobal.getInstance().redisApp.get(RedisKey.LOGIN_TOKEN, openId, String.class, -1);
|
|
|
|
|
return token.equals(tokenInRedis);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-01 11:27:00 +08:00
|
|
|
|
2019-07-01 16:57:23 +08:00
|
|
|
private static boolean parseLoginResult(String orderResult){
|
2019-07-01 11:27:00 +08:00
|
|
|
try {
|
2019-07-01 16:57:23 +08:00
|
|
|
JSONObject jsonObject = JSONObject.parseObject(orderResult);
|
|
|
|
|
int state = jsonObject.getIntValue("state");
|
|
|
|
|
if(state != 1){
|
|
|
|
|
return false;
|
2019-07-01 11:27:00 +08:00
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
2019-07-01 16:57:23 +08:00
|
|
|
return true;
|
2019-07-01 11:27:00 +08:00
|
|
|
}
|
|
|
|
|
|
2018-12-26 14:42:05 +08:00
|
|
|
}
|