陶杰
2024-08-22 ee9032d9baf5f33e376d2d2699136e0a7b26bec7
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
package com.mzl.flower.service.register;
import com.aliyuncs.exceptions.ClientException;
import com.mzl.flower.base.cache.StringCacheClient;
import com.mzl.flower.config.SmsProperties;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.constant.Constants;
import com.mzl.flower.dto.request.SmsSendDTO;
import com.mzl.flower.utils.SmsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
 
@Service
@Transactional
public class SmsService {
 
    public static final String SMS_CODE_KEY = "SMS-CODE-KEY";
 
    public static final String SEPARATOR = ":";
 
    private final SmsProperties smsProperties;
 
    private final StringCacheClient stringCacheClient;
 
    public SmsService(SmsProperties smsProperties, StringCacheClient stringCacheClient) {
        this.smsProperties = smsProperties;
        this.stringCacheClient = stringCacheClient;
    }
 
 
    public static String generateSmsCode() {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            sb.append(random.nextInt(10)); // 生成0-9的随机数
        }
        return sb.toString();
    }
 
    /**
     * 发送短信验证码
     * @param dto
     */
    public void sendSmsCode(SmsSendDTO dto) {
 
        if(existsCode(dto.getTel())){
            throw new ValidationException("短信验证码已发送,请勿频繁发送");
        }
        String smsCode = generateSmsCode();
        String key;
        if(Constants.USER_TYPE.admin.name().equals(dto.getUserType())){
            key = SMS_CODE_KEY + SEPARATOR + Constants.USER_TYPE.admin.name() + SEPARATOR + dto.getTel();
        }else if(Constants.USER_TYPE.supplier.name().equals(dto.getUserType())){
            key = SMS_CODE_KEY + SEPARATOR + Constants.USER_TYPE.supplier.name() + SEPARATOR + dto.getTel();
        }else if(Constants.USER_TYPE.partner.name().equals(dto.getUserType())){
            key = SMS_CODE_KEY + SEPARATOR + Constants.USER_TYPE.partner.name() + SEPARATOR + dto.getTel();
        }else if(Constants.USER_TYPE.customer.name().equals(dto.getUserType())){
            key = SMS_CODE_KEY + SEPARATOR + Constants.USER_TYPE.customer.name() + SEPARATOR + dto.getTel();
        }else{
            throw new ValidationException("未知用户类型");
        }
 
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("code", smsCode);
        try {
            SmsUtil.sendSms(dto.getTel(),smsProperties.getVerificationCode(),paramMap);
            stringCacheClient.set(SMS_CODE_KEY + SEPARATOR + SEPARATOR + dto.getTel(),smsCode,60);
            stringCacheClient.set(key,smsCode,600);
        } catch (ClientException e) {
            throw new RuntimeException("短信发送失败");
        }
        //todo 发送短信
    }
 
    private boolean existsCode(String tel) {
        String smsCode = stringCacheClient.get(SMS_CODE_KEY + SEPARATOR + SEPARATOR + tel);
        if(StringUtils.isNotBlank(smsCode)){
            return true;
        }
        return false;
    }
}