1.增加短信验证码接口

master
panqihua 2 years ago
parent 466798d1aa
commit c375ffeb5d
  1. 30
      ruoyi-admin/src/main/java/com/ruoyi/web/controller/platform/PlatformSmsController.java
  2. 42
      ruoyi-admin/src/main/resources/logback.xml
  3. 2
      ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java
  4. 49
      sql/ttsbg.sql
  5. 122
      ttsbg-platform/src/main/java/com/ruoyi/platform/domain/PlatformSms.java
  6. 62
      ttsbg-platform/src/main/java/com/ruoyi/platform/mapper/PlatformSmsMapper.java
  7. 70
      ttsbg-platform/src/main/java/com/ruoyi/platform/service/IPlatformSmsService.java
  8. 114
      ttsbg-platform/src/main/java/com/ruoyi/platform/service/impl/PlatformSmsServiceImpl.java
  9. 85
      ttsbg-platform/src/main/resources/mapper/platform/PlatformSmsMapper.xml

@ -0,0 +1,30 @@
package com.ruoyi.web.controller.platform;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.PlatformConfig;
import com.ruoyi.platform.domain.PlatformSms;
import com.ruoyi.platform.service.IPlatformSmsService;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/platform/public/sms")
public class PlatformSmsController extends BaseController {
@Autowired
private IPlatformSmsService platformSmsService;
@Autowired
private ISysConfigService sysConfigService;
@PostMapping("send")
public AjaxResult send(@Validated @RequestBody PlatformSms sms) {
return success(platformSmsService.send(sms.getMobile(), Integer.valueOf(sysConfigService.selectConfigById(PlatformConfig.smsTime.getConfigId()).getConfigValue())));
}
}

@ -11,7 +11,7 @@
<pattern>${log.pattern}</pattern> <pattern>${log.pattern}</pattern>
</encoder> </encoder>
</appender> </appender>
<!-- 系统日志输出 --> <!-- 系统日志输出 -->
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender"> <appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/sys-info.log</file> <file>${log.path}/sys-info.log</file>
@ -34,8 +34,8 @@
<onMismatch>DENY</onMismatch> <onMismatch>DENY</onMismatch>
</filter> </filter>
</appender> </appender>
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender"> <appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/sys-error.log</file> <file>${log.path}/sys-error.log</file>
<!-- 循环政策:基于时间创建日志文件 --> <!-- 循环政策:基于时间创建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
@ -56,8 +56,8 @@
<onMismatch>DENY</onMismatch> <onMismatch>DENY</onMismatch>
</filter> </filter>
</appender> </appender>
<!-- 用户访问日志输出 --> <!-- 用户访问日志输出 -->
<appender name="sys-user" class="ch.qos.logback.core.rolling.RollingFileAppender"> <appender name="sys-user" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/sys-user.log</file> <file>${log.path}/sys-user.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
@ -70,24 +70,26 @@
<pattern>${log.pattern}</pattern> <pattern>${log.pattern}</pattern>
</encoder> </encoder>
</appender> </appender>
<!-- 系统模块日志级别控制 -->
<logger name="com.ruoyi" level="info" />
<!-- Spring日志级别控制 -->
<logger name="org.springframework" level="warn" />
<root level="info"> <!-- 系统模块日志级别控制 -->
<appender-ref ref="console" /> <logger name="com.ruoyi" level="debug"/>
</root> <!-- Spring日志级别控制 -->
<logger name="org.springframework" level="warn"/>
<!--系统操作日志--> <!-- mybatisplus-->
<logger name="com.baomidou.mybatisplus" level="debug"/>
<root level="info"> <root level="info">
<appender-ref ref="file_info" /> <appender-ref ref="console"/>
<appender-ref ref="file_error" />
</root> </root>
<!--系统用户操作日志--> <!--系统操作日志-->
<root level="info">
<appender-ref ref="file_info"/>
<appender-ref ref="file_error"/>
</root>
<!--系统用户操作日志-->
<logger name="sys-user" level="info"> <logger name="sys-user" level="info">
<appender-ref ref="sys-user"/> <appender-ref ref="sys-user"/>
</logger> </logger>
</configuration> </configuration>

@ -117,6 +117,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll() .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
//平台设置 //平台设置
.antMatchers("/platform/config/**").permitAll() .antMatchers("/platform/config/**").permitAll()
//平台短信发送
.antMatchers("/platform/public/sms/send").permitAll()
// 除上面外的所有请求全部需要鉴权认证 // 除上面外的所有请求全部需要鉴权认证
.anyRequest().authenticated() .anyRequest().authenticated()
.and() .and()

