java110 лет назад: 5
Родитель
Сommit
a2e439d3d0

+ 1 - 1
java110-utils/src/main/java/com/java110/utils/constant/WechatConstant.java

@@ -14,7 +14,7 @@ public class WechatConstant {
     public static final String WECHAT_DOMAIN = "WECHAT";
 
     //微信ID
-    public static final String PAGE_WECHAT_APP_ID = "_wId";//微信id 一般是appId
+    public static final String PAGE_WECHAT_APP_ID = "wId";//微信id 一般是appId
 
     public static final String KEY_PROPERTY_FEE_TEMPLATE_ID = "PROPERTY_FEE_TEMPLATE_ID";
 

+ 2 - 0
service-front/src/main/java/com/java110/front/controller/EnvController.java

@@ -26,4 +26,6 @@ public class EnvController {
         String env = MappingCache.getValue("HC_ENV");
         return new ResponseEntity<>(env, HttpStatus.OK);
     }
+
+
 }

+ 8 - 1
service-front/src/main/java/com/java110/front/controller/wechat/LoginOwnerWechatAuthController.java

@@ -4,8 +4,10 @@ import com.alibaba.fastjson.JSONObject;
 import com.java110.core.base.controller.BaseController;
 import com.java110.core.context.IPageData;
 import com.java110.core.context.PageData;
+import com.java110.core.factory.WechatFactory;
 import com.java110.front.smo.ownerLogin.IOwnerAppLoginSMO;
 import com.java110.utils.constant.CommonConstant;
+import com.java110.vo.ResultVo;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -43,7 +45,7 @@ public class LoginOwnerWechatAuthController extends BaseController {
         IPageData pd = PageData.newInstance().builder("", "", "", JSONObject.toJSONString(params),
                 "login", "", "", "", appId
         );
-        ResponseEntity responseEntity = ownerAppLoginSMOImpl.getPageAccessToken(pd);
+        ResponseEntity responseEntity = ownerAppLoginSMOImpl.getPageAccessToken(pd,request);
         request.setAttribute(CommonConstant.CONTEXT_PAGE_DATA, pd);
         return responseEntity;
     }
@@ -63,4 +65,9 @@ public class LoginOwnerWechatAuthController extends BaseController {
 
     }
 
+    @RequestMapping(path = "/getWId")
+    public ResponseEntity<String> getWId(@RequestParam String appId){
+        return ResultVo.createResponseEntity(WechatFactory.getWId(appId));
+    }
+
 }

+ 12 - 12
service-front/src/main/java/com/java110/front/controller/wechat/WechatGatewayController.java

@@ -6,6 +6,7 @@ import com.java110.core.base.controller.BaseController;
 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.smallWeChat.SmallWeChatDto;
 import com.java110.dto.smallWechatAttr.SmallWechatAttrDto;
 import com.java110.front.smo.wechatGateway.IWechatGatewaySMO;
