调解系统后端服务

SmsUtils.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package com.ruoyi.wisdomarbitrate.utils;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import cn.hutool.extra.spring.SpringUtil;
  5. import cn.hutool.json.JSONObject;
  6. import com.ruoyi.common.enums.SMSStatusEnum;
  7. import com.ruoyi.common.utils.ThreadUtil;
  8. import com.ruoyi.system.domain.entity.sms.MsSmsSendRecordParam;
  9. import com.ruoyi.system.domain.entity.sms.MsSmsTemplate;
  10. import com.ruoyi.system.mapper.SysRoleMapper;
  11. import com.ruoyi.system.mapper.SysUserMapper;
  12. import com.ruoyi.system.mapper.flow.MsCaseFlowRoleSmsRelatedMapper;
  13. import com.ruoyi.system.mapper.sms.MsSmsSendRecordParamMapper;
  14. import com.ruoyi.system.mapper.sms.MsSmsTemplateMapper;
  15. import com.ruoyi.wisdomarbitrate.domain.dto.sendrecord.SmsSendRecord;
  16. import com.ruoyi.wisdomarbitrate.domain.entity.mscase.MsCaseApplication;
  17. import com.ruoyi.wisdomarbitrate.mapper.sendrecord.SmsRecordMapper;
  18. import com.ruoyi.wisdomarbitrate.service.shortmessage.ShortMessageService;
  19. import com.tencentcloudapi.common.Credential;
  20. import com.tencentcloudapi.common.exception.TencentCloudSDKException;
  21. import com.tencentcloudapi.sms.v20210111.SmsClient;
  22. import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
  23. import com.tencentcloudapi.sms.v20210111.models.SendStatus;
  24. import lombok.AllArgsConstructor;
  25. import lombok.Data;
  26. import lombok.NoArgsConstructor;
  27. import lombok.extern.slf4j.Slf4j;
  28. import lombok.var;
  29. import tk.mybatis.mapper.entity.Example;
  30. import java.util.ArrayList;
  31. import java.util.Date;
  32. import java.util.List;
  33. import java.util.Objects;
  34. import java.util.concurrent.CompletableFuture;
  35. import java.util.concurrent.ExecutorService;
  36. @Slf4j
  37. public class SmsUtils {
  38. //应用id
  39. private static final String SDK_APP_ID = "1400854852";
  40. //API的SecretId
  41. private static final String SECRET_ID = "AKIDeEf2A8uX1HSainvvnXAc3X9ZlhtyvkMp";
  42. //API的SecretKey
  43. private static final String SECRET_KEY = "QjphKo8zkHZigT8j9PVtFPJyfIvO3d6V";
  44. //签名内容
  45. private static final String SIGN_NAME = "乙巢智慧仲裁网";
  46. private static SmsRecordMapper recordMapper = SpringUtil.getBean(SmsRecordMapper.class);
  47. private static MsSmsSendRecordParamMapper recordParamMapper = SpringUtil.getBean(MsSmsSendRecordParamMapper.class);
  48. private static MsSmsTemplateMapper templateMapper = SpringUtil.getBean(MsSmsTemplateMapper.class);
  49. private static SysUserMapper sysUserMapper = SpringUtil.getBean(SysUserMapper.class);
  50. private static MsCaseFlowRoleSmsRelatedMapper flowRoleRelatedMapper = SpringUtil.getBean(MsCaseFlowRoleSmsRelatedMapper.class);
  51. private static SysRoleMapper roleMapper = SpringUtil.getBean(SysRoleMapper.class);
  52. private static ShortMessageService shortMessageService = SpringUtil.getBean(ShortMessageService.class);
  53. public static JSONObject sendSms(SendSmsRequest request) {
  54. JSONObject jsonObject = new JSONObject();
  55. Credential cred = new Credential(SECRET_ID, SECRET_KEY);
  56. SmsClient client = new SmsClient(cred, "ap-guangzhou");
  57. final var req = new com.tencentcloudapi.sms.v20210111.models.SendSmsRequest();
  58. req.setPhoneNumberSet(new String[]{"+86" + request.getPhone()});
  59. req.setSmsSdkAppId(SDK_APP_ID);
  60. req.setSignName(SIGN_NAME);
  61. req.setTemplateId(request.getTemplateId());
  62. req.setTemplateParamSet(request.getTemplateParamSet());
  63. SendSmsResponse res = null;
  64. try {
  65. res = client.SendSms(req);
  66. } catch (TencentCloudSDKException e) {
  67. log.error("发送短信出错:", e);
  68. jsonObject.set("status", SMSStatusEnum.FAIL.getCode());
  69. jsonObject.set("reason",e.getMessage());
  70. return jsonObject;
  71. }
  72. SendStatus sendStatus = res.getSendStatusSet()[0];
  73. log.info("发送短信结果:Code={}, Message={}", sendStatus.getCode(), sendStatus.getMessage());
  74. if (Objects.nonNull(res.getSendStatusSet()) && res.getSendStatusSet().length > 0 && "Ok".equals(res.getSendStatusSet()[0].getCode())) {
  75. jsonObject.set("status", SMSStatusEnum.SENDING.getCode());
  76. } else {
  77. jsonObject.set("status", SMSStatusEnum.FAIL.getCode());
  78. }
  79. jsonObject.set("sid", sendStatus.getSerialNo());
  80. return jsonObject;
  81. }
  82. public static void sendSms(MsCaseApplication application, String templateId, String phone, String[] templateParamSet) {
  83. if(application==null||StrUtil.isEmpty(templateId)||StrUtil.isEmpty(phone)||templateParamSet==null||templateParamSet.length==0){
  84. return;
  85. }
  86. Example templateExam = new Example(MsSmsTemplate.class);
  87. templateExam.createCriteria().andEqualTo("templateId", templateId);
  88. List<MsSmsTemplate> templates = templateMapper.selectByExample(templateExam);
  89. if (CollectionUtil.isEmpty(templates)) {
  90. return ;
  91. }
  92. MsSmsTemplate template = templates.get(0);
  93. SendSmsRequest request = new SendSmsRequest(phone, templateId, templateParamSet, application.getId());
  94. Credential cred = new Credential(SECRET_ID, SECRET_KEY);
  95. SmsClient client = new SmsClient(cred, "ap-guangzhou");
  96. final var req = new com.tencentcloudapi.sms.v20210111.models.SendSmsRequest();
  97. req.setPhoneNumberSet(new String[]{"+86" + request.getPhone()});
  98. req.setSmsSdkAppId(SDK_APP_ID);
  99. req.setSignName(SIGN_NAME);
  100. req.setTemplateId(request.getTemplateId());
  101. req.setTemplateParamSet(request.getTemplateParamSet());
  102. sendSms(client,template,application,phone, templateParamSet,req);
  103. }
  104. private static void sendSms(SmsClient client,MsSmsTemplate template, MsCaseApplication application, String phone, String[] templateParamSet, com.tencentcloudapi.sms.v20210111.models.SendSmsRequest req) {
  105. SmsSendRecord smsSendRecord=new SmsSendRecord();
  106. smsSendRecord.setMsSmsTemplateId(template.getId());
  107. smsSendRecord.setCaseId(application.getId());
  108. smsSendRecord.setCaseNum(application.getCaseNum());
  109. smsSendRecord.setSendTime(new Date());
  110. smsSendRecord.setPhone(phone);
  111. smsSendRecord.setCreateTime(new Date());
  112. // smsSendRecord.setCreateBy(SecurityUtils.getUsername());
  113. // SendSmsRequest request = new SendSmsRequest(phone, template.getTemplateId(), templateParamSet, application.getId());
  114. req.setPhoneNumberSet(new String[]{"+86" + phone});
  115. req.setTemplateId(template.getTemplateId());
  116. req.setTemplateParamSet(templateParamSet);
  117. try {
  118. SendSmsResponse res=client.SendSms(req);
  119. SendStatus sendStatus = res.getSendStatusSet()[0];
  120. if (Objects.nonNull(res.getSendStatusSet()) && res.getSendStatusSet().length > 0 && "Ok".equals(res.getSendStatusSet()[0].getCode())) {
  121. smsSendRecord.setSendStatus( SMSStatusEnum.SENDING.getCode());
  122. } else {
  123. smsSendRecord.setSendStatus(SMSStatusEnum.FAIL.getCode());
  124. smsSendRecord.setReason(sendStatus.getMessage());
  125. }
  126. smsSendRecord.setSid(sendStatus.getSerialNo());
  127. } catch (TencentCloudSDKException e) {
  128. smsSendRecord.setSendStatus(SMSStatusEnum.FAIL.getCode());
  129. smsSendRecord.setReason(e.getMessage());
  130. }
  131. int i = recordMapper.saveSmsSendRecord(smsSendRecord);
  132. if(i>0){
  133. List<MsSmsSendRecordParam> recordParams=new ArrayList<>();
  134. for (String paramValue : templateParamSet) {
  135. // 新增参数
  136. MsSmsSendRecordParam recordParam = new MsSmsSendRecordParam();
  137. recordParam.setSmsRecordId(smsSendRecord.getId());
  138. recordParam.setParamValue(paramValue);
  139. recordParams.add(recordParam);
  140. }
  141. // recordParamMapper.batchInsert(recordParams);
  142. // shortMessageService.insertShortMessageHistoryRecord(smsSendRecord,recordParams);
  143. ExecutorService executor = ThreadUtil.createThreadPool();
  144. CompletableFuture.runAsync(() -> {
  145. recordParamMapper.batchInsert(recordParams);
  146. }, executor);
  147. // 新增历史记录表
  148. CompletableFuture.runAsync(() -> {
  149. shortMessageService.insertShortMessageHistoryRecord(smsSendRecord,recordParams);
  150. }, executor);
  151. }
  152. }
  153. /**
  154. * 参数对象
  155. */
  156. @Data
  157. @NoArgsConstructor
  158. @AllArgsConstructor
  159. public static class SendSmsRequest {
  160. /**
  161. * 电话
  162. */
  163. private String phone;
  164. /**
  165. * 模板 ID: 必须填写已审核通过的模板 ID
  166. */
  167. private String templateId;
  168. /**
  169. * 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空
  170. */
  171. private String[] templateParamSet;
  172. private Long caseId;
  173. }
  174. }