瀏覽代碼

新增短信功能

Administrator 3 年之前
父節點
當前提交
2a907194db

+ 33 - 0
boman-web-core/src/main/java/com/boman/web/core/service/TableServiceCmdService.java

@@ -3,6 +3,8 @@ package com.boman.web.core.service;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
+import com.aliyuncs.exceptions.ClientException;
 import com.boman.common.core.utils.SecurityUtils;
 import com.boman.common.core.utils.StringUtils;
 import com.boman.common.core.utils.collection.CollectionUtils;
@@ -21,6 +23,7 @@ import com.boman.gen.api.RemoteGenTableColumnService;
 import com.boman.gen.api.RemoteGenTableService;
 import com.boman.jflow.api.RemoteJflowProcessService;
 import com.boman.system.api.RemoteMenuService;
+import com.boman.system.api.RemoteUserService;
 import com.boman.web.core.domain.ActionType;
 import com.boman.web.core.domain.RowResult;
 import com.boman.web.core.domain.TableContext;
@@ -32,6 +35,7 @@ import com.boman.web.core.service.update.IBaseUpdateService;
 import com.boman.web.core.utils.ColumnUtils;
 import com.boman.web.core.utils.IdUtils;
 import com.boman.web.core.utils.JSONObjectUtils;
+import com.boman.web.core.utils.SendSmsUtils;
 import com.boman.wechat.api.RemoteWechatService;
 import com.google.common.base.Strings;
 import com.google.common.collect.Lists;
@@ -39,6 +43,7 @@ import org.apache.commons.lang3.BooleanUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.aop.framework.AopContext;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.transaction.annotation.Isolation;
 import org.springframework.transaction.annotation.Transactional;
@@ -94,6 +99,8 @@ public class TableServiceCmdService {
     private RemoteJflowProcessService remoteJflowProcessService;
     @Resource
     private RemoteWechatService remoteWechatService;
+    @Resource
+    private RemoteUserService remoteUserService;
 
 
     private static final Logger LOGGER = LoggerFactory.getLogger(TableServiceCmdService.class);
@@ -211,6 +218,32 @@ public class TableServiceCmdService {
             wxMsgDto.setParams(messageInfo);
             LOGGER.info("收文人id:{}, 发送数据: {}", receiveUserIds, messageInfo);
             remoteWechatService.pushMsg(wxMsgDto);
+            List<Long> userIds = new ArrayList<>(new HashSet(receiveUserIds));
+            AjaxResult userList = remoteUserService.selectUserByIds(userIds);
+            List<Map<String, Object>> users = (List<Map<String, Object>>) userList.get("data");
+            String accessKeyId = "LTAI5tNA2fcBJH6EWRH6Pxr6";
+            String accessKeySecret = "5WdaPEOvC3u9LC7pwy2DQ9pgmJvgUr";
+            String signName = "";
+            String templateCode = "";
+            //自定义的模板变量
+            String templateParam = "";
+            StringBuilder sb = new StringBuilder();
+            if (users != null && users.size() > 0) {
+                for (Map<String, Object> user : users) {
+                    String phoneNumber = user.get("phonenumber").toString();
+                    if (StringUtils.isNotBlank(phoneNumber)) {
+                        sb.append(phoneNumber).append(",");
+                    }
+                }
+            }
+            if (StringUtils.isNotBlank(sb.toString())) {
+                try {
+                    String phoneNumbers = sb.toString().substring(0, sb.toString().length() - 1);
+                    SendSmsUtils.sendSms(phoneNumbers, templateParam, accessKeyId, accessKeySecret, signName, templateCode);
+                } catch (ClientException e) {
+                    e.printStackTrace();
+                }
+            }
         }
         return AjaxResult.success(resultList);
     }

+ 36 - 4
boman-web-core/src/main/java/com/boman/web/core/service/save/BaseSaveServiceImpl.java

@@ -1,15 +1,20 @@
 package com.boman.web.core.service.save;
 
 import com.alibaba.fastjson.JSONObject;
