Commit 17d85dd0 by 唐功亮

【补充】 发送验证码 -保存验证码日志

parent f1662b05
package io.office.modules.manage.controller;
import java.util.Arrays;
import java.util.Map;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.office.modules.manage.entity.LogMessageEntity;
import io.office.modules.manage.service.LogMessageService;
import io.office.common.utils.PageUtils;
import io.office.common.utils.R;
/**
* ${comments}
*
* @author tgl
* @email
* @date 2021-12-08 11:04:42
*/
@RestController
@RequestMapping("/logmessage")
public class LogMessageController {
@Autowired
private LogMessageService logMessageService;
/**
* 列表
*/
@RequestMapping("/list")
// @RequiresPermissions("manage:logmessage:list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = logMessageService.queryPage(params);
return R.ok().put("page", page);
}
/**
* 信息
*/
@RequestMapping("/info/{id}")
// @RequiresPermissions("manage:logmessage:info")
public R info(@PathVariable("id") Integer id){
LogMessageEntity logMessage = logMessageService.getById(id);
return R.ok().put("logMessage", logMessage);
}
/**
* 保存
*/
@RequestMapping("/save")
// @RequiresPermissions("manage:logmessage:save")
public R save(@RequestBody LogMessageEntity logMessage){
logMessageService.save(logMessage);
return R.ok();
}
/**
* 修改
*/
@RequestMapping("/update")
// @RequiresPermissions("manage:logmessage:update")
public R update(@RequestBody LogMessageEntity logMessage){
logMessageService.updateById(logMessage);
return R.ok();
}
/**
* 删除
*/
@RequestMapping("/delete")
// @RequiresPermissions("manage:logmessage:delete")
public R delete(@RequestBody Integer[] ids){
logMessageService.removeByIds(Arrays.asList(ids));
return R.ok();
}
}
package io.office.modules.manage.dao;
import io.office.modules.manage.entity.LogMessageEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* ${comments}
*
* @author wudi
* @email
* @date 2021-12-08 11:04:42
*/
@Mapper
public interface LogMessageDao extends BaseMapper<LogMessageEntity> {
}
package io.office.modules.manage.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
/**
* ${comments}
*
* @author wudi
* @email
* @date 2021-12-08 11:04:42
*/
@Data
@TableName("log_message")
public class LogMessageEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主键
*/
@TableId
private Integer id;
/**
* 用户名
*/
private String username;
/**
* 手机号
*/
private String phone;
/**
*时间
*/
private Date time;
/**
* 短信内容
*/
private String pass;
/**
* ip
*/
private String ip;
/**
* 服务器IP
*/
private String url;
/**
* 分类
*/
private String type;
/**
* 发送状态
*/
private String state;
public LogMessageEntity(String username, String phone, Date time, String pass, String ip, String url, String type, String state) {
this.username = username;
this.phone = phone;
this.time = time;
this.pass = pass;
this.ip = ip;
this.url = url;
this.type = type;
this.state = state;
}
}
package io.office.modules.manage.service;
import com.baomidou.mybatisplus.extension.service.IService;
import io.office.common.utils.PageUtils;
import io.office.modules.manage.entity.LogMessageEntity;
import java.util.Map;
/**
* ${comments}
*
* @author wudi
* @email
* @date 2021-12-08 11:04:42
*/
public interface LogMessageService extends IService<LogMessageEntity> {
PageUtils queryPage(Map<String, Object> params);
}
package io.office.modules.manage.service.impl;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.office.common.utils.PageUtils;
import io.office.common.utils.Query;
import io.office.modules.manage.dao.LogMessageDao;
import io.office.modules.manage.entity.LogMessageEntity;
import io.office.modules.manage.service.LogMessageService;
@Service("logMessageService")
public class LogMessageServiceImpl extends ServiceImpl<LogMessageDao, LogMessageEntity> implements LogMessageService {
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<LogMessageEntity> page = this.page(
new Query<LogMessageEntity>().getPage(params),
new QueryWrapper<LogMessageEntity>()
);
return new PageUtils(page);
}
}
\ No newline at end of file
...@@ -4,8 +4,11 @@ import com.alibaba.fastjson.JSON; ...@@ -4,8 +4,11 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import io.office.common.exception.RRException; import io.office.common.exception.RRException;
import io.office.common.utils.IPUtils; import io.office.common.utils.IPUtils;
import io.office.modules.manage.dao.LogMessageDao;
import io.office.modules.manage.dao.TimescodeRegiIpDao; import io.office.modules.manage.dao.TimescodeRegiIpDao;
import io.office.modules.manage.entity.LogMessageEntity;
import io.office.modules.manage.entity.TimescodeRegiIpEntity; import io.office.modules.manage.entity.TimescodeRegiIpEntity;
import io.office.modules.manage.service.LogMessageService;
import io.office.modules.manage.utils.DateUtils; import io.office.modules.manage.utils.DateUtils;
import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.PostMethod;
...@@ -15,7 +18,10 @@ import org.dom4j.Node; ...@@ -15,7 +18,10 @@ import org.dom4j.Node;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
...@@ -38,6 +44,10 @@ public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, Time ...@@ -38,6 +44,10 @@ public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, Time
@Autowired @Autowired
private TimescodeRegiIpDao timescodeRegiIpDao; private TimescodeRegiIpDao timescodeRegiIpDao;
@Autowired
private LogMessageDao logMessageDao;
@Override @Override
public PageUtils queryPage(Map<String, Object> params) { public PageUtils queryPage(Map<String, Object> params) {
IPage<TimescodeRegiEntity> page = this.page( IPage<TimescodeRegiEntity> page = this.page(
...@@ -48,16 +58,39 @@ public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, Time ...@@ -48,16 +58,39 @@ public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, Time
return new PageUtils(page); return new PageUtils(page);
} }
/**
* 验证手机号 由于号码段不断的更新,只需要判断手机号有11位,并且全是数字以及1开头等
* @param phoneNumber 手机号码
* @return
*/
private static boolean matchPhoneNumber(String phoneNumber) {
String regex = "1[358][0-9]{9}";
if(phoneNumber==null||phoneNumber.length()<=0){
return false;
}
return Pattern.matches(regex, phoneNumber);
}
@Override @Override
public String getVerificationCode(Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) { public String getVerificationCode(Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
//判断手机号码格式
//判断手机号码是否在黑名单内
//验证码随机6位数字(验证码) //验证码随机6位数字(验证码)
String pass=(int)((Math.random()*9+1)*100000)+""; String pass=(int)((Math.random()*9+1)*100000)+"";
//获取IP(可能不是真实IP) //获取IP(可能不是真实IP)
String ip = IPUtils.getIpAddr(request); String ip = IPUtils.getIpAddr(request);
//手机号 //手机号
String phone = String.valueOf(params.get("phone")==null?"":params.get("phone")); String phone = String.valueOf(params.get("phone")==null?"":params.get("phone"));
String userName = String.valueOf(params.get("userName")==null?"":params.get("userName"));
//判断手机号码格式
if (!matchPhoneNumber(phone)){
throw new RRException("手机格式不正确,请重新填写");
}
//TODO tgl 判断手机号码是否在黑名单内
//发送状态
String state = "";
// 以下添加同一个手机号短信发送次数验证 // 以下添加同一个手机号短信发送次数验证
int flag = 1; int flag = 1;
TimescodeRegiEntity timescodeRegiEntity = timescodeRegiDao.selectById(phone); TimescodeRegiEntity timescodeRegiEntity = timescodeRegiDao.selectById(phone);
...@@ -137,17 +170,6 @@ public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, Time ...@@ -137,17 +170,6 @@ public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, Time
String userid = "9220"; // 此处按接口文档填写 String userid = "9220"; // 此处按接口文档填写
String content = "您在中国物品编码中心网站注册的手机验证码为:" + pass + ",五分钟内有效,请正确输入!同一手机号每日最多操作三次!同一个ip每日最多操作十次!【中国物品编码中心】"; String content = "您在中国物品编码中心网站注册的手机验证码为:" + pass + ",五分钟内有效,请正确输入!同一手机号每日最多操作三次!同一个ip每日最多操作十次!【中国物品编码中心】";
try{ try{
/*Map<String, String> params_aspx = new HashMap<>();
params.put("action", "send");
params.put("userid", userid);
params.put("account",account);
params.put("password",password);
params.put("mobile",phone);
params.put("content",content);
params.put("sendTime",DateUtils.getDay());
params.put("mobilenumber","2");
params.put("countnumber","2");
params.put("telephonenumber","0");*/
NameValuePair[] data = { NameValuePair[] data = {
new NameValuePair("action", "send"), new NameValuePair("action", "send"),
new NameValuePair("userid", userid), new NameValuePair("userid", userid),
...@@ -166,15 +188,22 @@ public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, Time ...@@ -166,15 +188,22 @@ public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, Time
// 获取returnstatus节点对象 // 获取returnstatus节点对象
String returnstatus = document.selectSingleNode("//returnstatus").getText(); String returnstatus = document.selectSingleNode("//returnstatus").getText();
if (returnstatus.equals("Success")){ if (returnstatus.equals("Success")){
//保存验证码信息log_message表
state="发送成功";
return "Success"; return "Success";
}else if (returnstatus.equals("Faild")){ }else if (returnstatus.equals("Faild")){
state="发送失败";
throw new RRException("验证码发送失败:"+ document.selectSingleNode("//message").getText()); throw new RRException("验证码发送失败:"+ document.selectSingleNode("//message").getText());
}else { }else {
state="发送失败";
throw new RRException("验证码发送失败,请稍后再试!"); throw new RRException("验证码发送失败,请稍后再试!");
} }
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();
throw new RRException("网络错误,无法连接到服务器!"); throw new RRException("网络错误,无法连接到服务器!");
}finally {
logMessageDao.insert(new LogMessageEntity(userName,phone,new Date(),pass,ip,
request.getRequestURL().toString(),"注册新用户",state));
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment