sinake
4 天以前 8a4acc6de1b0a3352d0e60a6483f4abfc55c6fae
ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/SmsController.java
@@ -13,6 +13,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.nio.charset.StandardCharsets;
@@ -28,19 +29,22 @@
@RestController
@RequestMapping("/sms")
public class SmsController {
    @Value("${sms.url}")
    private String url="http://103.21.119.249:3131/http/send.aspx";
    @Value("${sms.loginName}")
    private String loginName="qddxfsyy";
    @Value("${sms.passWord}")
    private String passWord="Qyfy_0718";
    @ApiOperation("短信服务->发送短信")
    @PostMapping("/send")
    public AjaxResult getApiList(@RequestBody SmsDTO DTO) {
        if (ObjectUtils.isNotEmpty(DTO.getTel())&&ObjectUtils.isNotEmpty(DTO.getMessageContent())) {
            String key= keyForMd5(loginName.toLowerCase(),passWord.toLowerCase());
            key="5b2d0edb45ac3583543fbf55d8af1e6d";
            String key= keyForMd5(loginName,passWord);
            String content="{\"LoginName\":\""+loginName+"\",\"PassWord\":\""+passWord+"\"," +
                    "\"Key\":\""+key+"\"," +
                    "\"Tel\":\""+DTO.getTel()+"\",\"MessageContent\":\""+DTO.getMessageContent()+"\"}";
           String result= HttpUtils.sendPost(url, Base64.getEncoder().encodeToString(content.getBytes()) );
            String result="";
            result= HttpUtils.sendPost(url, Base64.getEncoder().encodeToString(content.getBytes()) );
            return AjaxResult.success(result);
        } else {
            return AjaxResult.error("电话和消息不能为空");
@@ -52,36 +56,32 @@
    }
    public static String md5(String str) {
        return hashPasswordForStoring(str,"MD5");
    }
    public static String SHA1(String str) {
        return hashPasswordForStoring(str,"SHA1");
    }
    public static String hashPasswordForStoring(String password, String algorithm) {
        try {
            // 1. 获取哈希算法实例
            MessageDigest md = MessageDigest.getInstance(algorithm);
            // 2. 密码转 UTF-8 字节(和 .NET 完全一致)
            byte[] passwordBytes = password.getBytes(StandardCharsets.UTF_8);
            // 3. 计算哈希
            byte[] hashBytes = md.digest(passwordBytes);
            // 4. 转为大写十六进制字符串(和 .NET 输出格式一致)
            return bytesToHexUpperCase(hashBytes);
            // 1. 获取 MD5 消息摘要实例
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 2. 使用 UTF-8 编码将字符串转换为字节数组并计算哈希
            byte[] hashBytes = md.digest(str.getBytes(StandardCharsets.UTF_8));
            // 3. 将字节数组转换为十六进制字符串(小写)
            return bytesToHex(hashBytes).toLowerCase();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("不支持的哈希算法: " + algorithm, e);
            // MD5 是标准算法,正常情况下不会抛出此异常
            throw new RuntimeException("系统不支持 MD5 算法", e);
        }
    }
    private static String bytesToHexUpperCase(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
    /**
     * 将字节数组转换为十六进制字符串
     *
     * @param bytes 字节数组
     * @return 十六进制字符串
     */
    private static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        for (byte b : bytes) {
            // 转两位大写十六进制
            sb.append(String.format("%02X", b));
            // 将每个字节的高4位和低4位分别转换为十六进制字符
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }