浏览代码

新增下发短信

Administrator 2 年之前
父节点
当前提交
4e18bfb096

+ 0 - 1
ruoyi-admin/pom.xml

@@ -16,7 +16,6 @@
     </description>
 
     <dependencies>
-
         <!-- spring-boot-devtools -->
         <dependency>
             <groupId>org.springframework.boot</groupId>

+ 182 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/sms/SendSmsController.java

@@ -0,0 +1,182 @@
+package com.ruoyi.web.controller.sms;
+
+import javax.servlet.http.HttpServletResponse;
+
+import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
+import com.aliyun.tea.TeaException;
+import com.aliyun.teautil.models.RuntimeOptions;
+import com.ruoyi.common.constant.CacheConstants;
+import com.ruoyi.common.constant.Constants;
+import com.ruoyi.common.core.redis.RedisCache;
+import com.ruoyi.common.utils.SendSmsUtils;
+import com.ruoyi.common.utils.StringUtils;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.PutMapping;
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import com.ruoyi.common.annotation.Log;
+import com.ruoyi.common.core.controller.BaseController;
+import com.ruoyi.common.core.domain.AjaxResult;
+import com.ruoyi.common.enums.BusinessType;
+import com.ruoyi.system.domain.SendSms;
+import com.ruoyi.system.service.ISendSmsService;
+import com.ruoyi.common.utils.poi.ExcelUtil;
+import com.ruoyi.common.core.page.TableDataInfo;
+
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 短信日志Controller
+ * 
+ * @author ruoyi
+ * @date 2023-02-08
+ */
+@RestController
+@RequestMapping("/sms")
+public class SendSmsController extends BaseController
+{
+    @Autowired
+    private ISendSmsService sendSmsService;
+
+    @Autowired
+    private RedisCache redisCache;
+
+    /**
+     * 查询短信日志列表
+     */
+    @PreAuthorize("@ss.hasPermi('system:sms:list')")
+    @GetMapping("/list")
+    public TableDataInfo list(SendSms sendSms)
+    {
+        startPage();
+        List<SendSms> list = sendSmsService.selectSendSmsList(sendSms);
+        return getDataTable(list);
+    }
+
+    /**
+     * 导出短信日志列表
+     */
+    @PreAuthorize("@ss.hasPermi('system:sms:export')")
+    @Log(title = "短信日志", businessType = BusinessType.EXPORT)
+    @PostMapping("/export")
+    public void export(HttpServletResponse response, SendSms sendSms)
+    {
+        List<SendSms> list = sendSmsService.selectSendSmsList(sendSms);
+        ExcelUtil<SendSms> util = new ExcelUtil<SendSms>(SendSms.class);
+        util.exportExcel(response, list, "短信日志数据");
+    }
+
+    /**
+     * 获取短信日志详细信息
+     */
+    @PreAuthorize("@ss.hasPermi('system:sms:query')")
+    @GetMapping(value = "/{id}")
+    public AjaxResult getInfo(@PathVariable("id") Long id)
+    {
+        return success(sendSmsService.selectSendSmsById(id));
+    }
+
+    /**
+     * 新增短信日志
+     */
+    @PreAuthorize("@ss.hasPermi('system:sms:add')")
+    @Log(title = "短信日志", businessType = BusinessType.INSERT)
+    @PostMapping
+    public AjaxResult add(@RequestBody SendSms sendSms)
+    {
+        return toAjax(sendSmsService.insertSendSms(sendSms));
+    }
+
+    /**
+     * 修改短信日志
+     */
+    @PreAuthorize("@ss.hasPermi('system:sms:edit')")
+    @Log(title = "短信日志", businessType = BusinessType.UPDATE)
+    @PutMapping
+    public AjaxResult edit(@RequestBody SendSms sendSms)
+    {
+        return toAjax(sendSmsService.updateSendSms(sendSms));
+    }
+
+    /**
+     * 删除短信日志
+     */
+    @PreAuthorize("@ss.hasPermi('system:sms:remove')")
+    @Log(title = "短信日志", businessType = BusinessType.DELETE)
+	@DeleteMapping("/{ids}")
+    public AjaxResult remove(@PathVariable Long[] ids)
+    {
+        return toAjax(sendSmsService.deleteSendSmsByIds(ids));
+    }
+
+
+    /**
+     * 单独发送短信的接口
+     */
+    @PostMapping("/sendSmsOnly")
+    public AjaxResult sendSms(@RequestBody SendSms sendSms) {
+        String phone = sendSms.getPhone();
+        if (StringUtils.isBlank(phone)) {
+            return AjaxResult.error("未获取到手机号");
+        }
+        try {
+            // 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html
+            com.aliyun.dysmsapi20170525.Client client = SendSmsUtils.createClient();
+            //获取用户验证码
+            String verifyKey = CacheConstants.SMS_CODE_KEY + sendSms.getPhone();
+            String smsCode = "{\"code\":"+redisCache.getCacheObject(verifyKey)+"}";
+            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
+                    //手机号码
+                    .setPhoneNumbers(phone)
+                    //短信签名名称。
+                    .setSignName("your_value")
+                    //短信模板CODE
+                    .setTemplateCode("your_value")
+                    //短信模板变量对应的实际值{"name": code}
+                    .setTemplateParam(smsCode);
+            try {
+                // 复制代码运行请自行打印 API 的返回值
+                SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, new RuntimeOptions());
+                String code = sendSmsResponse.getBody().code;
+                if ("OK".equals(code)) {
+                    sendSms.setStatus("发送成功");
+                    sendSmsService.insertSendSms(sendSms);
+                    return AjaxResult.success("发送成功");
+                } else {
+                    sendSms.setStatus("发送失败");
+                    sendSmsService.insertSendSms(sendSms);
+                    return AjaxResult.error(sendSmsResponse.getBody().message);
+                }
+            } 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 AjaxResult.error("发送失败");
+    }
+
+    /**
+     * 获取四位数短信验证码
+     * @param sendSms
+     * @return
+     */
+    @PostMapping("/getCode")
+    public AjaxResult getCode(@RequestBody SendSms sendSms) {
+        String code = SendSmsUtils.getCode(4);
+        String verifyKey = CacheConstants.SMS_CODE_KEY + sendSms.getPhone();
+        redisCache.setCacheObject(verifyKey, code, Constants.SMS_EXPIRATION, TimeUnit.MINUTES);
+        return AjaxResult.error("发送失败");
+    }
+}

