| | |
| | | package com.oying.utils; |
| | | |
| | | import com.oying.exception.BadRequestException; |
| | | import lombok.Data; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | 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 { |
| | | @Value("${winner-look.url}") |
| | | private String url; |
| | | @Value("${winner-look.user-code}") |
| | | private String userCode; |
| | | @Value("${winner-look.user-pass}") |
| | | private String userPass; |
| | | |
| | | public static void main(String[] args) { |
| | | String url = "https://118.178.116.15:8443/winnerrxd/api/trigger/SendMsg"; |
| | | Map<String, Object> paramMap = new HashMap<>(); |
| | | paramMap.put("userCode", "CQLYSXYJ"); |
| | | paramMap.put("userPass", "lych1205!"); |
| | | paramMap.put("DesNo", "15213186640"); |
| | | paramMap.put("Msg", "短信内容【立研】"); |
| | | paramMap.put("smsType", "101"); |
| | | System.out.println(HttpRequest.exchangeString(HttpMethod.POST, url, paramMap)); |
| | | public static void sendMsg(String url, 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, url, 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; |
| | | } |
| | | } |