yangys
2024-05-18 040976de6f9934b99f30268a28e2ecf42260e217
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.qianwen.core.sms;
 
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import java.time.Duration;
import java.util.Collection;
import java.util.Map;
import com.qianwen.core.redis.cache.BladeRedis;
import com.qianwen.core.sms.model.SmsCode;
import com.qianwen.core.sms.model.SmsData;
import com.qianwen.core.sms.model.SmsResponse;
import com.qianwen.core.sms.props.SmsProperties;
import com.qianwen.core.tool.jackson.JsonUtil;
import com.qianwen.core.tool.utils.StringUtil;
import org.springframework.http.HttpStatus;
 
 
public class AliSmsTemplate implements SmsTemplate {
    private static final int SUCCESS = 200;
    private static final String FAIL = "fail";
    private static final String OK = "ok";
    private static final String DOMAIN = "dysmsapi.aliyuncs.com";
    private static final String VERSION = "2017-05-25";
    private static final String ACTION = "SendSms";
    private final SmsProperties smsProperties;
    private final IAcsClient acsClient;
    private final BladeRedis bladeRedis;
 
    public AliSmsTemplate(final SmsProperties smsProperties, final IAcsClient acsClient, final BladeRedis bladeRedis) {
        this.smsProperties = smsProperties;
        this.acsClient = acsClient;
        this.bladeRedis = bladeRedis;
    }
 
    @Override // com.qianwen.core.sms.SmsTemplate
    public SmsResponse sendMessage(SmsData smsData, Collection<String> phones) {
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(DOMAIN);
        request.setSysVersion(VERSION);
        request.setSysAction(ACTION);
        request.putQueryParameter("PhoneNumbers", StringUtil.join(phones));
        request.putQueryParameter("TemplateCode", this.smsProperties.getTemplateId());
        request.putQueryParameter("TemplateParam", JsonUtil.toJson(smsData.getParams()));
        request.putQueryParameter("SignName", this.smsProperties.getSignName());
        try {
            CommonResponse response = this.acsClient.getCommonResponse(request);
            Map<String, Object> data = JsonUtil.toMap(response.getData());
            String code = FAIL;
            if (data != null) {
                code = String.valueOf(data.get("Code"));
            }
            return new SmsResponse(response.getHttpStatus() == SUCCESS && code.equalsIgnoreCase(OK), Integer.valueOf(response.getHttpStatus()), response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
            return new SmsResponse(Boolean.FALSE.booleanValue(), Integer.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()), e.getMessage());
        }
    }
 
    @Override // com.qianwen.core.sms.SmsTemplate
    public SmsCode sendValidate(SmsData smsData, String phone) {
        SmsCode smsCode = new SmsCode();
        boolean temp = sendSingle(smsData, phone);
        if (temp && StringUtil.isNotBlank(smsData.getKey())) {
            String id = StringUtil.randomUUID();
            String value = smsData.getParams().get(smsData.getKey());
            this.bladeRedis.setEx(cacheKey(phone, id), value, Duration.ofMinutes(30L));
            smsCode.setId(id).setValue(value);
        } else {
            smsCode.setSuccess(Boolean.FALSE.booleanValue());
        }
        return smsCode;
    }
 
    @Override // com.qianwen.core.sms.SmsTemplate
    public boolean validateMessage(SmsCode smsCode) {
        String id = smsCode.getId();
        String value = smsCode.getValue();
        String phone = smsCode.getPhone();
        String cache = (String) this.bladeRedis.get(cacheKey(phone, id));
        if (StringUtil.isNotBlank(value) && StringUtil.equalsIgnoreCase(cache, value)) {
            this.bladeRedis.del(cacheKey(phone, id));
            return true;
        }
        return false;
    }
}