+ 6 - 0
ruoyi-common/pom.xml

@@ -17,6 +17,12 @@
 
     <dependencies>
 
+        <!--阿里短信服务-->
+        <dependency>
+            <groupId>com.aliyun</groupId>
+            <artifactId>dysmsapi20170525</artifactId>
+            <version>2.0.23</version>
+        </dependency>
         <!--计算经纬度-->
         <dependency>
             <groupId>org.gavaghan</groupId>

+ 4 - 0
ruoyi-common/src/main/java/com/ruoyi/common/constant/CacheConstants.java

@@ -16,6 +16,10 @@ public class CacheConstants
      * 验证码 redis key
      */
     public static final String CAPTCHA_CODE_KEY = "captcha_codes:";
+    /**
+     * 短信验证码 redis key
+     */
+    public static final String SMS_CODE_KEY = "SMS_codes:";
 
     /**
      * 参数管理 cache key

+ 5 - 0
ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java

@@ -69,6 +69,11 @@ public class Constants
      */
     public static final Integer CAPTCHA_EXPIRATION = 2;
 
+    /**
+     * 短信验证码有效期(分钟)
+     */
+    public static final Integer SMS_EXPIRATION = 10;
+
     /**
      * 令牌
      */

+ 5 - 0
ruoyi-common/src/main/java/com/ruoyi/common/utils/DateUtils.java

@@ -28,11 +28,16 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
 
     public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
 
+    public static String YYYYMMDD = "yyyyMMdd";
+
     private static String[] parsePatterns = {
             "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", 
             "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
             "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
 
+    public static String getDateNow() {
+        return dateTimeNow(YYYYMMDD);
+    }
     /**
      * 获取当前Date型日期
      * 

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

@@ -0,0 +1,43 @@
+package com.ruoyi.common.utils;
+
+
+
+import com.aliyun.tea.*;
+
+/**
+ * @author tjf
+ * @Date: 2021/07/15/10:21
+ */
+public class SendSmsUtils {
+    //短信参数
+    static final String ACCESS_KEY_ID = "LTAI5tNA2fcBJH6EWRH6Pxr6";
+    static final String ACCESS_KEY_SECRET = "5WdaPEOvC3u9LC7pwy2DQ9pgmJvgUr";
+
+
+    //生成X位验证码
+    public static String getCode(Integer num) {
+        String[] codes = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
+        StringBuilder code = new StringBuilder();
+        for (int i = 0; i < num; i++) {
+            int j = (int) (Math.random() * 10);
+            code.append(codes[j]);
+        }
+        return code.toString();
+    }
+
+    /**
+     * 使用AK&SK初始化账号Client
+     * @return Client
+     * @throws Exception
+     */
+    public static com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
+        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
+                // 必填,您的 AccessKey ID
+                .setAccessKeyId(ACCESS_KEY_ID)
+                // 必填,您的 AccessKey Secret
+                .setAccessKeySecret(ACCESS_KEY_SECRET);
+        // 访问的域名
+        config.endpoint = "dysmsapi.aliyuncs.com";
+        return new com.aliyun.dysmsapi20170525.Client(config);
+    }
+}

