xin
2025-06-04 0c22832b808f8aea168cc25b3a968c65b3b44883
发送短信
1 files deleted
2 files added
5 files modified
349 ■■■■■ changed files
oying-common/src/main/java/com/oying/utils/HttpRequest.java 28 ●●●● patch | view | raw | blame | history
oying-system/src/main/java/com/oying/modules/security/rest/VerificationController.java 7 ●●●● patch | view | raw | blame | history
oying-system/src/main/resources/config/application-dev.yml 2 ●●● patch | view | raw | blame | history
oying-system/src/main/resources/config/application-prod.yml 2 ●●● patch | view | raw | blame | history
oying-tools/src/main/java/com/oying/utils/SendMessageUtils.java 58 ●●●● patch | view | raw | blame | history
oying-tools/src/main/java/com/oying/utils/Test.java 182 ●●●●● patch | view | raw | blame | history
oying-tools/src/main/java/com/oying/utils/WinnerLookEnum.java 50 ●●●●● patch | view | raw | blame | history
oying-tools/src/main/java/com/oying/utils/WinnerLookProperties.java 20 ●●●●● patch | view | raw | blame | history
oying-common/src/main/java/com/oying/utils/HttpRequest.java
@@ -14,6 +14,7 @@
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import javax.net.ssl.SSLContext;
@@ -22,6 +23,8 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
@@ -79,20 +82,33 @@
        return headers;
    }
    private static HttpHeaders get() {
    private static HttpHeaders getMsg() {
        HttpHeaders headers = new HttpHeaders();
        headers.set("accept", "*/*");
        headers.set("connection", "Keep-Alive");
        headers.set("user-agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        List<MediaType> mediaTypes = new ArrayList<>();
        mediaTypes.add(MediaType.ALL);
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); // 表单格式
        headers.setAccept(mediaTypes);
        headers.setConnection("Keep-Alive");
        headers.set("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        return headers;
    }
    /**
     * 短信
     * http返回String xml
     */
    public static String exchangeMsg(HttpMethod httpMethod, String url, MultiValueMap<String, String> map) {
        HttpHeaders headers = getMsg();
        HttpEntity<Object> httpEntity = new HttpEntity<>(map, headers);
        RestTemplate restTemplate = new RestTemplate(getFactory());
        return restTemplate.exchange(url, httpMethod, httpEntity, String.class).getBody();
    }
    /**
     * http返回String
     */
    public static String exchangeString(HttpMethod httpMethod, String url, Map<String, Object> map) {
        HttpHeaders headers = get();
        HttpHeaders headers = getHeaders();
        HttpEntity<Object> httpEntity = new HttpEntity<>(map, headers);
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.exchange(url, httpMethod, httpEntity, String.class).getBody();
oying-system/src/main/java/com/oying/modules/security/rest/VerificationController.java
@@ -4,6 +4,8 @@
import com.oying.annotation.rest.AnonymousGetMapping;
import com.oying.utils.R;
import com.oying.utils.RedisUtils;
import com.oying.utils.SendMessageUtils;
import com.oying.utils.WinnerLookProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
@@ -31,15 +33,18 @@
    private String key;
    @Value("${sms.time}")
    private Long time;
    private final WinnerLookProperties properties;
    @AnonymousGetMapping
    @ApiOperation("短信验证码")
    public ResponseEntity<Object> toPayAsPc(@RequestParam String phone) {
    public ResponseEntity<Object> verification(@RequestParam String phone) {
        String uuid = key + IdUtil.simpleUUID();
        System.out.println(phone);
        //创建验证码
        String verification = (int) ((Math.random() * 9 + 1) * 100000) + "";
        redisUtils.set(uuid, verification, time);
        String message = "您的验证码为:" + verification + ",请勿泄露于他人!";
        SendMessageUtils.sendMsg(phone, message, properties);
        return ResponseEntity.ok(R.success(uuid));
    }
}
oying-system/src/main/resources/config/application-dev.yml
@@ -178,6 +178,6 @@
  endpoint: https://obs.cn-southwest-2.myhuaweicloud.com
winner-look:
  url: https://118.178.116.15:8443/
  url-send-msg: https://118.178.116.15:8443/winnerrxd/api/trigger/SendMsg
  user-code: CQLYSXYJ
  user-pass: lych1205!
oying-system/src/main/resources/config/application-prod.yml
@@ -188,6 +188,6 @@
  endpoint: https://obs.cn-southwest-2.myhuaweicloud.com
winner-look:
  url: https://118.178.116.15:8443/
  url-send-msg: https://118.178.116.15:8443/winnerrxd/api/trigger/SendMsg
  user-code: CQLYSXYJ
  user-pass: lych1205!
oying-tools/src/main/java/com/oying/utils/SendMessageUtils.java
@@ -1,35 +1,61 @@
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 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;
    }
}
oying-tools/src/main/java/com/oying/utils/Test.java
File was deleted
oying-tools/src/main/java/com/oying/utils/WinnerLookEnum.java
New file
@@ -0,0 +1,50 @@
package com.oying.utils;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public enum WinnerLookEnum {
    ONE(-1, "应用程序错误"),
    THREE(-3, "用户名或密码错误"),
    FOUR(-4, "短信内容和备案的模板不一样"),
    FIVE(-5, "签名不正确"),
    SEVEN(-7, "余额不足"),
    EIGHT(-8, "无可用通道或不在通道时间范围"),
    NINE(-9, "无效号码"),
    TEN(-10, "签名内容不符合长度"),
    ELEVEN(-11, "用户有效期过期"),
    TWELVE(-12, "黑名单"),
    SIXTEEN(-16, "接口请求过于频繁,余额接口 5s 秒一次,其他接口适当调整"),
    SEVENTEEN(-17, "非法 IP"),
    EIGHTEEN(-18, "Msg 内容格式错误"),
    NINETEEN(-19, "短信数量错误,小于 1 /大于 500(个性化)/大于 1000(群发)"),
    TWENTY(-20, "号码错误或者黑名单"),
    TWENTY_THREE(-23, "解密失败"),
    TWENTY_FOUR(-24, "短信包含用户敏感信息"),
    TWENTY_FIVE(-25, "用户被冻结"),
    TWENTY_SIX(-26, "无效数据"),
    TWENTY_SEVEN(-27, "请求参数错误"),
    TWENTY_EIGHT(-28, "无效数据"),
    FORTY_ONE(-41, "指定短信模板类型错误或短信类型参数错误"),
    FORTY_FOUR(-44, "自定义扩展号不符合规则(1-16 位数字)"),
    FORTY_SIX(-46, "用户黑名单"),
    FORTY_SEVEN(-47, "系统黑名单"),
    FORTY_EIGHT(-48, "号码超频拦截"),
    FIFTY_ONE(-51, "超过设置的每月短信条数的限制"),
    FIFTY_FIVE(-54, "短信包含系统敏感信息"),
    ZERO(0, "未知错误");
    private final int key;
    private final String value;
    public static String find(Integer key) {
        for (WinnerLookEnum value : WinnerLookEnum.values()) {
            if (value.getKey() == key) {
                return value.getValue();
            }
        }
        return ZERO.getValue();
    }
}
oying-tools/src/main/java/com/oying/utils/WinnerLookProperties.java
New file
@@ -0,0 +1,20 @@
package com.oying.utils;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
 * @author xin
 * @description
 * @date 2025/6/4 14:32
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "winner-look")
public class WinnerLookProperties {
    private String urlSendMsg;
    private String userCode;
    private String userPass;
}