huangxz
2026-03-04 31620fa7106fb5ba0fa511de1454a5c690f3f076
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.xiaopeng.automaticPilot.service.impl;
 
import com.xiaopeng.api.feignClient.operation.SmsClient;
import com.xiaopeng.api.vo.operation.SmsVo;
import com.xiaopeng.automaticPilot.dto.XpCommonResponse;
import com.xiaopeng.automaticPilot.dto.RobotaxiSmsCodeRequest;
import com.xiaopeng.automaticPilot.dto.RobotaxiSmsValidateRequest;
import com.xiaopeng.automaticPilot.service.VehicleRtInfoService;
import com.xiaopeng.api.dto.vehicleConvoy.UpdateVehicleStateDto;
import com.xiaopeng.api.feignClient.vehicleConvoy.XpVehicleClient;
import com.xiaopeng.api.model.vehicleConvoy.XpVehicle;
import com.xiaopeng.common.core.utils.MsgUtil;
import com.xiaopeng.common.core.utils.StringUtils;
import com.xiaopeng.common.redis.service.RedisService;
import com.xiaopeng.automaticPilot.enums.RobotaxiErrorEnum;
import com.xiaopeng.automaticPilot.enums.SmsPushType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
 
@Slf4j
@Service
public class VehicleRtInfoServiceImpl implements VehicleRtInfoService {
 
    private static final long CODE_TTL_MINUTES = 5L;
    public static final String ROBOTAXI_VERIFY_CODE_SMS_TPL = "RobotaxiVerifyCodeSmsTpl";
 
    @Resource
    private RedisService redisService;
 
    @Resource
    private XpVehicleClient xpVehicleClient;
 
    @Resource
    private SmsClient smsClient;
 
    @Value("${robotaxi.sms.app-id:1476153389173952512}")
    private String smsAppId;
 
    @Value("#{'${robotaxi.sms.whitelist.mobiles:}'.split(',')}")
    private List<String> whitelistMobiles;
 
    @Override
    public XpCommonResponse<?> getRobotaxiSmsCode(RobotaxiSmsCodeRequest request) {
        XpCommonResponse<?> paramCheckError = validateSmsCodeRequest(request);
        if (paramCheckError != null) {
            return paramCheckError;
        }
 
        XpCommonResponse<?> vehicleCheckError = validateVehicleReadyIfNeeded(request.getSmsType(), request.getVin());
        if (vehicleCheckError != null) {
            return vehicleCheckError;
        }
 
        String code = MsgUtil.createCode();
        log.info("Robotaxi SMS code generated for mobile {}: {}", request.getMobile(), code);
        
        // TODO: Call SMS interface to send code when available
//         sendSms(request.getMobile(), code);
        Map<String, String> param = Collections.singletonMap("code", code);
        SmsVo smsVo = new SmsVo();
        smsVo.setAppId(smsAppId);
        smsVo.setParams(param);
        smsVo.setTemplateCode(ROBOTAXI_VERIFY_CODE_SMS_TPL);
        smsVo.setReceivers(request.getMobile());
        smsVo.setPushType(SmsPushType.SMS.getCode());
        smsClient.pushSms(smsVo);
 
        String key = buildSmsCodeKey(request.getSmsType(), request.getVin(), request.getMobile());
        redisService.setCacheObject(key, code, CODE_TTL_MINUTES, TimeUnit.MINUTES);
        return XpCommonResponse.ok(Collections.emptyMap());
    }
 
    @Override
    public XpCommonResponse<?> validateRobotaxiSmsCode(RobotaxiSmsValidateRequest request) {
        XpCommonResponse<?> paramCheckError = validateSmsValidateRequest(request);
        if (paramCheckError != null) {
            return paramCheckError;
        }
 
        XpCommonResponse<?> vehicleCheckError = validateVehicleReadyIfNeeded(request.getSmsType(), request.getVin());
        if (vehicleCheckError != null) {
            return vehicleCheckError;
        }
 
        boolean isWhitelisted = whitelistMobiles != null && whitelistMobiles.contains(request.getMobile());
        
        String key = buildSmsCodeKey(request.getSmsType(), request.getVin(), request.getMobile());
 
        if (!isWhitelisted) {
            String cachedCode = redisService.getCacheObject(key);
            if (StringUtils.isEmpty(cachedCode)) {
                return XpCommonResponse.fail(RobotaxiErrorEnum.GENERAL_ERROR);
            }
            if (!cachedCode.equals(request.getCode())) {
                return XpCommonResponse.fail(RobotaxiErrorEnum.VERIFY_CODE_ERROR);
            }
            redisService.deleteObject(key);
        } else {
            redisService.deleteObject(key);
        }
        return XpCommonResponse.ok(Collections.emptyMap());
    }
 
    private XpCommonResponse<?> validateSmsCodeRequest(RobotaxiSmsCodeRequest request) {
        if (StringUtils.isAnyEmpty(request.getSmsType(), request.getVin(), request.getMobile())) {
            return XpCommonResponse.fail(RobotaxiErrorEnum.GENERAL_ERROR);
        }
        if (!isSupportedSmsType(request.getSmsType())) {
            return XpCommonResponse.fail(RobotaxiErrorEnum.GENERAL_ERROR);
        }
        return null;
    }
 
    private XpCommonResponse<?> validateSmsValidateRequest(RobotaxiSmsValidateRequest request) {
        if (StringUtils.isAnyEmpty(request.getSmsType(), request.getVin(), request.getMobile(), request.getCode())) {
            return XpCommonResponse.fail(RobotaxiErrorEnum.GENERAL_ERROR);
        }
        if (!isSupportedSmsType(request.getSmsType())) {
            return XpCommonResponse.fail(RobotaxiErrorEnum.GENERAL_ERROR);
        }
        return null;
    }
 
    private boolean isSupportedSmsType(String smsType) {
        return SmsType.ACCEPT_RIDES.name().equals(smsType) || SmsType.STOP_ACCEPT_RIDES.name().equals(smsType);
    }
 
    private XpCommonResponse<?> validateVehicleReadyIfNeeded(String smsType, String vin) {
        if (!SmsType.ACCEPT_RIDES.name().equals(smsType)) {
            return null;
        }
//        XpVehicle vehicle = xpVehicleClient.findByVehicleVin(vin).getData();
//        if (vehicle == null) {
//            return XpCommonResponse.fail(RobotaxiErrorEnum.MOBILE_VIN_MISMATCH);
//        }
//        Integer status = vehicle.getStatus();
//        Integer operatingStatus = vehicle.getOperatingStatus();
//        if (status == null || status != 2 || operatingStatus == null || operatingStatus != 1) {
//            return XpCommonResponse.fail(RobotaxiErrorEnum.VEHICLE_NOT_READY);
//        }
 
        return null;
    }
 
    private String buildSmsCodeKey(String smsType, String vin, String mobile) {
        return "ROBOTAXI_SMS_CODE:" + smsType + ":" + vin + ":" + mobile;
    }
}