Search in sources :

Example 1 with MessageVo

use of com.tansci.domain.message.vo.MessageVo in project tansci by typ1805.

the class AliSmsServiceImpl method send.

/**
 * @MonthName: send
 * @Description: 发短信
 * @Author: tanyp
 * @Date: 2021/6/7 14:50
 * @Param: [dto]
 * @return: MessageVo
 */
@Override
public MessageVo send(MessageDto dto) {
    try {
        log.info("======发送短信开始,请求参数:{}", JSON.toJSON(dto));
        Client client = createClient();
        // 组装请求对象
        SendSmsRequest request = new SendSmsRequest();
        // 外部流水扩展字段
        String outId = UUID.randomUUID().toString();
        request.setOutId(outId);
        // 支持对多个手机号码发送短信,手机号码之间以英文逗号(,)分隔。上限为1000个手机号码。批量调用相对于单条调用及时性稍有延迟。
        request.setPhoneNumbers(dto.getPhone());
        // 短信签名名称
        request.setSignName(smsConfig.getSignName());
        // 短信模板ID
        request.setTemplateCode(dto.getCode());
        // 短信模板变量对应的实际值,JSON格式。如果JSON中需要带换行符,请参照标准的JSON协议处理。
        request.setTemplateParam(JSON.toJSONString(dto.getParam()));
        // 发送短信
        SendSmsResponse res = client.sendSms(request);
        TemplateDetails details = new TemplateDetails();
        MessageVo message = MessageVo.builder().build();
        if (Objects.equals(Constants.NEWS_SUCCESS_CODE, res.body.getCode())) {
            log.info("======发送短信成功,返回值:{}", JSON.toJSON(res.body));
            message.setCode(Constants.NEWS_SUCCESS_CODE);
            message.setMessage(Constants.NEWS_SUCCESS_MESSAGE);
            details.setState(0);
        } else {
            log.info("======发送短信失败,返回值:{}", JSON.toJSON(res.body));
            message.setCode(Constants.NEWS_FAIL_CODE);
            message.setMessage(Constants.NEWS_FAIL_MESSAGE);
            details.setState(1);
        }
        details.setCode(dto.getCode());
        details.setContent(JSON.toJSONString(request));
        details.setSendTime(LocalDateTime.now());
        templateDetailsService.save(details);
        return message;
    } catch (Exception e) {
        log.error("======发送短信异常:{}", e.getMessage());
        e.printStackTrace();
        return MessageVo.builder().code(Constants.NEWS_FAIL_CODE).message(Constants.NEWS_FAIL_MESSAGE).build();
    }
}
Also used : TemplateDetails(com.tansci.domain.message.TemplateDetails) Client(com.aliyun.dysmsapi20170525.Client) MessageVo(com.tansci.domain.message.vo.MessageVo)

Example 2 with MessageVo

use of com.tansci.domain.message.vo.MessageVo 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 3 with MessageVo

use of com.tansci.domain.message.vo.MessageVo 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 4 with MessageVo

use of com.tansci.domain.message.vo.MessageVo 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)

Aggregations

MessageVo (com.tansci.domain.message.vo.MessageVo)4 BusinessException (com.tansci.common.exception.BusinessException)3 Template (com.tansci.domain.message.Template)2 Transient (java.beans.Transient)2 Client (com.aliyun.dysmsapi20170525.Client)1 TemplateDetails (com.tansci.domain.message.TemplateDetails)1 Transactional (org.springframework.transaction.annotation.Transactional)1