@@ -60,11 +61,11 @@ public class WechatGatewayController extends BaseController {
         String timestamp = request.getParameter("timestamp");
         String nonce = request.getParameter("nonce");
         String echostr = request.getParameter("echostr");
-        String communityId = request.getParameter("communityId");
+        String wId = request.getParameter(WechatConstant.PAGE_WECHAT_APP_ID);
         String java110AppId = request.getParameter("java110AppId");
 
-        if (!StringUtil.isEmpty(communityId)) {
-            token = getToken(java110AppId,communityId);
+        if (!StringUtil.isEmpty(wId)) {
+            token = getToken(java110AppId, wId);
         }
         String responseStr = "";
         logger.debug("token = " + token + "||||" + "signature = " + signature + "|||" + "timestamp = "
@@ -108,10 +109,9 @@ public class WechatGatewayController extends BaseController {
         String openId = request.getParameter("openid");
         String java110AppId = request.getParameter("java110AppId");
         String responseStr = "";
-
-        String communityId = request.getParameter("communityId");
-        if (!StringUtil.isEmpty(communityId)) {
-            token = getToken(java110AppId,communityId);
+        String wId = request.getParameter(WechatConstant.PAGE_WECHAT_APP_ID);
+        if (!StringUtil.isEmpty(wId)) {
+            token = getToken(java110AppId, wId);
         }
         ResponseEntity<String> responseEntity = null;
         logger.debug("token = " + token + "||||" + "signature = " + signature + "|||" + "timestamp = "
@@ -164,13 +164,12 @@ public class WechatGatewayController extends BaseController {
         return responseEntity;
     }
 
-    private SmallWeChatDto getSmallWechat(String java110AppId, String communityId) {
+    private SmallWeChatDto getSmallWechat(String java110AppId, String appId) {
         IPageData pd = PageData.newInstance().builder("-1", "", "", "",
                 "", "", "", "",
                 java110AppId);
         SmallWeChatDto smallWeChatDto = new SmallWeChatDto();
-        smallWeChatDto.setObjType(SmallWeChatDto.OBJ_TYPE_COMMUNITY);
-        smallWeChatDto.setObjId(communityId);
+        smallWeChatDto.setAppId(appId);
         smallWeChatDto = wechatGatewaySMOImpl.getSmallWechat(pd, smallWeChatDto);
 
         return smallWeChatDto;
@@ -183,8 +182,9 @@ public class WechatGatewayController extends BaseController {
      * @param java110AppId
      * @return
      */
-    private String getToken(String java110AppId, String communityId) {
-        SmallWeChatDto smallWeChatDto = getSmallWechat(java110AppId, communityId);
+    private String getToken(String java110AppId, String wId) {
+        String appId = WechatFactory.getAppId(wId);
+        SmallWeChatDto smallWeChatDto = getSmallWechat(java110AppId, appId);
         String token = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, WechatConstant.TOKEN);
         if (smallWeChatDto == null) {
             return token;

+ 1 - 1
service-front/src/main/java/com/java110/front/smo/ownerLogin/IOwnerAppLoginSMO.java

@@ -39,7 +39,7 @@ public interface IOwnerAppLoginSMO {
      * @return
      * @throws SMOException
      */
-    ResponseEntity<String> getPageAccessToken(IPageData pd) throws SMOException;
+    ResponseEntity<String> getPageAccessToken(IPageData pd,HttpServletRequest request) throws SMOException;
 
 
     /**

+ 33 - 15
service-front/src/main/java/com/java110/front/smo/ownerLogin/impl/OwnerAppLoginSMOImpl.java

@@ -6,6 +6,7 @@ import com.java110.core.base.smo.front.AbstractFrontServiceSMO;
 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;
@@ -75,9 +76,9 @@ public class OwnerAppLoginSMOImpl extends AbstractFrontServiceSMO implements IOw
         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);
+        userDto = super.postForApi(pd, userDto, ServiceCodeConstant.SERVICE_CODE_USER_LOGIN, UserDto.class);
 
-        if(userDto == null){
+        if (userDto == null) {
             responseEntity = new ResponseEntity<>("用户名或密码错误", HttpStatus.BAD_REQUEST);
             return responseEntity;
         }
@@ -166,7 +167,7 @@ public class OwnerAppLoginSMOImpl extends AbstractFrontServiceSMO implements IOw
     }
 
     @Override
-    public ResponseEntity<String> getPageAccessToken(IPageData pd) throws SMOException {
+    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");
@@ -179,19 +180,22 @@ public class OwnerAppLoginSMOImpl extends AbstractFrontServiceSMO implements IOw
         JSONObject param = JSONObject.parseObject(paramStr);
         String redirectUrl = param.getString("redirectUrl");
         String errorUrl = param.getString("errorUrl");
-        SmallWeChatDto smallWeChatDto = getSmallWechat(pd, paramIn);
-
+        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.getAppId());
-            smallWeChatDto.setAppSecret(wechatAuthProperties.getSecret());
+            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", wechatAuthProperties.getWechatAppId())
-                .replace("SECRET", wechatAuthProperties.getWechatAppSecret())
+        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);
@@ -209,7 +213,7 @@ public class OwnerAppLoginSMOImpl extends AbstractFrontServiceSMO implements IOw
 
         int loginFlag = paramIn.getInteger("loginFlag");
         //说明是登录页面,下发code 就可以,不需要下发key 之类
-        if(loginFlag == LOGIN_PAGE){
+        if (loginFlag == LOGIN_PAGE) {
             //将openId放到redis 缓存,给前段下发临时票据
             String code = UUID.randomUUID().toString();
             CommonCache.setValue(code, openId, expireTime);
@@ -247,7 +251,7 @@ public class OwnerAppLoginSMOImpl extends AbstractFrontServiceSMO implements IOw
         userDto.setUserId(ownerAppUserDtos.get(0).getUserId());
         UserDto tmpUserDto = super.getForApi(pd, userDto, ServiceCodeConstant.QUERY_USER_SECRET, UserDto.class);
 
-        if(StringUtil.isEmpty(tmpUserDto.getKey())){
+        if (StringUtil.isEmpty(tmpUserDto.getKey())) {
             String code = UUID.randomUUID().toString();
             CommonCache.setValue(code, openId, expireTime);
             return ResultVo.redirectPage(errorUrl + "?code=" + code);
@@ -278,6 +282,20 @@ public class OwnerAppLoginSMOImpl extends AbstractFrontServiceSMO implements IOw
         param.put("redirectUrl", redirectUrl);
         param.put("errorUrl", errorUrl);
         CommonCache.setValue(urlCode, param.toJSONString(), expireTime);
+        String wId = request.getHeader("wid");
+        SmallWeChatDto smallWeChatDto = null;
+        if (!StringUtil.isEmpty(wId)) {
+            JSONObject paramIn = new JSONObject();
+            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());
+        }
 
         URL url = null;
         String openUrl = "";
@@ -290,14 +308,14 @@ public class OwnerAppLoginSMOImpl extends AbstractFrontServiceSMO implements IOw
             }
 
             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+"&loginFlag="+loginFlag),
+                                                    + "/app/loginOwnerWechatAuth?appId=992020061452450002&urlCode=" + urlCode + "&loginFlag=" + loginFlag + "&wId=" + wId),
                                             "UTF-8")).replace("STATE", "1");
 
         } catch (Exception e) {
@@ -379,7 +397,7 @@ public class OwnerAppLoginSMOImpl extends AbstractFrontServiceSMO implements IOw
                 pd.getAppId());
         responseEntity = this.callCenterService(restTemplate, pd, "",
                 ServiceConstant.SERVICE_API_URL + "/api/smallWeChat.listSmallWeChats?appId="
-                        + paramIn.getString("appId") + "&page=1&row=1&communityId="+ownerAppUserDtos.get(0).getCommunityId(), HttpMethod.GET);
+                        + paramIn.getString("appId") + "&page=1&row=1&communityId=" + ownerAppUserDtos.get(0).getCommunityId(), HttpMethod.GET);
 
         if (responseEntity.getStatusCode() != HttpStatus.OK) {
             return responseEntity;