中扬CRM客户关系管理系统
#
LSH
2023-10-06 c313cc06a02705d249da8f40086e152d39108d4b
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package com.zy.crm.manager.utils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponseBody;
import darabonba.core.client.ClientOverrideConfiguration;
 
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * 短信发送工具
 */
public class SmsUtils {
 
    /**
     * 阿里云市场API
     */
    private static boolean aliyun_1(String phone, String code) {
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        try {
            HashMap<String, Object> headers = new HashMap<>();
            HashMap<String, Object> param = new HashMap<>();
 
            String APPCODE = "15ce5d8be5e348c7b680dfd7cfb8307e";
            headers.put("Authorization", "APPCODE " + APPCODE);
 
            param.put("templateId", "MEA92C3A45");
            param.put("receive", phone);
            param.put("tag", code);
 
            String response = new HttpHandler.Builder()
                    .setUri("https://smkjdxtzjk.market.alicloudapi.com")
                    .setPath("/standard/sms/send")
                    .setHeaders(headers)
                    .setParams(param)
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            if (Integer.parseInt(jsonObject.get("code").toString()) == 200) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    /**
     * 阿里云市场API  分配了新的规划申请单任务!请注意查收!
     */
    private static boolean aliyun_2(String phone, String code) {
        try {
            HashMap<String, Object> headers = new HashMap<>();
            HashMap<String, Object> param = new HashMap<>();
 
            String APPCODE = "15ce5d8be5e348c7b680dfd7cfb8307e";
            headers.put("Authorization", "APPCODE " + APPCODE);
 
            param.put("templateId", "M6F3C81FE3");
            param.put("receive", phone);
            param.put("tag", code);
 
            String response = new HttpHandler.Builder()
                    .setUri("https://smkjdxtzjk.market.alicloudapi.com")
                    .setPath("/standard/sms/send")
                    .setHeaders(headers)
                    .setParams(param)
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            if (Integer.parseInt(jsonObject.get("code").toString()) == 200) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
    private static AsyncClient getClient() {
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId("LTAI4GBCtqGZAn5XDEREh1Pp")
                .accessKeySecret("SQQkh2kps3wxfbNXUg5nLZgWS2CMjm")
                .build());
 
        // Configure the Client
        AsyncClient client = AsyncClient.builder()
                //.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
                .credentialsProvider(provider)
                //.serviceConfiguration(Configuration.create()) // Service-level configuration
                // Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                        //.setConnectTimeout(Duration.ofSeconds(30))
                )
                .build();
 
        return client;
    }
 
    /**
     * 发送短信验证码-阿里云原生
     */
    private static boolean aliyun_origin(String phone, String code) {
        AsyncClient client = getClient();
        try {
            HashMap<String, Object> templateParam = new HashMap<>();
            templateParam.put("code", code);
 
            // Parameter settings for API request
            SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                    .phoneNumbers(phone)
                    .signName("某某商场")
                    .templateCode("SMS_195220399")
                    .templateParam(JSON.toJSONString(templateParam))
                    // Request-level configuration rewrite, can set Http request parameters, etc.
                    // .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
                    .build();
 
            // Asynchronously get the return value of the API request
            CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
            // Synchronously get the return value of the API request
            SendSmsResponse resp = response.get();
            SendSmsResponseBody body = resp.getBody();
//            System.out.println(body.getCode());
//            System.out.println(body.getMessage());
            if (body.getCode().equals("OK")) {
                return true;
            }
            return false;
        } catch (Exception e) {
            return false;
        } finally {
            // Finally, close the client
            client.close();
        }
    }
 
    /**
     * 发送短信验证码-阿里云原生
     */
    public static boolean sendSmsCode(String phone, String code) {
        return aliyun_1(phone, code);
    }
 
    /**
     * 发送短信-阿里云原生
     */
    public static boolean sendSmsCodeText(String phone, String code) {
        return aliyun_2(phone, code);
    }
 
    /**
     * 获取随机数
     * @param length 随机数长度
     */
    public static String getRandomNum(Integer length) {
        Random random = new Random();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            buffer.append(random.nextInt(10));
        }
        return buffer.toString();
    }
 
    /**
     * 正则验证手机号
     */
    public static boolean verifyPhone(String phone) {
        // 定义手机号码的正则表达式
        String regex = "^1[3456789]\\d{9}$";
        // 编译正则表达式
        Pattern pattern = Pattern.compile(regex);
        // 创建 Matcher 对象
        Matcher matcher = pattern.matcher(phone);
        // 进行匹配
        if (!matcher.matches()) {
            return false;
        }
        return true;
    }
 
    public static void main(String[] args) throws Exception {
        sendSmsCode("17788886666","666666");
    }
 
}