+ 111 - 0
ruoyi-system/src/main/java/com/ruoyi/system/domain/SendSms.java

@@ -0,0 +1,111 @@
+package com.ruoyi.system.domain;
+
+import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.apache.commons.lang3.builder.ToStringStyle;
+import com.ruoyi.common.annotation.Excel;
+import com.ruoyi.common.core.domain.BaseEntity;
+
+/**
+ * 短信日志对象 send_sms
+ * 
+ * @author ruoyi
+ * @date 2023-02-08
+ */
+public class SendSms extends BaseEntity
+{
+    private static final long serialVersionUID = 1L;
+
+    /** $column.columnComment */
+    private Long id;
+
+    /** 发送的手机号 */
+    @Excel(name = "发送的手机号")
+    private String phone;
+
+    /** 状态 */
+    @Excel(name = "状态")
+    private String status;
+
+    /** 发送内容 */
+    @Excel(name = "发送内容")
+    private String content;
+
+    /** 修改时间 */
+    @Excel(name = "修改时间")
+    private String modifyDate;
+
+    /** 是否删除 */
+    @Excel(name = "是否删除")
+    private String isDel;
+
+    public void setId(Long id)
+    {
+        this.id = id;
+    }
+
+    public Long getId() 
+    {
+        return id;
+    }
+    public void setPhone(String phone) 
+    {
+        this.phone = phone;
+    }
+
+    public String getPhone() 
+    {
+        return phone;
+    }
+    public void setStatus(String status) 
+    {
+        this.status = status;
+    }
+
+    public String getStatus() 
+    {
+        return status;
+    }
+    public void setContent(String content) 
+    {
+        this.content = content;
+    }
+
+    public String getContent() 
+    {
+        return content;
+    }
+    public void setModifyDate(String modifyDate) 
+    {
+        this.modifyDate = modifyDate;
+    }
+
+    public String getModifyDate() 
+    {
+        return modifyDate;
+    }
+    public void setIsDel(String isDel) 
+    {
+        this.isDel = isDel;
+    }
+
+    public String getIsDel() 
+    {
+        return isDel;
+    }
+
+    @Override
+    public String toString() {
+        return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
+            .append("id", getId())
+            .append("phone", getPhone())
+            .append("status", getStatus())
+            .append("content", getContent())
+            .append("modifyDate", getModifyDate())
+            .append("createBy", getCreateBy())
+            .append("createTime", getCreateTime())
+            .append("updateBy", getUpdateBy())
+            .append("updateTime", getUpdateTime())
+            .append("isDel", getIsDel())
+            .toString();
+    }
+}

+ 61 - 0
ruoyi-system/src/main/java/com/ruoyi/system/mapper/SendSmsMapper.java

@@ -0,0 +1,61 @@
+package com.ruoyi.system.mapper;
+
+import java.util.List;
+import com.ruoyi.system.domain.SendSms;
+
+/**
+ * 短信日志Mapper接口
+ * 
+ * @author ruoyi
+ * @date 2023-02-08
+ */
+public interface SendSmsMapper 
+{
+    /**
+     * 查询短信日志
+     * 
+     * @param id 短信日志主键
+     * @return 短信日志
+     */
+    public SendSms selectSendSmsById(Long id);
+
+    /**
+     * 查询短信日志列表
+     * 
+     * @param sendSms 短信日志
+     * @return 短信日志集合
+     */
+    public List<SendSms> selectSendSmsList(SendSms sendSms);
+
+    /**
+     * 新增短信日志
+     * 
+     * @param sendSms 短信日志
+     * @return 结果
+     */
+    public int insertSendSms(SendSms sendSms);
+
+    /**
+     * 修改短信日志
+     * 
+     * @param sendSms 短信日志
+     * @return 结果
+     */
+    public int updateSendSms(SendSms sendSms);
+
+    /**
+     * 删除短信日志
+     * 
+     * @param id 短信日志主键
+     * @return 结果
+     */
+    public int deleteSendSmsById(Long id);
+
+    /**
+     * 批量删除短信日志
+     * 
+     * @param ids 需要删除的数据主键集合
+     * @return 结果
+     */
+    public int deleteSendSmsByIds(Long[] ids);
+}

