“mitao”
2026-04-17 d699da385dda6257677ee93bef47388c0635f64e
tcm-training-admin-backend/src/main/java/com/tcm/training/admin/app/auth/service/AppSmsCodeService.java
@@ -17,6 +17,7 @@
    private static final String LOGIN_CODE_CACHE_PREFIX = "app:sms:login:";
    private static final String REGISTER_CODE_CACHE_PREFIX = "app:sms:register:";
    private static final String PASSWORD_RESET_CODE_CACHE_PREFIX = "app:sms:password-reset:";
    private static final String PHONE_CHANGE_CODE_CACHE_PREFIX = "app:sms:phone-change:";
    private final StringRedisTemplate stringRedisTemplate;
    private final AppSmsSender appSmsSender;
@@ -55,6 +56,13 @@
    }
    /**
     * 生成并发送修改手机号验证码,同时写入缓存。
     */
    public AppSmsCodeSendResponse createPhoneChangeCode(String phone) {
        return createCode(phone, PHONE_CHANGE_CODE_CACHE_PREFIX, "phone-change");
    }
    /**
     * 校验登录验证码,成功后销毁缓存中的一次性验证码。
     */
    public void validateLoginCode(String phone, String code) {
@@ -76,6 +84,13 @@
    }
    /**
     * 校验修改手机号验证码,成功后销毁缓存中的一次性验证码。
     */
    public void validatePhoneChangeCode(String phone, String code) {
        validateCode(phone, code, PHONE_CHANGE_CODE_CACHE_PREFIX);
    }
    /**
     * 统一生成指定场景的验证码并写入缓存。
     */
    private AppSmsCodeSendResponse createCode(String phone, String cachePrefix, String scene) {
@@ -90,8 +105,10 @@
            appSmsSender.sendLoginCode(phone, code);
        } else if ("register".equals(scene)) {
            appSmsSender.sendRegisterCode(phone, code);
        } else {
        } else if ("password-reset".equals(scene)) {
            appSmsSender.sendPasswordResetCode(phone, code);
        } else {
            appSmsSender.sendPhoneChangeCode(phone, code);
        }
        return new AppSmsCodeSendResponse(appSmsAuthProperties.getExpireSeconds());
    }