Browse Source

BUG修复

yingp 6 years ago
parent
commit
cfff73fa2d
15 changed files with 629 additions and 107 deletions
  1. 1 1
      README.md
  2. 54 0
      apis/account-api/src/main/java/com/usoftchina/uu/account/api/PasswordApi.java
  3. 34 0
      apis/account-api/src/main/java/com/usoftchina/uu/account/dto/EmailCheckToken.java
  4. 33 0
      apis/mobile-grpc-api/src/test/java/com/usoftchina/uu/mobile/grpc/test/AbstractGRpcServerTest.java
  5. 154 0
      apis/mobile-grpc-api/src/test/java/com/usoftchina/uu/mobile/grpc/test/MobileAccountServiceTest.java
  6. 15 0
      services/account-service/src/main/java/com/usoftchina/uu/account/po/Company.java
  7. 4 1
      services/account-service/src/main/java/com/usoftchina/uu/account/service/impl/AccountServiceImpl.java
  8. 58 0
      services/account-service/src/main/java/com/usoftchina/uu/account/service/impl/CompanyServiceImpl.java
  9. 71 0
      services/account-service/src/main/java/com/usoftchina/uu/account/service/impl/PasswordServiceImpl.java
  10. 0 1
      services/mobile-grpc-service/build.gradle
  11. 32 22
      services/mobile-grpc-service/src/main/java/com/usoftchina/uu/mobile/grpc/service/impl/MobileAccountServiceImplWithoutAuth.java
  12. 20 38
      services/mobile-grpc-service/src/main/java/com/usoftchina/uu/mobile/grpc/service/impl/MobilePasswordServiceImpl.java
  13. 122 43
      services/mobile-grpc-service/src/main/java/com/usoftchina/uu/mobile/grpc/util/MobileBeanMapper.java
  14. 3 1
      services/mobile-grpc-service/src/main/java/com/usoftchina/uu/mobile/grpc/util/ResponseUtils.java
  15. 28 0
      shared/core/src/main/java/com/usoftchina/uu/util/StringUtils.java

+ 1 - 1
README.md

@@ -26,7 +26,7 @@
 ### 本地构建
 
 ```
-gradle build -x test
+gradlew build -x test
 ```
 
 ### 测试运行环境

+ 54 - 0
apis/account-api/src/main/java/com/usoftchina/uu/account/api/PasswordApi.java

@@ -0,0 +1,54 @@
+package com.usoftchina.uu.account.api;
+
+import com.usoftchina.uu.account.dto.EmailCheckToken;
+
+/**
+ * @author yingp
+ * @date 2019/4/25
+ */
+public interface PasswordApi {
+    /**
+     * 通过手机号找回密码,发送短信验证码
+     *
+     * @param mobile
+     * @return 验证码
+     */
+    String sendCheckCodeByMobile(String mobile);
+
+    /**
+     * 通过手机号找回密码,校验验证码
+     *
+     * @param token  获取验证码返回的token
+     * @param mobile 获取验证码的手机号
+     * @param code   用户输入的验证码
+     * @return 校验通过 true
+     */
+    boolean checkByCheckCode(String token, String mobile, String code);
+
+    /**
+     * 通过手机号找回密码,重置密码
+     *
+     * @param token    获取验证码返回的token
+     * @param mobile   获取验证码的手机号
+     * @param code     用户输入的验证码
+     * @param password 用户新密码,未加密
+     * @return 重置成功 true
+     */
+    boolean resetPasswordByCheckCode(String token, String mobile, String code, String password);
+
+    /**
+     * 通过邮箱修改密码,获取邮箱
+     *
+     * @param mobile 手机号
+     * @return 邮箱
+     */
+    EmailCheckToken getEmailByMobile(String mobile);
+
+    /**
+     * 发送邮件
+     *
+     * @param token getEmailByMobile获取的EmailCheckToken.token
+     * @return
+     */
+    boolean sendCheckTokenByEmail(String token);
+}

+ 34 - 0
apis/account-api/src/main/java/com/usoftchina/uu/account/dto/EmailCheckToken.java

@@ -0,0 +1,34 @@
+package com.usoftchina.uu.account.dto;
+
+/**
+ * @author yingp
+ * @date 2019/4/25
+ */
+public class EmailCheckToken {
+    private String email;
+    private String token;
+
+    public EmailCheckToken() {
+    }
+
+    public EmailCheckToken(String email, String token) {
+        this.email = email;
+        this.token = token;
+    }
+
+    public String getEmail() {
+        return email;
+    }
+
+    public void setEmail(String email) {
+        this.email = email;
+    }
+
+    public String getToken() {
+        return token;
+    }
+
+    public void setToken(String token) {
+        this.token = token;
+    }
+}

+ 33 - 0
apis/mobile-grpc-api/src/test/java/com/usoftchina/uu/mobile/grpc/test/AbstractGRpcServerTest.java

