Browse Source

新增短信

Administrator 1 year ago
parent
commit
4b157a8102

+ 2 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java

@@ -84,8 +84,10 @@ public class CommonController
             // 上传并返回新文件名称
             String fileName = FileUploadUtils.upload(filePath, file);
             String url = serverConfig.getUrl() + fileName;
+            String urlOnline = "https://qszd.qs163.cn/prod-api/" + fileName;
             AjaxResult ajax = AjaxResult.success();
             ajax.put("url", url);
+            ajax.put("urlOnline", urlOnline);
             ajax.put("fileName", fileName);
             ajax.put("newFileName", FileUtils.getName(fileName));
             ajax.put("originalFilename", file.getOriginalFilename());

+ 27 - 0
ruoyi-common/src/main/java/com/ruoyi/common/utils/SendSmsUtils.java

@@ -1,7 +1,10 @@
 package com.ruoyi.common.utils;
 
 
+import com.aliyun.dysmsapi20170525.models.SendBatchSmsRequest;
+import com.aliyun.dysmsapi20170525.models.SendBatchSmsResponse;
 import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
+import com.aliyun.tea.TeaException;
 import com.aliyun.teautil.models.RuntimeOptions;
 
 /**
@@ -80,6 +83,30 @@ public class SendSmsUtils {
         return code;
     }
 
+    /**
+     * 阿里云批量发送 短信接口,一次最多100个手机号码
+     *
+     * @return
+     * @throws
+     */
+    public static SendBatchSmsResponse sendBatchSms(SendBatchSmsRequest sendBatchSmsRequest){
+        try {
+            com.aliyun.dysmsapi20170525.Client client = SendSmsUtils.createClient();
+            RuntimeOptions runtime = new RuntimeOptions();
+            SendBatchSmsResponse sendBatchSmsResponse = client.sendBatchSmsWithOptions(sendBatchSmsRequest, runtime);
+            return sendBatchSmsResponse;
+            // 复制代码运行请自行打印 API 的返回值
+        } catch (TeaException error) {
+            // 如有需要,请打印 error
+            com.aliyun.teautil.Common.assertAsString(error.message);
+        } catch (Exception _error) {
+            TeaException error = new TeaException(_error.getMessage(), _error);
+            // 如有需要,请打印 error
+            com.aliyun.teautil.Common.assertAsString(error.message);
+        }
+        return null;
+    }
+
     /**
      * 发送注册的随机密码
      *

+ 60 - 4
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/InvestigateTableServiceImpl.java

@@ -3,13 +3,14 @@ package com.ruoyi.system.service.impl;
 import java.util.ArrayList;
 import java.util.List;
 
-import com.ruoyi.common.core.domain.AjaxResult;
-import com.ruoyi.common.core.domain.entity.SysUser;
+import com.alibaba.fastjson2.JSONObject;
+import com.aliyun.dysmsapi20170525.models.SendBatchSmsRequest;
+import com.aliyun.dysmsapi20170525.models.SendBatchSmsResponse;
 import com.ruoyi.common.utils.DateUtils;
+import com.ruoyi.common.utils.SendSmsUtils;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.system.domain.InvestigateDispositionTable;
 import com.ruoyi.system.domain.InvestigateUser;
-import com.ruoyi.system.domain.SysUserRole;
 import com.ruoyi.system.mapper.InvestigateDispositionTableMapper;
 import com.ruoyi.system.mapper.InvestigateUserMapper;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -69,15 +70,70 @@ public class InvestigateTableServiceImpl implements IInvestigateTableService {
         List<InvestigateUser> investigateUserList = investigateTable.getInvestigateUserList();
         investigateTable.setCipher(getCode(4));
         int i = investigateTableMapper.insertInvestigateTable(investigateTable);
-        if (investigateUserList != null) {
+        Long investigateTableId = investigateTable.getInvestigateTableId();
+        if (investigateUserList != null && investigateUserList.size() > 0) {
             for (InvestigateUser investigateUser : investigateUserList) {
                 investigateUser.setInvestigateId(investigateTable.getInvestigateTableId());
             }
             investigateUserMapper.batchDispositionUser(investigateUserList);
+            //发送短信
+            sendBatchSmsTable(investigateTableId,investigateUserList);
         }
         return i;
     }
 
+    public static void sendBatchSmsTable(Long investigateTableId, List<InvestigateUser> investigateUserList) {
+        for (InvestigateUser investigateUser : investigateUserList) {
+            StringBuffer phoneNumberJson = new StringBuffer();
+            StringBuffer templateParamJson = new StringBuffer();
+            StringBuffer signNameJson = new StringBuffer();
+            phoneNumberJson.append("[");
+            templateParamJson.append("[");
+            signNameJson.append("[");
+
+            // 防止有空行,手机号是必须要有的
+            String phone = investigateUser.getPhonenumber();
+            if (StringUtils.isEmpty(phone)) {
+                continue;
+            }
+            phoneNumberJson.append("\"" + phone + "\",");
+            signNameJson.append("\"中新云\",");
+            //发送短信
+            JSONObject jsonObject = new JSONObject();
+            jsonObject.put("number", investigateTableId);
+            templateParamJson.append(jsonObject).append(",");
+
+
+            phoneNumberJson.deleteCharAt(phoneNumberJson.length() - 1);//移除最后一个逗号字符,
+            templateParamJson.deleteCharAt(templateParamJson.length() - 1);//移除最后一个逗号字符
+            signNameJson.deleteCharAt(signNameJson.length() - 1);
+            phoneNumberJson.append("]");
+            templateParamJson.append("]");
+            signNameJson.append("]");
+
+            long startTimeSql = System.currentTimeMillis();
+            SendBatchSmsRequest sendBatchSmsRequest = new SendBatchSmsRequest();
+            //组装电话号码
+            sendBatchSmsRequest.setPhoneNumberJson(phoneNumberJson.toString());
+            //签名名称
+            sendBatchSmsRequest.setSignNameJson(signNameJson.toString());
+            //替换参数
+            sendBatchSmsRequest.setTemplateParamJson(templateParamJson.toString());
+            //固定的模板名称
+            sendBatchSmsRequest.setTemplateCode("SMS_463622836");
+            long startTimeSend = System.currentTimeMillis();
+            SendBatchSmsResponse sendBatchSmsResponse = SendSmsUtils.sendBatchSms(sendBatchSmsRequest);
+            long endTimeSend = System.currentTimeMillis() - startTimeSend;
+            System.out.println("执行批量下发短信接口请求:" + endTimeSend + "ms");
+            String code = sendBatchSmsResponse.getBody().getCode();
+            if (sendBatchSmsResponse.getBody().getCode() != null && "OK".equals(code)) {
+                System.out.println("批量短信发送成功");
+            } else {
+                System.out.println("批量短信发送失败");
+            }
+        }
+    }
+
     //生成X位验证码
     public static String getCode(Integer num) {
         String[] codes = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};

+ 1 - 1
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/InvestigateUserServiceImpl.java

@@ -116,7 +116,7 @@ public class InvestigateUserServiceImpl implements IInvestigateUserService {
             jsonObject.put("code", cipher);
             //查询考察密码
             String phonenumber = investigateUser.getPhonenumber();
-            String templateCode = "SMS_463605860";
+            String templateCode = "SMS_463627835";
             String code = SendSmsUtils.sendSms(phonenumber, templateCode, jsonObject.toString());
             System.out.println("发送短信回执"+code);
             return AjaxResult.success();