MessageService.java 1.95 KB
package cn.com.glsx.stdinterface.modules.service;

import cn.com.glsx.stdinterface.modules.util.RopHttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

/**
 * @author zhouhaibao
 * @version 1.0
 * @description: 短信发送服务
 * @date 2021/11/14 14:23
 */
@Slf4j
@Service
@RefreshScope
public class MessageService {

    @Value("${sendMessageUrl}")
    private String sendMessageUrl;

    @Value("${sendMessageMethod}")
    private String sendMessageMethod;

    /**
     * 发送短信
     *
     * @param phone   电话号码,批量用逗号隔开
     * @param source  来源
     * @param type    类型
     * @param context 发送内容
     */
    @Async
    public void sendMessage(String phone, String source, String type, String context) {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>(5);
        try {
            params.add("phone", phone);
            params.add("source", source);
            params.add("messageSource", source);
            params.add("type", type);
            params.add("context", context);
            String post = RopHttpUtil.postE(params, String.class, sendMessageUrl + "?method=" + sendMessageMethod, sendMessageMethod);
            log.info("发送短信结果{}", post);
        } catch (Exception e) {
            log.error("发送短信到{}异常,{}", phone, e.getMessage());
            //throw new StdInterfaceException("444444", e.getMessage());
        }
    }

//    public static void main(String[] args) {
//        MessageService messageService = new MessageService();
//        messageService.sendMessage("18682185876", "eshield_workOrder_warm", "1", "this is a test!");
//    }

}