miduo_login/src/main/java/com/ljsd/controller/GetUserController.java

214 lines
7.6 KiB
Java
Raw Normal View History

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;
2019-09-02 15:13:41 +08:00
import com.ljsd.util.*;
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();
}
2020-07-26 18:48:06 +08:00
/**
* openId id
* version
* serverId
* token
* platform androidios 3
* admin
* gid pid
*
*
*---verty--
* openid token
*
* ---return--
*
* uidopenid + serverid + platid
*
*/
2018-12-26 14:42:05 +08:00
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;
}
2020-04-26 17:22:52 +08:00
String version = request.getParameter("version");
if (version == null) {
version="未知版本号";
}
LOGGER.info("the opendId = {},isTestLan={},version{}",openId,isTestLan,version);
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;
}
2019-09-02 15:13:41 +08:00
String admin = request.getParameter("admin"); //平台类型
2019-10-09 19:55:23 +08:00
String gid = request.getParameter("gid"); //gid
String pid = request.getParameter("pid"); //pid
2019-09-02 15:13:41 +08:00
/*if (StringUtils.checkIsEmpty(admin)) {
response.sendError(400, "platform is empety");
return;
}*/
2019-09-19 15:20:15 +08:00
LOGGER.info("the opendId = {},token={},platform={}",openId,token,platform);
2018-12-26 14:42:05 +08:00
try {
2019-09-02 15:13:41 +08:00
if(!KTSDKConstans.appsecret.equals(admin)){
2020-07-26 18:48:06 +08:00
if(isTestLan==1){ //test
2019-09-02 15:13:41 +08:00
boolean result = loginVerfifyByTestLan(openId, token);
if(!result){
response.sendError(400, "verify fail");
2020-07-26 18:48:06 +08:00
LOGGER.error("test verify fail");
2019-09-02 15:13:41 +08:00
return;
}
2020-07-26 18:48:06 +08:00
}else if(!"3".equals(platform)){//正式 切不是pc
2020-08-18 01:16:49 +08:00
String sub_channel = request.getParameter("sub_channel");
boolean result =false;
if(sub_channel!=null&&!sub_channel.equals("")){
if(sub_channel.equals("1000")){
result = loginVerfify(pid, openId, token,MHTSDKConstans.rhappid);
}else {
result = loginVerfify(pid, openId, token,MHTSDKConstans.rhappidOnline);
}
}
2019-09-02 15:13:41 +08:00
if(!result){
response.sendError(400, "verify fail");
2020-07-26 18:48:06 +08:00
LOGGER.error("verify fail");
2019-09-02 15:13:41 +08:00
return;
}
2019-08-19 10:06:30 +08:00
}
}
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);
2019-10-09 19:55:23 +08:00
2019-08-19 10:06:30 +08:00
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);
2019-11-29 15:05:48 +08:00
if (gid != null && !gid.isEmpty()) {
dbObject.put("gid", gid);
}
if (pid != null && !pid.isEmpty()) {
dbObject.put("pid", pid);
}
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);
BaseGlobal.getInstance().redisApp.set(RedisKey.TOKEN, String.valueOf(uid), utoken, -1, false);
2019-11-29 15:35:10 +08:00
BaseGlobal.getInstance().redisApp.set(RedisKey.PIDGIDTEMP, String.valueOf(uid), pid+"#"+gid, -1, false);
2019-11-29 15:05:48 +08:00
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
2020-08-18 01:16:49 +08:00
public static boolean loginVerfify(String channelName,String openId,String token,String appId){
2019-07-01 11:27:00 +08:00
try{
2020-07-26 18:48:06 +08:00
String loginUrl = MHTSDKConstans.loginVerify;
2019-07-01 11:27:00 +08:00
Map<String, String> params = new HashMap<String, String>();
2020-07-26 18:48:06 +08:00
params.put("userToken", token);
2020-08-18 01:16:49 +08:00
params.put("appId", appId);
2020-07-26 18:48:06 +08:00
params.put("channelName", channelName);
params.put("uid", openId);
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);
2019-09-19 15:08:32 +08:00
LOGGER.info("tokenInRedis={}",tokenInRedis);
2019-08-19 10:06:30 +08:00
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);
2020-07-26 18:48:06 +08:00
int state = jsonObject.getIntValue("code");
if(state != 200){
String content = jsonObject.getString("message");
LOGGER.info("parseLoginResult content={}",content);
2019-07-01 16:57:23 +08:00
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
}
2020-07-26 18:48:06 +08:00
public static void main(String[] args) {
2020-08-18 01:16:49 +08:00
// boolean mht = loginVerfify("MHT", "2318137", "be385683efe228aadac0c8b5822a6fba");
2020-07-26 18:48:06 +08:00
}
2018-12-26 14:42:05 +08:00
}