All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.mhuang.pacebox.sms.jd.JdSmsHandler Maven / Gradle / Ivy

The newest version!
package tech.mhuang.pacebox.sms.jd;

import com.jdcloud.sdk.auth.CredentialsProvider;
import com.jdcloud.sdk.auth.StaticCredentialsProvider;
import com.jdcloud.sdk.http.HttpRequestConfig;
import com.jdcloud.sdk.http.Protocol;
import com.jdcloud.sdk.service.sms.client.SmsClient;
import com.jdcloud.sdk.service.sms.model.BatchSendRequest;
import com.jdcloud.sdk.service.sms.model.BatchSendResult;
import tech.mhuang.pacebox.core.dict.BasicDict;
import tech.mhuang.pacebox.core.util.CollectionUtil;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.sms.BaseSmsHandler;
import tech.mhuang.pacebox.sms.SmsFieldProperties;
import tech.mhuang.pacebox.sms.domain.SmsSendRequest;
import tech.mhuang.pacebox.sms.domain.SmsSendResult;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 京东短信接入
 *
 * @author mhuang
 * @since 1.0.0
 */
public class JdSmsHandler implements BaseSmsHandler {
    private SmsFieldProperties properties;
    private SmsClient smsClient;

    @Override
    public void setFieldProperties(SmsFieldProperties properties) {
        this.properties = properties;
        CredentialsProvider credentialsProvider = new StaticCredentialsProvider(properties.getAccessKey(),
                properties.getAccessSecret());
        smsClient = SmsClient.builder().credentialsProvider(credentialsProvider)
                .httpRequestConfig(new HttpRequestConfig.Builder().protocol(Protocol.HTTP)
                        .proxyHost(properties.getProxyHost()).proxyPort(properties.getProxyPort()).build()).build();
    }

    @Override
    public SmsSendResult send(SmsSendRequest smsSendRequest) {
        BatchSendRequest request = new BatchSendRequest();
        request.setRegionId(properties.getRegion());
        request.setTemplateId(smsSendRequest.getTemplateCode());
        request.setSignId(smsSendRequest.getSign());
        request.setPhoneList(Arrays.asList(Objects.requireNonNull(StringUtil.split(smsSendRequest.getMobile(), ","))));
        BasicDict params = smsSendRequest.getTemplateParam();
        List paramList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(params)) {
            paramList = params.values().stream().map(value -> String.valueOf(params)).collect(Collectors.toList());
        }
        request.setParams(paramList);
        BatchSendResult result = smsClient.batchSend(request).getResult();
        SmsSendResult.SmsSendResultBuilder builder = SmsSendResult.builder();
        builder.extendParam(smsSendRequest.getExtendParam()).success(result.getStatus()).message(result.getMessage()).bizId(result.getData().getSequenceNumber());
        return builder.build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy