Преглед изворни кода

完成短信模板的模块

YunaiV пре 4 година
родитељ
комит
fdc60fbc90

+ 10 - 0
ruoyi-ui/src/api/system/sms/smsTemplate.js

@@ -43,6 +43,15 @@ export function getSmsTemplatePage(query) {
   })
 }
 
+// 创建短信模板
+export function sendSms(data) {
+  return request({
+    url: '/system/sms-template/send-sms',
+    method: 'post',
+    data: data
+  })
+}
+
 // 导出短信模板 Excel
 export function exportSmsTemplateExcel(query) {
   return request({
@@ -52,3 +61,4 @@ export function exportSmsTemplateExcel(query) {
     responseType: 'blob'
   })
 }
+

+ 83 - 3
ruoyi-ui/src/views/system/sms/smsTemplate.vue

@@ -81,8 +81,8 @@
       </el-table-column>
       <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
         <template slot-scope="scope">
-          <el-button size="mini" type="text" icon="el-icon-share" @click="handleUpdate(scope.row)"
-                     v-hasPermi="['system:sms-template:update']">测试</el-button>
+          <el-button size="mini" type="text" icon="el-icon-share" @click="handleSendSms(scope.row)"
+                     v-hasPermi="['system:sms-template:send-sms']">测试</el-button>
           <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                      v-hasPermi="['system:sms-template:update']">修改</el-button>
           <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
@@ -137,11 +137,32 @@
         <el-button @click="cancel">取 消</el-button>
       </div>
     </el-dialog>
+
+    <!-- 对话框(发送短信) -->
+    <el-dialog title="测试发送短信" :visible.sync="sendSmsOpen" width="500px" append-to-body>
+      <el-form ref="sendSmsForm" :model="sendSmsForm" :rules="sendSmsRules" label-width="140px">
+        <el-form-item label="模板内容" prop="content">
+          <el-input v-model="sendSmsForm.content" type="textarea" placeholder="请输入模板内容" readonly />
+        </el-form-item>
+        <el-form-item label="手机号" prop="mobile">
+          <el-input v-model="sendSmsForm.mobile" placeholder="请输入手机号" />
+        </el-form-item>
+        <el-form-item v-for="param in sendSmsForm.params" :label="'参数 {' + param + '}'" :prop="'templateParams.' + param">
+          <el-input v-model="sendSmsForm.templateParams[param]" :placeholder="'请输入 ' + param + ' 参数'" />
+        </el-form-item>
+      </el-form>
+      <div slot="footer" class="dialog-footer">
+        <el-button type="primary" @click="submitSendSmsForm">确 定</el-button>
+        <el-button @click="cancelSendSms">取 消</el-button>
+      </div>
+    </el-dialog>
+
   </div>
 </template>
 
 <script>
-import { createSmsTemplate, updateSmsTemplate, deleteSmsTemplate, getSmsTemplate, getSmsTemplatePage, exportSmsTemplateExcel } from "@/api/system/sms/smsTemplate";
+import { createSmsTemplate, updateSmsTemplate, deleteSmsTemplate, getSmsTemplate, getSmsTemplatePage,
+  exportSmsTemplateExcel, sendSms } from "@/api/system/sms/smsTemplate";
 import {  getSimpleSmsChannels } from "@/api/system/sms/smsChannel";
 
 export default {
@@ -186,6 +207,16 @@ export default {
       },
       // 短信渠道
       channelOptions: [],
+      // 发送短信
+      sendSmsOpen: false,
+      sendSmsForm: {
+        params: [], // 模板的参数列表
+      },
+      sendSmsRules: {
+        mobile: [{ required: true, message: "手机不能为空", trigger: "blur" }],
+        templateCode: [{ required: true, message: "手机不能为空", trigger: "blur" }],
+        templateParams: { }
+      }
     };
   },
   created() {
@@ -311,6 +342,55 @@ export default {
         this.downloadExcel(response, '短信模板.xls');
       })
     },
