generated from root/miduo_server
409 lines
18 KiB
Java
409 lines
18 KiB
Java
package com.ljsd.controller;
|
||
|
||
import com.ljsd.redis.RedisKey;
|
||
import com.ljsd.service.*;
|
||
import com.ljsd.util.AppConstans;
|
||
import com.ljsd.util.BaseGlobal;
|
||
import com.mongodb.BasicDBObject;
|
||
import com.mongodb.DBObject;
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
|
||
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.HashMap;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.Random;
|
||
|
||
public class GetUserController extends HttpServlet {
|
||
private final static String _COLLECTION_NAME = "user_info";
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(GetUserController.class);
|
||
|
||
public static Map<String, VerifyService> serviceMap = new HashMap<>();
|
||
public static void initHandler(){
|
||
//初始化所有需要的验证方式
|
||
// serviceMap.put("MHT", new LdVerifyService("MHT"));
|
||
// serviceMap.put("XP", new XPVerifyService("XP"));
|
||
// serviceMap.put("CH",new CaohuaVerifyService("CH"));
|
||
// serviceMap.put("QIANYOU", new QiYouVerifyService("huangjiayl&qypay"));
|
||
// serviceMap.put("YX", new YxylVerifyService("YX"));
|
||
// serviceMap.put("DY", new V3367VerifyService("DY"));
|
||
// serviceMap.put("QUICK2", new QuickVerifyService2("QUICK2"));
|
||
// serviceMap.put("YGXQ", new YouguXiaoqiVerifyService("YGXQ"));
|
||
// serviceMap.put("DUOYOU", new DuoYouVerifyService("DUOYOU"));
|
||
// serviceMap.put("AIWAN", new AiWanVerifyService("AIWAN"));
|
||
// serviceMap.put("HF", new HFVerifyService("HF"));
|
||
// serviceMap.put("QUICK3", new QuickVerifyService3("QUICK3"));
|
||
// 越南Gamota
|
||
serviceMap.put("GAMOTA", new VietnamGamotaVerifyService("GAMOTA"));
|
||
// QUICK渠道
|
||
serviceMap.put("QUICK", new QuickVerifyService("QUICK"));
|
||
// 海贼
|
||
serviceMap.put("ONEPIECE01", new QuickOnePiece01VerifyService("ONEPIECE01"));
|
||
serviceMap.put("OPXQ", new QuickopxqVerifyService("OPXQ"));
|
||
serviceMap.put("OPAZ", new QuickopazVerifyService("OPAZ"));
|
||
serviceMap.put("OPAQ", new QuickopaqVerifyService("OPAQ"));
|
||
serviceMap.put("OPYY", new QuickopyyVerifyService("OPYY"));
|
||
// 悠谷
|
||
serviceMap.put("YOUGU", new YouGuVerifyService("YG"));
|
||
serviceMap.put("XQDC", new XiaoqiDiscountVerifyService("XQDC"));
|
||
serviceMap.put("YGDC", new YouguDiscountVerifyService("YGDC"));
|
||
serviceMap.put("XQOT", new XqTenthVerifyService("XQOT"));
|
||
serviceMap.put("AIQU", new AiquVerifyService("AIQU"));
|
||
serviceMap.put("XQFP", new XqfpVerifyService("XQFP"));
|
||
serviceMap.put("ZZXX", new ZzxxVerifyService("ZZXX"));
|
||
serviceMap.put("ZZXXXQ", new ZzxxxqVerifyService("ZZXXXQ"));
|
||
serviceMap.put("AQLM", new AQLMVerifyService("AQLM"));
|
||
serviceMap.put("AQLMYJ", new YgAqlmYjVerifyService("AQLMYJ"));
|
||
serviceMap.put("GOHU", new GonghuiVerifyService("GOHU"));
|
||
|
||
// 自在修仙西游
|
||
serviceMap.put("ZXXQ01", new Zzxxxq01VerifyService("ZXXQ01"));
|
||
serviceMap.put("ZXQD101", new ZzxxQd101VerifyService("ZXQD101"));
|
||
serviceMap.put("ZXQD501", new ZzxxQd501VerifyService("ZXQD501"));
|
||
serviceMap.put("ZXQDB01", new ZzxxQdb01VerifyService("ZXQDB01"));
|
||
|
||
serviceMap.put("XYZB", new QuickxyzbVerifyService("XYZB"));
|
||
serviceMap.put("XYZB02", new Quickxyzb02VerifyService("XYZB02"));
|
||
serviceMap.put("WYDDS", new EightUVerifyService("WYDDS"));
|
||
serviceMap.put("U1GAME", new U1GameVerifyService("U1GAME"));
|
||
serviceMap.put("FENGTI", new FengTiVerifyService("FENGTI"));
|
||
serviceMap.put("XYZB03", new Quickxyzb03VerifyService("XYZB03"));
|
||
serviceMap.put("XYZB04", new Quickxyzb04VerifyService("XYZB04"));
|
||
//太初行027渠道
|
||
serviceMap.put("TCX027", new Tcx027VerifyService("TCX027"));
|
||
serviceMap.put("TCX02702", new Tcx02702VerifyService("TCX02702"));
|
||
serviceMap.put("TCX02703", new Tcx02703VerifyService("TCX02703"));
|
||
//太初行quick直播渠道
|
||
serviceMap.put("TCXQUZB", new QuickTcxZbVerifyService("TCXQUZB"));
|
||
//太初行quick直播2渠道
|
||
serviceMap.put("TCXQUZB2", new QuickTcxZb2VerifyService("TCXQUZB2"));
|
||
//西游悠谷直播1
|
||
serviceMap.put("XYYGZB01", new XiyouYouGuZhiboVerifyService("XYYGZB01"));
|
||
}
|
||
|
||
|
||
public GetUserController() {
|
||
super();
|
||
}
|
||
|
||
@Override
|
||
public void destroy() {
|
||
super.destroy();
|
||
}
|
||
|
||
|
||
/**
|
||
* openId 账号id
|
||
* version
|
||
* serverId
|
||
* token
|
||
* platform android、ios 3 跳过验证
|
||
* admin
|
||
* gid pid
|
||
*---verty--
|
||
* openid 和token校验
|
||
* ---return--
|
||
* uid(openid + serverid + platid)
|
||
*/
|
||
@Override
|
||
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||
DBObject res = new BasicDBObject();
|
||
String channel = request.getParameter("channel");
|
||
if(channel!= null){
|
||
if("TCX027".equals(channel) || "TCX02702".equals(channel) || "TCX02703".equals(channel)) {
|
||
this.do027Get(request, response);
|
||
return;
|
||
}
|
||
}
|
||
String openId = request.getParameter("openId");
|
||
if (openId == null || openId.isEmpty()) {
|
||
returnErrorToFront(res,response, "openId is empty");
|
||
return;
|
||
}
|
||
try {
|
||
String banInfo = BaseGlobal.getInstance().redisApp.get(RedisKey.Ban_Open_Id, openId, String.class, -1);
|
||
if (banInfo != null) {
|
||
String[] split = banInfo.split("\\|");
|
||
long currentTimeMillis = System.currentTimeMillis();
|
||
if (Long.parseLong(split[0]) > currentTimeMillis) {
|
||
returnErrorToFront(res,response,banInfo);
|
||
LOGGER.info("doGet can not login Ban_Open_Id=>{} openId=>{} banInfo==null ? {}", RedisKey.Ban_Open_Id, openId, banInfo);
|
||
return;
|
||
} else {
|
||
BaseGlobal.getInstance().redisApp.del(RedisKey.Ban_Open_Id, openId);
|
||
}
|
||
}
|
||
} catch (Exception e) {
|
||
LOGGER.error("登陆报错:{}",e.getMessage(),e);
|
||
}
|
||
String serverId = request.getParameter("serverId");
|
||
if (serverId == null || serverId.isEmpty()) {
|
||
returnErrorToFront(res,response,"serverId is empty");
|
||
return;
|
||
}
|
||
String token = request.getParameter("token");
|
||
if (token == null || token.isEmpty()) {
|
||
returnErrorToFront(res,response,"token is empty");
|
||
return;
|
||
}
|
||
//平台类型
|
||
String platform = request.getParameter("platform");
|
||
if (platform == null || platform.isEmpty()) {
|
||
returnErrorToFront(res,response,"platform is empty");
|
||
return;
|
||
}
|
||
//平台类型
|
||
String admin = request.getParameter("admin");
|
||
if(admin == null){
|
||
admin = "";;
|
||
}
|
||
//gid
|
||
String gid = request.getParameter("gid");
|
||
//pid
|
||
String pid = request.getParameter("pid");
|
||
String sub_channel = request.getParameter("sub_channel");
|
||
LOGGER.info("the opendId = {},token={},platform={} admin=>{} channel=>{} sub_channel={}",openId,token,platform,admin,channel,sub_channel);
|
||
try {
|
||
//根据前端发过来的种类进行校验
|
||
if(channel==null|| channel.isEmpty()){
|
||
channel="MHT";
|
||
}
|
||
boolean verify;
|
||
// 内网或者 游心 开天,不需要验证
|
||
if (AppConstans.appsecret.equals(admin) || "YX_YSL".equals(channel) || "KT".equals(channel)) {
|
||
verify = true;
|
||
} else {
|
||
verify = serviceMap.get(channel).verify(response, request, admin, platform, pid, openId, token);
|
||
}
|
||
if(!verify){
|
||
returnErrorToFront(res,response,"校验失败,请重新登录");
|
||
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);
|
||
|
||
DBObject serverDBObject = new BasicDBObject();
|
||
serverDBObject.put("server_id", serverId);
|
||
List<DBObject> server_info = BaseGlobal.getInstance().mongoDBPool.find("server_info", serverDBObject);
|
||
if(server_info.isEmpty()){
|
||
returnErrorToFront(res,response,"服务器信息获取错误");
|
||
return;
|
||
}
|
||
int uid;
|
||
//加分布式锁
|
||
List<DBObject> userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject);
|
||
if (userInfos.isEmpty()) {
|
||
try{
|
||
boolean getLock=false;
|
||
while(!getLock){
|
||
getLock = BaseGlobal.getInstance().redisApp.tryGetDistributedLock(RedisKey.REGISTER_LOCK, "", "get", 800);
|
||
if(getLock){
|
||
userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject);
|
||
//双重校验
|
||
if (userInfos.isEmpty()) {
|
||
DBObject next = server_info.iterator().next();
|
||
Object register_state = next.get("register_state");
|
||
if(register_state!=null&& register_state.equals("0")){
|
||
returnErrorToFront(res,response,"该服务器已停止注册,请选择其他服务器游戏");
|
||
return;
|
||
}
|
||
uid = BaseGlobal.getInstance().mongoDBPool.inc("uid") + 10000000;
|
||
dbObject.put("uid", uid);
|
||
dbObject.put("_id", uid);
|
||
userInfos.add(dbObject);
|
||
if (gid != null && !gid.isEmpty()) {
|
||
dbObject.put("gid", gid);
|
||
}
|
||
if (pid != null && !pid.isEmpty()) {
|
||
dbObject.put("pid", pid);
|
||
}
|
||
BaseGlobal.getInstance().mongoDBPool.save(_COLLECTION_NAME, dbObject);
|
||
}
|
||
}
|
||
}
|
||
}finally {
|
||
BaseGlobal.getInstance().redisApp.releaseDistributedLock(RedisKey.REGISTER_LOCK,"","get");
|
||
}
|
||
}
|
||
Random random = new Random();
|
||
int utoken = Math.abs(random.nextInt());
|
||
uid = (int) userInfos.get(0).get("uid");
|
||
res.put("uid", uid);
|
||
res.put("token", utoken);
|
||
res.put("errorCode", 0);
|
||
BaseGlobal.getInstance().redisApp.set(RedisKey.TOKEN, String.valueOf(uid), utoken, -1, false);
|
||
BaseGlobal.getInstance().redisApp.set(RedisKey.PIDGIDTEMP, String.valueOf(uid), pid+"#"+gid, -1, false);
|
||
PrintWriter out = response.getWriter();
|
||
out.print(res);
|
||
out.flush();
|
||
out.close();
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 027渠道要服务器获取openId
|
||
* @param request
|
||
* @param response
|
||
* @throws ServletException
|
||
* @throws IOException
|
||
*/
|
||
public void do027Get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||
DBObject res = new BasicDBObject();
|
||
String tranOpenId = request.getParameter("openId");
|
||
if (tranOpenId == null) {
|
||
returnErrorToFront(res,response, "openId is empty");
|
||
return;
|
||
}
|
||
String serverId = request.getParameter("serverId");
|
||
if (serverId == null || serverId.isEmpty()) {
|
||
returnErrorToFront(res,response,"serverId is empty");
|
||
return;
|
||
}
|
||
String token = request.getParameter("token");
|
||
if (token == null || token.isEmpty()) {
|
||
returnErrorToFront(res,response,"token is empty");
|
||
return;
|
||
}
|
||
//平台类型
|
||
String platform = request.getParameter("platform");
|
||
if (platform == null || platform.isEmpty()) {
|
||
returnErrorToFront(res,response,"platform is empty");
|
||
return;
|
||
}
|
||
//平台类型
|
||
String admin = request.getParameter("admin");
|
||
if(admin == null){
|
||
admin = "";
|
||
}
|
||
//gid
|
||
String gid = request.getParameter("gid");
|
||
//pid
|
||
String pid = request.getParameter("pid");
|
||
String channel = request.getParameter("channel");
|
||
String sub_channel = request.getParameter("sub_channel");
|
||
String openId = null;
|
||
LOGGER.info("the 027 get opendId = {},token={},platform={} admin=>{} channel=>{} sub_channel={}",tranOpenId,token,platform,admin,channel,sub_channel);
|
||
try {
|
||
//根据前端发过来的种类进行校验
|
||
if(channel==null|| channel.isEmpty()){
|
||
channel="MHT";
|
||
}
|
||
// 内网或者 游心 开天,不需要验证
|
||
if (AppConstans.appsecret.equals(admin) || "YX_YSL".equals(channel) || "KT".equals(channel)) {
|
||
openId = tranOpenId;
|
||
} else {
|
||
openId = serviceMap.get(channel).verifyGetOpenId(response, request, admin, platform, pid, tranOpenId, token);
|
||
}
|
||
if(openId == null){
|
||
returnErrorToFront(res,response,"validate error,login again");
|
||
return;
|
||
}
|
||
String banInfo = BaseGlobal.getInstance().redisApp.get(RedisKey.Ban_Open_Id, openId, String.class, -1);
|
||
if (banInfo != null) {
|
||
String[] split = banInfo.split("\\|");
|
||
long currentTimeMillis = System.currentTimeMillis();
|
||
if (Long.parseLong(split[0]) > currentTimeMillis) {
|
||
returnErrorToFront(res,response,banInfo);
|
||
LOGGER.info("doGet can not login Ban_Open_Id=>{} openId=>{} banInfo==null ? {}", RedisKey.Ban_Open_Id, openId, banInfo);
|
||
return;
|
||
} else {
|
||
BaseGlobal.getInstance().redisApp.del(RedisKey.Ban_Open_Id, openId);
|
||
}
|
||
}
|
||
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);
|
||
|
||
DBObject serverDBObject = new BasicDBObject();
|
||
serverDBObject.put("server_id", serverId);
|
||
List<DBObject> server_info = BaseGlobal.getInstance().mongoDBPool.find("server_info", serverDBObject);
|
||
if(server_info.isEmpty()){
|
||
returnErrorToFront(res,response,"服务器信息获取错误");
|
||
return;
|
||
}
|
||
int uid;
|
||
//加分布式锁
|
||
List<DBObject> userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject);
|
||
if (userInfos.isEmpty()) {
|
||
try{
|
||
boolean getLock=false;
|
||
while(!getLock){
|
||
getLock = BaseGlobal.getInstance().redisApp.tryGetDistributedLock(RedisKey.REGISTER_LOCK, "", "get", 800);
|
||
if(getLock){
|
||
userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject);
|
||
//双重校验
|
||
if (userInfos.isEmpty()) {
|
||
DBObject next = server_info.iterator().next();
|
||
Object register_state = next.get("register_state");
|
||
if(register_state!=null&& register_state.equals("0")){
|
||
returnErrorToFront(res,response,"该服务器已停止注册,请选择其他服务器游戏");
|
||
return;
|
||
}
|
||
uid = BaseGlobal.getInstance().mongoDBPool.inc("uid") + 10000000;
|
||
dbObject.put("uid", uid);
|
||
dbObject.put("_id", uid);
|
||
userInfos.add(dbObject);
|
||
if (gid != null && !gid.isEmpty()) {
|
||
dbObject.put("gid", gid);
|
||
}
|
||
if (pid != null && !pid.isEmpty()) {
|
||
dbObject.put("pid", pid);
|
||
}
|
||
BaseGlobal.getInstance().mongoDBPool.save(_COLLECTION_NAME, dbObject);
|
||
}
|
||
}
|
||
}
|
||
}finally {
|
||
BaseGlobal.getInstance().redisApp.releaseDistributedLock(RedisKey.REGISTER_LOCK,"","get");
|
||
}
|
||
}
|
||
Random random = new Random();
|
||
int utoken = Math.abs(random.nextInt());
|
||
uid = (int) userInfos.get(0).get("uid");
|
||
res.put("uid", uid);
|
||
res.put("token", utoken);
|
||
res.put("errorCode", 0);
|
||
BaseGlobal.getInstance().redisApp.set(RedisKey.TOKEN, String.valueOf(uid), utoken, -1, false);
|
||
BaseGlobal.getInstance().redisApp.set(RedisKey.PIDGIDTEMP, String.valueOf(uid), pid+"#"+gid, -1, false);
|
||
PrintWriter out = response.getWriter();
|
||
out.print(res);
|
||
out.flush();
|
||
out.close();
|
||
} catch (Exception e) {
|
||
LOGGER.error("登陆报错:{}",e.getMessage(),e);
|
||
}
|
||
}
|
||
|
||
private void returnErrorToFront(DBObject res,HttpServletResponse response,String errorReason) throws IOException {
|
||
res.put("errorCode", -1);
|
||
res.put("reason",errorReason);
|
||
PrintWriter out = response.getWriter();
|
||
out.print(res);
|
||
out.flush();
|
||
out.close();
|
||
}
|
||
|
||
@Override
|
||
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
||
throws ServletException, IOException {
|
||
this.doGet(request, response);
|
||
|
||
}
|
||
|
||
}
|