Search in sources :

Example 6 with BusinessException

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();
    }
}
Also used : BusinessException(com.tansci.common.exception.BusinessException) TemplateDetails(com.tansci.domain.message.TemplateDetails) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Date(java.util.Date) Template(com.tansci.domain.message.Template)

Example 7 with BusinessException

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;
}
Also used : BusinessException(com.tansci.common.exception.BusinessException) MessageVo(com.tansci.domain.message.vo.MessageVo) Template(com.tansci.domain.message.Template) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with BusinessException

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;
}
Also used : BusinessException(com.tansci.common.exception.BusinessException) MessageVo(com.tansci.domain.message.vo.MessageVo) Template(com.tansci.domain.message.Template) Transient(java.beans.Transient)

Example 9 with BusinessException

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;
}
Also used : BusinessException(com.tansci.common.exception.BusinessException) MessageVo(com.tansci.domain.message.vo.MessageVo) Transient(java.beans.Transient)

Example 10 with BusinessException

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();
}
Also used : BusinessException(com.tansci.common.exception.BusinessException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

BusinessException (com.tansci.common.exception.BusinessException)10 Template (com.tansci.domain.message.Template)5 TemplateDetails (com.tansci.domain.message.TemplateDetails)3 MessageVo (com.tansci.domain.message.vo.MessageVo)3 Date (java.util.Date)3 Transient (java.beans.Transient)2 MessagingException (javax.mail.MessagingException)2 MimeMessage (javax.mail.internet.MimeMessage)2 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)2 BCryptPasswordEncoder (org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)2 PasswordEncoder (org.springframework.security.crypto.password.PasswordEncoder)2 Transactional (org.springframework.transaction.annotation.Transactional)2 SysUser (com.tansci.domain.system.SysUser)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 FileSystemResource (org.springframework.core.io.FileSystemResource)1 MailException (org.springframework.mail.MailException)1 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1