@@ -0,0 +1,33 @@
+package com.usoftchina.uu.mobile.grpc.test;
+
+import io.grpc.ManagedChannel;
+import io.grpc.ManagedChannelBuilder;
+import org.junit.After;
+import org.junit.Before;
+
+import java.util.Optional;
+
+/**
+ * @author yingp
+ * @date 2019/4/12
+ */
+public abstract class AbstractGRpcServerTest {
+
+    protected ManagedChannel channel;
+
+    @Before
+    public final void setupChannels() {
+        channel = onChannelBuild(ManagedChannelBuilder.forAddress("10.1.81.83", 9620)
+                .usePlaintext()
+        ).build();
+    }
+
+    protected ManagedChannelBuilder<?> onChannelBuild(ManagedChannelBuilder<?> channelBuilder) {
+        return channelBuilder;
+    }
+
+    @After
+    public final void shutdownChannels() {
+        Optional.ofNullable(channel).ifPresent(ManagedChannel::shutdownNow);
+    }
+}

+ 154 - 0
apis/mobile-grpc-api/src/test/java/com/usoftchina/uu/mobile/grpc/test/MobileAccountServiceTest.java

@@ -0,0 +1,154 @@
+package com.usoftchina.uu.mobile.grpc.test;
+
+import com.usoftchina.uu.mobile.grpc.api.*;
+import io.grpc.StatusRuntimeException;
+import org.junit.Before;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.runners.MethodSorters;
+
+/**
+ * @author yingp
+ * @date 2019/4/22
+ */
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class MobileAccountServiceTest extends AbstractGRpcServerTest{
+
+    private AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub;
+
+    @Before
+    public void setup() {
+        accountServiceBlockingStub = AccountServiceGrpc.newBlockingStub(channel);
+    }
+
+    @Test
+    public void testA_signin(){
+        try {
+            AccountSigninResponse response = accountServiceBlockingStub.signin(AccountSigninRequest.newBuilder()
+                    .setMobile("13798490565")
+                    .setPassword("fghjFGHJ12")
+                    .setDeviceInfo(DeviceInfo.newBuilder()
+                            .setOs(DeviceInfo.OS.ANDROID))
+                    .build());
+            ResponseHeader header = response.getResponseHeader();
+            if (header.getSuccess()) {
+                System.out.println(response.getAccount());
+                if (response.getActiveCompanyId() > 0) {
+                    System.out.println("current selected company " + response.getActiveCompanyId());
+                } else if (response.getCompanyCount() > 0){
+                    response.getCompanyList().forEach(System.out::println);
+                }
+            } else {
+                // error return by ResponseHeader
+                System.err.println(header.getCode() + "," + header.getMessage());
+            }
+        } catch (StatusRuntimeException e) {
+            // error return by Exception
+            System.err.println(e.getStatus().getCode() + "," + e.getStatus().getDescription());
+        }
+    }
+
+    @Test
+    public void testB_signup() {
+        AccountSignupRequest request = AccountSignupRequest.newBuilder()
+                .setMobile("13798490565")
+                .setEmail("yingp@usoftchina.com")
+                .setRealname("应鹏")
+                .setSex(AccountInfo.Sex.MALE)
+                .setPassword("123456").build();
+        try {
+            AccountSignupResponse response = accountServiceBlockingStub.signup(request);
+            ResponseHeader header = response.getResponseHeader();
+            if (header.getSuccess()) {
+                System.out.println("success");
+            } else {
+                // error return by ResponseHeader
+                System.err.println(header.getCode() + "," + header.getMessage());
+            }
+        } catch (StatusRuntimeException e) {
+            // error return by Exception
+            System.err.println(e.getStatus().getCode() + "," + e.getStatus().getDescription());
+        }
+    }
+
+    @Test
+    public void testC_switchCompany() {
+        SwitchCompanyRequest request = SwitchCompanyRequest.newBuilder()
+                .setCompanyId(100001).build();
+        try {
+            SwitchCompanyResponse response = accountServiceBlockingStub.switchCompany(request);
+            ResponseHeader header = response.getResponseHeader();
+            if (header.getSuccess()) {
+                // new authentication
+                System.out.println(response.getAuthedToken());
+            } else {
+                // error return by ResponseHeader
+                System.err.println(header.getCode() + "," + header.getMessage());
+            }
+        } catch (StatusRuntimeException e) {
+            // error return by Exception
+            System.err.println(e.getStatus().getCode() + "," + e.getStatus().getDescription());
+        }
+    }
+
+    @Test
+    public void testD_getInfo() {
+        GetAccountInfoRequest request = GetAccountInfoRequest.newBuilder().build();
+        try {
+            GetAccountInfoResponse response = accountServiceBlockingStub.getInfo(request);
+            ResponseHeader header = response.getResponseHeader();
+            if (header.getSuccess()) {
+                System.out.println(response.getAccount());
+                if (response.getActiveCompanyId() > 0) {
+                    System.out.println("current selected company " + response.getActiveCompanyId());
+                }
+                if (response.getCompanyCount() > 0){
+                    response.getCompanyList().forEach(System.out::println);
+                }
+            } else {
+                // error return by ResponseHeader
+                System.err.println(header.getCode() + "," + header.getMessage());
+            }
+        } catch (StatusRuntimeException e) {
+            // error return by Exception
+            System.err.println(e.getStatus().getCode() + "," + e.getStatus().getDescription());
+        }
+    }
+
+    @Test
+    public void testE_saveInfo() {
+        SaveAccountInfoRequest request = SaveAccountInfoRequest.newBuilder()
+                .setAvatarUrl("https://www.baidu.com/xxx.png").build();
+        try {
+            SaveAccountInfoResponse response = accountServiceBlockingStub.saveInfo(request);
+            ResponseHeader header = response.getResponseHeader();
+            if (header.getSuccess()) {
+                System.out.println("success");
+            } else {
+                // error return by ResponseHeader
+                System.err.println(header.getCode() + "," + header.getMessage());
+            }
+        } catch (StatusRuntimeException e) {
+            // error return by Exception
+            System.err.println(e.getStatus().getCode() + "," + e.getStatus().getDescription());
+        }
+    }
+
+    @Test
+    public void testF_signout() {
+        AccountSignoutRequest request = AccountSignoutRequest.newBuilder().build();
+        try {
+            AccountSignoutResponse response = accountServiceBlockingStub.signout(request);
+            ResponseHeader header = response.getResponseHeader();
+            if (header.getSuccess()) {
+                System.out.println("success");
+            } else {
+                // error return by ResponseHeader
+                System.err.println(header.getCode() + "," + header.getMessage());
+            }
+        } catch (StatusRuntimeException e) {
+            // error return by Exception
+            System.err.println(e.getStatus().getCode() + "," + e.getStatus().getDescription());
+        }
+    }
+}

+ 15 - 0
services/account-service/src/main/java/com/usoftchina/uu/account/po/Company.java

@@ -1,5 +1,7 @@
 package com.usoftchina.uu.account.po;
 
+import com.usoftchina.sso.dto.SsoUserSpace;
+
 /**
  * @author yingp
  * @date 2019/4/18
@@ -9,6 +11,19 @@ public class Company {
     private String name;
     private String secretKey;
 
+    public Company() {
+    }
+
+    public Company(Long id, String name, String secretKey) {
+        this.id = id;
+        this.name = name;
+        this.secretKey = secretKey;
+    }
+
+    public Company(SsoUserSpace space) {
+        this(space.getSpaceUU(), space.getSpaceName(), space.getAccessSecret());
+    }
+
     public Long getId() {
         return id;
     }

+ 4 - 1
services/account-service/src/main/java/com/usoftchina/uu/account/service/impl/AccountServiceImpl.java

@@ -326,7 +326,10 @@ public class AccountServiceImpl implements AccountApi {
 
     @Override
     public boolean bindCompany(Long accountId, Long companyId) {
-        return accountCompanyMapper.insert(accountId, companyId) > 0;
+        if (!hasBindCompany(accountId, companyId)) {
+            return accountCompanyMapper.insert(accountId, companyId) > 0;
+        }
+        return true;
     }
 
     @Override

+ 58 - 0
services/account-service/src/main/java/com/usoftchina/uu/account/service/impl/CompanyServiceImpl.java

@@ -1,17 +1,24 @@
 package com.usoftchina.uu.account.service.impl;
 
+import com.usoftchina.sso.api.SsoUserApi;
+import com.usoftchina.sso.dto.SsoUserSpaceList;
+import com.usoftchina.uu.account.api.AccountApi;
 import com.usoftchina.uu.account.api.CompanyApi;
+import com.usoftchina.uu.account.dto.AccountDTO;
 import com.usoftchina.uu.account.dto.CompanyDTO;
 import com.usoftchina.uu.account.mapper.AccountCompanyMapper;
 import com.usoftchina.uu.account.mapper.CompanyMapper;
 import com.usoftchina.uu.account.po.Company;
 import com.usoftchina.uu.util.BeanMapper;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.CacheManager;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
 
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * @author yingp
@@ -26,9 +33,60 @@ public class CompanyServiceImpl implements CompanyApi {
     @Autowired
     private AccountCompanyMapper accountCompanyMapper;
 
+    @Autowired
+    private AccountApi accountApi;
+
+    @Autowired
+    private SsoUserApi ssoUserApi;
+
+    @Autowired
+    private CacheManager cacheManager;
+
+    /**
+     * 从账户中获取账户绑定的企业
+     *
+     * @param accountId
+     * @return
+     */
+    private List<Company> getFromSsoByAccountId(Long accountId) {
+        AccountDTO accountDTO = accountApi.findByPrimaryKey(accountId);
+        if (null != accountDTO) {
+            SsoUserSpaceList spaceList = ssoUserApi.getUserSpacesByMobile(accountDTO.getMobile());
+            if (null != spaceList && null != spaceList.getSpaces()) {
+                return spaceList.getSpaces().stream().map(Company::new).collect(Collectors.toList());
+            }
+        }
+        return null;
+    }
+
+    private boolean saveOrUpdateByPrimaryKey(Company company) {
+        Company oldOne = companyMapper.selectByPrimaryKey(company.getId());
+        boolean success;
+        if (null == oldOne) {
+            success = companyMapper.insertSelective(company) > 0;
+        } else {
+            success = companyMapper.updateByPrimaryKeySelective(company) > 0;
+        }
+        if (success) {
+            // 手动清理缓存
+            cacheManager.getCache(CACHE_NAME).evict(company.getId());
+        }
+        return success;
+    }
+
     @Override
     public List<CompanyDTO> findByAccountId(Long accountId) {
         List<Company> companyList = accountCompanyMapper.selectCompanyListByAccountId(accountId);
+        if (CollectionUtils.isEmpty(companyList)) {
+            // 如果没有企业信息,从账户中心获取
+            companyList = getFromSsoByAccountId(accountId);
+            if (CollectionUtils.isEmpty(companyList)) {
+                companyList.forEach(company -> {
+                    saveOrUpdateByPrimaryKey(company);
+                    accountApi.bindCompany(accountId, company.getId());
+                });
+            }
+        }
         return BeanMapper.mapList(companyList, CompanyDTO.class);
     }
 

