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("loginLogger"); 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(); 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(); 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; } }