|
@@ -11,8 +11,10 @@ import com.book.push.service.dao.AdminConfigService;
|
|
import com.book.push.service.dao.ConfigService;
|
|
import com.book.push.service.dao.ConfigService;
|
|
import com.book.push.service.dao.OpHostService;
|
|
import com.book.push.service.dao.OpHostService;
|
|
import com.book.push.service.dao.UserService;
|
|
import com.book.push.service.dao.UserService;
|
|
|
|
+import com.book.push.service.redis.RedisService;
|
|
import com.book.push.service.wx.WxThirdPartService;
|
|
import com.book.push.service.wx.WxThirdPartService;
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.AllArgsConstructor;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import me.chanjar.weixin.common.api.WxConsts;
|
|
import me.chanjar.weixin.common.api.WxConsts;
|
|
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
|
|
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
|
|
import me.chanjar.weixin.common.error.WxErrorException;
|
|
import me.chanjar.weixin.common.error.WxErrorException;
|
|
@@ -35,9 +37,10 @@ import org.springframework.web.bind.annotation.RequestParam;
|
|
*/
|
|
*/
|
|
@AllArgsConstructor
|
|
@AllArgsConstructor
|
|
@Controller
|
|
@Controller
|
|
-@RequestMapping("/wx/redirect/{appid}")
|
|
|
|
|
|
+@Slf4j
|
|
|
|
+@RequestMapping("/wx/redirect")
|
|
public class WxRedirectController {
|
|
public class WxRedirectController {
|
|
- public static final Logger logger = LoggerFactory.getLogger(WxRedirectController.class);
|
|
|
|
|
|
+
|
|
@Autowired
|
|
@Autowired
|
|
private UserService userService;
|
|
private UserService userService;
|
|
@Autowired
|
|
@Autowired
|
|
@@ -50,14 +53,10 @@ public class WxRedirectController {
|
|
private OpHostService opHostService;
|
|
private OpHostService opHostService;
|
|
@Autowired
|
|
@Autowired
|
|
private UserSilentService userSilentService;
|
|
private UserSilentService userSilentService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private RedisService redisService;
|
|
|
|
|
|
|
|
|
|
- /**
|
|
|
|
- * //FIXME
|
|
|
|
- * 前端页面url
|
|
|
|
- */
|
|
|
|
-// private final static String FRONT_URL = "%s://%s%s?channelId=%d/userId=%d";
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 接收微信静默授权,获取用户信息,跳转前台页面
|
|
* 接收微信静默授权,获取用户信息,跳转前台页面
|
|
*
|
|
*
|
|
@@ -70,7 +69,7 @@ public class WxRedirectController {
|
|
public String login(@PathVariable String appid, @RequestParam String code, @RequestParam String state, ModelMap map) {
|
|
public String login(@PathVariable String appid, @RequestParam String code, @RequestParam String state, ModelMap map) {
|
|
|
|
|
|
String[] s = state.split("_");
|
|
String[] s = state.split("_");
|
|
- if (s.length != 2 ) {
|
|
|
|
|
|
+ if (s.length != 2) {
|
|
// logger.error("静默登录state错误");
|
|
// logger.error("静默登录state错误");
|
|
// String frontUrl = getFrontUrl(ophost, page, userAdminId, userAdminId);
|
|
// String frontUrl = getFrontUrl(ophost, page, userAdminId, userAdminId);
|
|
return "error";
|
|
return "error";
|
|
@@ -89,7 +88,6 @@ public class WxRedirectController {
|
|
Integer userAdminId = adminConfig.getAdminId();
|
|
Integer userAdminId = adminConfig.getAdminId();
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
Config config = configService.selectByName(SiteCons.SILENT_DEFAULT_CHANNEL);
|
|
Config config = configService.selectByName(SiteCons.SILENT_DEFAULT_CHANNEL);
|
|
Integer defaultAdminId = Integer.parseInt(config.getValue());
|
|
Integer defaultAdminId = Integer.parseInt(config.getValue());
|
|
AdminConfig defaultAdminConfig = adminConfigService.selectByAdminId(defaultAdminId);
|
|
AdminConfig defaultAdminConfig = adminConfigService.selectByAdminId(defaultAdminId);
|
|
@@ -107,23 +105,23 @@ public class WxRedirectController {
|
|
WxMpUser wxMpUser = wxMpServiceByAppid.getUserService()
|
|
WxMpUser wxMpUser = wxMpServiceByAppid.getUserService()
|
|
.userInfo(openId, null);
|
|
.userInfo(openId, null);
|
|
|
|
|
|
- user = userService.createUserByWxUser(wxMpUser,channelId);
|
|
|
|
|
|
+ user = userService.createUserByWxUser(wxMpUser, channelId);
|
|
|
|
|
|
}
|
|
}
|
|
if (userSlientExist(user, null, defaultAdminConfig.getAppid())) {
|
|
if (userSlientExist(user, null, defaultAdminConfig.getAppid())) {
|
|
- String frontUrl = getFrontUrl(adminConfig.getAppid(),ophost, page, userAdminId, String.valueOf(user.getId()));
|
|
|
|
|
|
+ String frontUrl = getFrontUrl(adminConfig.getAppid(), ophost, page, userAdminId, String.valueOf(user.getId()));
|
|
return "redirect:" + frontUrl;
|
|
return "redirect:" + frontUrl;
|
|
} else {
|
|
} else {
|
|
- if (defaultAdminId.equals(channelId)){
|
|
|
|
|
|
+ if (defaultAdminId.equals(channelId)) {
|
|
//保存静默授权
|
|
//保存静默授权
|
|
- saveUserSlitent(user,openId,appid);
|
|
|
|
- String frontUrl = getFrontUrl(adminConfig.getAppid(),ophost, page, userAdminId, String.valueOf(user.getId()));
|
|
|
|
|
|
+ saveUserSlitent(user, openId, appid);
|
|
|
|
+ String frontUrl = getFrontUrl(adminConfig.getAppid(), ophost, page, userAdminId, String.valueOf(user.getId()));
|
|
return "redirect:" + frontUrl;
|
|
return "redirect:" + frontUrl;
|
|
- }else {
|
|
|
|
|
|
+ } else {
|
|
Integer platformId = defaultAdminConfig.getPlatformId();
|
|
Integer platformId = defaultAdminConfig.getPlatformId();
|
|
String redirect_url = UrlCons.OAUTH2_REDIRECT_URL_SLIENT.replace("{appid}", defaultAdminConfig.getAppid());
|
|
String redirect_url = UrlCons.OAUTH2_REDIRECT_URL_SLIENT.replace("{appid}", defaultAdminConfig.getAppid());
|
|
Ophost menuHost = opHostService.selectById(defaultAdminConfig.getMenuophostId());
|
|
Ophost menuHost = opHostService.selectById(defaultAdminConfig.getMenuophostId());
|
|
- redirect_url = String.format(redirect_url,appid,menuHost.getHost());
|
|
|
|
|
|
+ redirect_url = String.format(redirect_url, appid, menuHost.getHost());
|
|
|
|
|
|
wxOpenService = thirdPartService.getWxOpenServiceByPlatFormId(platformId);
|
|
wxOpenService = thirdPartService.getWxOpenServiceByPlatFormId(platformId);
|
|
String auto2Url = wxOpenService.getWxOpenComponentService().getWxMpServiceByAppid(defaultAdminConfig.getAppid()).getOAuth2Service()
|
|
String auto2Url = wxOpenService.getWxOpenComponentService().getWxMpServiceByAppid(defaultAdminConfig.getAppid()).getOAuth2Service()
|
|
@@ -133,22 +131,21 @@ public class WxRedirectController {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
}
|
|
}
|
|
} catch (WxErrorException e) {
|
|
} catch (WxErrorException e) {
|
|
// logger.error(e.getMessage());
|
|
// logger.error(e.getMessage());
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
- String frontUrl = getFrontUrl(adminConfig.getAppid(),ophost, state, 0, "");
|
|
|
|
|
|
+ String frontUrl = getFrontUrl(adminConfig.getAppid(), ophost, state, 0, "");
|
|
return "redirect:" + frontUrl;
|
|
return "redirect:" + frontUrl;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
+
|
|
@RequestMapping("/slient")
|
|
@RequestMapping("/slient")
|
|
public String slient(@PathVariable String appid, @RequestParam String code, @RequestParam String state, ModelMap map) {
|
|
public String slient(@PathVariable String appid, @RequestParam String code, @RequestParam String state, ModelMap map) {
|
|
String[] s = state.split("_");
|
|
String[] s = state.split("_");
|
|
- if (s.length != 3 ) {
|
|
|
|
|
|
+ if (s.length != 3) {
|
|
// logger.error("静默登录state错误");
|
|
// logger.error("静默登录state错误");
|
|
// String frontUrl = getFrontUrl(ophost, "page", userAdminId, userAdminId);
|
|
// String frontUrl = getFrontUrl(ophost, "page", userAdminId, userAdminId);
|
|
return "error";
|
|
return "error";
|
|
@@ -168,26 +165,22 @@ public class WxRedirectController {
|
|
Integer userAdminId = adminConfig.getAdminId();
|
|
Integer userAdminId = adminConfig.getAdminId();
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
WxOpenService wxOpenService = thirdPartService.getWxOpenServiceByPlatFormId(fromPlatformId);
|
|
WxOpenService wxOpenService = thirdPartService.getWxOpenServiceByPlatFormId(fromPlatformId);
|
|
try {
|
|
try {
|
|
WxOAuth2AccessToken accessToken = wxOpenService.getWxOpenComponentService().getWxMpServiceByAppid(appid).getOAuth2Service().getAccessToken(code);
|
|
WxOAuth2AccessToken accessToken = wxOpenService.getWxOpenComponentService().getWxMpServiceByAppid(appid).getOAuth2Service().getAccessToken(code);
|
|
String openId = accessToken.getOpenId();
|
|
String openId = accessToken.getOpenId();
|
|
- saveUserSlitent(user,openId,appid);
|
|
|
|
- String frontUrl = getFrontUrl(adminConfig.getAppid(),ophost, page, userAdminId, String.valueOf(user.getId()));
|
|
|
|
|
|
+ saveUserSlitent(user, openId, appid);
|
|
|
|
+ String frontUrl = getFrontUrl(adminConfig.getAppid(), ophost, page, userAdminId, String.valueOf(user.getId()));
|
|
return "redirect:" + frontUrl;
|
|
return "redirect:" + frontUrl;
|
|
- }catch (Exception e){
|
|
|
|
|
|
+ } catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
- String frontUrl = getFrontUrl(adminConfig.getAppid(),ophost, page, 0, "");
|
|
|
|
|
|
+ String frontUrl = getFrontUrl(adminConfig.getAppid(), ophost, page, 0, "");
|
|
return "redirect:" + frontUrl;
|
|
return "redirect:" + frontUrl;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
+
|
|
private void saveUserSlitent(User user, String openId, String appid) {
|
|
private void saveUserSlitent(User user, String openId, String appid) {
|
|
UserSilent userSilent = new UserSilent();
|
|
UserSilent userSilent = new UserSilent();
|
|
userSilent.setUserId(user.getId());
|
|
userSilent.setUserId(user.getId());
|
|
@@ -211,9 +204,18 @@ public class WxRedirectController {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- private String getFrontUrl(String appid,Ophost ophost, String state, Integer userAdminId, String userId) {
|
|
|
|
- String host = ophost.getHost();
|
|
|
|
- String frontUrl = String.format(UrlCons.FRONT_URL,appid,host, state, userAdminId, userId);
|
|
|
|
- return frontUrl;
|
|
|
|
|
|
+ private String getFrontUrl(String appid, Ophost ophost, String state, Integer userAdminId, String userId) {
|
|
|
|
+
|
|
|
|
+ String s = redisService.get(state);
|
|
|
|
+ if (s!=null && s.startsWith("http")){
|
|
|
|
+ String sp = s.contains("?") ? "&" : "?";
|
|
|
|
+ s = String.format(s + sp + UrlCons.URL_PARAMS, userAdminId, userId);
|
|
|
|
+ return s;
|
|
|
|
+ }else {
|
|
|
|
+ String host = ophost.getHost();
|
|
|
|
+ String frontUrl = String.format(UrlCons.FRONT_URL,appid,host, state, userAdminId, userId);
|
|
|
|
+ return frontUrl;
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|
|
}
|
|
}
|