hefeixia
2021-02-18 5b8c95c760840f09910730943b21391e47187315
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
package chat.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 com.google.gson.Gson;
 
import chat.consts.RestResult;
 
public class SmsService {
 
    private static class AliyunCommonResponse {
        String Message;
        String Code;
    }
    
    public RestResult.RestCode sendAliyunCode(String mobile, String code) {
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI4GGfU6RdYxSFvQ3LiwSP", "gXh3SVeyOf2eoVTg7OkKutyC8rHg2a");
        IAcsClient client = new DefaultAcsClient(profile);
 
        String templateparam = "{\"code\":\"" + code + "\"}";
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", "医视无忧");
 
        request.putQueryParameter("TemplateCode", "SMS_206100254");
        request.putQueryParameter("TemplateParam", templateparam);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            if (response.getData() != null) {
                AliyunCommonResponse aliyunCommonResponse = new Gson().fromJson(response.getData(), AliyunCommonResponse.class);
                if (aliyunCommonResponse != null) {
                    if (aliyunCommonResponse.Code.equalsIgnoreCase("OK")) {
                        return RestResult.RestCode.SUCCESS;
                    } else {
                        System.out.println("Send aliyun sms failure with message:" + aliyunCommonResponse.Message);
                    }
                }
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return RestResult.RestCode.ERROR_SERVER_ERROR;
    }
}