java110
2020-06-15 9bfac9553887f1b5d37fa2eeb49263d9ef6dfb44
service-front/src/main/java/com/java110/front/smo/ownerLogin/impl/OwnerAppLoginSMOImpl.java
@@ -2,16 +2,17 @@
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.dto.owner.OwnerAppUserDto;
import com.java110.front.properties.WechatAuthProperties;
import com.java110.front.smo.AppAbstractComponentSMO;
import com.java110.front.smo.ownerLogin.IOwnerAppLoginSMO;
import com.java110.utils.cache.CommonCache;
import com.java110.utils.constant.CommonConstant;
import com.java110.utils.constant.ResponseConstant;
import com.java110.utils.constant.ServiceCodeConstant;
import com.java110.utils.constant.ServiceConstant;
import com.java110.utils.constant.WechatConstant;
import com.java110.utils.exception.SMOException;
@@ -32,6 +33,7 @@
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@@ -39,7 +41,7 @@
 * wx登录
 */
@Service("ownerAppLoginSMOImpl")
public class OwnerAppLoginSMOImpl extends AppAbstractComponentSMO implements IOwnerAppLoginSMO {
public class OwnerAppLoginSMOImpl extends AbstractFrontServiceSMO implements IOwnerAppLoginSMO {
    private final static Logger logger = LoggerFactory.getLogger(OwnerAppLoginSMOImpl.class);
@@ -55,9 +57,56 @@
    private WechatAuthProperties wechatAuthProperties;
    @Override
    public ResponseEntity<String>
    doLogin(IPageData pd) throws SMOException {
        return businessProcess(pd);
    public ResponseEntity<String> doLogin(IPageData pd) throws SMOException {
        JSONObject paramIn = JSONObject.parseObject(pd.getReqData());
        Assert.hasKeyAndValue(paramIn, "username", "请求报文中未包含用户名");
        Assert.hasKeyAndValue(paramIn, "password", "请求报文中未包含密码");
        logger.debug("doLogin入参:" + paramIn.toJSONString());
        ResponseEntity<String> responseEntity;
        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) {
            return responseEntity;
        }
        JSONObject userInfo = JSONObject.parseObject(responseEntity.getBody());
        //根据用户查询商户信息
        String userId = userInfo.getString("userId");
        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("result", 0);
        paramOut.put("owner", appUser);
        paramOut.put("token", userInfo.getString("token"));
        String appId = pd.getAppId();
        if ("992020061452450002".equals(appId)) { //公众号
            return wechat(pd, paramIn, paramOut, userId, ownerAppUserDtos);
        } else if ("992019111758490006".equals(appId)) { //小程序
            return mina(pd, paramIn, paramOut, userId, ownerAppUserDtos);
        } else {//app
            return new ResponseEntity<>(paramOut.toJSONString(), HttpStatus.OK);
        }
    }
    @Override
@@ -92,15 +141,11 @@
        pd = PageData.newInstance().builder("-1", "", "", pd.getReqData(),
                "", "", "", "",
                pd.getAppId());
        ResponseEntity responseEntity = this.callCenterService(restTemplate, pd, "", ServiceConstant.SERVICE_API_URL + "/api/owner.listAppUserBindingOwners?openId=" + openId, HttpMethod.GET);
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            //将openId放到redis 缓存,给前段下发临时票据
            String code = UUID.randomUUID().toString();
            CommonCache.setValue(code, openId, expireTime);
            return ResultVo.redirectPage("/#/pages/login/login?code=" + code);
        }
        JSONObject ownerInfo = JSONObject.parseObject(responseEntity.getBody().toString());
        if (ownerInfo.getInteger("total") < 1) {
        OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto();
        ownerAppUserDto.setOpenId(openId);
        List<OwnerAppUserDto> ownerAppUserDtos = super.getForApis(pd, ownerAppUserDto, ServiceCodeConstant.LIST_APPUSERBINDINGOWNERS, OwnerAppUserDto.class);
        if (ownerAppUserDtos == null || ownerAppUserDtos.size() < 1) {
            //将openId放到redis 缓存,给前段下发临时票据
            String code = UUID.randomUUID().toString();
            CommonCache.setValue(code, openId, expireTime);
@@ -109,8 +154,8 @@
        // String accessToken = paramObj.getString("access_token");//暂时不用
        Map userMap = new HashMap();
        userMap.put(CommonConstant.LOGIN_USER_ID, ownerInfo.getString("userId"));
        userMap.put(CommonConstant.LOGIN_USER_NAME, ownerInfo.getString("appUserName"));
        userMap.put(CommonConstant.LOGIN_USER_ID, ownerAppUserDtos.get(0).getUserId());
        userMap.put(CommonConstant.LOGIN_USER_NAME, ownerAppUserDtos.get(0).getAppUserName());
        String token = "";
        try {
            token = AuthenticationFactory.createAndSaveToken(userMap);
@@ -170,69 +215,6 @@
        return ResultVo.createResponseEntity(ResultVo.CODE_MACHINE_OK, ResultVo.MSG_OK, urlObj);
    }
    @Override
    protected void validate(IPageData pd, JSONObject paramIn) {
        Assert.hasKeyAndValue(paramIn, "username", "请求报文中未包含用户名");
        Assert.hasKeyAndValue(paramIn, "password", "请求报文中未包含密码");
    }
    @Override
    protected ResponseEntity<String> doBusinessProcess(IPageData pd, JSONObject paramIn) {
        logger.debug("doLogin入参:" + paramIn.toJSONString());
        ResponseEntity<String> responseEntity;
        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) {
            return responseEntity;
        }
        JSONObject userInfo = JSONObject.parseObject(responseEntity.getBody());
        //根据用户查询商户信息
        String userId = userInfo.getString("userId");
        pd = PageData.newInstance().builder(userId, "", "", pd.getReqData(),
                "", "", "", "",
                pd.getAppId());
        responseEntity = this.callCenterService(restTemplate, pd, "", ServiceConstant.SERVICE_API_URL + "/api/owner.listAppUserBindingOwners?userid=" + userId, HttpMethod.GET);
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            return responseEntity;
        }
        JSONObject ownerInfo = JSONObject.parseObject(responseEntity.getBody().toString());
        if (ownerInfo.getInteger("total") < 1) {
            responseEntity = new ResponseEntity<>("用户未绑定业主", HttpStatus.BAD_REQUEST);
            return responseEntity;
        }
        JSONArray auditAppUserBindingOwners = ownerInfo.getJSONArray("auditAppUserBindingOwners");
        JSONObject appUser = auditAppUserBindingOwners.getJSONObject(0);
        appUser.put("userId", userId);
        appUser.put("userName", paramIn.getString("username"));
        JSONObject paramOut = new JSONObject();
        paramOut.put("result", 0);
        paramOut.put("owner", appUser);
        paramOut.put("token", userInfo.getString("token"));
        String appId = pd.getAppId();
        if ("992020061452450002".equals(appId)) { //公众号
            return wechat(pd, paramIn, paramOut, userId, auditAppUserBindingOwners);
        } else if ("992019111758490006".equals(appId)) { //小程序
            return mina(pd, paramIn, paramOut, userId, auditAppUserBindingOwners);
        } else {//app
            return new ResponseEntity<>(paramOut.toJSONString(), HttpStatus.OK);
        }
    }
    /**
     * 公众号登录
     *
@@ -240,11 +222,11 @@
     * @param paramIn
     * @param paramOut
     * @param userId
     * @param auditAppUserBindingOwners
     * @param ownerAppUserDtos
     * @return
     */
    private ResponseEntity<String> wechat(IPageData pd, JSONObject paramIn, JSONObject paramOut, String userId,
                                          JSONArray auditAppUserBindingOwners) {
                                          List<OwnerAppUserDto> ownerAppUserDtos) {
        ResponseEntity<String> responseEntity = null;
        //查询微信信息
@@ -262,39 +244,37 @@
            return responseEntity;
        }
        JSONObject curOwnerApp = judgeCurrentOwnerBind(auditAppUserBindingOwners, OwnerAppUserDto.APP_TYPE_WECHAT);
        OwnerAppUserDto curOwnerApp = judgeCurrentOwnerBind(ownerAppUserDtos, OwnerAppUserDto.APP_TYPE_WECHAT);
        //说明 当前的openId 就是最新的
        if (curOwnerApp != null && openId.equals(curOwnerApp.getString("openId"))) {
        if (curOwnerApp != null && openId.equals(curOwnerApp.getOpenId())) {
            return new ResponseEntity<>(paramOut.toJSONString(), HttpStatus.OK);
        }
        JSONObject userOwnerInfo = new JSONObject();
        userOwnerInfo.put("openId", openId);
        userOwnerInfo.put("appType", OwnerAppUserDto.APP_TYPE_WECHAT);
        OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto();
        ownerAppUserDto.setOpenId(openId);
        ownerAppUserDto.setAppType(OwnerAppUserDto.APP_TYPE_WECHAT);
        if (curOwnerApp != null) {
            userOwnerInfo.put("appUserId", curOwnerApp.getString("appUserId"));
            userOwnerInfo.put("communityId", curOwnerApp.getString("communityId"));
            ownerAppUserDto.setAppUserId(curOwnerApp.getAppUserId());
            ownerAppUserDto.setCommunityId(curOwnerApp.getCommunityId());
        } else {
            userOwnerInfo.put("oldAppUserId", auditAppUserBindingOwners.getJSONObject(0).getString("appUserId"));
            userOwnerInfo.put("appUserId", "-1");
            userOwnerInfo.put("communityId", auditAppUserBindingOwners.getJSONObject(0).getString("communityId"));
            ownerAppUserDto.setOldAppUserId(ownerAppUserDtos.get(0).getAppUserId());
            ownerAppUserDto.setAppUserId("-1");
            ownerAppUserDto.setCommunityId(ownerAppUserDtos.get(0).getCommunityId());
        }
        //查询微信信息
        pd = PageData.newInstance().builder(userId, "", "", pd.getReqData(),
                "", "", "", "",
                pd.getAppId());
        responseEntity = this.callCenterService(restTemplate, pd, userOwnerInfo.toJSONString(),
                ServiceConstant.SERVICE_API_URL + "/api/owner.refreshAppUserBindingOwnerOpenId", HttpMethod.POST);
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            return responseEntity;
        }
        super.postForApi(pd, ownerAppUserDto, ServiceCodeConstant.REFRESH_APP_USER_BINDING_OWNER_OPEN_ID,
                OwnerAppUserDto.class);
        return new ResponseEntity<>(paramOut.toJSONString(), HttpStatus.OK);
    }
    private ResponseEntity<String> mina(IPageData pd, JSONObject paramIn, JSONObject paramOut, String userId, JSONArray auditAppUserBindingOwners) {
    private ResponseEntity<String> mina(IPageData pd, JSONObject paramIn, JSONObject paramOut, String userId, List<OwnerAppUserDto> ownerAppUserDtos) {
        ResponseEntity<String> responseEntity = null;
        //查询微信信息
@@ -339,50 +319,46 @@
        String openId = responseObj.getString("openid");
        JSONObject curOwnerApp = judgeCurrentOwnerBind(auditAppUserBindingOwners, OwnerAppUserDto.APP_TYPE_WECHAT_MINA);
        OwnerAppUserDto ownerAppUserDto = judgeCurrentOwnerBind(ownerAppUserDtos, OwnerAppUserDto.APP_TYPE_WECHAT_MINA);
        //说明 当前的openId 就是最新的
        if (curOwnerApp != null && openId.equals(curOwnerApp.getString("openId"))) {
        if (ownerAppUserDto != null && openId.equals(ownerAppUserDto.getOpenId())) {
            return new ResponseEntity<>(paramOut.toJSONString(), HttpStatus.OK);
        }
        JSONObject userOwnerInfo = new JSONObject();
        userOwnerInfo.put("openId", openId);
        userOwnerInfo.put("appType", OwnerAppUserDto.APP_TYPE_WECHAT_MINA);
        if (curOwnerApp != null) {
            userOwnerInfo.put("appUserId", curOwnerApp.getString("appUserId"));
            userOwnerInfo.put("communityId", curOwnerApp.getString("communityId"));
        OwnerAppUserDto tmpOwnerAppUserDto = new OwnerAppUserDto();
        ownerAppUserDto.setOpenId(openId);
        ownerAppUserDto.setAppType(OwnerAppUserDto.APP_TYPE_WECHAT_MINA);
        if (ownerAppUserDto != null) {
            ownerAppUserDto.setAppUserId(tmpOwnerAppUserDto.getAppUserId());
            ownerAppUserDto.setCommunityId(tmpOwnerAppUserDto.getCommunityId());
        } else {
            userOwnerInfo.put("oldAppUserId", auditAppUserBindingOwners.getJSONObject(0).getString("appUserId"));
            userOwnerInfo.put("appUserId", "-1");
            userOwnerInfo.put("communityId", auditAppUserBindingOwners.getJSONObject(0).getString("communityId"));
            ownerAppUserDto.setOldAppUserId(ownerAppUserDtos.get(0).getAppUserId());
            ownerAppUserDto.setAppUserId("-1");
            ownerAppUserDto.setCommunityId(ownerAppUserDtos.get(0).getCommunityId());
        }
        //查询微信信息
        pd = PageData.newInstance().builder(userId, "", "", pd.getReqData(),
                "", "", "", "",
                pd.getAppId());
        responseEntity = this.callCenterService(restTemplate, pd, userOwnerInfo.toJSONString(),
                ServiceConstant.SERVICE_API_URL + "/api/owner.refreshAppUserBindingOwnerOpenId", HttpMethod.POST);
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            return responseEntity;
        }
        super.postForApi(pd, ownerAppUserDto, ServiceCodeConstant.REFRESH_APP_USER_BINDING_OWNER_OPEN_ID,
                OwnerAppUserDto.class);
        return new ResponseEntity<>(paramOut.toJSONString(), HttpStatus.OK);
    }
    /**
     * 判断 绑定表里是否存在当前 端 绑定信息
     *
     * @param auditAppUserBindingOwners
     * @param ownerAppUserDtos
     * @param appType
     * @return
     */
    private JSONObject judgeCurrentOwnerBind(JSONArray auditAppUserBindingOwners, String appType) {
    private OwnerAppUserDto judgeCurrentOwnerBind(List<OwnerAppUserDto> ownerAppUserDtos, String appType) {
        for (int appUserIndex = 0; appUserIndex < auditAppUserBindingOwners.size(); appUserIndex++) {
            if (appType.equals(auditAppUserBindingOwners.getJSONObject(appUserIndex).getString("appType"))) {
                return auditAppUserBindingOwners.getJSONObject(appUserIndex);
        for (OwnerAppUserDto ownerAppUserDto : ownerAppUserDtos) {
            if (appType.equals(ownerAppUserDto.getAppType())) {
                return ownerAppUserDto;
            }
        }
        return null;