xin
2025-06-04 cf71e8dd17fa12a11d84ecd4c09d07d8f92792b1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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 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;
    }
}