From 5700f42a37a2f9ecc27161e2b0559a820d320f6b Mon Sep 17 00:00:00 2001
From: wuxw <928255095@qq.com>
Date: 星期一, 17 三月 2025 15:11:40 +0800
Subject: [PATCH] 开发完成同一个手机号登录不同物业公司的支持

---
 service-user/src/main/java/com/java110/user/cmd/user/UserLoginCmd.java |  255 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 254 insertions(+), 1 deletions(-)

diff --git a/service-user/src/main/java/com/java110/user/cmd/user/UserLoginCmd.java b/service-user/src/main/java/com/java110/user/cmd/user/UserLoginCmd.java
index 544cac0..050d396 100644
--- a/service-user/src/main/java/com/java110/user/cmd/user/UserLoginCmd.java
+++ b/service-user/src/main/java/com/java110/user/cmd/user/UserLoginCmd.java
@@ -1,4 +1,257 @@
 package com.java110.user.cmd.user;
 
-public class UserLoginCmd {
+import com.alibaba.fastjson.JSONObject;
+import com.java110.core.annotation.Java110Cmd;
+import com.java110.core.context.ICmdDataFlowContext;
+import com.java110.core.event.cmd.Cmd;
+import com.java110.core.event.cmd.CmdEvent;
+import com.java110.core.factory.AuthenticationFactory;
+import com.java110.core.factory.GenerateCodeFactory;
+import com.java110.core.log.LoggerFactory;
+import com.java110.dto.app.AppDto;
+import com.java110.dto.community.CommunityDto;
+import com.java110.dto.msg.SmsDto;
+import com.java110.dto.owner.OwnerDto;
+import com.java110.dto.user.UserAttrDto;
+import com.java110.dto.user.UserDto;
+import com.java110.intf.common.ISmsInnerServiceSMO;
+import com.java110.intf.community.ICommunityInnerServiceSMO;
+import com.java110.intf.user.*;
+import com.java110.po.owner.OwnerAppUserPo;
+import com.java110.po.user.UserPo;
+import com.java110.po.user.UserAttrPo;
+import com.java110.utils.constant.CommonConstant;
+import com.java110.utils.constant.ResponseConstant;
+import com.java110.utils.constant.UserLevelConstant;
+import com.java110.utils.exception.CmdException;
+import com.java110.utils.exception.SMOException;
+import com.java110.utils.util.*;
+import com.java110.vo.ResultVo;
+import org.slf4j.Logger;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+/**
+ * 鐢ㄦ埛鐧诲綍
+ */
+@Java110Cmd(serviceCode = "user.userLogin")
+public class UserLoginCmd extends Cmd {
+
+    private final static Logger logger = LoggerFactory.getLogger(UserLoginCmd.class);
+
+
+    @Autowired
+    private IUserInnerServiceSMO userInnerServiceSMOImpl;
+
+    @Autowired
+    private IUserAttrV1InnerServiceSMO userAttrV1InnerServiceSMOImpl;
+
+    @Autowired
+    private ISmsInnerServiceSMO smsInnerServiceSMOImpl;
+
+    @Autowired
+    private IOwnerAppUserV1InnerServiceSMO ownerAppUserV1InnerServiceSMOImpl;
+
+    @Autowired
+    private IOwnerV1InnerServiceSMO ownerV1InnerServiceSMOImpl;
+
+    @Autowired
+    private ICommunityInnerServiceSMO communityInnerServiceSMOImpl;
+
+    @Autowired
+    private IUserV1InnerServiceSMO userV1InnerServiceSMOImpl;
+
+    @Override
+    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
+        if (!reqJson.containsKey("userName")) {
+            Assert.hasKeyAndValue(reqJson, "key", "鏈寘鍚玨ey");
+        }
+
+        if (!reqJson.containsKey("key")) {
+            Assert.hasKeyAndValue(reqJson, "userName", "鏈寘鍚玼serName");
+            Assert.hasKeyAndValue(reqJson, "password", "鏈寘鍚玴assword");
+        }
+    }
+
+    @Override
+    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
+        //1.0 浼樺厛鐢� 鎵嬫満鍙风櫥褰�
+        UserDto userDto = new UserDto();
+        String errorInfo = "";
+        if (reqJson.containsKey("levelCd")) {
+            userDto.setLevelCd(reqJson.getString("levelCd"));
+        }
+        if (reqJson.containsKey("userName")) {
+            if (ValidatorUtil.isMobile(reqJson.getString("userName"))) {//鐢ㄦ埛涓存椂绉橀挜鐧诲綍
+                userDto.setTel(reqJson.getString("userName"));
+            } else {
+                userDto.setUserName(reqJson.getString("userName"));
+            }
+            //楠岃瘉鐮佺櫥褰�
+            if (reqJson.containsKey("loginByPhone") && reqJson.getBoolean("loginByPhone")) {
+                SmsDto smsDto = new SmsDto();
+                smsDto.setTel(reqJson.getString("userName"));
+                smsDto.setCode(reqJson.getString("password"));
+                smsDto = smsInnerServiceSMOImpl.validateCode(smsDto);
+                if (!smsDto.isSuccess()) {
+                    throw new SMOException("楠岃瘉鐮侀敊璇�");
+                }
+
+            } else {
+                userDto.setPassword(AuthenticationFactory.passwdMd5(reqJson.getString("password")));
+            }
+            errorInfo = "鐢ㄦ埛鍚嶆垨瀵嗙爜閿欒";
+        } else {
+            userDto.setKey(reqJson.getString("key"));
+            errorInfo = "涓存椂绁ㄦ嵁閿欒";
+        }
+
+        List<UserDto> userDtos = userInnerServiceSMOImpl.getUsers(userDto);
+
+        //todo 楠岃瘉鐮佺櫥褰曪紝鍒ゆ柇鏄惁鏄惁涓氫富锛屽苟涓旀槸鍚︾粦瀹氬叧绯� 濡傛灉娌℃湁 鑷姩缁戝畾鍏崇郴
+        if (ListUtil.isNull(userDtos)) {
+            userDtos = ifOwnerLoginByPhone(reqJson, context);
+        }
+        if (ListUtil.isNull(userDtos)) {
+            throw new SMOException(errorInfo);
+        }
+
+
+        //琛ㄥ悕鐧诲綍鎴愬姛
+        UserDto tmpUserDto = userDtos.get(0);
+
+        List<UserAttrDto> userAttrDtos = tmpUserDto.getUserAttrs();
+
+        UserAttrDto userAttrDto = getCurrentUserAttrDto(userAttrDtos, UserAttrDto.SPEC_KEY);
+        String newKey = UUID.randomUUID().toString();
+        if (userAttrDto != null) {
+            UserAttrPo userAttrPo = BeanConvertUtil.covertBean(userAttrDto, UserAttrPo.class);
+            userAttrPo.setValue(newKey);
+            userAttrPo.setStatusCd("0");
+            //super.update(context, userAttrPo, BusinessTypeConstant.BUSINESS_TYPE_UPDATE_USER_ATTR_INFO);
+            userAttrV1InnerServiceSMOImpl.updateUserAttr(userAttrPo);
+        } else {
+            UserAttrPo userAttrPo = new UserAttrPo();
+            userAttrPo.setAttrId("-1");
+            userAttrPo.setUserId(tmpUserDto.getUserId());
+            userAttrPo.setSpecCd(UserAttrDto.SPEC_KEY);
+            userAttrPo.setValue(newKey);
+            userAttrPo.setStatusCd("0");
+            //super.insert(context, userAttrPo, BusinessTypeConstant.BUSINESS_TYPE_SAVE_USER_ATTR_INFO);
+            userAttrV1InnerServiceSMOImpl.saveUserAttr(userAttrPo);
+        }
+
+
+        try {
+            Map userMap = new HashMap();
+            userMap.put(CommonConstant.LOGIN_USER_ID, tmpUserDto.getUserId());
+            userMap.put(CommonConstant.LOGIN_USER_NAME, tmpUserDto.getUserName());
+            String token = AuthenticationFactory.createAndSaveToken(userMap);
+            tmpUserDto.setPassword("");
+            tmpUserDto.setToken(token);
+            tmpUserDto.setKey(newKey);
+            context.setResponseEntity(ResultVo.createResponseEntity(tmpUserDto));
+        } catch (Exception e) {
+            logger.error("鐧诲綍寮傚父锛�", e);
+            throw new SMOException(ResponseConstant.RESULT_CODE_INNER_ERROR, "绯荤粺鍐呴儴閿欒锛岃鑱旂郴绠$悊鍛�");
+        }
+    }
+
+    /**
+     * 楠岃瘉鐮佺櫥褰曪紝鍒ゆ柇鏄惁鏄惁涓氫富锛屽苟涓旀槸鍚︾粦瀹氬叧绯� 濡傛灉娌℃湁 鑷姩缁戝畾鍏崇郴
+     *
+     * @param reqJson
+     * @param context
+     */
+    private List<UserDto> ifOwnerLoginByPhone(JSONObject reqJson, ICmdDataFlowContext context) {
+        if (!reqJson.containsKey("loginByPhone") || !reqJson.getBoolean("loginByPhone")) {
+            return null;
+        }
+        String appId = context.getReqHeaders().get("app-id");
+
+        //todo 涓氫富鎵嬫満绔�
+        if (!AppDto.WECHAT_OWNER_APP_ID.equals(appId)
+                && !AppDto.WECHAT_MINA_OWNER_APP_ID.equals(appId)
+                && !AppDto.OWNER_APP_APP_ID.equals(appId)) {
+            return null;
+        }
+
+        if (StringUtil.isEmpty(reqJson.getString("userName"))) {
+            return null;
+        }
+
+        // todo 鏌ヨ涓氫富鎴栨垚鍛�
+        OwnerDto ownerDto = new OwnerDto();
+        ownerDto.setLink(reqJson.getString("userName"));
+        ownerDto.setPage(1);
+        ownerDto.setRow(1);
+        List<OwnerDto> ownerDtos = ownerV1InnerServiceSMOImpl.queryOwners(ownerDto);
+
+        // 璇存槑涓氫富涓嶅瓨鍦� 鐩存帴杩斿洖璺戝紓甯�
+        if (ownerDtos == null || ownerDtos.size() < 1) {
+            return null;
+        }
+
+        CommunityDto communityDto = new CommunityDto();
+        communityDto.setCommunityId(ownerDtos.get(0).getCommunityId());
+        List<CommunityDto> communityDtos = communityInnerServiceSMOImpl.queryCommunitys(communityDto);
+        Assert.listNotNull(communityDtos, "鏈寘鍚皬鍖轰俊鎭�");
+        CommunityDto tmpCommunityDto = communityDtos.get(0);
+
+        UserPo userPo = new UserPo();
+        userPo.setUserId(GenerateCodeFactory.getUserId());
+        userPo.setName(ownerDtos.get(0).getName());
+        userPo.setTel(ownerDtos.get(0).getLink());
+        userPo.setPassword(AuthenticationFactory.passwdMd5(reqJson.getString("password")));
+        userPo.setLevelCd(UserLevelConstant.USER_LEVEL_ORDINARY);
+        userPo.setAge(ownerDtos.get(0).getAge());
+        userPo.setAddress(ownerDtos.get(0).getAddress());
+        userPo.setSex(ownerDtos.get(0).getSex());
+        int flag = userV1InnerServiceSMOImpl.saveUser(userPo);
+        if (flag < 1) {
+            throw new CmdException("娉ㄥ唽澶辫触");
+        }
+
+        OwnerAppUserPo ownerAppUserPo = new OwnerAppUserPo();
+        //鐘舵�佺被鍨嬶紝10000 瀹℃牳涓紝12000 瀹℃牳鎴愬姛锛�13000 瀹℃牳澶辫触
+        ownerAppUserPo.setState("12000");
+        ownerAppUserPo.setAppTypeCd("10010");
+        ownerAppUserPo.setAppUserId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_appUserId));
+        ownerAppUserPo.setMemberId(ownerDtos.get(0).getMemberId());
+        ownerAppUserPo.setCommunityName(tmpCommunityDto.getName());
+        ownerAppUserPo.setCommunityId(ownerDtos.get(0).getCommunityId());
+        ownerAppUserPo.setAppUserName(ownerDtos.get(0).getName());
+        ownerAppUserPo.setIdCard(ownerDtos.get(0).getIdCard());
+        ownerAppUserPo.setAppType("WECHAT");
+        ownerAppUserPo.setLink(ownerDtos.get(0).getLink());
+        ownerAppUserPo.setUserId(userPo.getUserId());
+        ownerAppUserPo.setOpenId("-1");
+
+        flag = ownerAppUserV1InnerServiceSMOImpl.saveOwnerAppUser(ownerAppUserPo);
+        if (flag < 1) {
+            throw new CmdException("娣诲姞鐢ㄦ埛涓氫富鍏崇郴澶辫触");
+        }
+
+        UserDto userDto = new UserDto();
+        userDto.setUserId(userPo.getUserId());
+        List<UserDto> userDtos = userInnerServiceSMOImpl.getUsers(userDto);
+        return userDtos;
+    }
+
+    private UserAttrDto getCurrentUserAttrDto(List<UserAttrDto> userAttrDtos, String specCd) {
+        if (userAttrDtos == null) {
+            return null;
+        }
+        for (UserAttrDto userAttrDto : userAttrDtos) {
+            if (specCd.equals(userAttrDto.getSpecCd())) {
+                return userAttrDto;
+            }
+        }
+
+        return null;
+    }
 }

--
Gitblit v1.8.0