huliguo
10 天以前 e3a2245265516fef78b4737d6fffc939e7c5e0af
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.ruoyi.errand.utils.sms;
 
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Service;
 
 
public class AliyunSmsService {
 
    // 阿里云短信服务配置
    private static final String ACCESS_KEY_ID = "LTAI5tRtwxPMw8aohbLLcjjo";
    private static final String ACCESS_KEY_SECRET = "NSKeley8OjCsl7AQJnDKxPIh1dIaOu";
    private static final String SIGN_NAME = "玮儿科技";
    private static final String TEMPLATE_CODE = "SMS_319401491";
    // 新增跑腿员接单提醒模板 CODE
    private static final String TEMPLATE_CODE_ORDER = "SMS_489100144";
    /**
     * 发送短信验证码
     * @param phoneNumber 手机号码
     * @param code 验证码
     * @return 是否发送成功
     */
    public static boolean sendVerificationCode(String phoneNumber, String code) {
        // 初始化客户端
        DefaultProfile profile = DefaultProfile.getProfile(
            "cn-chengdu",
                ACCESS_KEY_ID,
                ACCESS_KEY_SECRET
        );
        IAcsClient client = new DefaultAcsClient(profile);
        
        // 构建请求
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        
        // 设置请求参数
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", SIGN_NAME);
        request.putQueryParameter("TemplateCode", TEMPLATE_CODE);
        
        // 模板参数JSON,根据实际模板调整
        String templateParam = String.format("{\"code\":\"%s\"}", code);
        request.putQueryParameter("TemplateParam", templateParam);
        
        try {
            // 发送请求并获取响应
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            
            // 解析响应结果
            return response.getHttpStatus() == 200 && 
                   response.getData().contains("\"Code\":\"OK\"");
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        
        return false;
    }
    
    /**
     * 生成随机验证码
     * @param length 验证码长度
     * @return 随机验证码
     */
    public static String generateVerificationCode(int length) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append((int)(Math.random() * 10));
        }
        return sb.toString();
    }
    /**
     * 发送跑腿员接单提醒短信
     * @param phoneNumber 跑腿员手机号码
     * @return 是否发送成功
     */
    public static boolean sendOrderRemindSms(String phoneNumber) {
        // 初始化客户端(复用已有的配置)
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-chengdu",
                ACCESS_KEY_ID,
                ACCESS_KEY_SECRET
        );
        IAcsClient client = new DefaultAcsClient(profile);
 
        // 构建请求
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
 
        // 设置请求参数
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", SIGN_NAME);
        request.putQueryParameter("TemplateCode", TEMPLATE_CODE_ORDER);
 
        // 该模板无自定义变量,传空 JSON(根据模板实际情况,若有变量需按格式填充)
        request.putQueryParameter("TemplateParam", "{}");
 
        try {
            // 发送请求并获取响应
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
 
            // 解析响应结果
            return response.getHttpStatus() == 200 &&
                    response.getData().contains("\"Code\":\"OK\"");
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
 
        return false;
    }
 
 
    public static void main(String[] args) {
      /*  String phoneNumber = "19923261698";
        boolean result = sendVerificationCode(phoneNumber,"123123");
        System.out.println("短信发送结果:" + result);*/
        // 测试发送接单提醒
        String phoneNumberOrder = "19923261698"; // 替换为实际跑腿员手机号
        boolean resultOrder = sendOrderRemindSms(phoneNumberOrder);
        System.out.println("接单提醒短信发送结果:" + resultOrder);
 
    }
}