+ 71 - 0
services/account-service/src/main/java/com/usoftchina/uu/account/service/impl/PasswordServiceImpl.java

@@ -0,0 +1,71 @@
+package com.usoftchina.uu.account.service.impl;
+
+import com.usoftchina.sso.api.SsoPasswordApi;
+import com.usoftchina.sso.dto.SsoCheckCode;
+import com.usoftchina.sso.dto.SsoEmailCheck;
+import com.usoftchina.sso.dto.SsoResult;
+import com.usoftchina.uu.account.api.PasswordApi;
+import com.usoftchina.uu.account.dto.EmailCheckToken;
+import com.usoftchina.uu.exception.ExceptionCode;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+/**
+ * @author yingp
+ * @date 2019/4/25
+ */
+@Service
+public class PasswordServiceImpl implements PasswordApi {
+    @Autowired
+    private SsoPasswordApi ssoPasswordApi;
+
+    @Override
+    public String sendCheckCodeByMobile(String mobile) {
+        SsoResult<SsoCheckCode> result = ssoPasswordApi.sendCheckCodeByMobile(mobile);
+        if (result.isSuccess()) {
+            return result.getContent().getToken();
+        }
+        ExceptionCode.SMS_VALIDCODE_SEND_FAIL.occur(result.getErrMsg());
+        return null;
+    }
+
+    @Override
+    public boolean checkByCheckCode(String token, String mobile, String code) {
+        SsoResult result = ssoPasswordApi.checkByCheckCode(mobile, token, code);
+        if (result.isSuccess()) {
+            return true;
+        }
+        ExceptionCode.SMS_VALIDCODE_ERROR.occur(result.getErrMsg());
+        return false;
+    }
+
+    @Override
+    public boolean resetPasswordByCheckCode(String token, String mobile, String code, String password) {
+        SsoResult result = ssoPasswordApi.resetPasswordByCheckCode(mobile, token, code, password);
+        if (result.isSuccess()) {
+            return true;
+        }
+        ExceptionCode.SMS_VALIDCODE_ERROR.occur(result.getErrMsg());
+        return false;
+    }
+
+    @Override
+    public EmailCheckToken getEmailByMobile(String mobile) {
+        SsoResult<SsoEmailCheck> result = ssoPasswordApi.getEmailByMobile(mobile);
+        if (result.isSuccess()) {
+            return new EmailCheckToken(result.getContent().getEmail(), result.getContent().getToken());
+        }
+        ExceptionCode.USER_ACCOUNT_ERROR.occur(result.getErrMsg());
+        return null;
+    }
+
+    @Override
+    public boolean sendCheckTokenByEmail(String token) {
+        SsoResult result = ssoPasswordApi.sendCheckTokenByEmail(token);
+        if (result.isSuccess()) {
+            return true;
+        }
+        ExceptionCode.EMAIL_VALIDCODE_SEND_FAIL.occur(result.getErrMsg());
+        return false;
+    }
+}

+ 0 - 1
services/mobile-grpc-service/build.gradle

@@ -8,5 +8,4 @@ dependencies {
     compile project(':apis:home-api')
     compile project(':apis:message-api')
     compile project(':shared:core')
-    compile project(':external:sso-feign-client')
 }

+ 32 - 22
services/mobile-grpc-service/src/main/java/com/usoftchina/uu/mobile/grpc/service/impl/MobileAccountServiceImplWithoutAuth.java

@@ -14,6 +14,8 @@ import com.usoftchina.uu.mobile.grpc.util.MobileBeanMapper;
 import com.usoftchina.uu.mobile.grpc.util.ResponseUtils;
 import io.grpc.stub.StreamObserver;
 import org.lognet.springboot.grpc.GRpcService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.CollectionUtils;
 
