Commit fdc60fbc90795e45debf5761798c92e4ac168f26
1 parent
73a2f4d8
完成短信模板的模块
Showing
7 changed files
with
116 additions
and
14 deletions
ruoyi-ui/src/api/system/sms/smsTemplate.js
| @@ -43,6 +43,15 @@ export function getSmsTemplatePage(query) { | @@ -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 | // 导出短信模板 Excel | 55 | // 导出短信模板 Excel |
| 47 | export function exportSmsTemplateExcel(query) { | 56 | export function exportSmsTemplateExcel(query) { |
| 48 | return request({ | 57 | return request({ |
| @@ -52,3 +61,4 @@ export function exportSmsTemplateExcel(query) { | @@ -52,3 +61,4 @@ export function exportSmsTemplateExcel(query) { | ||
| 52 | responseType: 'blob' | 61 | responseType: 'blob' |
| 53 | }) | 62 | }) |
| 54 | } | 63 | } |
| 64 | + |
ruoyi-ui/src/views/system/sms/smsTemplate.vue
| @@ -81,8 +81,8 @@ | @@ -81,8 +81,8 @@ | ||
| 81 | </el-table-column> | 81 | </el-table-column> |
| 82 | <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150"> | 82 | <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150"> |
| 83 | <template slot-scope="scope"> | 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 | <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" | 86 | <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" |
| 87 | v-hasPermi="['system:sms-template:update']">修改</el-button> | 87 | v-hasPermi="['system:sms-template:update']">修改</el-button> |
| 88 | <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" | 88 | <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" |
| @@ -137,11 +137,32 @@ | @@ -137,11 +137,32 @@ | ||
| 137 | <el-button @click="cancel">取 消</el-button> | 137 | <el-button @click="cancel">取 消</el-button> |
| 138 | </div> | 138 | </div> |
| 139 | </el-dialog> | 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 | </div> | 160 | </div> |
| 141 | </template> | 161 | </template> |
| 142 | 162 | ||
| 143 | <script> | 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 | import { getSimpleSmsChannels } from "@/api/system/sms/smsChannel"; | 166 | import { getSimpleSmsChannels } from "@/api/system/sms/smsChannel"; |
| 146 | 167 | ||
| 147 | export default { | 168 | export default { |
| @@ -186,6 +207,16 @@ export default { | @@ -186,6 +207,16 @@ export default { | ||
| 186 | }, | 207 | }, |
| 187 | // 短信渠道 | 208 | // 短信渠道 |
| 188 | channelOptions: [], | 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 | created() { | 222 | created() { |
| @@ -311,6 +342,55 @@ export default { | @@ -311,6 +342,55 @@ export default { | ||
| 311 | this.downloadExcel(response, '短信模板.xls'); | 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 | formatChannelSignature(channelId) { | 395 | formatChannelSignature(channelId) { |
| 316 | for (const channel of this.channelOptions) { | 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,6 +7,7 @@ import cn.iocoder.dashboard.framework.logger.operatelog.core.annotations.Operate | ||
| 7 | import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.*; | 7 | import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.*; |
| 8 | import cn.iocoder.dashboard.modules.system.convert.sms.SysSmsTemplateConvert; | 8 | import cn.iocoder.dashboard.modules.system.convert.sms.SysSmsTemplateConvert; |
| 9 | import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO; | 9 | import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO; |
| 10 | +import cn.iocoder.dashboard.modules.system.service.sms.SysSmsService; | ||
| 10 | import cn.iocoder.dashboard.modules.system.service.sms.SysSmsTemplateService; | 11 | import cn.iocoder.dashboard.modules.system.service.sms.SysSmsTemplateService; |
| 11 | import io.swagger.annotations.Api; | 12 | import io.swagger.annotations.Api; |
| 12 | import io.swagger.annotations.ApiImplicitParam; | 13 | import io.swagger.annotations.ApiImplicitParam; |
| @@ -30,6 +31,8 @@ public class SysSmsTemplateController { | @@ -30,6 +31,8 @@ public class SysSmsTemplateController { | ||
| 30 | 31 | ||
| 31 | @Resource | 32 | @Resource |
| 32 | private SysSmsTemplateService smsTemplateService; | 33 | private SysSmsTemplateService smsTemplateService; |
| 34 | + @Resource | ||
| 35 | + private SysSmsService smsService; | ||
| 33 | 36 | ||
| 34 | @PostMapping("/create") | 37 | @PostMapping("/create") |
| 35 | @ApiOperation("创建短信模板") | 38 | @ApiOperation("创建短信模板") |
| @@ -85,10 +88,11 @@ public class SysSmsTemplateController { | @@ -85,10 +88,11 @@ public class SysSmsTemplateController { | ||
| 85 | } | 88 | } |
| 86 | 89 | ||
| 87 | @PostMapping("/send-sms") | 90 | @PostMapping("/send-sms") |
| 88 | - @ApiOperation("导出短信模板 Excel") | 91 | + @ApiOperation("发送短信") |
| 89 | @PreAuthorize("@ss.hasPermission('system:sms-template:send-sms')") | 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,6 +7,7 @@ import lombok.EqualsAndHashCode; | ||
| 7 | import lombok.ToString; | 7 | import lombok.ToString; |
| 8 | 8 | ||
| 9 | import java.util.Date; | 9 | import java.util.Date; |
| 10 | +import java.util.List; | ||
| 10 | 11 | ||
| 11 | @ApiModel("短信模板 Response VO") | 12 | @ApiModel("短信模板 Response VO") |
| 12 | @Data | 13 | @Data |
| @@ -20,6 +21,9 @@ public class SysSmsTemplateRespVO extends SysSmsTemplateBaseVO { | @@ -20,6 +21,9 @@ public class SysSmsTemplateRespVO extends SysSmsTemplateBaseVO { | ||
| 20 | @ApiModelProperty(value = "短信渠道编码", required = true, example = "ALIYUN") | 21 | @ApiModelProperty(value = "短信渠道编码", required = true, example = "ALIYUN") |
| 21 | private String channelCode; | 22 | private String channelCode; |
| 22 | 23 | ||
| 24 | + @ApiModelProperty(value = "参数数组", example = "name,code") | ||
| 25 | + private List<String> params; | ||
| 26 | + | ||
| 23 | @ApiModelProperty(value = "创建时间", required = true) | 27 | @ApiModelProperty(value = "创建时间", required = true) |
| 24 | private Date createTime; | 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 +11,15 @@ import java.util.Map; | ||
| 11 | @Data | 11 | @Data |
| 12 | public class SysSmsTemplateSendReqVO { | 12 | public class SysSmsTemplateSendReqVO { |
| 13 | 13 | ||
| 14 | + @ApiModelProperty(value = "手机号", required = true, example = "15601691300") | ||
| 15 | + @NotNull(message = "手机号不能为空") | ||
| 16 | + private String mobile; | ||
| 17 | + | ||
| 14 | @ApiModelProperty(value = "模板编码", required = true, example = "test_01") | 18 | @ApiModelProperty(value = "模板编码", required = true, example = "test_01") |
| 15 | @NotNull(message = "模板编码不能为空") | 19 | @NotNull(message = "模板编码不能为空") |
| 16 | - private String code; | 20 | + private String templateCode; |
| 17 | 21 | ||
| 18 | @ApiModelProperty(value = "模板参数") | 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,7 +14,7 @@ import java.util.Map; | ||
| 14 | */ | 14 | */ |
| 15 | public interface SysSmsService { | 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 | String templateCode, Map<String, Object> templateParams); | 18 | String templateCode, Map<String, Object> templateParams); |
| 19 | 19 | ||
| 20 | void sendBatchSms(List<String> mobiles, List<Long> userIds, Integer userType, | 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,7 +54,7 @@ public class SysSmsServiceImpl implements SysSmsService { | ||
| 54 | private SysUserService userService; | 54 | private SysUserService userService; |
| 55 | 55 | ||
| 56 | @Override | 56 | @Override |
| 57 | - public void sendSingleSms(String mobile, Long userId, Integer userType, | 57 | + public Long sendSingleSms(String mobile, Long userId, Integer userType, |
| 58 | String templateCode, Map<String, Object> templateParams) { | 58 | String templateCode, Map<String, Object> templateParams) { |
| 59 | // 校验短信模板是否合法 | 59 | // 校验短信模板是否合法 |
| 60 | SysSmsTemplateDO template = this.checkSmsTemplateValid(templateCode); | 60 | SysSmsTemplateDO template = this.checkSmsTemplateValid(templateCode); |
| @@ -67,11 +67,11 @@ public class SysSmsServiceImpl implements SysSmsService { | @@ -67,11 +67,11 @@ public class SysSmsServiceImpl implements SysSmsService { | ||
| 67 | Long sendLogId = smsLogService.createSmsLog(mobile, userId, userType, isSend, template, content, templateParams); | 67 | Long sendLogId = smsLogService.createSmsLog(mobile, userId, userType, isSend, template, content, templateParams); |
| 68 | 68 | ||
| 69 | // 发送 MQ 消息,异步执行发送短信 | 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 | @Override | 77 | @Override |