+ 61 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/ISendSmsService.java

@@ -0,0 +1,61 @@
+package com.ruoyi.system.service;
+
+import java.util.List;
+import com.ruoyi.system.domain.SendSms;
+
+/**
+ * 短信日志Service接口
+ * 
+ * @author ruoyi
+ * @date 2023-02-08
+ */
+public interface ISendSmsService 
+{
+    /**
+     * 查询短信日志
+     * 
+     * @param id 短信日志主键
+     * @return 短信日志
+     */
+    public SendSms selectSendSmsById(Long id);
+
+    /**
+     * 查询短信日志列表
+     * 
+     * @param sendSms 短信日志
+     * @return 短信日志集合
+     */
+    public List<SendSms> selectSendSmsList(SendSms sendSms);
+
+    /**
+     * 新增短信日志
+     * 
+     * @param sendSms 短信日志
+     * @return 结果
+     */
+    public int insertSendSms(SendSms sendSms);
+
+    /**
+     * 修改短信日志
+     * 
+     * @param sendSms 短信日志
+     * @return 结果
+     */
+    public int updateSendSms(SendSms sendSms);
+
+    /**
+     * 批量删除短信日志
+     * 
+     * @param ids 需要删除的短信日志主键集合
+     * @return 结果
+     */
+    public int deleteSendSmsByIds(Long[] ids);
+
+    /**
+     * 删除短信日志信息
+     * 
+     * @param id 短信日志主键
+     * @return 结果
+     */
+    public int deleteSendSmsById(Long id);
+}

+ 96 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SendSmsServiceImpl.java

@@ -0,0 +1,96 @@
+package com.ruoyi.system.service.impl;
+
+import java.util.List;
+import com.ruoyi.common.utils.DateUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import com.ruoyi.system.mapper.SendSmsMapper;
+import com.ruoyi.system.domain.SendSms;
+import com.ruoyi.system.service.ISendSmsService;
+
+/**
+ * 短信日志Service业务层处理
+ * 
+ * @author ruoyi
+ * @date 2023-02-08
+ */
+@Service
+public class SendSmsServiceImpl implements ISendSmsService 
+{
+    @Autowired
+    private SendSmsMapper sendSmsMapper;
+
+    /**
+     * 查询短信日志
+     * 
+     * @param id 短信日志主键
+     * @return 短信日志
+     */
+    @Override
+    public SendSms selectSendSmsById(Long id)
+    {
+        return sendSmsMapper.selectSendSmsById(id);
+    }
+
+    /**
+     * 查询短信日志列表
+     * 
+     * @param sendSms 短信日志
+     * @return 短信日志
+     */
+    @Override
+    public List<SendSms> selectSendSmsList(SendSms sendSms)
+    {
+        return sendSmsMapper.selectSendSmsList(sendSms);
+    }
+
+    /**
+     * 新增短信日志
+     * 
+     * @param sendSms 短信日志
+     * @return 结果
+     */
+    @Override
+    public int insertSendSms(SendSms sendSms)
+    {
+        sendSms.setCreateTime(DateUtils.getNowDate());
+        return sendSmsMapper.insertSendSms(sendSms);
+    }
+
+    /**
+     * 修改短信日志
+     * 
+     * @param sendSms 短信日志
+     * @return 结果
+     */
+    @Override
+    public int updateSendSms(SendSms sendSms)
+    {
+        sendSms.setUpdateTime(DateUtils.getNowDate());
+        return sendSmsMapper.updateSendSms(sendSms);
+    }
+
+    /**
+     * 批量删除短信日志
+     * 
+     * @param ids 需要删除的短信日志主键
+     * @return 结果
+     */
+    @Override
+    public int deleteSendSmsByIds(Long[] ids)
+    {
+        return sendSmsMapper.deleteSendSmsByIds(ids);
+    }
+
+    /**
+     * 删除短信日志信息
+     * 
+     * @param id 短信日志主键
+     * @return 结果
+     */
+    @Override
+    public int deleteSendSmsById(Long id)
+    {
+        return sendSmsMapper.deleteSendSmsById(id);
+    }
+}