|
|
@@ -8,14 +8,19 @@ import com.usoftchina.uu.account.context.AccountHolder;
|
|
|
import com.usoftchina.uu.account.context.TokenHolder;
|
|
|
import com.usoftchina.uu.account.dto.AccountDTO;
|
|
|
import com.usoftchina.uu.account.dto.CompanyDTO;
|
|
|
+import com.usoftchina.uu.account.dto.DeviceInfoDTO;
|
|
|
import com.usoftchina.uu.account.dto.TokenDTO;
|
|
|
+import com.usoftchina.uu.exception.ExceptionCode;
|
|
|
import com.usoftchina.uu.mobile.grpc.api.*;
|
|
|
import com.usoftchina.uu.mobile.grpc.interceptor.MobileAuthenticationInterceptor;
|
|
|
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;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
@@ -24,7 +29,9 @@ import java.util.List;
|
|
|
* @date 2019/4/18
|
|
|
*/
|
|
|
@GRpcService(interceptors = MobileAuthenticationInterceptor.class)
|
|
|
-public class MobileAccountServiceImplWithAuth extends AccountServiceGrpc.AccountServiceImplBase {
|
|
|
+public class MobileAccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBase {
|
|
|
+ private final static Logger logger = LoggerFactory.getLogger(MobileAccountServiceImpl.class);
|
|
|
+
|
|
|
@Autowired
|
|
|
private AccountApi accountApi;
|
|
|
|
|
|
@@ -37,6 +44,80 @@ public class MobileAccountServiceImplWithAuth extends AccountServiceGrpc.Account
|
|
|
@Autowired
|
|
|
private CompanyApi companyApi;
|
|
|
|
|
|
+ @Override
|
|
|
+ public void signup(AccountSignupRequest request, StreamObserver<AccountSignupResponse> responseObserver) {
|
|
|
+ AccountSignupResponse.Builder builder = AccountSignupResponse.newBuilder();
|
|
|
+ try {
|
|
|
+ AccountDTO accountDTO = getAccountFromRequest(request);
|
|
|
+ boolean success = accountApi.register(accountDTO, request.getPassword());
|
|
|
+ if (success) {
|
|
|
+ builder.setResponseHeader(ResponseUtils.success());
|
|
|
+ } else {
|
|
|
+ builder.setResponseHeader(ResponseUtils.fail(ExceptionCode.USER_REGISTER_FAILED));
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ builder.setResponseHeader(ResponseUtils.error(e));
|
|
|
+ }
|
|
|
+ responseObserver.onNext(builder.build());
|
|
|
+ responseObserver.onCompleted();
|
|
|
+ }
|
|
|
+
|
|
|
+ private AccountDTO getAccountFromRequest(AccountSignupRequest request) {
|
|
|
+ AccountDTO accountDTO = new AccountDTO();
|
|
|
+ accountDTO.setSex(request.getSexValue());
|
|
|
+ accountDTO.setRealname(request.getRealname());
|
|
|
+ accountDTO.setEmail(request.getEmail());
|
|
|
+ accountDTO.setAvatarUrl(request.getAvatarUrl());
|
|
|
+ accountDTO.setMobile(request.getMobile());
|
|
|
+ return accountDTO;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void signin(AccountSigninRequest request, StreamObserver<AccountSigninResponse> responseObserver) {
|
|
|
+ AccountSigninResponse.Builder builder = AccountSigninResponse.newBuilder();
|
|
|
+ try {
|
|
|
+ AccountDTO accountDTO = accountApi.findByMobile(request.getMobile());
|
|
|
+ if (null == accountDTO) {
|
|
|
+ builder.setResponseHeader(ResponseUtils.fail(ExceptionCode.USER_NOT_EXIST));
|
|
|
+ } else {
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ companyDTOList.forEach(companyDTO -> {
|
|
|
+ builder.addCompany(MobileBeanMapper.map(companyDTO));
|
|
|
+ });
|
|
|
+ }
|
|
|
+ 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.setAuthedToken(token)
|
|
|
+ .setAccount(accountInfo)
|
|
|
+ .setResponseHeader(ResponseUtils.success());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("signin error", e);
|
|
|
+ builder.setResponseHeader(ResponseUtils.error(e));
|
|
|
+ }
|
|
|
+ responseObserver.onNext(builder.build());
|
|
|
+ responseObserver.onCompleted();
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public void switchCompany(SwitchCompanyRequest request, StreamObserver<SwitchCompanyResponse> responseObserver) {
|
|
|
SwitchCompanyResponse.Builder builder = SwitchCompanyResponse.newBuilder();
|