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.
97 lines
3.6 KiB
97 lines
3.6 KiB
package com.fr.plugin.web.hander; |
|
|
|
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.log.FineLoggerFactory; |
|
import com.fr.plugin.entitys.YTUserEntity; |
|
import com.fr.plugin.utils.UserSync; |
|
import com.fr.plugin.utils.YiTuanMaManager; |
|
import com.fr.plugin.utils.YiTuanMaUtils; |
|
import com.fr.stable.StringUtils; |
|
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 PCLoginHandler extends BaseHttpHandler { |
|
@Override |
|
public RequestMethod getMethod() { |
|
return null; |
|
} |
|
|
|
@Override |
|
public String getPath() { |
|
return "/yt/pclogin"; |
|
} |
|
|
|
@Override |
|
public boolean isPublic() { |
|
return true; |
|
} |
|
|
|
|
|
private void sendRedirect(HttpServletResponse res, String url) { |
|
res.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); |
|
res.setHeader("Location", url); |
|
} |
|
|
|
private void login(HttpServletRequest req, HttpServletResponse res, String username) { |
|
String token = null; |
|
try { |
|
token = LoginService.getInstance().login(req, res, username); |
|
req.setAttribute("fine_auth_token", token); |
|
FineLoggerFactory.getLogger().error("login success"); |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
FineLoggerFactory.getLogger().error("login failed"); |
|
} |
|
} |
|
|
|
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("access_token"); |
|
String clientId = req.getParameter("clientId"); |
|
if (StringUtils.isNotBlank(authCode)) { |
|
String accessToken = WebUtils.getHTTPRequestParameter(req, "access_token"); |
|
String userId = YiTuanMaUtils.getUserInfoByAccessToken(accessToken); |
|
YTUserEntity userEntity = UserSync.findById(userId); |
|
String fsUserName = ""; |
|
if (userEntity != null) { |
|
String phone = userEntity.getPhone(); |
|
User user = getUserByPhone(phone); |
|
if (user != null) { |
|
fsUserName = user.getUserName(); |
|
} |
|
} |
|
if (StringUtils.isBlank(fsUserName)) { |
|
WebUtils.printAsString(httpServletResponse, "无法解析用户,请重新登录"); |
|
return; |
|
} |
|
String callback = req.getParameter("callBack"); |
|
login(req, httpServletResponse, fsUserName); |
|
if (StringUtils.isNotBlank(callback)) { |
|
sendRedirect(httpServletResponse, callback); |
|
return; |
|
} |
|
YiTuanMaManager yiTuanMaManager = YiTuanMaManager.getInstance(); |
|
sendRedirect(httpServletResponse, yiTuanMaManager.getUrl()); |
|
} else { |
|
WebUtils.printAsString(httpServletResponse,"未收到授权码"); |
|
} |
|
} |
|
}
|
|
|