| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- package com.ruoyi.wisdomarbitrate.utils;
-
- import cn.hutool.core.collection.CollectionUtil;
- import cn.hutool.core.util.StrUtil;
- import cn.hutool.extra.spring.SpringUtil;
- import cn.hutool.json.JSONObject;
- import com.ruoyi.common.enums.SMSStatusEnum;
- import com.ruoyi.common.utils.ThreadUtil;
- import com.ruoyi.system.domain.entity.sms.MsSmsSendRecordParam;
- import com.ruoyi.system.domain.entity.sms.MsSmsTemplate;
- import com.ruoyi.system.mapper.SysRoleMapper;
- import com.ruoyi.system.mapper.SysUserMapper;
- import com.ruoyi.system.mapper.flow.MsCaseFlowRoleSmsRelatedMapper;
- import com.ruoyi.system.mapper.sms.MsSmsSendRecordParamMapper;
- import com.ruoyi.system.mapper.sms.MsSmsTemplateMapper;
- import com.ruoyi.wisdomarbitrate.domain.dto.sendrecord.SmsSendRecord;
- import com.ruoyi.wisdomarbitrate.domain.entity.mscase.MsCaseApplication;
- import com.ruoyi.wisdomarbitrate.mapper.sendrecord.SmsRecordMapper;
- import com.ruoyi.wisdomarbitrate.service.shortmessage.ShortMessageService;
- import com.tencentcloudapi.common.Credential;
- import com.tencentcloudapi.common.exception.TencentCloudSDKException;
- import com.tencentcloudapi.sms.v20210111.SmsClient;
- import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
- import com.tencentcloudapi.sms.v20210111.models.SendStatus;
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import lombok.var;
- import tk.mybatis.mapper.entity.Example;
-
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.Objects;
- import java.util.concurrent.CompletableFuture;
- import java.util.concurrent.ExecutorService;
-
- @Slf4j
- public class SmsUtils {
- //应用id
- private static final String SDK_APP_ID = "1400854852";
- //API的SecretId
- private static final String SECRET_ID = "AKIDeEf2A8uX1HSainvvnXAc3X9ZlhtyvkMp";
- //API的SecretKey
- private static final String SECRET_KEY = "QjphKo8zkHZigT8j9PVtFPJyfIvO3d6V";
- //签名内容
- private static final String SIGN_NAME = "乙巢智慧仲裁网";
- private static SmsRecordMapper recordMapper = SpringUtil.getBean(SmsRecordMapper.class);
- private static MsSmsSendRecordParamMapper recordParamMapper = SpringUtil.getBean(MsSmsSendRecordParamMapper.class);
- private static MsSmsTemplateMapper templateMapper = SpringUtil.getBean(MsSmsTemplateMapper.class);
- private static SysUserMapper sysUserMapper = SpringUtil.getBean(SysUserMapper.class);
- private static MsCaseFlowRoleSmsRelatedMapper flowRoleRelatedMapper = SpringUtil.getBean(MsCaseFlowRoleSmsRelatedMapper.class);
- private static SysRoleMapper roleMapper = SpringUtil.getBean(SysRoleMapper.class);
- private static ShortMessageService shortMessageService = SpringUtil.getBean(ShortMessageService.class);
-
- public static JSONObject sendSms(SendSmsRequest request) {
- JSONObject jsonObject = new JSONObject();
- Credential cred = new Credential(SECRET_ID, SECRET_KEY);
-
- SmsClient client = new SmsClient(cred, "ap-guangzhou");
-
- final var req = new com.tencentcloudapi.sms.v20210111.models.SendSmsRequest();
- req.setPhoneNumberSet(new String[]{"+86" + request.getPhone()});
- req.setSmsSdkAppId(SDK_APP_ID);
- req.setSignName(SIGN_NAME);
- req.setTemplateId(request.getTemplateId());
- req.setTemplateParamSet(request.getTemplateParamSet());
- SendSmsResponse res = null;
- try {
- res = client.SendSms(req);
- } catch (TencentCloudSDKException e) {
- log.error("发送短信出错:", e);
- jsonObject.set("status", SMSStatusEnum.FAIL.getCode());
- jsonObject.set("reason",e.getMessage());
- return jsonObject;
- }
- SendStatus sendStatus = res.getSendStatusSet()[0];
- log.info("发送短信结果:Code={}, Message={}", sendStatus.getCode(), sendStatus.getMessage());
-
- if (Objects.nonNull(res.getSendStatusSet()) && res.getSendStatusSet().length > 0 && "Ok".equals(res.getSendStatusSet()[0].getCode())) {
- jsonObject.set("status", SMSStatusEnum.SENDING.getCode());
- } else {
- jsonObject.set("status", SMSStatusEnum.FAIL.getCode());
- }
- jsonObject.set("sid", sendStatus.getSerialNo());
- return jsonObject;
- }
-
- public static void sendSms(MsCaseApplication application, String templateId, String phone, String[] templateParamSet) {
- if(application==null||StrUtil.isEmpty(templateId)||StrUtil.isEmpty(phone)||templateParamSet==null||templateParamSet.length==0){
- return;
- }
- Example templateExam = new Example(MsSmsTemplate.class);
- templateExam.createCriteria().andEqualTo("templateId", templateId);
- List<MsSmsTemplate> templates = templateMapper.selectByExample(templateExam);
- if (CollectionUtil.isEmpty(templates)) {
- return ;
- }
- MsSmsTemplate template = templates.get(0);
- SendSmsRequest request = new SendSmsRequest(phone, templateId, templateParamSet, application.getId());
- Credential cred = new Credential(SECRET_ID, SECRET_KEY);
-
- SmsClient client = new SmsClient(cred, "ap-guangzhou");
-
- final var req = new com.tencentcloudapi.sms.v20210111.models.SendSmsRequest();
- req.setPhoneNumberSet(new String[]{"+86" + request.getPhone()});
- req.setSmsSdkAppId(SDK_APP_ID);
- req.setSignName(SIGN_NAME);
- req.setTemplateId(request.getTemplateId());
- req.setTemplateParamSet(request.getTemplateParamSet());
- sendSms(client,template,application,phone, templateParamSet,req);
- }
-
-
- private static void sendSms(SmsClient client,MsSmsTemplate template, MsCaseApplication application, String phone, String[] templateParamSet, com.tencentcloudapi.sms.v20210111.models.SendSmsRequest req) {
- SmsSendRecord smsSendRecord=new SmsSendRecord();
- smsSendRecord.setMsSmsTemplateId(template.getId());
- smsSendRecord.setCaseId(application.getId());
- smsSendRecord.setCaseNum(application.getCaseNum());
- smsSendRecord.setSendTime(new Date());
- smsSendRecord.setPhone(phone);
- smsSendRecord.setCreateTime(new Date());
- // smsSendRecord.setCreateBy(SecurityUtils.getUsername());
- // SendSmsRequest request = new SendSmsRequest(phone, template.getTemplateId(), templateParamSet, application.getId());
- req.setPhoneNumberSet(new String[]{"+86" + phone});
- req.setTemplateId(template.getTemplateId());
- req.setTemplateParamSet(templateParamSet);
- try {
- SendSmsResponse res=client.SendSms(req);
- SendStatus sendStatus = res.getSendStatusSet()[0];
- if (Objects.nonNull(res.getSendStatusSet()) && res.getSendStatusSet().length > 0 && "Ok".equals(res.getSendStatusSet()[0].getCode())) {
- smsSendRecord.setSendStatus( SMSStatusEnum.SENDING.getCode());
- } else {
- smsSendRecord.setSendStatus(SMSStatusEnum.FAIL.getCode());
- smsSendRecord.setReason(sendStatus.getMessage());
- }
- smsSendRecord.setSid(sendStatus.getSerialNo());
- } catch (TencentCloudSDKException e) {
- smsSendRecord.setSendStatus(SMSStatusEnum.FAIL.getCode());
- smsSendRecord.setReason(e.getMessage());
- }
- int i = recordMapper.saveSmsSendRecord(smsSendRecord);
- if(i>0){
- List<MsSmsSendRecordParam> recordParams=new ArrayList<>();
- for (String paramValue : templateParamSet) {
- // 新增参数
- MsSmsSendRecordParam recordParam = new MsSmsSendRecordParam();
- recordParam.setSmsRecordId(smsSendRecord.getId());
- recordParam.setParamValue(paramValue);
- recordParams.add(recordParam);
- }
- // recordParamMapper.batchInsert(recordParams);
- // shortMessageService.insertShortMessageHistoryRecord(smsSendRecord,recordParams);
- ExecutorService executor = ThreadUtil.createThreadPool();
- CompletableFuture.runAsync(() -> {
- recordParamMapper.batchInsert(recordParams);
-
- }, executor);
- // 新增历史记录表
- CompletableFuture.runAsync(() -> {
- shortMessageService.insertShortMessageHistoryRecord(smsSendRecord,recordParams);
-
- }, executor);
-
- }
- }
-
- /**
- * 参数对象
- */
- @Data
- @NoArgsConstructor
- @AllArgsConstructor
- public static class SendSmsRequest {
- /**
- * 电话
- */
- private String phone;
-
- /**
- * 模板 ID: 必须填写已审核通过的模板 ID
- */
- private String templateId;
-
- /**
- * 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空
- */
- private String[] templateParamSet;
- private Long caseId;
-
- }
- }
|