Sunshine
2024-11-04 177f10973fd9bc1f0930369bb086ad9f0053440c
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
package com.nova.sankuai.infra.utils;
 
import com.nova.sankuai.domain.entity.SmsTemplate;
import com.nova.sankuai.domain.entity.SystemParam;
import com.nova.sankuai.infra.config.CommonException;
import com.nova.sankuai.infra.constants.Constants;
import com.nova.sankuai.infra.mapper.SystemParamMapper;
import com.nova.sankuai.infra.utils.sms.FirstMessage;
import com.nova.sankuai.infra.utils.sms.shandongMessage.ShanDongMessageSendUtil;
import com.nova.sankuai.service.ISmsTemplateService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
 
/**
 * @author 短信发送工具类
 */
@Component
@AllArgsConstructor
public class MessageSendUtil {
 
 
    private static final String LOGIN_CODE = "${loginCode}";
    private static final String CUSTOMER_NAME = "${customerName}";
    private static final String MANAGER_NAME = "${managerName}";
    private static final String MANAGER_PHONE = "${managerPhone}";
    private static final String TOTAL_CREDIT = "${totalCredit}";
    private final ISmsTemplateService service;
 
    private static final Logger logger = LoggerFactory.getLogger("smsLogger");
 
    private final SystemParamMapper systemParamMapper;
 
 
    public boolean sendCode(String phone, String code, Integer source, String ip) {
        if (StringUtils.isEmpty(phone)) {
            throw new CommonException("发送短信的手机号不能为空");
        }
        SmsTemplate template = service.lambdaQuery().eq(SmsTemplate::getCode, Constants.MessageCode.LOGIN_NOTIFY).eq(SmsTemplate::getSource, source).one();
        if (template == null || StringUtils.isEmpty(template.getContent())) {
            throw new CommonException("短信模板不能为空");
        }
        String content = template.getContent();
        logger.info("登录验证码日志: 短信模板配置参数{}",template);
        if (StringUtils.isNotBlank(code)) {
            content = content.replace(LOGIN_CODE, code);
        }
        boolean result = sendMessage(phone, content);
        return result;
    }
 
    public void sendNotify(String phone, String customerName, String managerName, String managerPhone, Integer source) {
        if (StringUtils.isEmpty(phone)) {
            throw new CommonException("发送短信的手机号不能为空");
        }
        SmsTemplate template = service.lambdaQuery().eq(SmsTemplate::getCode, Constants.MessageCode.APPLY_NOTIFY).eq(SmsTemplate::getSource, source).one();
        if (template == null || StringUtils.isEmpty(template.getContent())) {
            throw new CommonException("短信模板不能为空");
        }
        String content = template.getContent();
        if (StringUtils.isNotBlank(customerName)) {
            content = content.replace(CUSTOMER_NAME, customerName);
        }
        if (StringUtils.isNotBlank(managerName)) {
            content = content.replace(MANAGER_NAME, managerName);
        }
        if (StringUtils.isNotBlank(managerPhone)) {
            content = content.replace(MANAGER_PHONE, managerPhone);
        }
        sendMessage(phone, content);
    }
 
    public boolean sendMessage(String phone, String content) {
        boolean success = false;
        SystemParam defaultParam = systemParamMapper.getDefaultParam();
        logger.info("登录验证码日志: 系统配置参数{}",defaultParam);
        if (defaultParam.getMessageSend() == 0) {
            success = FirstMessage.sendMessage(phone, content);
            if (!success) {
                logger.info("登录验证码日志: 用户手机:" + phone + "原短信商" + "验证码发送返回失败");
                throw new CommonException("验证码发送失败");
            }
        } else if (defaultParam.getMessageSend() == 1) {
            success = ShanDongMessageSendUtil.sendMessage(phone, content);
            if (!success) {
                logger.info("登录验证码日志: 用户手机:" + phone + "山东短信商" + "验证码发送返回失败");
                throw new CommonException("验证码发送失败");
            }
        }
        return success;
    }
}