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

import cn.com.glsx.stdinterface.common.exception.StdInterfaceException;
import cn.com.glsx.stdinterface.modules.util.RopHttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
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
 */
@Service
@Slf4j
public class MessageService {

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

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

    /**
     * 发送短信
     *
     * @param phone   电话号码,批量用逗号隔开
     * @param source  来源
     * @param type    类型
     * @param context 发送内容
     */
    public void sendMessage(String phone, String source, String type, String context) {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>(4);
        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) {
            e.printStackTrace();
            log.info("发送短信异常,{}", e.getMessage());
            throw new StdInterfaceException("4444444", e.getMessage());
        }
    }


}