彭雪彬
2025-07-14 c1d20b425b10e8ba59f102dd1ab413055883eed0
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
62
63
64
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<String, String> 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(
                "<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;
    }
}