package com.oying.utils;
|
|
import com.oying.exception.BadRequestException;
|
import lombok.Data;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.http.HttpMethod;
|
|
import java.util.HashMap;
|
|
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.ObjectUtils;
|
|
import java.util.Map;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
/**
|
* @author xin
|
* @description
|
* @date 2025/6/4 01:10
|
*/
|
@Slf4j
|
@Data
|
public class SendMessageUtils {
|
|
public static void sendMsg(String phone, String message, WinnerLookProperties properties) {
|
Map<String, String> params = new HashMap<>();
|
params.put("userCode", properties.getUserCode());
|
params.put("userPass", properties.getUserPass());
|
params.put("DesNo", phone);
|
params.put("Msg", message + "【立研】");
|
params.put("smsType", "101");
|
String str = HttpRequest.exchangeMsg(HttpMethod.POST, properties.getUrlSendMsg(), convert(params));
|
if (ObjectUtils.isEmpty(extractWithRegex(str))) {
|
log.error("短信调用异常 {}", str);
|
throw new BadRequestException("短信调用异常");
|
}
|
int i = Integer.parseInt(str);
|
if (i < 0) {
|
throw new BadRequestException(WinnerLookEnum.find(i));
|
}
|
}
|
|
public static String extractWithRegex(String xml) {
|
Pattern pattern = Pattern.compile(
|
"<string[^>]*>([^<]*)</string>"
|
);
|
Matcher matcher = pattern.matcher(xml);
|
return matcher.find() ? matcher.group(1) : null;
|
}
|
|
/**
|
* Map转MultiValueMap
|
*/
|
public static <K, V> MultiValueMap<K, V> convert(Map<K, V> map) {
|
MultiValueMap<K, V> multiValueMap = new LinkedMultiValueMap<>();
|
map.forEach(multiValueMap::add);
|
return multiValueMap;
|
}
|
}
|