+import com.aliyuncs.exceptions.ClientException;
+import com.boman.common.core.utils.StringUtils;
 import com.boman.common.redis.service.RedisService;
 import com.boman.domain.GenTableColumn;
 import com.boman.domain.constant.FormDataConstant;
 import com.boman.domain.constant.TableNameConst;
+import com.boman.domain.dto.AjaxResult;
 import com.boman.domain.dto.WxMsgDto;
+import com.boman.system.api.RemoteUserService;
 import com.boman.web.core.domain.RowResult;
 import com.boman.web.core.mapper.StandardMapper;
 import com.boman.web.core.utils.ColumnUtils;
 import com.boman.web.core.utils.IdUtils;
+import com.boman.web.core.utils.SendSmsUtils;
 import com.boman.wechat.api.RemoteWechatService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -18,10 +23,7 @@ import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
 import java.sql.Timestamp;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 import static com.boman.common.core.utils.obj.ObjectUtils.*;
 import static com.boman.web.core.utils.ColumnUtils.*;
@@ -43,6 +45,9 @@ public class BaseSaveServiceImpl implements IBaseSaveService {
     @Resource
     private RemoteWechatService remoteWechatService;
 
+    @Resource
+    private RemoteUserService remoteUserService;
+
     /**
      * 功能描述: 保存一行
      *
@@ -116,6 +121,33 @@ public class BaseSaveServiceImpl implements IBaseSaveService {
             wxMsgDto.setParams(messageInfo);
             LOGGER.info("收文人id:{}, 发送数据: {}", receiveUserIds, messageInfo);
             remoteWechatService.pushMsg(wxMsgDto);
+
+            List<Long> userIds = new ArrayList<>(new HashSet(receiveUserIds));
+            AjaxResult userList = remoteUserService.selectUserByIds(userIds);
+            List<Map<String, Object>> users = (List<Map<String, Object>>) userList.get("data");
+            String accessKeyId = "LTAI5tNA2fcBJH6EWRH6Pxr6";
+            String accessKeySecret = "5WdaPEOvC3u9LC7pwy2DQ9pgmJvgUr";
+            String signName = "";
+            String templateCode = "";
+            //自定义的模板变量
+            String templateParam = "";
+            StringBuilder sb = new StringBuilder();
+            if (users != null && users.size() > 0) {
+                for (Map<String, Object> user : users) {
+                    String phoneNumber = user.get("phonenumber").toString();
+                    if (StringUtils.isNotBlank(phoneNumber)) {
+                        sb.append(phoneNumber).append(",");
+                    }
+                }
+            }
+            if (StringUtils.isNotBlank(sb.toString())) {
+                try {
+                    String phoneNumbers = sb.toString().substring(0, sb.toString().length() - 1);
+                    SendSmsUtils.sendSms(phoneNumbers, templateParam, accessKeyId, accessKeySecret, signName, templateCode);
+                } catch (ClientException e) {
+                    e.printStackTrace();
+                }
+            }
         }
         return rowResults;
     }

+ 5 - 2
boman-web-core/src/main/java/com/boman/web/core/utils/SendSmsUtils.java

@@ -10,6 +10,7 @@ import com.aliyuncs.exceptions.ClientException;
 import com.aliyuncs.profile.DefaultProfile;
 import com.aliyuncs.profile.IClientProfile;
 import com.boman.common.core.utils.DateUtils;
+import com.boman.common.core.utils.StringUtils;
 
 /**
  * @author tjf
@@ -55,8 +56,10 @@ public class SendSmsUtils {
         request.setSignName(signName);
         //必填:短信模板-可在短信控制台中找到
         request.setTemplateCode(templateCode);
-        //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
-        request.setTemplateParam("{\"name\":\"" + nikeName + "\"}");
+        if (StringUtils.isNotBlank(nikeName)){
+            //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
+            request.setTemplateParam("{\"name\":\"" + nikeName + "\"}");
+        }
         //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
         request.setOutId("jiaoyuju");
         //hint 此处可能会抛出异常,注意catch