JPushConfig.java 1.12 KB
package com.diligrp.rider.config;

import cn.jpush.api.JPushClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * JPushClient Bean 装配
 *
 * 即使 jpush.enabled=false 也会创建一个 client(避免依赖注入失败),
 * 调用时由 JPushService 检查 enabled 开关决定是否真的发起推送。
 */
@Slf4j
@Configuration
@RequiredArgsConstructor
public class JPushConfig {

    private final JPushProperties properties;

    @Bean
    public JPushClient jPushClient() {
        if (!properties.isEnabled()) {
            log.warn("JPush 未启用(jpush.enabled=false),将创建占位 client,所有推送会被跳过");
        }
        // 即便未启用,也用占位值构造,避免 NPE
        String masterSecret = properties.getMasterSecret() != null ? properties.getMasterSecret() : "placeholder";
        String appKey = properties.getAppKey() != null ? properties.getAppKey() : "placeholder";
        return new JPushClient(masterSecret, appKey);
    }
}