| | |
| | | package com.ycl.service.message.impl; |
| | | |
| | | import cn.hutool.core.net.url.UrlBuilder; |
| | | import cn.hutool.core.net.url.UrlPath; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.ycl.controller.video.common.constant.ChannelCode; |
| | | import com.ycl.controller.video.common.util.CommonUtils; |
| | | import com.ycl.common.constant.ChannelCode; |
| | | import com.ycl.common.util.CommonUtils; |
| | | import com.ycl.dto.message.MessageParam; |
| | | import com.ycl.entity.message.Message; |
| | | import com.ycl.entity.message.SendReq; |
| | | import com.ycl.entity.message.SendRes; |
| | | import com.ycl.entity.user.UmsAdmin; |
| | | import com.ycl.exception.ApiException; |
| | | import com.ycl.mapper.message.MessageMapper; |
| | | import com.ycl.mapper.user.UmsAdminMapper; |
| | | import com.ycl.service.message.IMessageService; |
| | | import com.ycl.utils.MD5Util; |
| | | import com.ycl.vo.message.MessageVO; |
| | | import lombok.SneakyThrows; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.BeanUtils; |
| | |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Properties; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public String sendMessage(Message message) { |
| | | Message savedMessage = messageMapper.selectOne(new LambdaQueryWrapper<Message>() |
| | | .eq(Message::getTargetTo, message.getTargetTo()) |
| | | .eq(Message::getTargetFrom, message.getTargetFrom())); |
| | | if (savedMessage != null) { |
| | | messageMapper.deleteById(savedMessage); |
| | | |
| | | if (!StringUtils.equals(message.getChannelCode(),ChannelCode.SMS)){ |
| | | Message savedMessage = messageMapper.selectOne(new LambdaQueryWrapper<Message>() |
| | | .eq(Message::getTargetTo, message.getTargetTo()) |
| | | .eq(Message::getTargetFrom, message.getTargetFrom()) |
| | | .eq(Message::getBody, message.getBody()) |
| | | .eq(Message::getHead, message.getHead())); |
| | | if (savedMessage != null) { |
| | | messageMapper.deleteById(savedMessage); |
| | | } |
| | | } |
| | | |
| | | ArrayList<Long> ids = new ArrayList<>(); |
| | | |
| | | for (String receive : message.getTargetTo().split(",")) { |
| | | Message sendMessage = new Message(); |
| | | BeanUtils.copyProperties(message, sendMessage); |
| | | sendMessage.setCreateTime(new Date()); |
| | | sendMessage.setSendTime(new Date()); |
| | | sendMessage.setTargetTo(receive); |
| | | sendMessage.setSendTime(new Date()); |
| | | this.save(sendMessage); |
| | | ids.add(sendMessage.getId()); |
| | | message.setTargetFrom(umsAdminMapper.selectById(message.getCreateUser()).getUsername()); |
| | | if (message.getId()==null) { |
| | | for (String receive : message.getTargetTo().split(",")) { |
| | | Message sendMessage = new Message(); |
| | | BeanUtils.copyProperties(message, sendMessage); |
| | | sendMessage.setCreateTime(new Date()); |
| | | sendMessage.setSendTime(new Date()); |
| | | sendMessage.setTargetTo(receive); |
| | | sendMessage.setSendTime(new Date()); |
| | | this.save(sendMessage); |
| | | ids.add(sendMessage.getId()); |
| | | } |
| | | } |
| | | switch (message.getChannelCode()) { |
| | | //邮件发送 |
| | |
| | | //短信发送 |
| | | case ChannelCode.MAIL: |
| | | sendMail(message); |
| | | break; |
| | | case ChannelCode.INNER: |
| | | break; |
| | | default: |
| | | throw new ApiException("未匹配到该类型"); |
| | |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @SneakyThrows |
| | | public String sendMessageByPhone(Message message) { |
| | | Message sendMessage = new Message(); |
| | | BeanUtils.copyProperties(message, sendMessage); |
| | | sendMessage.setCreateTime(new Date()); |
| | | sendMessage.setSendTime(new Date()); |
| | | sendMessage.setSendTime(new Date()); |
| | | this.save(sendMessage); |
| | | |
| | | String context = "【" + message.getHead() + "】" + message.getBody(); |
| | | String addSerial = ""; |
| | | SendReq sendReq = new SendReq(); |
| | | sendReq.setEcName(ecName); |
| | | sendReq.setApId(apId); |
| | | sendReq.setSign(sign); |
| | | sendReq.setContext(context); |
| | | sendReq.setMobiles(message.getPhoneNumber()); |
| | | sendReq.setAddSerial(addSerial); |
| | | |
| | | StringBuffer mac = new StringBuffer(); |
| | | mac.append(ecName); |
| | | mac.append(apId); |
| | | mac.append(sendReq.getMobiles()); |
| | | mac.append(context); |
| | | mac.append(sign); |
| | | mac.append(addSerial); |
| | | sendReq.setMac(MD5Util.md5Encrypt32Lower(mac.toString())); |
| | | |
| | | HttpHeaders httpHeaders = new HttpHeaders(); |
| | | httpHeaders.setContentType(MediaType.APPLICATION_JSON); |
| | | |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | |
| | | HttpEntity<String> stringHttpEntity = new HttpEntity<>(Base64Utils.encodeToString(objectMapper.writeValueAsBytes(mac)), httpHeaders); |
| | | |
| | | SendRes sendRes = objectMapper.readValue(restTemplate.postForEntity(url, stringHttpEntity, String.class).getBody(), SendRes.class); |
| | | Message sendedMessage = new Message(); |
| | | if (sendRes.getRspcod().equals("success")) { |
| | | sendedMessage.setStatus(2); |
| | | sendedMessage.setRespondResult("发送成功"); |
| | | sendedMessage.setId(sendMessage.getId()); |
| | | sendedMessage.setUpdateTime(new Date()); |
| | | sendedMessage.setSendTime(new Date()); |
| | | messageMapper.updateById(sendedMessage); |
| | | return "send message success!"; |
| | | } else { |
| | | sendedMessage.setStatus(3); |
| | | sendedMessage.setRespondResult("发送失败"); |
| | | sendedMessage.setSendTime(new Date()); |
| | | sendedMessage.setId(sendedMessage.getId()); |
| | | sendedMessage.setUpdateTime(new Date()); |
| | | messageMapper.updateById(sendedMessage); |
| | | return "send message fail!"; |
| | | } |
| | | } |
| | | |
| | | //发送邮件 |
| | | @SneakyThrows |
| | | private void sendMail(Message message) { |
| | |
| | | javaMailSender.setHost(sendHost); |
| | | javaMailSender.setUsername(userName); |
| | | javaMailSender.setPassword(password); |
| | | javaMailSender.setPort(465); |
| | | Properties properties = new Properties(); |
| | | properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); |
| | | properties.put("mail.smtp.auth", "true");//必须是字符串而不能是boolean否则报错 |
| | | properties.put("mail.smtp.starttls.enable", "true"); |
| | | properties.put("mail.smtp.timeout", "1000"); |
| | | properties.put("mail.smtp.host", sendHost); |
| | | javaMailSender.setJavaMailProperties(properties); |
| | | MimeMessage mimeMessage = javaMailSender.createMimeMessage(); |
| | | MimeMessageHelper helper = new MimeMessageHelper(mimeMessage); |
| | | helper.setText(message.getBody()); |
| | |
| | | } |
| | | |
| | | @Override |
| | | public Page<Message> list(MessageParam messageParam) { |
| | | public Page<MessageVO> list(MessageParam messageParam) { |
| | | Page<Message> page = new Page<>(messageParam.getCurrent(), messageParam.getPageSize()); |
| | | return messageMapper.selectMessagePage(page,messageParam); |
| | | return messageMapper.selectMessagePage(page, messageParam); |
| | | } |
| | | |
| | | @Override |