peter
2025-11-19 c8a4e883abfe89e83fda268c14f30f3e6e45edee
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
package com.nq.utils.smsUtil.support;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.nq.common.ServerResponse;
import com.nq.utils.smsUtil.SMSProvider;
 
public class AliyunSMSProvider implements SMSProvider {
    @Override
    public ServerResponse sendSingleMessage(String mobile, String content) throws Exception {
        return null;
    }
 
    @Override
    public ServerResponse sendMessageByTempId(String mobile, String content, String templateId) throws Exception {
        return null;
    }
 
    @Override
    public ServerResponse sendVerifyMessage(String mobile, String verifyCode) throws Exception {
        //String smsContent = "【" + this.sign + "】您的验证码为" + verifyCode + ",在10分钟内有效。";
        String smsContent = "Your verification code is "+verifyCode+",at 10 Valid in minutes。";
        return  msgSender(mobile,smsContent);
    }
 
    @Override
    public String formatVerifyCode(String code) {
        return null;
    }
 
    @Override
    public ServerResponse sendInternationalMessage(String content, String phone) throws Exception {
        String smsContent = "Your verification code is "+content+",at 10 Valid in minutes。";
        return  msgSender(phone,smsContent);
    }
 
 
    @Override
    public ServerResponse sendCustomMessage(String mobile, String content) throws Exception {
        return null;
    }
    private ServerResponse msgSender(String mobile,String smsContent){
        DefaultProfile profile = DefaultProfile.getProfile("ap-southeast-1", "", "");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.ap-southeast-1.aliyuncs.com");
        request.setSysVersion("2018-05-01");
        request.setSysAction("SendMessageToGlobe");
        request.putQueryParameter("RegionId", "ap-southeast-1");
        request.putQueryParameter("To", mobile);
        request.putQueryParameter("Message", smsContent);
        try {
            CommonResponse response = client.getCommonResponse(request);
            String data = response.getData();
            JSONObject jsonObject = JSON.parseObject(data);
 
            ServerResponse serverResponse = ServerResponse.createByErrorMsg("系统错误");
 
 
            System.out.println(response.getData());
            if(jsonObject.getString("ResponseCode").equals("OK")) {
                serverResponse=ServerResponse.createBySuccessMsg("短信发送成功!");
            }else{
                serverResponse = ServerResponse.createByErrorMsg("短信发送失败,请联系平台处理!");
            }
            return serverResponse;
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static void main(String[] args) {
        AliyunSMSProvider aliyunSMSProvider = new AliyunSMSProvider();
        try {
 
        }catch (Exception e){
            e.printStackTrace();
        }
 
    }
}