+    /** 发送短息按钮 */
+    handleSendSms(row) {
+      this.resetSendSms(row);
+      // 设置参数
+      this.sendSmsForm.content = row.content;
+      this.sendSmsForm.params = row.params;
+      this.sendSmsForm.templateCode = row.code;
+      this.sendSmsForm.templateParams = row.params.reduce(function(obj, item) {
+        obj[item] = undefined;
+        return obj;
+      }, {});
+      // 根据 row 重置 rules
+      this.sendSmsRules.templateParams = row.params.reduce(function(obj, item) {
+        obj[item] = { required: true, message: '参数 ' + item + " 不能为空", trigger: "change" };
+        return obj;
+      }, {});
+      // 设置打开
+      this.sendSmsOpen = true;
+    },
+    /** 重置发送短信的表单 */
+    resetSendSms() {
+      // 根据 row 重置表单
+      this.sendSmsForm = {
+        content: undefined,
+        params: undefined,
+        mobile: undefined,
+        templateCode: undefined,
+        templateParams: {}
+      };
+      this.resetForm("sendSmsForm");
+    },
+    /** 取消发送短信 */
+    cancelSendSms() {
+      this.sendSmsOpen = false;
+      this.resetSendSms();
+    },
+    /** 提交按钮 */
+    submitSendSmsForm() {
+      this.$refs["sendSmsForm"].validate(valid => {
+        if (!valid) {
+          return;
+        }
+        // 添加的提交
+        sendSms(this.sendSmsForm).then(response => {
+          this.msgSuccess("提交发送成功!发送结果,见发送日志编号:" + response.data);
+          this.sendSmsOpen = false;
+        });
+      });
+    },
     /** 格式化短信渠道 */
     formatChannelSignature(channelId) {
       for (const channel of this.channelOptions) {

+ 7 - 3
src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/SysSmsTemplateController.java

@@ -7,6 +7,7 @@ import cn.iocoder.dashboard.framework.logger.operatelog.core.annotations.Operate
 import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.*;
 import cn.iocoder.dashboard.modules.system.convert.sms.SysSmsTemplateConvert;
 import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO;
+import cn.iocoder.dashboard.modules.system.service.sms.SysSmsService;
 import cn.iocoder.dashboard.modules.system.service.sms.SysSmsTemplateService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiImplicitParam;
@@ -30,6 +31,8 @@ public class SysSmsTemplateController {
 
     @Resource
     private SysSmsTemplateService smsTemplateService;
+    @Resource
+    private SysSmsService smsService;
 
     @PostMapping("/create")
     @ApiOperation("创建短信模板")
@@ -85,10 +88,11 @@ public class SysSmsTemplateController {
     }
 
     @PostMapping("/send-sms")
-    @ApiOperation("导出短信模板 Excel")
+    @ApiOperation("发送短信")
     @PreAuthorize("@ss.hasPermission('system:sms-template:send-sms')")
-    public CommonResult<Boolean> sendSms(@Valid @RequestBody SysSmsTemplateSendReqVO sendReqVO) {
-        return success(true);
+    public CommonResult<Long> sendSms(@Valid @RequestBody SysSmsTemplateSendReqVO sendReqVO) {
+        return success(smsService.sendSingleSms(sendReqVO.getMobile(), null, null,
+                sendReqVO.getTemplateCode(), sendReqVO.getTemplateParams()));
     }
 
 }

+ 4 - 0
src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/vo/template/SysSmsTemplateRespVO.java

@@ -7,6 +7,7 @@ import lombok.EqualsAndHashCode;
 import lombok.ToString;
 
 import java.util.Date;
+import java.util.List;
 
 @ApiModel("短信模板 Response VO")
 @Data
@@ -20,6 +21,9 @@ public class SysSmsTemplateRespVO extends SysSmsTemplateBaseVO {
     @ApiModelProperty(value = "短信渠道编码", required = true, example = "ALIYUN")
     private String channelCode;
 
+    @ApiModelProperty(value = "参数数组", example = "name,code")
+    private List<String> params;
+
     @ApiModelProperty(value = "创建时间", required = true)
     private Date createTime;
 

+ 6 - 2
src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/vo/template/SysSmsTemplateSendReqVO.java

@@ -11,11 +11,15 @@ import java.util.Map;
 @Data
 public class SysSmsTemplateSendReqVO {
 
+    @ApiModelProperty(value = "手机号", required = true, example = "15601691300")
+    @NotNull(message = "手机号不能为空")
+    private String mobile;
+
     @ApiModelProperty(value = "模板编码", required = true, example = "test_01")
     @NotNull(message = "模板编码不能为空")
-    private String code;
+    private String templateCode;
 
     @ApiModelProperty(value = "模板参数")
-    private Map<String, Object> params;
+    private Map<String, Object> templateParams;
 
 }

+ 1 - 1
src/main/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsService.java

@@ -14,7 +14,7 @@ import java.util.Map;
  */
 public interface SysSmsService {
 
-    void sendSingleSms(String mobile, Long userId, Integer userType,
+    Long sendSingleSms(String mobile, Long userId, Integer userType,
                        String templateCode, Map<String, Object> templateParams);
 
     void sendBatchSms(List<String> mobiles, List<Long> userIds, Integer userType,

+ 5 - 5
src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsServiceImpl.java

@@ -54,7 +54,7 @@ public class SysSmsServiceImpl implements SysSmsService {
     private SysUserService userService;
 
     @Override
-    public void sendSingleSms(String mobile, Long userId, Integer userType,
+    public Long sendSingleSms(String mobile, Long userId, Integer userType,
                               String templateCode, Map<String, Object> templateParams) {
         // 校验短信模板是否合法
         SysSmsTemplateDO template = this.checkSmsTemplateValid(templateCode);
@@ -67,11 +67,11 @@ public class SysSmsServiceImpl implements SysSmsService {
         Long sendLogId = smsLogService.createSmsLog(mobile, userId, userType, isSend, template, content, templateParams);
 
         // 发送 MQ 消息,异步执行发送短信
-        if (!isSend) {
-            return;
+        if (isSend) {
+            List<KeyValue<String, Object>> newTemplateParams = this.buildTemplateParams(template, templateParams);
+            smsProducer.sendSmsSendMessage(sendLogId, mobile, template.getChannelId(), template.getApiTemplateId(), newTemplateParams);
         }
-        List<KeyValue<String, Object>> newTemplateParams = this.buildTemplateParams(template, templateParams);
-        smsProducer.sendSmsSendMessage(sendLogId, mobile, template.getChannelId(), template.getApiTemplateId(), newTemplateParams);
+        return sendLogId;
     }
 
     @Override