Commit fdc60fbc90795e45debf5761798c92e4ac168f26

Authored by YunaiV
1 parent 73a2f4d8

完成短信模板的模块

ruoyi-ui/src/api/system/sms/smsTemplate.js
... ... @@ -43,6 +43,15 @@ export function getSmsTemplatePage(query) {
43 43 })
44 44 }
45 45  
  46 +// 创建短信模板
  47 +export function sendSms(data) {
  48 + return request({
  49 + url: '/system/sms-template/send-sms',
  50 + method: 'post',
  51 + data: data
  52 + })
  53 +}
  54 +
46 55 // 导出短信模板 Excel
47 56 export function exportSmsTemplateExcel(query) {
48 57 return request({
... ... @@ -52,3 +61,4 @@ export function exportSmsTemplateExcel(query) {
52 61 responseType: 'blob'
53 62 })
54 63 }
  64 +
... ...
ruoyi-ui/src/views/system/sms/smsTemplate.vue
... ... @@ -81,8 +81,8 @@
81 81 </el-table-column>
82 82 <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
83 83 <template slot-scope="scope">
84   - <el-button size="mini" type="text" icon="el-icon-share" @click="handleUpdate(scope.row)"
85   - v-hasPermi="['system:sms-template:update']">测试</el-button>
  84 + <el-button size="mini" type="text" icon="el-icon-share" @click="handleSendSms(scope.row)"
  85 + v-hasPermi="['system:sms-template:send-sms']">测试</el-button>
86 86 <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
87 87 v-hasPermi="['system:sms-template:update']">修改</el-button>
88 88 <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
... ... @@ -137,11 +137,32 @@
137 137 <el-button @click="cancel">取 消</el-button>
138 138 </div>
139 139 </el-dialog>
  140 +
  141 + <!-- 对话框(发送短信) -->
  142 + <el-dialog title="测试发送短信" :visible.sync="sendSmsOpen" width="500px" append-to-body>
  143 + <el-form ref="sendSmsForm" :model="sendSmsForm" :rules="sendSmsRules" label-width="140px">
  144 + <el-form-item label="模板内容" prop="content">
  145 + <el-input v-model="sendSmsForm.content" type="textarea" placeholder="请输入模板内容" readonly />
  146 + </el-form-item>
  147 + <el-form-item label="手机号" prop="mobile">
  148 + <el-input v-model="sendSmsForm.mobile" placeholder="请输入手机号" />
  149 + </el-form-item>
  150 + <el-form-item v-for="param in sendSmsForm.params" :label="'参数 {' + param + '}'" :prop="'templateParams.' + param">
  151 + <el-input v-model="sendSmsForm.templateParams[param]" :placeholder="'请输入 ' + param + ' 参数'" />
  152 + </el-form-item>
  153 + </el-form>
  154 + <div slot="footer" class="dialog-footer">
  155 + <el-button type="primary" @click="submitSendSmsForm">确 定</el-button>
  156 + <el-button @click="cancelSendSms">取 消</el-button>
  157 + </div>
  158 + </el-dialog>
  159 +
140 160 </div>
141 161 </template>
142 162  
143 163 <script>
144   -import { createSmsTemplate, updateSmsTemplate, deleteSmsTemplate, getSmsTemplate, getSmsTemplatePage, exportSmsTemplateExcel } from "@/api/system/sms/smsTemplate";
  164 +import { createSmsTemplate, updateSmsTemplate, deleteSmsTemplate, getSmsTemplate, getSmsTemplatePage,
  165 + exportSmsTemplateExcel, sendSms } from "@/api/system/sms/smsTemplate";
145 166 import { getSimpleSmsChannels } from "@/api/system/sms/smsChannel";
146 167  
147 168 export default {
... ... @@ -186,6 +207,16 @@ export default {
186 207 },
187 208 // 短信渠道
188 209 channelOptions: [],
  210 + // 发送短信
  211 + sendSmsOpen: false,
  212 + sendSmsForm: {
  213 + params: [], // 模板的参数列表
  214 + },
  215 + sendSmsRules: {
  216 + mobile: [{ required: true, message: "手机不能为空", trigger: "blur" }],
  217 + templateCode: [{ required: true, message: "手机不能为空", trigger: "blur" }],
  218 + templateParams: { }
  219 + }
189 220 };
190 221 },
191 222 created() {
... ... @@ -311,6 +342,55 @@ export default {
311 342 this.downloadExcel(response, '短信模板.xls');
312 343 })
313 344 },
  345 + /** 发送短息按钮 */
  346 + handleSendSms(row) {
  347 + this.resetSendSms(row);
  348 + // 设置参数
  349 + this.sendSmsForm.content = row.content;
  350 + this.sendSmsForm.params = row.params;
  351 + this.sendSmsForm.templateCode = row.code;
  352 + this.sendSmsForm.templateParams = row.params.reduce(function(obj, item) {
  353 + obj[item] = undefined;
  354 + return obj;
  355 + }, {});
  356 + // 根据 row 重置 rules
  357 + this.sendSmsRules.templateParams = row.params.reduce(function(obj, item) {
  358 + obj[item] = { required: true, message: '参数 ' + item + " 不能为空", trigger: "change" };
  359 + return obj;
  360 + }, {});
  361 + // 设置打开
  362 + this.sendSmsOpen = true;
  363 + },
  364 + /** 重置发送短信的表单 */
  365 + resetSendSms() {
  366 + // 根据 row 重置表单
  367 + this.sendSmsForm = {
  368 + content: undefined,
  369 + params: undefined,
  370 + mobile: undefined,
  371 + templateCode: undefined,
  372 + templateParams: {}
  373 + };
  374 + this.resetForm("sendSmsForm");
  375 + },
  376 + /** 取消发送短信 */
  377 + cancelSendSms() {
  378 + this.sendSmsOpen = false;
  379 + this.resetSendSms();
  380 + },
  381 + /** 提交按钮 */
  382 + submitSendSmsForm() {
  383 + this.$refs["sendSmsForm"].validate(valid => {
  384 + if (!valid) {
  385 + return;
  386 + }
  387 + // 添加的提交
  388 + sendSms(this.sendSmsForm).then(response => {
  389 + this.msgSuccess("提交发送成功!发送结果,见发送日志编号:" + response.data);
  390 + this.sendSmsOpen = false;
  391 + });
  392 + });
  393 + },
