From 368deac57a30df0d5506dd1b970ab61b455c79f0 Mon Sep 17 00:00:00 2001
From: wuxw <928255095@qq.com>
Date: 星期六, 11 七月 2020 22:47:15 +0800
Subject: [PATCH] 优化代码
---
service-front/src/main/java/com/java110/front/smo/ownerLogin/impl/OwnerAppLoginSMOImpl.java | 183 ++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 161 insertions(+), 22 deletions(-)
diff --git a/service-front/src/main/java/com/java110/front/smo/ownerLogin/impl/OwnerAppLoginSMOImpl.java b/service-front/src/main/java/com/java110/front/smo/ownerLogin/impl/OwnerAppLoginSMOImpl.java
index 756a30c..db19b6c 100644
--- a/service-front/src/main/java/com/java110/front/smo/ownerLogin/impl/OwnerAppLoginSMOImpl.java
+++ b/service-front/src/main/java/com/java110/front/smo/ownerLogin/impl/OwnerAppLoginSMOImpl.java
@@ -6,7 +6,10 @@
import com.java110.core.context.IPageData;
import com.java110.core.context.PageData;
import com.java110.core.factory.AuthenticationFactory;
+import com.java110.core.factory.WechatFactory;
import com.java110.dto.owner.OwnerAppUserDto;
+import com.java110.dto.smallWeChat.SmallWeChatDto;
+import com.java110.dto.user.UserDto;
import com.java110.front.properties.WechatAuthProperties;
import com.java110.front.smo.ownerLogin.IOwnerAppLoginSMO;
import com.java110.utils.cache.CommonCache;
@@ -17,6 +20,7 @@
import com.java110.utils.constant.WechatConstant;
import com.java110.utils.exception.SMOException;
import com.java110.utils.util.Assert;
+import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.slf4j.Logger;
@@ -47,6 +51,8 @@
private final static int expireTime = 7200;
+ private final static int LOGIN_PAGE = 1;
+
@Autowired
private RestTemplate restTemplate;
@@ -67,15 +73,18 @@
JSONObject loginInfo = JSONObject.parseObject(pd.getReqData());
loginInfo.put("passwd", AuthenticationFactory.passwdMd5(loginInfo.getString("password")));
- responseEntity = this.callCenterService(restTemplate, pd, loginInfo.toJSONString(), ServiceConstant.SERVICE_API_URL + "/api/user.service.login", HttpMethod.POST);
- if (responseEntity.getStatusCode() != HttpStatus.OK) {
+ UserDto userDto = new UserDto();
+ userDto.setUserName(loginInfo.getString("username"));
+ userDto.setPassword(loginInfo.getString("password"));
+ userDto = super.postForApi(pd, userDto, ServiceCodeConstant.SERVICE_CODE_USER_LOGIN, UserDto.class);
+
+ if (userDto == null) {
+ responseEntity = new ResponseEntity<>("鐢ㄦ埛鍚嶆垨瀵嗙爜閿欒", HttpStatus.BAD_REQUEST);
return responseEntity;
}
- JSONObject userInfo = JSONObject.parseObject(responseEntity.getBody());
-
//鏍规嵁鐢ㄦ埛鏌ヨ鍟嗘埛淇℃伅
- String userId = userInfo.getString("userId");
+ String userId = userDto.getUserId();
pd = PageData.newInstance().builder(userId, "", "", pd.getReqData(),
"", "", "", "",
@@ -96,7 +105,12 @@
JSONObject paramOut = new JSONObject();
paramOut.put("result", 0);
paramOut.put("owner", appUser);
- paramOut.put("token", userInfo.getString("token"));
+ paramOut.put("token", userDto.getToken());
+
+ userDto = new UserDto();
+ userDto.setUserId(ownerAppUserDtos.get(0).getUserId());
+ UserDto tmpUserDto = super.getForApi(pd, userDto, ServiceCodeConstant.QUERY_USER_SECRET, UserDto.class);
+ paramOut.put("key", tmpUserDto.getKey());
String appId = pd.getAppId();
@@ -110,25 +124,85 @@
}
@Override
- public ResponseEntity<String> getPageAccessToken(IPageData pd) throws SMOException {
+ public ResponseEntity<String> doLoginByKey(IPageData pd) throws SMOException {
+ JSONObject paramIn = JSONObject.parseObject(pd.getReqData());
+ Assert.hasKeyAndValue(paramIn, "key", "璇锋眰鎶ユ枃涓湭鍖呭惈涓存椂绉橀挜");
+ logger.debug("doLogin鍏ュ弬锛�" + paramIn.toJSONString());
+ ResponseEntity<String> responseEntity;
+
+ JSONObject loginInfo = JSONObject.parseObject(pd.getReqData());
+
+ UserDto userDto = new UserDto();
+ userDto.setKey(paramIn.getString("key"));
+ userDto = super.postForApi(pd, userDto, ServiceCodeConstant.SERVICE_CODE_USER_LOGIN, UserDto.class);
+
+
+ //鏍规嵁鐢ㄦ埛鏌ヨ鍟嗘埛淇℃伅
+ String userId = userDto.getUserId();
+
+ pd = PageData.newInstance().builder(userId, "", "", pd.getReqData(),
+ "", "", "", "",
+ pd.getAppId());
+ OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto();
+ ownerAppUserDto.setUserId(userId);
+ List<OwnerAppUserDto> ownerAppUserDtos = super.getForApis(pd, ownerAppUserDto, ServiceCodeConstant.LIST_APPUSERBINDINGOWNERS, OwnerAppUserDto.class);
+
+
+ if (ownerAppUserDtos == null || ownerAppUserDtos.size() < 1) {
+ responseEntity = new ResponseEntity<>("鐢ㄦ埛鏈粦瀹氫笟涓�", HttpStatus.BAD_REQUEST);
+ return responseEntity;
+ }
+
+ JSONObject appUser = JSONObject.parseObject(JSONObject.toJSONString(ownerAppUserDtos.get(0)));
+ appUser.put("userId", userId);
+ appUser.put("userName", paramIn.getString("username"));
+ JSONObject paramOut = new JSONObject();
+ paramOut.put("code", 0);
+ paramOut.put("msg", "鎴愬姛");
+ paramOut.put("owner", appUser);
+ paramOut.put("token", userDto.getToken());
+ paramOut.put("key", userDto.getKey());
+ return new ResponseEntity<>(paramOut.toJSONString(), HttpStatus.OK);
+
+ }
+
+ @Override
+ public ResponseEntity<String> getPageAccessToken(IPageData pd, HttpServletRequest request) throws SMOException {
JSONObject paramIn = JSONObject.parseObject(pd.getReqData());
String authCode = paramIn.getString("code");
String state = paramIn.getString("state");
- String urlCode = CommonCache.getAndRemoveValue(paramIn.getString("urlCode"));
+ String paramStr = CommonCache.getAndRemoveValue(paramIn.getString("urlCode"));
- if (StringUtil.isEmpty(urlCode)) {
- return ResultVo.redirectPage("/#/pages/login/login");
+ if (StringUtil.isEmpty(paramStr)) {
+ return ResultVo.redirectPage("/");
}
- String url = WechatConstant.APP_GET_ACCESS_TOKEN_URL.replace("APPID", wechatAuthProperties.getWechatAppId())
- .replace("SECRET", wechatAuthProperties.getWechatAppSecret())
+ JSONObject param = JSONObject.parseObject(paramStr);
+ String redirectUrl = param.getString("redirectUrl");
+ String errorUrl = param.getString("errorUrl");
+ String wId = paramIn.getString("wId");
+ SmallWeChatDto smallWeChatDto = null;
+ if (!StringUtil.isEmpty(wId)) {
+ paramIn.put("appId", WechatFactory.getAppId(wId));
+ smallWeChatDto = getSmallWechat(pd, paramIn);
+ }
+ if (smallWeChatDto == null) { //浠庨厤缃枃浠朵腑鑾峰彇 灏忕▼搴忛厤缃俊鎭�
+ smallWeChatDto = new SmallWeChatDto();
+ smallWeChatDto.setAppId(wechatAuthProperties.getWechatAppId());
+ smallWeChatDto.setAppSecret(wechatAuthProperties.getWechatAppSecret());
+ smallWeChatDto.setMchId(wechatAuthProperties.getMchId());
+ smallWeChatDto.setPayPassword(wechatAuthProperties.getKey());
+ }
+
+ String url = WechatConstant.APP_GET_ACCESS_TOKEN_URL.replace("APPID", smallWeChatDto.getAppId())
+ .replace("SECRET", smallWeChatDto.getAppSecret())
.replace("CODE", authCode);
ResponseEntity<String> paramOut = outRestTemplate.getForEntity(url, String.class);
- logger.debug("璋冪敤寰俊鎹㈠幓token ", paramOut);
+ logger.debug("璋冪敤寰俊鎹㈠幓openId ", paramOut);
if (paramOut.getStatusCode() != HttpStatus.OK) {
- return ResultVo.redirectPage("/#/pages/login/login");
+ return ResultVo.redirectPage(errorUrl);
}
@@ -136,8 +210,17 @@
//鑾峰彇 openId
String openId = paramObj.getString("openid");
- //鍒ゆ柇褰撳墠openId 鏄惁缁戝畾浜嗕笟涓�
+ int loginFlag = paramIn.getInteger("loginFlag");
+ //璇存槑鏄櫥褰曢〉闈紝涓嬪彂code 灏卞彲浠ワ紝涓嶉渶瑕佷笅鍙慿ey 涔嬬被
+ if (loginFlag == LOGIN_PAGE) {
+ //灏唎penId鏀惧埌redis 缂撳瓨锛岀粰鍓嶆涓嬪彂涓存椂绁ㄦ嵁
+ String code = UUID.randomUUID().toString();
+ CommonCache.setValue(code, openId, expireTime);
+ return ResultVo.redirectPage(errorUrl + "?code=" + code);
+ }
+
+ //鍒ゆ柇褰撳墠openId 鏄惁缁戝畾浜嗕笟涓�
pd = PageData.newInstance().builder("-1", "", "", pd.getReqData(),
"", "", "", "",
pd.getAppId());
@@ -149,7 +232,7 @@
//灏唎penId鏀惧埌redis 缂撳瓨锛岀粰鍓嶆涓嬪彂涓存椂绁ㄦ嵁
String code = UUID.randomUUID().toString();
CommonCache.setValue(code, openId, expireTime);
- return ResultVo.redirectPage("/#/pages/login/login?code=" + code);
+ return ResultVo.redirectPage(errorUrl + "?code=" + code);
}
// String accessToken = paramObj.getString("access_token");//鏆傛椂涓嶇敤
@@ -163,7 +246,18 @@
} catch (Exception e) {
logger.error("鍒涘缓token澶辫触");
}
- return ResultVo.redirectPage("/");
+ //鏌ヨ鐢ㄦ埛key
+ UserDto userDto = new UserDto();
+ userDto.setUserId(ownerAppUserDtos.get(0).getUserId());
+ UserDto tmpUserDto = super.getForApi(pd, userDto, ServiceCodeConstant.QUERY_USER_SECRET, UserDto.class);
+
+ if (StringUtil.isEmpty(tmpUserDto.getKey())) {
+ String code = UUID.randomUUID().toString();
+ CommonCache.setValue(code, openId, expireTime);
+ return ResultVo.redirectPage(errorUrl + "?code=" + code);
+ }
+ redirectUrl = redirectUrl + (redirectUrl.indexOf("?") > 0 ? "&key=" + tmpUserDto.getKey() : "?key=" + tmpUserDto.getKey());
+ return ResultVo.redirectPage(redirectUrl);
}
@@ -178,10 +272,30 @@
* @throws SMOException
*/
@Override
- public ResponseEntity<String> refreshToken(IPageData pd, String redirectUrl, HttpServletRequest request, HttpServletResponse response) throws SMOException {
+ public ResponseEntity<String> refreshToken(IPageData pd, String redirectUrl,
+ String errorUrl,
+ String loginFlag,
+ HttpServletRequest request, HttpServletResponse response) throws SMOException {
//鍒嗛厤urlCode
String urlCode = UUID.randomUUID().toString();
- CommonCache.setValue(urlCode, redirectUrl, expireTime);
+ JSONObject param = new JSONObject();
+ param.put("redirectUrl", redirectUrl);
+ param.put("errorUrl", errorUrl);
+ CommonCache.setValue(urlCode, param.toJSONString(), expireTime);
+ String wAppId = request.getHeader("w-app-id");
+ SmallWeChatDto smallWeChatDto = null;
+ if (!StringUtil.isEmpty(wAppId)) {
+ JSONObject paramIn = new JSONObject();
+ paramIn.put("appId", wAppId);
+ smallWeChatDto = getSmallWechat(pd, paramIn);
+ }
+ if (smallWeChatDto == null) { //浠庨厤缃枃浠朵腑鑾峰彇 灏忕▼搴忛厤缃俊鎭�
+ smallWeChatDto = new SmallWeChatDto();
+ smallWeChatDto.setAppId(wechatAuthProperties.getWechatAppId());
+ smallWeChatDto.setAppSecret(wechatAuthProperties.getWechatAppSecret());
+ smallWeChatDto.setMchId(wechatAuthProperties.getMchId());
+ smallWeChatDto.setPayPassword(wechatAuthProperties.getKey());
+ }
URL url = null;
String openUrl = "";
@@ -194,14 +308,15 @@
}
openUrl = WechatConstant.OPEN_AUTH
- .replace("APPID", wechatAuthProperties.getWechatAppId())
+ .replace("APPID", smallWeChatDto.getAppId())
.replace("SCOPE", "snsapi_base")
.replace(
"REDIRECT_URL",
URLEncoder
.encode(
(newUrl
- + "/app/loginOwnerWechatAuth?appId=992020061452450002&urlCode=" + urlCode),
+ + "/app/loginOwnerWechatAuth?appId=992020061452450002&urlCode=" +
+ urlCode + "&loginFlag=" + loginFlag + "&wId=" + WechatFactory.getWId(wAppId)),
"UTF-8")).replace("STATE", "1");
} catch (Exception e) {
@@ -283,7 +398,7 @@
pd.getAppId());
responseEntity = this.callCenterService(restTemplate, pd, "",
ServiceConstant.SERVICE_API_URL + "/api/smallWeChat.listSmallWeChats?appId="
- + paramIn.getString("appId") + "&page=1&row=1", HttpMethod.GET);
+ + paramIn.getString("appId") + "&page=1&row=1&communityId=" + ownerAppUserDtos.get(0).getCommunityId(), HttpMethod.GET);
if (responseEntity.getStatusCode() != HttpStatus.OK) {
return responseEntity;
@@ -364,6 +479,30 @@
return null;
}
+ private SmallWeChatDto getSmallWechat(IPageData pd, JSONObject paramIn) {
+
+ ResponseEntity responseEntity = null;
+
+ pd = PageData.newInstance().builder(pd.getUserId(), "", "", pd.getReqData(),
+ "", "", "", "",
+ pd.getAppId());
+ responseEntity = this.callCenterService(restTemplate, pd, "",
+ ServiceConstant.SERVICE_API_URL + "/api/smallWeChat.listSmallWeChats?appId="
+ + paramIn.getString("appId") + "&page=1&row=1", HttpMethod.GET);
+
+ if (responseEntity.getStatusCode() != HttpStatus.OK) {
+ return null;
+ }
+ JSONObject smallWechatObj = JSONObject.parseObject(responseEntity.getBody().toString());
+ JSONArray smallWeChats = smallWechatObj.getJSONArray("smallWeChats");
+
+ if (smallWeChats == null || smallWeChats.size() < 1) {
+ return null;
+ }
+
+ return BeanConvertUtil.covertBean(smallWeChats.get(0), SmallWeChatDto.class);
+ }
+
public RestTemplate getRestTemplate() {
return restTemplate;
}
--
Gitblit v1.8.0