@@ -25,6 +27,8 @@ import java.util.List;
  */
 @GRpcService
 public class MobileAccountServiceImplWithoutAuth extends AccountServiceGrpc.AccountServiceImplBase {
+    private final static Logger logger = LoggerFactory.getLogger(MobileAccountServiceImplWithoutAuth.class);
+
     @Autowired
     private AccountApi accountApi;
 
@@ -73,32 +77,38 @@ public class MobileAccountServiceImplWithoutAuth extends AccountServiceGrpc.Acco
             if (null == accountDTO) {
                 builder.setResponseHeader(ResponseUtils.fail(ExceptionCode.USER_NOT_EXIST));
             } else {
-                Long activeCompanyId = null;
-                List<CompanyDTO> companyDTOList = companyApi.findByAccountId(accountDTO.getId());
-                // 默认登录
-                if (!CollectionUtils.isEmpty(companyDTOList)) {
-                    if (companyDTOList.size() == 1) {
-                        activeCompanyId = companyDTOList.get(0).getId();
-                    }
-                    for (int i = 0, len = companyDTOList.size(); i < len; i++) {
-                        builder.setCompany(i, MobileBeanMapper.map(companyDTOList.get(i)));
+                boolean success = accountApi.checkPasswordByMobile(request.getMobile(), request.getPassword());
+                if (!success) {
+                    builder.setResponseHeader(ResponseUtils.fail(ExceptionCode.USER_PWD_ERROR));
+                } else {
+                    Long activeCompanyId = null;
+                    List<CompanyDTO> companyDTOList = companyApi.findByAccountId(accountDTO.getId());
+                    // 默认登录
+                    if (!CollectionUtils.isEmpty(companyDTOList)) {
+                        if (companyDTOList.size() == 1) {
+                            activeCompanyId = companyDTOList.get(0).getId();
+                        }
+                        for (int i = 0, len = companyDTOList.size(); i < len; i++) {
+                            builder.setCompany(i, MobileBeanMapper.map(companyDTOList.get(i)));
+                        }
                     }
-                }
-                AccountInfo accountInfo = MobileBeanMapper.map(accountDTO);
-                // 保存设备信息
-                DeviceInfoDTO deviceInfoDTO = MobileBeanMapper.map(request.getDeviceInfo());
-                deviceInfoDTO.setAccountId(accountDTO.getId());
-                deviceInfoApi.save(deviceInfoDTO);
-                // 保存token信息
-                TokenDTO tokenDTO = new TokenDTO(accountDTO.getId(), activeCompanyId);
-                tokenApi.save(tokenDTO);
-                AuthedToken token = MobileBeanMapper.map(tokenDTO);
+                    AccountInfo accountInfo = MobileBeanMapper.map(accountDTO);
+                    // 保存设备信息
+                    DeviceInfoDTO deviceInfoDTO = MobileBeanMapper.map(request.getDeviceInfo());
+                    deviceInfoDTO.setAccountId(accountDTO.getId());
+                    deviceInfoApi.save(deviceInfoDTO);
+                    // 保存token信息
+                    TokenDTO tokenDTO = new TokenDTO(accountDTO.getId(), activeCompanyId);
+                    tokenApi.save(tokenDTO);
+                    AuthedToken token = MobileBeanMapper.map(tokenDTO);
 
-                builder.setResponseHeader(ResponseUtils.success())
-                        .setAuthedToken(token)
-                        .setAccount(accountInfo);
+                    builder.setAuthedToken(token)
+                            .setAccount(accountInfo)
+                            .setResponseHeader(ResponseUtils.success());
+                }
             }
         } catch (Exception e) {
+            logger.error("signin error", e);
             builder.setResponseHeader(ResponseUtils.error(e));
         }
         responseObserver.onNext(builder.build());

+ 20 - 38
services/mobile-grpc-service/src/main/java/com/usoftchina/uu/mobile/grpc/service/impl/MobilePasswordServiceImpl.java

@@ -1,14 +1,13 @@
 package com.usoftchina.uu.mobile.grpc.service.impl;
 
-import com.usoftchina.sso.api.SsoPasswordApi;
-import com.usoftchina.sso.dto.SsoCheckCode;
-import com.usoftchina.sso.dto.SsoEmailCheck;
-import com.usoftchina.sso.dto.SsoResult;
-import com.usoftchina.uu.exception.ExceptionCode;
+import com.usoftchina.uu.account.api.PasswordApi;
+import com.usoftchina.uu.account.dto.EmailCheckToken;
 import com.usoftchina.uu.mobile.grpc.api.*;
 import com.usoftchina.uu.mobile.grpc.util.ResponseUtils;
 import io.grpc.stub.StreamObserver;
 import org.lognet.springboot.grpc.GRpcService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 
 /**
@@ -18,22 +17,19 @@ import org.springframework.beans.factory.annotation.Autowired;
 @GRpcService
 public class MobilePasswordServiceImpl extends PasswordServiceGrpc.PasswordServiceImplBase {
 
+    private final static Logger logger = LoggerFactory.getLogger(MobilePasswordServiceImpl.class);
+
     @Autowired
-    private SsoPasswordApi ssoPasswordApi;
+    private PasswordApi passwordApi;
 
     @Override
     public void sendCheckCodeByMobile(SendCheckCodeByMobileRequest request, StreamObserver<SendCheckCodeByMobileResponse> responseObserver) {
         SendCheckCodeByMobileResponse.Builder builder = SendCheckCodeByMobileResponse.newBuilder();
         try {
-            SsoResult<SsoCheckCode> result = ssoPasswordApi.sendCheckCodeByMobile(request.getMobile());
-            if (result.isSuccess()) {
-                builder.setToken(result.getContent().getToken())
-                        .setResponseHeader(ResponseUtils.success());
-            } else {
-                builder.setResponseHeader(
-                        ResponseUtils.fail(ExceptionCode.SMS_VALIDCODE_SEND_FAIL.getCode(), result.getErrMsg()));
-            }
+            String token = passwordApi.sendCheckCodeByMobile(request.getMobile());
+            builder.setToken(token).setResponseHeader(ResponseUtils.success());
         } catch (Exception e) {
+            logger.error("sendCheckCodeByMobile error", e);
             builder.setResponseHeader(ResponseUtils.error(e));
         }
         responseObserver.onNext(builder.build());
@@ -44,15 +40,10 @@ public class MobilePasswordServiceImpl extends PasswordServiceGrpc.PasswordServi
     public void resetByCheckCode(ResetByCheckCodeRequest request, StreamObserver<ResetByCheckCodeResponse> responseObserver) {
         ResetByCheckCodeResponse.Builder builder = ResetByCheckCodeResponse.newBuilder();
         try {
-            SsoResult result = ssoPasswordApi.resetPasswordByCheckCode(request.getMobile(), request.getToken(),
-                    request.getCode(), request.getPassword());
-            if (result.isSuccess()) {
-                builder.setResponseHeader(ResponseUtils.success());
-            } else {
-                builder.setResponseHeader(
-                        ResponseUtils.fail(ExceptionCode.SMS_VALIDCODE_ERROR.getCode(), result.getErrMsg()));
-            }
+            passwordApi.resetPasswordByCheckCode(request.getMobile(), request.getToken(), request.getCode(),
+                    request.getPassword());
         } catch (Exception e) {
+            logger.error("resetByCheckCode error", e);
             builder.setResponseHeader(ResponseUtils.error(e));
         }
         responseObserver.onNext(builder.build());
@@ -63,16 +54,12 @@ public class MobilePasswordServiceImpl extends PasswordServiceGrpc.PasswordServi
     public void getEmailByMobile(GetEmailByMobileRequest request, StreamObserver<GetEmailByMobileResponse> responseObserver) {
         GetEmailByMobileResponse.Builder builder = GetEmailByMobileResponse.newBuilder();
         try {
-            SsoResult<SsoEmailCheck> result = ssoPasswordApi.getEmailByMobile(request.getMobile());
-            if (result.isSuccess()) {
-                builder.setToken(result.getContent().getToken())
-                        .setEmail(result.getContent().getEmail())
-                        .setResponseHeader(ResponseUtils.success());
-            } else {
-                builder.setResponseHeader(
-                        ResponseUtils.fail(ExceptionCode.USER_ACCOUNT_ERROR.getCode(), result.getErrMsg()));
-            }
+            EmailCheckToken token = passwordApi.getEmailByMobile(request.getMobile());
+            builder.setToken(token.getToken())
+                    .setEmail(token.getEmail())
+                    .setResponseHeader(ResponseUtils.success());
         } catch (Exception e) {
+            logger.error("getEmailByMobile error", e);
             builder.setResponseHeader(ResponseUtils.error(e));
         }
         responseObserver.onNext(builder.build());
@@ -83,14 +70,9 @@ public class MobilePasswordServiceImpl extends PasswordServiceGrpc.PasswordServi
     public void sendCheckEmail(SendCheckEmailRequest request, StreamObserver<SendCheckEmailResponse> responseObserver) {
         SendCheckEmailResponse.Builder builder = SendCheckEmailResponse.newBuilder();
         try {
-            SsoResult result = ssoPasswordApi.sendCheckTokenByEmail(request.getToken());
-            if (result.isSuccess()) {
-                builder.setResponseHeader(ResponseUtils.success());
-            } else {
-                builder.setResponseHeader(
-                        ResponseUtils.fail(ExceptionCode.EMAIL_VALIDCODE_SEND_FAIL.getCode(), result.getErrMsg()));
-            }
+            passwordApi.sendCheckTokenByEmail(request.getToken());
         } catch (Exception e) {
+            logger.error("sendCheckEmail error", e);
             builder.setResponseHeader(ResponseUtils.error(e));
         }
         responseObserver.onNext(builder.build());

+ 122 - 43
services/mobile-grpc-service/src/main/java/com/usoftchina/uu/mobile/grpc/util/MobileBeanMapper.java

@@ -29,19 +29,32 @@ public class MobileBeanMapper {
      * @return
      */
     public static AccountInfo map(AccountDTO accountDTO) {
+        AccountInfo.Builder builder = AccountInfo.newBuilder();
+
         AccountInfo.Sex sex;
         if (null != accountDTO.getSex() && AccountDTO.Sex.isFemale(accountDTO.getSex())) {
             sex = AccountInfo.Sex.FEMALE;
         } else {
             sex = AccountInfo.Sex.MALE;
         }
-        return AccountInfo.newBuilder()
-                .setAvatarUrl(accountDTO.getAvatarUrl())
-                .setEmail(accountDTO.getEmail())
-                .setId(accountDTO.getId())
-                .setMobile(accountDTO.getMobile())
-                .setRealname(accountDTO.getRealname())
-                .setSex(sex).build();
+        builder.setSex(sex);
+
+        if (null != accountDTO.getAvatarUrl()) {
+            builder.setAvatarUrl(accountDTO.getAvatarUrl());
+        }
+        if (null != accountDTO.getEmail()) {
+            builder.setEmail(accountDTO.getEmail());
+        }
+        if (null != accountDTO.getId()) {
+            builder.setId(accountDTO.getId());
+        }
+        if (null != accountDTO.getMobile()) {
+            builder.setMobile(accountDTO.getMobile());
+        }
+        if (null != accountDTO.getRealname()) {
+            builder.setRealname(accountDTO.getRealname());
+        }
+        return builder.build();
     }
 
     /**
@@ -51,10 +64,17 @@ public class MobileBeanMapper {
      * @return
      */
     public static AuthedToken map(TokenDTO tokenDTO) {
-        return AuthedToken.newBuilder()
-                .setToken(tokenDTO.getId())
-                .setTimestamp(tokenDTO.getTimestamp())
-                .setExpire(tokenDTO.getExpire()).build();
+        AuthedToken.Builder builder = AuthedToken.newBuilder();
+        if (null != tokenDTO.getId()) {
+            builder.setToken(tokenDTO.getId());
+        }
+        if (null != tokenDTO.getTimestamp()) {
+            builder.setTimestamp(tokenDTO.getTimestamp());
+        }
+        if (null != tokenDTO.getExpire()) {
+            builder.setExpire(tokenDTO.getExpire());
+        }
+        return builder.build();
     }
 
     /**
@@ -64,9 +84,14 @@ public class MobileBeanMapper {
      * @return
      */
     public static Company map(CompanyDTO companyDTO) {
-        return Company.newBuilder()
-                .setId(companyDTO.getId())
-                .setName(companyDTO.getName()).build();
+        Company.Builder builder = Company.newBuilder();
+        if (null != companyDTO.getId()) {
+            builder.setId(companyDTO.getId());
+        }
+        if (null != companyDTO.getName()) {
+            builder.setName(companyDTO.getName());
+        }
+        return builder.build();
     }
 
     /**
@@ -95,14 +120,29 @@ public class MobileBeanMapper {
      * @return
      */
     public static AppConfig map(AppConfigDTO appConfigDTO) {
+        AppConfig.Builder builder = AppConfig.newBuilder();
+
         ViewType type = appConfigDTO.viewType().isWidget() ? ViewType.WIDGET : ViewType.WEB;
-        return AppConfig.newBuilder().setAndroidWidget(appConfigDTO.getAndroidWidget())
-                .setIcon(appConfigDTO.getIcon())
-                .setId(appConfigDTO.getId())
-                .setIosWidget(appConfigDTO.getIosWidget())
-                .setName(appConfigDTO.getName())
-                .setViewType(type)
-                .setWebUrl(appConfigDTO.getWebUrl()).build();
+        builder.setViewType(type);
+        if (null != appConfigDTO.getAndroidWidget()) {
+            builder.setAndroidWidget(appConfigDTO.getAndroidWidget());
+        }
+        if (null != appConfigDTO.getIcon()) {
+            builder.setIcon(appConfigDTO.getIcon());
+        }
+        if (null != appConfigDTO.getId()) {
+            builder.setId(appConfigDTO.getId());
+        }
+        if (null != appConfigDTO.getIosWidget()) {
+            builder.setIosWidget(appConfigDTO.getIosWidget());
+        }
+        if (null != appConfigDTO.getName()) {
+            builder.setName(appConfigDTO.getName());
+        }
+        if (null != appConfigDTO.getWebUrl()) {
+            builder.setWebUrl(appConfigDTO.getWebUrl());
+        }
+        return builder.build();
     }
 
     /**
@@ -129,15 +169,29 @@ public class MobileBeanMapper {
      * @return
      */
     public static MessageConfig map(MessageConfigDTO messageConfigDTO) {
+        MessageConfig.Builder builder = MessageConfig.newBuilder();
+
         ViewType type = messageConfigDTO.viewType().isWidget() ? ViewType.WIDGET : ViewType.WEB;
-        return MessageConfig.newBuilder()
-                .setAndroidWidget(messageConfigDTO.getAndroidWidget())
-                .setCode(messageConfigDTO.getCode())
-                .setIcon(messageConfigDTO.getIcon())
-                .setIosWidget(messageConfigDTO.getIosWidget())
-                .setName(messageConfigDTO.getName())
-                .setViewType(type)
-                .setWebUrl(messageConfigDTO.getWebUrl()).build();
+        builder.setViewType(type);
+        if (null != messageConfigDTO.getAndroidWidget()) {
+            builder.setAndroidWidget(messageConfigDTO.getAndroidWidget());
+        }
+        if (null != messageConfigDTO.getCode()) {
+            builder.setCode(messageConfigDTO.getCode());
+        }
+        if (null != messageConfigDTO.getIcon()) {
+            builder.setIcon(messageConfigDTO.getIcon());
+        }
+        if (null != messageConfigDTO.getIosWidget()) {
+            builder.setIosWidget(messageConfigDTO.getIosWidget());
+        }
+        if (null != messageConfigDTO.getName()) {
+            builder.setName(messageConfigDTO.getName());
+        }
+        if (null != messageConfigDTO.getWebUrl()) {
+            builder.setWebUrl(messageConfigDTO.getWebUrl());
+        }
+        return builder.build();
     }
 
     /**
@@ -147,9 +201,14 @@ public class MobileBeanMapper {
      * @return
      */
     public static UnreadMessageCount map(UnreadMessageCountDTO unreadMessageCountDTO) {
-        return UnreadMessageCount.newBuilder()
-                .setCode(unreadMessageCountDTO.getCode())
-                .setCount(unreadMessageCountDTO.getCount()).build();
+        UnreadMessageCount.Builder builder = UnreadMessageCount.newBuilder();
+        if (null != unreadMessageCountDTO.getCode()) {
+            builder.setCode(unreadMessageCountDTO.getCode());
+        }
+        if (null != unreadMessageCountDTO.getCount()) {
+            builder.setCount(unreadMessageCountDTO.getCount());
+        }
+        return builder.build();
     }
 
     /**
@@ -173,10 +232,15 @@ public class MobileBeanMapper {
      * @return
      */
     public static MessageInfo map(MessageDTO messageDTO) {
-        return MessageInfo.newBuilder()
-                .setBody(messageDTO.getBody())
-                .setId(messageDTO.getId())
-                .setStatus(MessageInfo.Status.UNREAD).build();
+        MessageInfo.Builder builder = MessageInfo.newBuilder();
+        if (null != messageDTO.getBody()) {
+            builder.setBody(messageDTO.getBody());
+        }
+        if (null != messageDTO.getId()) {
+            builder.setId(messageDTO.getId());
+        }
+        builder.setStatus(MessageInfo.Status.UNREAD);
+        return builder.build();
     }
 
     /**
@@ -186,13 +250,28 @@ public class MobileBeanMapper {
      * @return
      */
     public static HomeConfig map(HomeConfigDTO homeConfigDTO) {
+        HomeConfig.Builder builder = HomeConfig.newBuilder();
+
         ViewType type = homeConfigDTO.viewType().isWidget() ? ViewType.WIDGET : ViewType.WEB;
-        return HomeConfig.newBuilder().setAndroidWidget(homeConfigDTO.getAndroidWidget())
-                .setIcon(homeConfigDTO.getIcon())
-                .setId(homeConfigDTO.getId())
-                .setIosWidget(homeConfigDTO.getIosWidget())
-                .setName(homeConfigDTO.getName())
-                .setViewType(type)
-                .setWebUrl(homeConfigDTO.getWebUrl()).build();
+        builder.setViewType(type);
+        if (null != homeConfigDTO.getAndroidWidget()) {
+            builder.setAndroidWidget(homeConfigDTO.getAndroidWidget());
+        }
+        if (null != homeConfigDTO.getIcon()) {
+            builder.setIcon(homeConfigDTO.getIcon());
+        }
+        if (null != homeConfigDTO.getId()) {
+            builder.setId(homeConfigDTO.getId());
+        }
+        if (null != homeConfigDTO.getIosWidget()) {
+            builder.setIosWidget(homeConfigDTO.getIosWidget());
+        }
+        if (null != homeConfigDTO.getName()) {
+            builder.setName(homeConfigDTO.getName());
+        }
+        if (null != homeConfigDTO.getWebUrl()) {
+            builder.setWebUrl(homeConfigDTO.getWebUrl());
+        }
+        return builder.build();
     }
 }

