| | |
| | | import com.aliyun.oss.OSSClientBuilder; |
| | | import com.aliyun.oss.OSSException; |
| | | import com.aliyun.oss.model.GetObjectRequest; |
| | | import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.mzl.flower.config.OssProperties; |
| | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.concurrent.CompletableFuture; |
| | |
| | | try { |
| | | // 下载Excel文件到本地临时文件 |
| | | File tempFile = File.createTempFile("temp", ".xlsx"); |
| | | ossClient.getObject(new GetObjectRequest(bucketName, smsTaskDTO.getFileUrl()), tempFile); |
| | | String fileUrl = smsTaskDTO.getFileUrl(); |
| | | String objectKey = fileUrl.replaceFirst("^https?://[^/]+/", ""); // 去掉协议部分 |
| | | ossClient.getObject(new GetObjectRequest(bucketName, objectKey), tempFile); |
| | | |
| | | // 解析Excel文件 |
| | | try (FileInputStream inputStream = new FileInputStream(tempFile); Workbook workbook = new XSSFWorkbook(inputStream)) { |
| | | try (FileInputStream inputStream = new FileInputStream(tempFile); |
| | | Workbook workbook = new XSSFWorkbook(inputStream)) { |
| | | |
| | | Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表 |
| | | int rowCount = sheet.getPhysicalNumberOfRows(); |
| | |
| | | Row row = sheet.getRow(i); |
| | | if (row != null) { |
| | | Cell cell = row.getCell(0); // 假设手机号在第一列 |
| | | if (cell != null && cell.getCellType() == CellType.STRING) { |
| | | String phoneNumber = cell.getStringCellValue(); |
| | | if (cell != null ) { |
| | | String phoneNumber = ""; |
| | | if (cell.getCellType() == CellType.STRING) { |
| | | phoneNumber = cell.getStringCellValue(); |
| | | } else if (cell.getCellType() == CellType.NUMERIC) { |
| | | // 将数字类型的手机号转换为字符串 |
| | | phoneNumber = String.valueOf((long) cell.getNumericCellValue()); |
| | | } |
| | | if (!PHONE_NUMBER_PATTERN.matcher(phoneNumber).matches()) { |
| | | message.append("第" + (i + 1) + "行手机号" + phoneNumber + "格式不正确"); |
| | | isValid = false; |
| | |
| | | phones.append(phoneNumber).append("\n"); |
| | | } |
| | | } else { |
| | | message.append("行上的单元格为空或无效 " + (i + 1)); |
| | | message.append("第" + (i + 1)+ "行上的单元格为空或无效 "); |
| | | isValid = false; |
| | | break; // 退出循环 |
| | | } |
| | |
| | | throw new ValidationException(message.toString()); |
| | | } else { |
| | | smsTaskDTO.setPhones(phones.toString()); |
| | | smsTaskDTO.setNum((long) rowCount); |
| | | smsTaskDTO.setNum((long) rowCount - 1); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | |
| | | detail.setSmsTaskId(smsTaskDO.getId()); |
| | | detail.setSmsTemplateId(smsTaskDO.getSmsTemplateId()); |
| | | detail.setPhone(phone); |
| | | detail.create(); |
| | | return detail; |
| | | }).collect(Collectors.toList()); |
| | | } |
| | |
| | | String templateCode = smsTemplateDO.getCode(); |
| | | for (SmsTaskDetailDO detail : smsTaskDetailDOList) { |
| | | try { |
| | | SmsUtil.sendSms(detail.getPhone(), templateCode, null); |
| | | detail.setResult(Constants.SMS_SEND_RESULT.success.name()); |
| | | SendSmsResponse sendSmsResponse = SmsUtil.sendSms(detail.getPhone(), templateCode, null); |
| | | if("OK".equals(sendSmsResponse.getCode())){ |
| | | detail.setResult(Constants.SMS_SEND_RESULT.success.name()); |
| | | }else{ |
| | | detail.setResult(Constants.SMS_SEND_RESULT.failure.name()); |
| | | } |
| | | detail.setResponseResult(sendSmsResponse.toString()); |
| | | } catch (Exception e) { |
| | | detail.setResult(Constants.SMS_SEND_RESULT.failure.name()); |
| | | System.err.println("Failed to send SMS to " + detail.getPhone() + ": " + e.getMessage()); |