|
|
@@ -35,10 +35,10 @@ import com.cyksj.redis.RedisService;
|
|
|
import com.cyksj.service.authorization.AuthorizationService;
|
|
|
import com.cyksj.service.distribute.DistributeService;
|
|
|
import com.cyksj.service.distribute.equipment.EquipmentDistributeService;
|
|
|
+import com.cyksj.service.mail.MailService;
|
|
|
import com.cyksj.service.template.TemplateCommonService;
|
|
|
import com.cyksj.service.user.UserService;
|
|
|
import com.cyksj.service.wechat.WeChatService;
|
|
|
-import com.cyksj.service.mail.MailService;
|
|
|
import com.cyksj.web.util.StpUserUtil;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
@@ -49,8 +49,6 @@ import org.springframework.web.bind.annotation.*;
|
|
|
import javax.servlet.http.Cookie;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.net.URLEncoder;
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Date;
|
|
|
|
|
|
/**
|
|
|
@@ -119,42 +117,9 @@ public class AuthorizationController {
|
|
|
*/
|
|
|
@GetMapping(value = "/weChat/baseRedirect")
|
|
|
public void redirect(String code, String state, HttpServletResponse response) throws Exception {
|
|
|
-
|
|
|
- // 转跳参数
|
|
|
- String json = Codec.DoBase64.custom().setData(state).decodeAsText();
|
|
|
- AuthRedirect re = Jsons.parseObject(json, AuthRedirect.class);
|
|
|
-
|
|
|
AuthToken token = weChatService.getAuthToken(code,weChatConfig.getAppid());
|
|
|
|
|
|
- // 根据openid 与 access_token 获取用户信息
|
|
|
- GzhOAuth2UserInfo userinfo = weChatService.getUserInfo(token.getAccessToken(), token.getOpenid());
|
|
|
-
|
|
|
- //保存用户信息
|
|
|
- User user = userService.saveAuth(userinfo, re.getSharedId(), re.getDsType(), re.getPid());
|
|
|
- if (user == null) {
|
|
|
- throw BusinessRuntimeException.getInstance("系统错误,请重新授权");
|
|
|
- }
|
|
|
-
|
|
|
- //写回登陆信息
|
|
|
- StpUserUtil.login(user.getId());
|
|
|
- response.addHeader("satoken-user",StpUserUtil.getTokenValue());
|
|
|
-
|
|
|
- Cookie cookie = new Cookie("satoken-user",StpUserUtil.getTokenValue());
|
|
|
- cookie.setMaxAge(31536000);
|
|
|
- cookie.setPath("/");
|
|
|
- response.addCookie(cookie);
|
|
|
- JSONObject jsonObject = new JSONObject();
|
|
|
- jsonObject.putOpt("uid",user.getId());
|
|
|
- jsonObject.putOpt("nickName",user.getNickname());
|
|
|
-
|
|
|
- Cookie userInfo = new Cookie("info",Base64.encode(jsonObject.toString()));
|
|
|
- cookie.setMaxAge(31536000);
|
|
|
- cookie.setPath("/");
|
|
|
- response.addCookie(userInfo);
|
|
|
- String url = redisService.getStr(re.getMappingId().toString());
|
|
|
- redisService.del(re.getMappingId().toString());
|
|
|
- log.info("微信授权转跳.url: {}",url);
|
|
|
- response.sendRedirect(url.contains("?") ? url + "&t=" + StpUserUtil.getTokenValue() : url + "?t=" + StpUserUtil.getTokenValue());
|
|
|
+ baseRedirect(token, state, response);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -233,22 +198,30 @@ public class AuthorizationController {
|
|
|
/**
|
|
|
* 网页微信授权登录
|
|
|
*/
|
|
|
- @GetMapping("/wx/login/authUrl")
|
|
|
- public Result<String> wxAuthorize() throws Exception {
|
|
|
+ @GetMapping("/wx/authLogin")
|
|
|
+ public Result<String> wxAuthorize(Long sharedId, Integer dsType, Long popularizeId) throws Exception {
|
|
|
String wxAuthUrl = redisService.getStr(RedisService.key.WX_LOGIN_AUTH_URI.getNameFormat(wxOpenPlatYHLXLoginConfig.getAppId()));
|
|
|
Long mappingId = SEQUENCE.nextId();
|
|
|
redisService.set(mappingId.toString(), wxOpenPlatYHLXLoginConfig.getLoginUrl(), 60 * 60 * 24L);
|
|
|
- AuthRedirect redirect = new AuthRedirect().setMappingId(mappingId);
|
|
|
+ AuthRedirect redirect = new AuthRedirect().setMappingId(mappingId).setSharedId(sharedId).setDsType(dsType).setPid(popularizeId);
|
|
|
String state = Codec.DoBase64.custom().setData(Jsons.toJson(redirect)).encodeAsText();
|
|
|
if (StrUtil.isEmpty(wxAuthUrl)) {
|
|
|
- wxAuthUrl = String.format("https://open.weixin.qq.com/connect/qrconnect?" +
|
|
|
- "appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_login&state=%s#wechat_redirect",
|
|
|
- wxOpenPlatYHLXLoginConfig.getAppId(),
|
|
|
- URLEncoder.encode(wxOpenPlatYHLXLoginConfig.getRedirectUri(), StandardCharsets.UTF_8), state);
|
|
|
+ wxAuthUrl = authorizationService.createWxAuthLoginUrl(state);
|
|
|
}
|
|
|
return GatewayResponse.SUCCESS.newBuilder().toResult(wxAuthUrl);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 网页微信授权后跳转
|
|
|
+ */
|
|
|
+ @GetMapping("/wx/authLogin/baseRedirect")
|
|
|
+ public void wxAuthLoginRedirect(String code, String state, HttpServletResponse response) throws Exception {
|
|
|
+
|
|
|
+ AuthToken token = weChatService.getAuthToken(code, wxOpenPlatYHLXLoginConfig.getAppId());
|
|
|
+ //重定向
|
|
|
+ baseRedirect(token, state, response);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 网页扫描微信公众号二维码 临时凭证ticket 关注后登录
|
|
|
*/
|
|
|
@@ -438,4 +411,39 @@ public class AuthorizationController {
|
|
|
LoginPhoneRep loginPhoneRep = new LoginPhoneRep(StpUserUtil.getTokenValue(), user.getId(), user.getNickname(), user.getHeadimgurl());
|
|
|
return GatewayResponse.SUCCESS.newBuilder().toResult(loginPhoneRep);
|
|
|
}
|
|
|
+
|
|
|
+ public void baseRedirect(AuthToken token, String state, HttpServletResponse response) throws Exception {
|
|
|
+ // 根据openid 与 access_token 获取用户信息
|
|
|
+ GzhOAuth2UserInfo userinfo = weChatService.getUserInfo(token.getAccessToken(), token.getOpenid());
|
|
|
+
|
|
|
+ // 转跳参数
|
|
|
+ String json = Codec.DoBase64.custom().setData(state).decodeAsText();
|
|
|
+ AuthRedirect re = Jsons.parseObject(json, AuthRedirect.class);
|
|
|
+ //保存用户信息
|
|
|
+ User user = userService.saveAuth(userinfo, re.getSharedId(), re.getDsType(), re.getPid());
|
|
|
+ if (user == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("系统错误,请重新授权");
|
|
|
+ }
|
|
|
+
|
|
|
+ //写回登陆信息
|
|
|
+ StpUserUtil.login(user.getId());
|
|
|
+ response.addHeader("satoken-user", StpUserUtil.getTokenValue());
|
|
|
+
|
|
|
+ Cookie cookie = new Cookie("satoken-user", StpUserUtil.getTokenValue());
|
|
|
+ cookie.setMaxAge(31536000);
|
|
|
+ cookie.setPath("/");
|
|
|
+ response.addCookie(cookie);
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.putOpt("uid", user.getId());
|
|
|
+ jsonObject.putOpt("nickName", user.getNickname());
|
|
|
+
|
|
|
+ Cookie userInfo = new Cookie("info", Base64.encode(jsonObject.toString()));
|
|
|
+ cookie.setMaxAge(31536000);
|
|
|
+ cookie.setPath("/");
|
|
|
+ response.addCookie(userInfo);
|
|
|
+ String url = redisService.getStr(re.getMappingId().toString());
|
|
|
+ redisService.del(re.getMappingId().toString());
|
|
|
+ log.info("微信授权转跳.url: {}", url);
|
|
|
+ response.sendRedirect(url.contains("?") ? url + "&t=" + StpUserUtil.getTokenValue() : url + "?t=" + StpUserUtil.getTokenValue());
|
|
|
+ }
|
|
|
}
|