|
@@ -8,22 +8,27 @@ package com.book.push.controller;
|
|
|
*/
|
|
|
|
|
|
import com.book.dao.cps.pojo.AdminConfig;
|
|
|
+import com.book.dao.cps.pojo.Config;
|
|
|
+import com.book.dao.cps.pojo.Ophost;
|
|
|
+import com.book.dao.cps.pojo.User;
|
|
|
+import com.book.dao.polardb.pojo.UserSilent;
|
|
|
+import com.book.push.cons.SiteCons;
|
|
|
import com.book.push.cons.UrlCons;
|
|
|
-import com.book.push.service.dao.AdminConfigService;
|
|
|
+import com.book.push.service.dao.*;
|
|
|
import com.book.push.service.redis.RedisService;
|
|
|
import com.book.push.service.wx.WxThirdPartService;
|
|
|
+import com.book.push.utils.UrlUtil;
|
|
|
+import com.book.push.utils.JsonUtils;
|
|
|
import com.book.push.utils.Md5Util;
|
|
|
-import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import me.chanjar.weixin.common.api.WxConsts;
|
|
|
+import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
|
|
|
+import me.chanjar.weixin.common.service.WxOAuth2Service;
|
|
|
import me.chanjar.weixin.open.api.WxOpenService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
-import org.springframework.web.bind.annotation.PathVariable;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
@@ -43,6 +48,14 @@ public class BookIndexController {
|
|
|
private Boolean debug;
|
|
|
@Autowired
|
|
|
private RedisService redisService;
|
|
|
+ @Autowired
|
|
|
+ private OpHostService opHostService;
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+ @Autowired
|
|
|
+ private UserSilentService userSilentService;
|
|
|
+ @Autowired
|
|
|
+ private ConfigService configService;
|
|
|
/**
|
|
|
* //FIXME
|
|
|
* <p>
|
|
@@ -52,46 +65,44 @@ public class BookIndexController {
|
|
|
|
|
|
// @GetMapping("/{state}")
|
|
|
// public String BookMain(HttpServletRequest request, @PathVariable String state) {
|
|
|
-
|
|
|
-
|
|
|
@GetMapping()
|
|
|
public String BookMain(HttpServletRequest request) {
|
|
|
|
|
|
/* StringBuffer url = request.getRequestURL();
|
|
|
String contextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).toString();*/
|
|
|
- String contextUrl = request.getServerName();
|
|
|
+ String contextUrl = request.getServerName();
|
|
|
// contextUrl = "mp.esalary.com.cn";
|
|
|
log.debug(contextUrl);
|
|
|
String[] split = contextUrl.split("\\.");
|
|
|
String appid = split[0];
|
|
|
if (debug) {
|
|
|
appid = "wxe1271417b2ff5b1c";
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
|
|
|
}
|
|
|
- String state ="home";
|
|
|
+ String state = "home";
|
|
|
// if (appid == null || !appid.startsWith("wx")) {
|
|
|
- String referer = request.getHeader("referer");
|
|
|
- if (referer==null){
|
|
|
- referer = "http://wxe1271417b2ff5b1c.esalary.com.cn/";
|
|
|
- }
|
|
|
- state = Md5Util.md5(referer);
|
|
|
+ String referer = request.getHeader("referer");
|
|
|
+ if (referer == null) {
|
|
|
+ referer = "http://wxe1271417b2ff5b1c.esalary.com.cn/";
|
|
|
+ }
|
|
|
+ state = Md5Util.md5(referer);
|
|
|
|
|
|
|
|
|
- redisService.set(state, referer, 60);
|
|
|
- if (referer == null) {
|
|
|
- log.error("非 发来源,失败 ");
|
|
|
- return "登录失败";
|
|
|
- }
|
|
|
- String replace = referer.replace("http://", "").replace("https://", "");
|
|
|
- appid = replace.split("\\.")[0];
|
|
|
- if (!appid.startsWith("wx")) {
|
|
|
- return "登录失败";
|
|
|
- }
|
|
|
+ redisService.set(state, referer, 60);
|
|
|
+ if (referer == null) {
|
|
|
+ log.error("非 发来源,失败 ");
|
|
|
+ return "登录失败";
|
|
|
+ }
|
|
|
+ String replace = referer.replace("http://", "").replace("https://", "");
|
|
|
+ appid = replace.split("\\.")[0];
|
|
|
+ if (!appid.startsWith("wx")) {
|
|
|
+ return "登录失败";
|
|
|
+ }
|
|
|
|
|
|
// }
|
|
|
- AdminConfig adminConfig = adminConfigService.selectByAppid(appid);
|
|
|
String redirect_url = String.format(UrlCons.OAUTH2_REDIRECT_URL_LOGIN, contextUrl);
|
|
|
+ AdminConfig adminConfig = adminConfigService.selectByAppid(appid);
|
|
|
|
|
|
|
|
|
Integer platformId = adminConfig.getPlatformId();
|
|
@@ -100,13 +111,99 @@ public class BookIndexController {
|
|
|
.buildAuthorizationUrl(redirect_url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, state + "_" + adminConfig.getAdminId());
|
|
|
// 静默授权
|
|
|
|
|
|
- log.info("跳转:"+auto2Url);
|
|
|
+ log.info("跳转:" + auto2Url);
|
|
|
return "redirect:" + auto2Url;
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
+ @GetMapping
|
|
|
+ public String login(HttpServletRequest request, @RequestParam String appid, @RequestParam String code, @RequestParam String state) {
|
|
|
+
|
|
|
+
|
|
|
+ WxOAuth2AccessToken oAuth2rRefreshToken = null;
|
|
|
+ AdminConfig adminConfig = adminConfigService.selectByAppid(appid);
|
|
|
+ if (code == null) {
|
|
|
+
|
|
|
+ String domainName = request.getServerName();
|
|
|
+ String referer = request.getHeader("referer");
|
|
|
+ referer = referer == null ? "home" : referer;
|
|
|
+ state = Md5Util.md5(referer);
|
|
|
+ String replace = referer.replace("http://", "").replace("https://", "");
|
|
|
+ appid = replace.split("\\.")[0];
|
|
|
+
|
|
|
+ if (redisService.get(state) == null) {
|
|
|
+ redisService.set(state, referer);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ oAuth2rRefreshToken = thirdPartService.getOAuth2rRefreshToken(adminConfig.getPlatformId(), appid);
|
|
|
+ if (oAuth2rRefreshToken == null) {
|
|
|
+ String redirectUrl = String.format(UrlCons.URL_OAUTH2_REDIRECT_LOGIN, domainName);
|
|
|
+ WxOAuth2Service oAuth2Service = thirdPartService.getWxOpenServiceByAuthorizerAppid(appid)
|
|
|
+ .getWxOpenComponentService().getWxMpServiceByAppid(appid).getOAuth2Service();
|
|
|
+ String auto2Url = oAuth2Service.buildAuthorizationUrl(redirectUrl, WxConsts.OAuth2Scope.SNSAPI_BASE, state);
|
|
|
+ return buildRedirectUrl(auto2Url);
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ oAuth2rRefreshToken = thirdPartService.updateOAuth2rRefreshToken(adminConfig.getPlatformId(), appid, code);
|
|
|
+
|
|
|
+ }
|
|
|
+ if (oAuth2rRefreshToken == null) {
|
|
|
+ log.error("登录出错,呜呜~~");
|
|
|
+ return buildRedirectUrl( null);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ User user = userService.selectByOpenId(oAuth2rRefreshToken.getOpenId());
|
|
|
+ if (user == null) {
|
|
|
+
|
|
|
+ user = userService.createUserByOpenid(oAuth2rRefreshToken.getOpenId(), adminConfig.getAdminId());
|
|
|
+ //二次静默
|
|
|
+ }else {
|
|
|
+ if(userSlientExist(user)!=null){
|
|
|
+ //二次静默
|
|
|
+
|
|
|
+ }else{
|
|
|
+ return buildRedirectUrl(getFontUlr(state,adminConfig,user));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return buildRedirectUrl("");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private String buildRedirectUrl(String fontUlr) {
|
|
|
+ return "redirect:" +fontUlr;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getFontUlr(String referer, AdminConfig adminConfig, User user) {
|
|
|
+ if (referer==null){
|
|
|
+ return "登录失败啦,请在网站里面进行登录";
|
|
|
+ }
|
|
|
+ Integer ophostId = adminConfig.getOphostId();
|
|
|
+ Ophost ophost = opHostService.selectById(ophostId);
|
|
|
+ String s = redisService.get(referer);
|
|
|
+ String url =UrlUtil.replaceDomain(s, adminConfig.getAppid() + "." + ophost.getHost());
|
|
|
+ return UrlUtil.addParams(url,String.format(UrlCons.URL_PARAMS,adminConfig.getAdminId(),user.getOpenid()));
|
|
|
+ }
|
|
|
+
|
|
|
+ private UserSilent userSlientExist(User user) {
|
|
|
+ Config config = configService.selectByName(SiteCons.SILENT_DEFAULT_CHANNEL);
|
|
|
+ Integer defaultAdminId = Integer.parseInt(config.getValue());
|
|
|
+ AdminConfig defaultAdminConfig = adminConfigService.selectByAdminId(defaultAdminId);
|
|
|
|
|
|
|
|
|
+ UserSilent userSilent = new UserSilent();
|
|
|
+ userSilent.setUserId(user.getId());
|
|
|
+ userSilent.setChannelId(user.getChannelId());
|
|
|
+ userSilent.setOpenid(user.getOpenid());
|
|
|
+ userSilent.setTriAppid(defaultAdminConfig.getAppid());
|
|
|
+ log.debug(JsonUtils.toJsonStr(userSilent));
|
|
|
+ UserSilent result = userSilentService.selectByUserSlient(userSilent);
|
|
|
+ return result ;
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
}
|