package com.ltkj.web.controller.ssm; import cn.hutool.core.date.DateTime; import com.aliyun.dysmsapi20170525.Client; import com.aliyun.dysmsapi20170525.models.SendSmsRequest; import com.aliyun.tea.TeaException; import com.aliyun.teautil.Common; import com.aliyun.teautil.models.RuntimeOptions; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.ltkj.common.core.domain.AjaxResult; import com.ltkj.hosp.domain.TjCustomer; import com.ltkj.hosp.domain.TjOrder; import com.ltkj.hosp.domain.TjSendRecord; import com.ltkj.hosp.domain.TjSendTemplate; import com.ltkj.hosp.service.ITjCustomerService; import com.ltkj.hosp.service.ITjOrderService; import com.ltkj.hosp.service.ITjSendRecordService; import com.ltkj.hosp.service.ITjSendTemplateService; import com.ltkj.web.controller.email.MailConfig; import com.ltkj.web.controller.message.ALiYunSMSConfig; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.MailException; import org.springframework.mail.SimpleMailMessage; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * @Company: 西安路泰科技有限公司 * @Author: zjh * @Date: 2023/3/20 15:13 */ @RestController @RequestMapping("/ali/send") @Api(tags = "短信或邮箱发送接口") @Slf4j public class aliController { //阿里云短信服务配置类 @Resource ALiYunSMSConfig aLiYunSMSConfig; @Resource private ITjOrderService tjOrderService; @Resource private MailConfig mailConfig; @Resource private ITjSendRecordService sendRecordService; @Resource private ITjCustomerService tjCustomerService; @Resource private ITjSendTemplateService templateService; /** * 批量发送邮件 */ @PostMapping("/sendEmail") @ApiOperation(value = "批量发送邮件") public AjaxResult sendEmail(@RequestBody Map map) { Integer tempId = (Integer) map.get("tempId"); List orderIds = (List) map.get("orderIds"); TjSendTemplate byId = templateService.getById(Long.valueOf(tempId)); List errorCus = new ArrayList(); if (byId != null) { for (String orderId : orderIds) { TjSendRecord tjSendRecord = new TjSendRecord(); LambdaQueryWrapper wq = new LambdaQueryWrapper<>(); wq.eq(TjOrder::getOrderId, orderId); TjOrder one = tjOrderService.getOne(wq); if (one != null) { TjCustomer tjCustomer = tjCustomerService.selectTjCustomerByCusId(one.getUserId()); if (tjCustomer.getCusEmail() != null) { String subject = "路泰体检中心"; String head = "尊敬的" + tjCustomer.getCusName() + "先生/女士,"; String content = head + byId.getTempContent(); tjSendRecord.setTjNumber(one.getTjNumber()); tjSendRecord.setType("1"); tjSendRecord.setSendStatus("1"); tjSendRecord.setSendTime(new DateTime()); tjSendRecord.setSubject(subject); tjSendRecord.setContent(content); SimpleMailMessage message = new SimpleMailMessage(); message.setFrom("1138758025@qq.com"); // 邮件接收人(可以使用 String[] 发送给多个用户) message.setTo(tjCustomer.getCusEmail()); message.setSubject(subject); message.setText(content); try { mailConfig.getMailSender("1138758025@qq.com", "lwsbmlbqgpaqfgdb").send(message); one.setSendEmail("1"); tjOrderService.updateById(one); } catch (MailException e) { e.printStackTrace(); } } errorCus.add(tjCustomer); tjSendRecord.setSendStatus("0"); sendRecordService.save(tjSendRecord); } } } return AjaxResult.success(errorCus); } /** * 批量发送短信 */ @PostMapping("/sendMessage") @ApiOperation(value = "批量发送短信") public AjaxResult sendMessage(@RequestBody Map map) { Integer tempId = (Integer) map.get("tempId"); List orderIds = (List) map.get("orderIds"); TjSendTemplate byId = templateService.getById(Long.valueOf(tempId)); if (byId != null) { for (String orderId : orderIds) { TjSendRecord tjSendRecord = new TjSendRecord(); LambdaQueryWrapper wq = new LambdaQueryWrapper<>(); wq.eq(TjOrder::getOrderId, orderId); TjOrder one = tjOrderService.getOne(wq); if (one != null) { TjCustomer tjCustomer = tjCustomerService.selectTjCustomerByCusId(one.getUserId()); if (tjCustomer.getCusEmail() != null) { String subject = "路泰体检中心"; String head = "尊敬的" + tjCustomer.getCusName() + "先生/女士,"; String content = head + byId.getTempContent(); tjSendRecord.setTjNumber(one.getTjNumber()); tjSendRecord.setType("2"); tjSendRecord.setSendStatus("1"); tjSendRecord.setSendTime(new DateTime()); tjSendRecord.setSubject(subject); tjSendRecord.setContent(content); one.setSendMessage("1"); tjOrderService.updateById(one); } sendRecordService.save(tjSendRecord); } } } return AjaxResult.success("短信发送成功!"); } /** * 发送短信 */ // @GetMapping("/sendMessage") // @ApiOperation(value = "批量发送短信") // public AjaxResult sendMessage(){ // try { // // 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。 // // 以下代码示例仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html // Client client = aLiYunSMSConfig.createClient(); // SendSmsRequest sendSmsRequest = new SendSmsRequest() // .setSignName("阿里云短信测试") // .setTemplateCode("SMS_154950909") // .setPhoneNumbers("15706053782") // .setTemplateParam("{\"code\":\"1234\"}"); // RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); // // 复制代码运行请自行打印 API 的返回值 // client.sendSmsWithOptions(sendSmsRequest, runtime); // } catch (TeaException error) { // // 如有需要,请打印 error // Common.assertAsString(error.message); // } catch (Exception _error) { // TeaException error = new TeaException(_error.getMessage(), _error); // // 如有需要,请打印 error // Common.assertAsString(error.message); // } // return AjaxResult.success(); // } }