You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

175 lines
6.8 KiB

package com.fr.plugin.web.hander;
import com.fanruan.api.log.LogKit;
import com.fr.decision.authority.AuthorityContext;
import com.fr.decision.authority.data.User;
import com.fr.decision.fun.impl.BaseHttpHandler;
import com.fr.decision.webservice.v10.login.LoginService;
import com.fr.general.FRLogger;
import com.fr.json.JSONObject;
import com.fr.log.FineLoggerFactory;
import com.fr.plugin.dao.MyEntityDao;
import com.fr.plugin.entitys.YTUserEntity;
import com.fr.plugin.entitys.YtOutputMyEntity;
import com.fr.plugin.utils.UserSync;
import com.fr.plugin.utils.YiTuanMaManager;
import com.fr.plugin.utils.YiTuanMaUtils;
import com.fr.plugin.yt.MyDecisionDBAccess;
import com.fr.stable.StringUtils;
import com.fr.stable.db.action.DBAction;
import com.fr.stable.db.dao.DAOContext;
import com.fr.stable.query.QueryFactory;
import com.fr.stable.query.restriction.RestrictionFactory;
import com.fr.third.springframework.web.bind.annotation.RequestMethod;
import com.fr.web.utils.WebUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
public class H5LoginHandler extends BaseHttpHandler {
@Override
public RequestMethod getMethod() {
return null;
}
@Override
public String getPath() {
return "/yt/h5login";
}
@Override
public boolean isPublic() {
return true;
}
private boolean autoLogin(HttpServletRequest req, HttpServletResponse res) throws Exception {
String authCode = WebUtils.getHTTPRequestParameter(req, "authCode");
String clientId = WebUtils.getHTTPRequestParameter(req, "clientId");
if (StringUtils.isBlank(clientId)) {
String id = req.getParameter("callBack");
clientId = getClientIdById(id);
}
if (StringUtils.isEmpty(authCode)) {
FRLogger.getLogger().error("==================== 获取xx码authCode失败!");
FineLoggerFactory.getLogger().info("登陆用户失败,响应:" + res);
return false;
}
String access_token = YiTuanMaUtils.getAccessToken(clientId);
String yiTuanMaUserId = YiTuanMaUtils.getCurrentUserId(clientId, authCode, access_token);
YTUserEntity userEntity = UserSync.findById(yiTuanMaUserId);
String fsUserName = "";
if (userEntity != null) {
String phone = userEntity.getPhone();
User user = getUserByPhone(phone);
if (user != null) {
fsUserName = user.getUserName();
}
}
if (StringUtils.isNotBlank(fsUserName)) {
String callBack = req.getParameter("callBack");
LoginService.getInstance().login(req, res, fsUserName);
if ("false".equalsIgnoreCase(callBack)) {
callBack = "";
}
if (StringUtils.isNotBlank(callBack)) {
if (!callBack.startsWith("http")) {
callBack = getCallbackUrlById(callBack);
}
sendRedirect(req, res, callBack);
} else {
String header = req.getHeader("user-agent");
String cloudHost = YiTuanMaManager.getInstance().getUrl();
if (isH5(header)) {
cloudHost += "/url/mobile";
}
sendRedirect(req, res, cloudHost);
}
}
return false;
}
private String getCallbackUrlById(String id) {
try {
return MyDecisionDBAccess.getAccessor().runDMLAction(new DBAction<String>() {
@Override
public String run(DAOContext daoContext) throws Exception {
MyEntityDao dao = daoContext.getDAO(MyEntityDao.class);
YtOutputMyEntity myEntity = dao.findOne(QueryFactory.create().addRestriction(RestrictionFactory.eq("id", id)));
LogKit.error("查询到:{} 回调地址为:{}", id, myEntity.getGoUrl());
return myEntity.getGoUrl();
}
});
} catch (Exception e) {
LogKit.error("查询配置失败", e);
}
return "";
}
private boolean isH5(String header) {
//Mozilla/5.0 (iPhone; CPU iPhone OS 13_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 TencentMTA/1 ESENYUN_TOKEN:cd97af45-7c7b-4da7-81ca-655347629c79
if (StringUtils.isNotBlank(header)) {
return header.contains("ESENYUN_TOKEN");
}
return false;
}
private void sendRedirect(HttpServletRequest req, HttpServletResponse res, String url) {
res.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
res.setHeader("Location", url);
}
private User getUserByPhone(String phone) {
try {
return AuthorityContext.getInstance().getUserController().findOne(QueryFactory.create().addRestriction(RestrictionFactory.eq("mobile", phone)));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public void handle(HttpServletRequest req, HttpServletResponse httpServletResponse) throws Exception {
String authCode = req.getParameter("authCode");
String clientId = req.getParameter("clientId");
if (StringUtils.isNotBlank(authCode)) {
boolean login = autoLogin(req, httpServletResponse);
JSONObject jsonObject = new JSONObject();
if (login) {
jsonObject.put("status", -1);
jsonObject.put("message", "登录失败");
com.fr.web.utils.WebUtils.flushSuccessMessageAutoClose(req, httpServletResponse, jsonObject);
return;
}
} else {
HashMap<String, String> hashMap = new HashMap<>();
if (StringUtils.isBlank(clientId)) {
String id = req.getParameter("callBack");
clientId = getClientIdById(id);
}
hashMap.put("ticket", YiTuanMaUtils.getTicket(clientId));
hashMap.put("clientId", clientId);
hashMap.put("servletURL", req.getContextPath() + req.getServletPath());
WebUtils.writeOutTemplate("/com/fr/plugin/web/login.tpl", httpServletResponse, hashMap);
}
}
private String getClientIdById(String id) {
try {
return MyDecisionDBAccess.getAccessor().runQueryAction(new DBAction<String>() {
@Override
public String run(DAOContext daoContext) throws Exception {
MyEntityDao dao = daoContext.getDAO(MyEntityDao.class);
YtOutputMyEntity myEntity = dao.findOne(QueryFactory.create().addRestriction(RestrictionFactory.eq("id", id)));
return myEntity.getClientId();
}
});
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}