use of com.tansci.common.exception.BusinessException in project tansci by typ1805.
the class MailServiceImpl method sendAttachFile.
/**
* @MonthName: sendAttachFile
* @Description: 带附件的邮件
* @Author: tanyp
* @Date: 2021/6/7 9:30
* @Param: [dto]
* @return: void
*/
@Override
public MessageVo sendAttachFile(MessageDto dto) {
try {
log.info("=======带附件的邮件开始,请求参数:{}", JSON.toJSON(dto));
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
// true表示构建一个可以带附件的邮件对象
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
message.setSubject(dto.getSubject());
message.setFrom(sender);
message.setTo(dto.getRecipient());
if (Objects.nonNull(dto.getCc())) {
message.setCc(dto.getCc());
}
if (Objects.nonNull(dto.getBcc())) {
message.setBcc(dto.getBcc());
}
message.setSentDate(new Date());
Template template = templateService.getOne(Wrappers.<Template>lambdaQuery().eq(Template::getCode, dto.getCode()));
if (Objects.isNull(template)) {
throw new BusinessException("邮件模板编码不存在,请核实!");
}
dto.setText(template.getContent());
String text = MessageUtils.assembly(dto.getText(), dto.getParam());
if (Objects.isNull(text)) {
throw new BusinessException("邮件模板参数有误,请核查!");
}
message.setText(text);
// 第一个参数是自定义的名称,后缀需要加上,第二个参数是文件的位置
dto.getAttachments().forEach(file -> {
try {
message.addAttachment(file.getName(), file);
} catch (MessagingException e) {
log.error("=========邮件附件解析异常:{}", e);
}
});
javaMailSender.send(mimeMessage);
log.info("=======带附件的邮件结束");
TemplateDetails details = new TemplateDetails();
details.setCode(dto.getCode());
details.setState(0);
details.setContent(JSON.toJSONString(message));
details.setSendTime(LocalDateTime.now());
templateDetailsService.save(details);
return MessageVo.builder().code(Constants.NEWS_SUCCESS_CODE).message(Constants.NEWS_SUCCESS_MESSAGE).build();
} catch (MessagingException e) {
log.error("==========邮件====sendAttachFile=====异常:{}", e);
return MessageVo.builder().code(Constants.NEWS_FAIL_CODE).message(Constants.NEWS_FAIL_MESSAGE).build();
}
}
use of com.tansci.common.exception.BusinessException in project tansci by typ1805.
the class TemplateServiceImpl method saveTemplate.
@Transactional
@Override
public Integer saveTemplate(Template template) {
log.info("=========saveTemplate:{}", JSON.toJSON(template));
String id = UUIDUtils.getUUID();
template.setId(id);
template.setDelFlag(0);
template.setCreater(SecurityUserUtils.getUser().getId());
template.setUpdateTime(LocalDateTime.now());
template.setCreateTime(LocalDateTime.now());
int row = 0;
switch(template.getBusinessType()) {
case 0:
// 短信
template.setState(0);
row = this.baseMapper.insert(template);
if (row > 0) {
// 请求阿里云短信API,添加模板
MessageVo message = aliSmsServiceImpl.addSmsTemplate(SmsTemplateDto.builder().templateType(template.getType()).templateName(template.getName()).templateContent(template.getContent()).remark(template.getRemark()).build());
// 更新模板
Template tem = new Template();
tem.setId(id);
if (message.getCode().equals(Constants.NEWS_SUCCESS_CODE)) {
tem.setCode(message.getTemplateCode());
tem.setState(0);
} else {
tem.setState(2);
}
tem.setUpdateTime(LocalDateTime.now());
this.updateById(tem);
}
break;
case 1:
// 邮件
template.setState(1);
template.setCode("YJ" + System.currentTimeMillis());
row = this.baseMapper.insert(template);
break;
default:
throw new BusinessException("添加失败,业务类型不存在!");
}
return row;
}
use of com.tansci.common.exception.BusinessException in project tansci by typ1805.
the class TemplateServiceImpl method delTemplate.
@Transient
@Override
public Integer delTemplate(String id) {
log.info("=========delTemplate:{}", id);
Template template = this.baseMapper.selectById(id);
if (Objects.isNull(template)) {
throw new BusinessException("删除失败,模板不存在!");
}
int row = 0;
switch(template.getBusinessType()) {
case 0:
// 请求阿里云短信API,修改模板
MessageVo message = aliSmsServiceImpl.deleteSmsTemplate(SmsTemplateDto.builder().templateCode(template.getCode()).build());
// 更新模板
if (!message.getCode().equals(Constants.NEWS_SUCCESS_CODE)) {
template.setDelFlag(1);
template.setUpdateTime(LocalDateTime.now());
this.baseMapper.updateById(template);
}
break;
case 1:
// 邮件
template.setDelFlag(1);
row = this.baseMapper.updateById(template);
break;
default:
throw new BusinessException("删除失败,业务类型不存在!");
}
return row;
}
use of com.tansci.common.exception.BusinessException in project tansci by typ1805.
the class TemplateServiceImpl method updateTemplate.
@Transient
@Override
public Integer updateTemplate(Template template) {
log.info("=========updateTemplate:{}", JSON.toJSON(template));
template.setUpdateTime(LocalDateTime.now());
int row = 0;
switch(template.getBusinessType()) {
case 0:
// 短信
template.setState(0);
row = this.baseMapper.updateById(template);
if (row > 0) {
// 请求阿里云短信API,修改模板
MessageVo message = aliSmsServiceImpl.modifySmsTemplate(SmsTemplateDto.builder().templateCode(template.getCode()).templateType(template.getType()).templateName(template.getName()).templateContent(template.getContent()).remark(template.getRemark()).build());
// 更新模板
if (!message.getCode().equals(Constants.NEWS_SUCCESS_CODE)) {
template.setState(2);
template.setUpdateTime(LocalDateTime.now());
this.baseMapper.updateById(template);
}
}
break;
case 1:
// 邮件
template.setState(1);
row = this.baseMapper.updateById(template);
break;
default:
throw new BusinessException("编辑失败,业务类型不存在!");
}
return row;
}
use of com.tansci.common.exception.BusinessException in project tansci by typ1805.
the class AuthorizedServiceImpl method wxLogin.
@Override
public AuthorizedVo wxLogin() {
if (!AuthorizedConfig.WX_APP_ID.startsWith("wx")) {
throw new BusinessException("无效的微信配置信息!");
}
// 微信开放平台授权baseUrl
String baseUrl = AuthorizedConfig.WX_BASE_URL + "?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=%s#wechat_redirect";
// 回调地址
String redirectUrl = AuthorizedConfig.WX_REDIRECT_URL;
try {
// url编码
redirectUrl = URLEncoder.encode(redirectUrl, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new BusinessException(e.getMessage());
}
// 防止csrf攻击(跨站请求伪造攻击),一般情况下会使用一个随机数
String state = UUIDUtils.getUUID();
// 生成qrcodeUrl
String qrcodeUrl = String.format(baseUrl, AuthorizedConfig.WX_APP_ID, redirectUrl, state);
log.info("========生成qrcodeUrl:{}===========", qrcodeUrl);
String qrcode = QRCodeUtils.crateQRCode(qrcodeUrl, null, null);
return AuthorizedVo.builder().state(state).qrcode(qrcode).build();
}
Aggregations