314 394 /** 格式化短信渠道 */
315 395 formatChannelSignature(channelId) {
316 396 for (const channel of this.channelOptions) {
... ...
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
7 7 import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.*;
8 8 import cn.iocoder.dashboard.modules.system.convert.sms.SysSmsTemplateConvert;
9 9 import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO;
  10 +import cn.iocoder.dashboard.modules.system.service.sms.SysSmsService;
10 11 import cn.iocoder.dashboard.modules.system.service.sms.SysSmsTemplateService;
11 12 import io.swagger.annotations.Api;
12 13 import io.swagger.annotations.ApiImplicitParam;
... ... @@ -30,6 +31,8 @@ public class SysSmsTemplateController {
30 31  
31 32 @Resource
32 33 private SysSmsTemplateService smsTemplateService;
  34 + @Resource
  35 + private SysSmsService smsService;
33 36  
34 37 @PostMapping("/create")
35 38 @ApiOperation("创建短信模板")
... ... @@ -85,10 +88,11 @@ public class SysSmsTemplateController {
85 88 }
86 89  
87 90 @PostMapping("/send-sms")
88   - @ApiOperation("导出短信模板 Excel")
  91 + @ApiOperation("发送短信")
89 92 @PreAuthorize("@ss.hasPermission('system:sms-template:send-sms')")
90   - public CommonResult<Boolean> sendSms(@Valid @RequestBody SysSmsTemplateSendReqVO sendReqVO) {
91   - return success(true);
  93 + public CommonResult<Long> sendSms(@Valid @RequestBody SysSmsTemplateSendReqVO sendReqVO) {
  94 + return success(smsService.sendSingleSms(sendReqVO.getMobile(), null, null,
  95 + sendReqVO.getTemplateCode(), sendReqVO.getTemplateParams()));
92 96 }
93 97  
94 98 }
... ...
src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/vo/template/SysSmsTemplateRespVO.java
... ... @@ -7,6 +7,7 @@ import lombok.EqualsAndHashCode;
7 7 import lombok.ToString;
8 8  
9 9 import java.util.Date;
  10 +import java.util.List;
10 11  
11 12 @ApiModel("短信模板 Response VO")
12 13 @Data
... ... @@ -20,6 +21,9 @@ public class SysSmsTemplateRespVO extends SysSmsTemplateBaseVO {
20 21 @ApiModelProperty(value = "短信渠道编码", required = true, example = "ALIYUN")
21 22 private String channelCode;
22 23  
  24 + @ApiModelProperty(value = "参数数组", example = "name,code")
  25 + private List<String> params;
  26 +
23 27 @ApiModelProperty(value = "创建时间", required = true)
24 28 private Date createTime;
25 29  
... ...
src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/vo/template/SysSmsTemplateSendReqVO.java
... ... @@ -11,11 +11,15 @@ import java.util.Map;
11 11 @Data
12 12 public class SysSmsTemplateSendReqVO {
13 13  
  14 + @ApiModelProperty(value = "手机号", required = true, example = "15601691300")
  15 + @NotNull(message = "手机号不能为空")
  16 + private String mobile;
  17 +
14 18 @ApiModelProperty(value = "模板编码", required = true, example = "test_01")
15 19 @NotNull(message = "模板编码不能为空")
16   - private String code;
  20 + private String templateCode;
17 21  
18 22 @ApiModelProperty(value = "模板参数")
19   - private Map<String, Object> params;
  23 + private Map<String, Object> templateParams;
20 24  
21 25 }
... ...
src/main/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsService.java
... ... @@ -14,7 +14,7 @@ import java.util.Map;
14 14 */
15 15 public interface SysSmsService {
16 16  
17   - void sendSingleSms(String mobile, Long userId, Integer userType,
  17 + Long sendSingleSms(String mobile, Long userId, Integer userType,
18 18 String templateCode, Map<String, Object> templateParams);
19 19  
20 20 void sendBatchSms(List<String> mobiles, List<Long> userIds, Integer userType,
... ...
src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsServiceImpl.java
... ... @@ -54,7 +54,7 @@ public class SysSmsServiceImpl implements SysSmsService {
54 54 private SysUserService userService;
55 55  
56 56 @Override
57   - public void sendSingleSms(String mobile, Long userId, Integer userType,
  57 + public Long sendSingleSms(String mobile, Long userId, Integer userType,
58 58 String templateCode, Map<String, Object> templateParams) {
59 59 // 校验短信模板是否合法
60 60 SysSmsTemplateDO template = this.checkSmsTemplateValid(templateCode);
... ... @@ -67,11 +67,11 @@ public class SysSmsServiceImpl implements SysSmsService {
67 67 Long sendLogId = smsLogService.createSmsLog(mobile, userId, userType, isSend, template, content, templateParams);
68 68  
69 69 // 发送 MQ 消息,异步执行发送短信
70   - if (!isSend) {
71   - return;
  70 + if (isSend) {
  71 + List<KeyValue<String, Object>> newTemplateParams = this.buildTemplateParams(template, templateParams);
  72 + smsProducer.sendSmsSendMessage(sendLogId, mobile, template.getChannelId(), template.getApiTemplateId(), newTemplateParams);
72 73 }
73   - List<KeyValue<String, Object>> newTemplateParams = this.buildTemplateParams(template, templateParams);
74   - smsProducer.sendSmsSendMessage(sendLogId, mobile, template.getChannelId(), template.getApiTemplateId(), newTemplateParams);
  74 + return sendLogId;
75 75 }
76 76  
77 77 @Override
... ...