@ -1,16 +1,32 @@
alter table sys_user CREATE TABLE IF NOT EXISTS `platform_sms`
add wechat_nick_name varchar(30) null comment '微信昵称'; (
alter table sys_user `id` INTEGER NOT NULL auto_increment COMMENT '主键',
add balance float null comment '账户余额'; `mobile` VARCHAR(255) NOT NULL COMMENT '中国大陆手机号',
alter table sys_user `code` VARCHAR(255) NOT NULL COMMENT '6位数字',
add referrer_id bigint null comment '推荐人ID'; `sendTime` datetime COMMENT '未发送或发送失败为空',
alter table sys_user `deadTime` datetime COMMENT '未发送或发送失败为空',
add constraint sys_user_sys_user_user_id_fk `error` VARCHAR(255),
foreign key (referrer_id) references sys_user (user_id); `remark` VARCHAR(255) COMMENT '注册|修改手机号',
alter table sys_user PRIMARY KEY (`id`)
modify user_name varchar(30) null comment '用户账号'; ) ENGINE = InnoDB COMMENT '短信验证码表';
# 收货地址表
create table ttsbg.platform_address CREATE TABLE IF NOT EXISTS `platform_store_account`
(
`storeId` INTEGER NOT NULL auto_increment,
`mobile` VARCHAR(255) NOT NULL COMMENT '中国大陆手机号',
`password` VARCHAR(255) NOT NULL COMMENT 'HS512 加密',
`status` INTEGER NOT NULL COMMENT '枚举值、详看数据字典编码',
`registerDate` VARCHAR(255) NOT NULL COMMENT 'yyyy-MM-dd',
`nickname` VARCHAR(255) NOT NULL COMMENT '待确定格式',
`sex` INTEGER NOT NULL COMMENT '枚举值、详看数据字典编码',
`avatar` VARCHAR(255) NOT NULL COMMENT '待确定存储格式',
`loginDate` VARCHAR(255) NOT NULL COMMENT 'yyyy-MM-dd',
PRIMARY KEY (`storeId`)
) ENGINE = InnoDB COMMENT '店家账号表';
# TODO:熟悉框架建立的表,后面可能要再调整
create table platform_address
( (
id bigint auto_increment id bigint auto_increment
primary key, primary key,
@ -28,12 +44,12 @@ create table ttsbg.platform_address
update_time datetime null, update_time datetime null,
user_id bigint null comment '关联用户', user_id bigint null comment '关联用户',
constraint platform_address_sys_user_user_id_fk constraint platform_address_sys_user_user_id_fk
foreign key (user_id) references ttsbg.sys_user (user_id) foreign key (user_id) references sys_user (user_id)
) )
comment '收货地址表'; comment '收货地址表';
# TODO:熟悉框架建立的表,后面可能要再调整
create table platform_follower create table platform_follower
( (
id bigint auto_increment id bigint auto_increment
@ -49,4 +65,5 @@ create table platform_follower
comment '粉丝表'; comment '粉丝表';
select max(deadTime)
from platform_sms;

@ -0,0 +1,122 @@
package com.ruoyi.platform.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.BaseEntity;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.util.Date;
/**
* 短信验证码对象 platform_sms
*
* @author ruoyi
* @date 2023-02-09
*/
public class PlatformSms extends BaseEntity {
private static final long serialVersionUID = 1L;
/**
* 主键
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 中国大陆手机号
*/
@Excel(name = "中国大陆手机号")
@NotBlank(message = "手机号不能为空")
@Pattern(message = "手机号不合法", regexp = "^1(3\\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$")
private String mobile;
/**
* 6位数字
*/
@Excel(name = "6位数字")
private String code;
/**
* 未发送或发送失败为空
*/
@Excel(name = "未发送或发送失败为空")
private Date sendtime;
/**
* 未发送或发送失败为空
*/
@Excel(name = "未发送或发送失败为空")
private Date deadtime;
/**
* $column.columnComment
*/
@Excel(name = "${comment}", readConverterExp = "$column.readConverterExp()")
private String error;
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getMobile() {
return mobile;
}
public void setCode(String code) {
this.code = code;
}
public String getCode() {
return code;
}
public void setSendtime(Date sendtime) {
this.sendtime = sendtime;
}
public Date getSendtime() {
return sendtime;
}
public void setDeadtime(Date deadtime) {
this.deadtime = deadtime;
}
public Date getDeadtime() {
return deadtime;
}
public void setError(String error) {
this.error = error;
}
public String getError() {
return error;
}
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
.append("id", getId())
.append("mobile", getMobile())
.append("code", getCode())
.append("sendtime", getSendtime())
.append("deadtime", getDeadtime())
.append("error", getError())
.append("remark", getRemark())
.toString();
}
}

@ -0,0 +1,62 @@
package com.ruoyi.platform.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.platform.domain.PlatformSms;
import java.util.List;
/**
* 短信验证码Mapper接口
*
* @author ruoyi
* @date 2023-02-09
*/
public interface PlatformSmsMapper extends BaseMapper<PlatformSms> {
/**
* 查询短信验证码
*
* @param id 短信验证码主键
* @return 短信验证码
*/
public PlatformSms selectPlatformSmsById(Long id);
/**
* 查询短信验证码列表
*
* @param platformSms 短信验证码
* @return 短信验证码集合
*/
public List<PlatformSms> selectPlatformSmsList(PlatformSms platformSms);
/**
* 新增短信验证码
*
* @param platformSms 短信验证码
* @return 结果
*/
public int insertPlatformSms(PlatformSms platformSms);
/**
* 修改短信验证码
*
* @param platformSms 短信验证码
* @return 结果
*/
public int updatePlatformSms(PlatformSms platformSms);
/**
* 删除短信验证码
*
* @param id 短信验证码主键
* @return 结果
*/
public int deletePlatformSmsById(Long id);
/**
* 批量删除短信验证码
*
* @param ids 需要删除的数据主键集合
* @return 结果
*/
public int deletePlatformSmsByIds(Long[] ids);
}

@ -0,0 +1,70 @@
package com.ruoyi.platform.service;
import com.ruoyi.platform.domain.PlatformSms;
import java.util.List;
/**
* 短信验证码Service接口
*
* @author ruoyi
* @date 2023-02-09
*/
public interface IPlatformSmsService {
/**
* 查询短信验证码
*
* @param id 短信验证码主键
* @return 短信验证码
*/
public PlatformSms selectPlatformSmsById(Long id);
/**
* 查询短信验证码列表
*
* @param platformSms 短信验证码
* @return 短信验证码集合
*/
public List<PlatformSms> selectPlatformSmsList(PlatformSms platformSms);
/**
* 新增短信验证码
*
* @param platformSms 短信验证码
* @return 结果
*/
public int insertPlatformSms(PlatformSms platformSms);
/**
* 修改短信验证码
*
* @param platformSms 短信验证码
* @return 结果
*/
public int updatePlatformSms(PlatformSms platformSms);
/**
* 批量删除短信验证码
*
* @param ids 需要删除的短信验证码主键集合
* @return 结果
*/
public int deletePlatformSmsByIds(Long[] ids);
/**
* 删除短信验证码信息
*
* @param id 短信验证码主键
* @return 结果
*/
public int deletePlatformSmsById(Long id);
/**
* 发送短信验证码
*
* @param mobile 手机号
* @param integer
* @return 结果
*/
String send(String mobile, Integer integer);
}

@ -0,0 +1,114 @@
package com.ruoyi.platform.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.platform.domain.PlatformSms;
import com.ruoyi.platform.mapper.PlatformSmsMapper;
import com.ruoyi.platform.service.IPlatformSmsService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
/**
* 短信验证码Service业务层处理
*
* @author ruoyi
* @date 2023-02-09
*/
@Service
public class PlatformSmsServiceImpl implements IPlatformSmsService {
@Autowired
private PlatformSmsMapper platformSmsMapper;
/**
* 查询短信验证码
*
* @param id 短信验证码主键
* @return 短信验证码
*/
@Override
public PlatformSms selectPlatformSmsById(Long id) {
return platformSmsMapper.selectPlatformSmsById(id);
}
/**
* 查询短信验证码列表
*
* @param platformSms 短信验证码
* @return 短信验证码
*/
@Override
public List<PlatformSms> selectPlatformSmsList(PlatformSms platformSms) {
return platformSmsMapper.selectPlatformSmsList(platformSms);
}
/**
* 新增短信验证码
*
* @param platformSms 短信验证码
* @return 结果
*/
@Override
public int insertPlatformSms(PlatformSms platformSms) {
return platformSmsMapper.insertPlatformSms(platformSms);
}
/**
* 修改短信验证码
*
* @param platformSms 短信验证码
* @return 结果
*/
@Override
public int updatePlatformSms(PlatformSms platformSms) {
return platformSmsMapper.updatePlatformSms(platformSms);
}
/**
* 批量删除短信验证码
*
* @param ids 需要删除的短信验证码主键
* @return 结果
*/
@Override
public int deletePlatformSmsByIds(Long[] ids) {
return platformSmsMapper.deletePlatformSmsByIds(ids);
}
/**
* 删除短信验证码信息
*
* @param id 短信验证码主键
* @return 结果
*/
@Override
public int deletePlatformSmsById(Long id) {
return platformSmsMapper.deletePlatformSmsById(id);
}
@Override
public String send(String mobile, Integer smsTime) {
LambdaQueryWrapper<PlatformSms> wrapper = new LambdaQueryWrapper<>();
Date d = new Date();
wrapper.select(PlatformSms::getId).eq(PlatformSms::getMobile, mobile).le(PlatformSms::getSendtime, d).ge(PlatformSms::getDeadtime, d);
PlatformSms sms = platformSmsMapper.selectOne(wrapper);
if (sms != null) {
return "短信发送过于频繁";
} else {
String code = RandomStringUtils.randomNumeric(6);
PlatformSms newSms = new PlatformSms();
newSms.setMobile(mobile);
newSms.setCode(code);
//TODO 发送验证码成功
newSms.setSendtime(new Date());
newSms.setDeadtime(DateUtils.addSeconds(newSms.getSendtime(), smsTime));
platformSmsMapper.insert(newSms);
return "短信验证码发送成功";
}
}
}

@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.system.mapper.PlatformSmsMapper">
<resultMap type="PlatformSms" id="PlatformSmsResult">
<result property="id" column="id"/>
<result property="mobile" column="mobile"/>
<result property="code" column="code"/>
<result property="sendtime" column="sendTime"/>
<result property="deadtime" column="deadTime"/>
<result property="error" column="error"/>
<result property="remark" column="remark"/>
</resultMap>
<sql id="selectPlatformSmsVo">
select id, mobile, code, sendTime, deadTime, error, remark
from platform_sms
</sql>
<select id="selectPlatformSmsList" parameterType="PlatformSms" resultMap="PlatformSmsResult">
<include refid="selectPlatformSmsVo"/>
<where>
<if test="mobile != null and mobile != ''">and mobile = #{mobile}</if>
<if test="code != null and code != ''">and code = #{code}</if>
<if test="sendtime != null ">and sendTime = #{sendtime}</if>
<if test="deadtime != null ">and deadTime = #{deadtime}</if>
<if test="error != null and error != ''">and error = #{error}</if>
</where>
</select>
<select id="selectPlatformSmsById" parameterType="Long" resultMap="PlatformSmsResult">
<include refid="selectPlatformSmsVo"/>
where id = #{id}
</select>
<insert id="insertPlatformSms" parameterType="PlatformSms">
insert into platform_sms
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="mobile != null and mobile != ''">mobile,</if>
<if test="code != null and code != ''">code,</if>
<if test="sendtime != null">sendTime,</if>
<if test="deadtime != null">deadTime,</if>
<if test="error != null and error != ''">error,</if>
<if test="remark != null and remark != ''">remark,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">#{id},</if>
<if test="mobile != null and mobile != ''">#{mobile},</if>
<if test="code != null and code != ''">#{code},</if>
<if test="sendtime != null">#{sendtime},</if>
<if test="deadtime != null">#{deadtime},</if>
<if test="error != null and error != ''">#{error},</if>
<if test="remark != null and remark != ''">#{remark},</if>
</trim>
</insert>
<update id="updatePlatformSms" parameterType="PlatformSms">
update platform_sms
<trim prefix="SET" suffixOverrides=",">
<if test="mobile != null and mobile != ''">mobile = #{mobile},</if>
<if test="code != null and code != ''">code = #{code},</if>
<if test="sendtime != null">sendTime = #{sendtime},</if>
<if test="deadtime != null">deadTime = #{deadtime},</if>
<if test="error != null and error != ''">error = #{error},</if>
<if test="remark != null and remark != ''">remark = #{remark},</if>
</trim>
where id = #{id}
</update>
<delete id="deletePlatformSmsById" parameterType="Long">
delete
from platform_sms
where id = #{id}
</delete>
<delete id="deletePlatformSmsByIds" parameterType="String">
delete from platform_sms where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
</mapper>
Loading…
Cancel
Save