+ 3 - 1
services/mobile-grpc-service/src/main/java/com/usoftchina/uu/mobile/grpc/util/ResponseUtils.java

@@ -4,6 +4,7 @@ import com.usoftchina.uu.exception.BaseException;
 import com.usoftchina.uu.exception.BaseExceptionCode;
 import com.usoftchina.uu.exception.ExceptionCode;
 import com.usoftchina.uu.mobile.grpc.api.ResponseHeader;
+import com.usoftchina.uu.util.StringUtils;
 
 /**
  * @author yingp
@@ -34,6 +35,7 @@ public class ResponseUtils {
         if (e instanceof BaseException) {
             return fail((BaseException) e);
         }
-        return fail(ExceptionCode.UNKNOWN_ERROR.getCode(), e.getMessage());
+        ExceptionCode exceptionCode = ExceptionCode.UNKNOWN_ERROR;
+        return fail(exceptionCode.getCode(), StringUtils.nullIf(e.getMessage(), exceptionCode.getMessage()));
     }
 }

+ 28 - 0
shared/core/src/main/java/com/usoftchina/uu/util/StringUtils.java

@@ -0,0 +1,28 @@
+package com.usoftchina.uu.util;
+
+/**
+ * @author yingp
+ * @date 2019/4/25
+ */
+public abstract class StringUtils extends org.springframework.util.StringUtils {
+    /**
+     * 为空取值
+     *
+     * @param target
+     * @param nullValue
+     * @return
+     */
+    public static String nullIf(String target, String nullValue) {
+        return (null == target || target.isEmpty()) ? nullValue : target;
+    }
+
+    /**
+     * 为空取空字符串
+     *
+     * @param target
+     * @return
+     */
+    public static String nullIf(String target) {
+        return nullIf(target, "");
+    }
+}