KKSU
2025-01-13 52e1d0189c197a77787bb276492a68b8d0f8b4c9
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
package cc.mrbird.febs.common.utils;
 
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import lombok.extern.slf4j.Slf4j;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * @author wzy
 * @date 2021-03-25
 **/
@Slf4j
public class ZzSmsSend {
 
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }
 
    private static final String SIGNNAME = "普珏";
//    private static final String TEMPLATECODE = "SMS_291555713";
    private static final String TEMPLATECODE = "SMS_310345616";
    public static boolean sendALiYun(String phone,String TemplateParam){
        com.aliyun.dysmsapi20170525.Client client = null;
        try {
            client = createClient("LTAI5t5ZXRtqy8CEGnUee1Z7", "zNxDI2GJq5XVWuYTtb2tl7YgyDSxEJ");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
//                .setSignName("阿里云短信测试")
                .setSignName(SIGNNAME)
//                .setTemplateCode("SMS_154950909")
                .setTemplateCode(TEMPLATECODE)
//                .setPhoneNumbers("15274802129")
                .setPhoneNumbers(phone)
//                .setTemplateParam("{\"code\":\"1234\"}");
                .setTemplateParam("{\"code\":\""+TemplateParam+"\"}");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
            log.info("短信发送:{}", sendSmsResponse);
            JSONObject jsonObject = JSONUtil.parseObj(sendSmsResponse);
            System.out.print(jsonObject);
            if(200 == sendSmsResponse.getStatusCode()) {
                return true;
            }else{
                return false;
            }
        } catch (TeaException error) {
            // 如有需要,请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要,请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return false;
 
    }
 
    private static final String URL = "http://zzsms365.com/v2sms.aspx?action=send";
 
    private static final String USER_ID = "1654";
    private static final String ACCOUNT = "1369815429";
    private static final String PWD = "1369815429";
 
    public static boolean sendVerifyCode(String telphone, String code, int time) {
        String content = "【美澳】您的验证码是{},请在{}分钟内输入,请勿泄露给他人,如非本人操作,请及时修改密码。";
        return send(telphone, StrUtil.format(content, code, time));
    }
 
    private static boolean send(String telphone, String content) {
        Map<String, Object> data = new HashMap<>();
        Long time = System.currentTimeMillis();
        data.put("userid", USER_ID);
        data.put("timestamp", time);
        data.put("mobile", telphone);
        String signStr = ACCOUNT + PWD + time;
        String sign = SecureUtil.md5(signStr);
        data.put("sign", sign);
        data.put("content", content);
 
        String response = HttpUtil.post(URL, data);
        log.info("短信发送:{}", response);
        if ("Success".equals(XmlUtil.xmlToMap(response).get("returnstatus"))) {
            return true;
        } else {
//            throw new GlobalException((String) XmlUtil.xmlToMap(response).get("message"));
            return false;
        }
    }
 
    public static void main(String[] args) {
//        Map<String, Object> data = new HashMap<>();
//        Long time = System.currentTimeMillis();
//        data.put("userid", USER_ID);
//        data.put("timestamp", time);
//        data.put("mobile", "15773002834");
//        String signStr = ACCOUNT + PWD + time;
//        String sign = SecureUtil.md5(signStr);
//        data.put("sign", sign);
//        data.put("content", "【HiBit】尊敬的用户,恭喜您于2010-03-25有一笔充值已成功到账,充值数量为10。");
//
//        String post = HttpUtil.post(URL, data);
//        System.out.println(post);
        boolean b = sendALiYun( "15274802129", "123456");
        System.out.println(b);
    }
}