LoginServiceImpl.java
7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.infoloop.tianting.service.impl;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.infoloop.rpc.meizhongyiheservice.HospitalStatus;
import com.infoloop.tianting.WOperatorStatus;
import com.infoloop.tianting.clientcustomerservice.ClientCustomerHospitalRecordHospitalStatusEnum;
import com.infoloop.tianting.constant.CommonConstants;
import com.infoloop.tianting.exception.ClientEndExceptions;
import com.infoloop.tianting.exception.ErrorCodeEnum;
import com.infoloop.tianting.model.dto.PermissionDTO.LoginDto;
import com.infoloop.tianting.service.LoginService;
import com.infoloop.tianting.service.client.ClientCustomerServiceRpcClient;
import com.infoloop.tianting.service.client.MeiZhongYiHeServiceClient;
import com.infoloop.tianting.service.client.OperatorServiceRpcClient;
import com.infoloop.tianting.service.client.WOperatorServiceRpcClient;
import com.infoloop.tianting.service.client.WxMiniProgramHttpClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class LoginServiceImpl implements LoginService {
private final ClientCustomerServiceRpcClient clientCustomerServiceRpcClient;
private final OperatorServiceRpcClient operatorServiceRpcClient;
private final WxMiniProgramHttpClient wxMiniProgramHttpClient;
private final WOperatorServiceRpcClient wOperatorServiceRpcClient;
private final MeiZhongYiHeServiceClient meiZhongYiHeServiceClient;
@Override
public SaTokenInfo login(LoginDto loginDto) throws NoSuchAlgorithmException {
if (loginDto.isShouldLoginWithOpenCode()) {
final var wxUserOpenIdDto = wxMiniProgramHttpClient.getUserOpenIdByCode(loginDto.getOpenCode());
StpUtil.login(wxUserOpenIdDto.getOpenid(), SaLoginModel.create()
.setExtra(CommonConstants.ENTERPRISE_ID, loginDto.getEnterpriseId())
.setExtra(CommonConstants.LOGIN_SOURCE, loginDto.getLoginSourceEnum().getValue())
.build());
return StpUtil.getTokenInfo();
} else if (loginDto.isShouldLoginWithHisCustomerNo()) {
var customer = clientCustomerServiceRpcClient.getClientCustomerByHISCustomerNo(loginDto.getHisCustomerNo()).getResponse();
if (customer.getId() == 0) {
// 根据hisCustomerNo拿不到用户的话再去根据合同号去his查一次,即认为是月子中心客户
customer = clientCustomerServiceRpcClient.getClientCustomerByContactNo(loginDto.getHisCustomerNo()).getResponse();
if (customer.getId() == 0) {
throw ClientEndExceptions.IncorrectRequestValue.build(ErrorCodeEnum.VALIDATE_FAILED.name());
} else {
var record = clientCustomerServiceRpcClient.getClientCustomerHospitalRecordsByCustomerId(customer.getEnterpriseId(), customer.getId());
if (!record.hasResponse() || record.getResponse().getHospitalStatus() != ClientCustomerHospitalRecordHospitalStatusEnum.CURRENT) {
throw ClientEndExceptions.IncorrectRequestValue.build(ErrorCodeEnum.VALIDATE_FAILED.name());
}
}
} else {
var record = clientCustomerServiceRpcClient.getClientCustomerHospitalRecordsByCustomerId(customer.getEnterpriseId(), customer.getId());
if (!record.hasResponse() || record.getResponse().getHospitalStatus() != ClientCustomerHospitalRecordHospitalStatusEnum.CURRENT) {
throw ClientEndExceptions.IncorrectRequestValue.build(ErrorCodeEnum.VALIDATE_FAILED.name());
}
//还需要去读取HIS系统最新用户住院信息
final var hisCustomerHospitalRecord = meiZhongYiHeServiceClient.getCustomerDetailById(customer.getHISCustomerId(), record.getResponse().getContractNo()).getCustomerHospitalRecord();
if (hisCustomerHospitalRecord.getHospitalStatus() != HospitalStatus.CURRENT) {
throw ClientEndExceptions.IncorrectRequestValue.build(ErrorCodeEnum.VALIDATE_FAILED.name());
}
}
final var wxUserOpenIdDto = wxMiniProgramHttpClient.getUserOpenIdByCode(loginDto.getOpenCode());
StpUtil.login(customer.getId(), SaLoginModel.create()
.setExtra(CommonConstants.ENTERPRISE_ID, customer.getEnterpriseId())
.setExtra(CommonConstants.LOGIN_SOURCE, loginDto.getLoginSourceEnum().getValue())
.setExtra(CommonConstants.OPEN_ID, wxUserOpenIdDto.getOpenid())
.build());
return StpUtil.getTokenInfo();
} else if (loginDto.isShouldLoginWithOperatorPhone()) {
final var operator = operatorServiceRpcClient.getCOperatorByMobileOrEmail(loginDto.getOperatorPhone());
if (operator.getId() == 0) {
throw ClientEndExceptions.AuthenticationFailure.build(ErrorCodeEnum.UNAUTHORIZED.name());
}
if (!operator.getPassword().equals(sha1Hash(loginDto.getPassword()))) {
throw ClientEndExceptions.AuthenticationFailure.build(ErrorCodeEnum.VALIDATE_FAILED.name());
}
if (operator.getStatus() != 1) {
throw ClientEndExceptions.AuthenticationFailure.build(ErrorCodeEnum.VALIDATE_FAILED.name());
}
StpUtil.login(operator.getId(), SaLoginModel.create()
.setExtra(CommonConstants.ENTERPRISE_ID, operator.getEnterpriseId())
.setExtra(CommonConstants.LOGIN_SOURCE, loginDto.getLoginSourceEnum().getValue())
.build());
return StpUtil.getTokenInfo();
} else {
final var operator = wOperatorServiceRpcClient.getWOperatorByEmail(loginDto.getEmail()).getResponse();
if (operator.getId() == 0) {
throw ClientEndExceptions.AuthenticationFailure.build(ErrorCodeEnum.UNAUTHORIZED.name());
}
if (!operator.getPassword().equals(sha1Hash(loginDto.getPassword()))) {
throw ClientEndExceptions.AuthenticationFailure.build(ErrorCodeEnum.VALIDATE_FAILED.name());
}
if (operator.getStatus() != WOperatorStatus.SHOW) {
throw ClientEndExceptions.AuthenticationFailure.build(ErrorCodeEnum.VALIDATE_FAILED.name());
}
StpUtil.login(operator.getId(), SaLoginModel.create()
.setExtra(CommonConstants.ENTERPRISE_ID, operator.getEnterpriseId())
.setExtra(CommonConstants.LOGIN_SOURCE, loginDto.getLoginSourceEnum().getValue())
.build());
return StpUtil.getTokenInfo();
}
}
public static String sha1Hash(String input) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(input.getBytes());
byte[] digest = md.digest();
return bytesToHex(digest);
}
private static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
}