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

com.taotao.boot.sms.netease.NeteaseCloudSendHandler Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.taotao.boot.sms.netease;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.taotao.boot.common.utils.log.LogUtils;
import com.taotao.boot.sms.common.exception.SendFailedException;
import com.taotao.boot.sms.common.handler.AbstractSendHandler;
import com.taotao.boot.sms.common.model.NoticeData;
import com.taotao.boot.sms.common.utils.RandomUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 网易云信发送处理
 *
 * @author shuigedeng
 * @version 2022.04
 * @since 2022-04-27 17:51:36
 */
public class NeteaseCloudSendHandler extends AbstractSendHandler {

    /** 请求路径URL */
    private static final String SERVER_URL = "https://api.netease.im/sms/sendtemplate.action";

    private final ObjectMapper objectMapper;

    private final RestTemplate restTemplate;

    public NeteaseCloudSendHandler(
            NeteaseCloudProperties properties,
            ApplicationEventPublisher eventPublisher,
            ObjectMapper objectMapper,
            RestTemplate restTemplate) {
        super(properties, eventPublisher);
        this.objectMapper = objectMapper;
        this.restTemplate = restTemplate;
    }

    private String buildStringArray(Collection items) {
        boolean firstParam = true;
        StringBuilder builder = new StringBuilder();
        builder.append("[");
        for (String item : items) {
            if (!firstParam) {
                builder.append(",");
            }
            builder.append("'");
            builder.append(item);
            builder.append("'");
            firstParam = false;
        }
        builder.append("]");
        return builder.toString();
    }

    @Override
    public boolean send(NoticeData noticeData, Collection phones) {
        String type = noticeData.getType();

        String templateId = properties.getTemplates(type);

        if (templateId == null) {
            LogUtils.debug("templateId invalid");
            publishSendFailEvent(noticeData, phones, new SendFailedException("templateId invalid"), null);
            return false;
        }

        List paramsOrder = properties.getParamsOrder(type);

        ArrayList params = new ArrayList<>();

        if (!paramsOrder.isEmpty()) {
            Map paramMap = noticeData.getParams();
            for (String paramName : paramsOrder) {
                String paramValue = paramMap.get(paramName);

                params.add(paramValue);
            }
        }

        String nonce = RandomUtils.nextString(6);
        String paramsString = buildStringArray(params);
        String mobilesString = buildStringArray(phones);
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = CheckSumBuilder.getCheckSum(properties.getAppSecret(), nonce, curTime);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.set("AppKey", properties.getAppKey());
        headers.set("CurTime", curTime);
        headers.set("CheckSum", checkSum);
        headers.set("Nonce", nonce);

        MultiValueMap body = new LinkedMultiValueMap<>();
        body.add("templateid", templateId);
        body.add("mobiles", mobilesString);
        body.add("params", paramsString);
        ResponseEntity httpResponse = null;
        try {
            httpResponse =
                    restTemplate.exchange(SERVER_URL, HttpMethod.POST, new HttpEntity<>(body, headers), String.class);

            if (httpResponse.getBody() == null) {
                LogUtils.debug("response body ie null");
                publishSendFailEvent(
                        noticeData, phones, new SendFailedException("response body ie null"), httpResponse);
                return false;
            }

            String responseContent = httpResponse.getBody();

            LogUtils.debug("responseContent: {}", responseContent);

            NeteaseCloudResult result = objectMapper.readValue(responseContent, NeteaseCloudResult.class);

            boolean succeed = NeteaseCloudResult.SUCCESS_CODE.equals(result.getCode());
            if (succeed) {
                publishSendSuccessEvent(noticeData, phones, httpResponse);
            } else {
                publishSendFailEvent(noticeData, phones, new SendFailedException(result.getMsg()), httpResponse);
            }
            return succeed;
        } catch (Exception e) {
            LogUtils.debug(e.getLocalizedMessage(), e);
            publishSendFailEvent(noticeData, phones, e, httpResponse);
            return false;
        }
    }

    @Override
    public String getChannelName() {
        return "netease";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy