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 final String SIGN = "【立研】"; public static final String MESSAGE = "您的验证码为:{code},请勿泄露于他人!"; public static void sendMsg(String url, String phone, String sign, String message, WinnerLookProperties properties) { Map params = new HashMap<>(); params.put("userCode", properties.getUserCode()); params.put("userPass", properties.getUserPass()); params.put("DesNo", phone); params.put("Msg", sign + message); params.put("smsType", "101"); String str = extractWithRegex(HttpRequest.exchangeMsg(HttpMethod.POST, url, convert(params))); if (ObjectUtils.isEmpty(str)) { log.error("短信调用异常 {}", str); throw new BadRequestException("短信调用异常"); } long i = Long.parseLong(str); if (i < 0) { throw new BadRequestException(WinnerLookEnum.find(str)); } } public static String extractWithRegex(String xml) { Pattern pattern = Pattern.compile( "]*>([^<]*)" ); Matcher matcher = pattern.matcher(xml); return matcher.find() ? matcher.group(1) : null; } /** * Map转MultiValueMap */ public static MultiValueMap convert(Map map) { MultiValueMap multiValueMap = new LinkedMultiValueMap<>(); map.forEach(multiValueMap::add); return multiValueMap; } }