Commit 2f32a5fb by 唐功亮

获取验证码

parent 1f1019fa
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.TimescodeRegiEntity;
import io.office.modules.manage.service.TimescodeRegiService;
import io.office.common.utils.PageUtils;
import io.office.common.utils.R;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 验证码获取
*
* @author tgl
* @email
* @date 2021-12-07 16:52:34
*/
@RestController
@RequestMapping("/timescoderegi")
public class TimescodeRegiController {
@Autowired
private TimescodeRegiService timescodeRegiService;
/**
* 获取验证码
*/
@RequestMapping("/api/getVerificationCode")
// @RequiresPermissions("manage:timescoderegi:list")
public R getVerificationCode(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response){
String msg = timescodeRegiService.getVerificationCode(params,request,response);
return R.ok();
}
/**
* 列表
*/
@RequestMapping("/list")
// @RequiresPermissions("manage:timescoderegi:list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = timescodeRegiService.queryPage(params);
return R.ok().put("page", page);
}
/**
* 信息
*/
@RequestMapping("/info/{phone}")
// @RequiresPermissions("manage:timescoderegi:info")
public R info(@PathVariable("phone") String phone){
TimescodeRegiEntity timescodeRegi = timescodeRegiService.getById(phone);
return R.ok().put("timescodeRegi", timescodeRegi);
}
/**
* 保存
*/
@RequestMapping("/save")
// @RequiresPermissions("manage:timescoderegi:save")
public R save(@RequestBody TimescodeRegiEntity timescodeRegi){
timescodeRegiService.save(timescodeRegi);
return R.ok();
}
/**
* 修改
*/
@RequestMapping("/update")
// @RequiresPermissions("manage:timescoderegi:update")
public R update(@RequestBody TimescodeRegiEntity timescodeRegi){
timescodeRegiService.updateById(timescodeRegi);
return R.ok();
}
/**
* 删除
*/
@RequestMapping("/delete")
// @RequiresPermissions("manage:timescoderegi:delete")
public R delete(@RequestBody String[] phones){
timescodeRegiService.removeByIds(Arrays.asList(phones));
return R.ok();
}
}
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.TimescodeRegiIpEntity;
import io.office.modules.manage.service.TimescodeRegiIpService;
import io.office.common.utils.PageUtils;
import io.office.common.utils.R;
/**
* ${comments}
*
* @author wudi
* @email
* @date 2021-12-07 17:34:57
*/
@RestController
@RequestMapping("/timescoderegiip")
public class TimescodeRegiIpController {
@Autowired
private TimescodeRegiIpService timescodeRegiIpService;
/**
* 列表
*/
@RequestMapping("/list")
// @RequiresPermissions("manage:timescoderegiip:list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = timescodeRegiIpService.queryPage(params);
return R.ok().put("page", page);
}
/**
* 信息
*/
@RequestMapping("/info/{ip}")
// @RequiresPermissions("manage:timescoderegiip:info")
public R info(@PathVariable("ip") String ip){
TimescodeRegiIpEntity timescodeRegiIp = timescodeRegiIpService.getById(ip);
return R.ok().put("timescodeRegiIp", timescodeRegiIp);
}
/**
* 保存
*/
@RequestMapping("/save")
// @RequiresPermissions("manage:timescoderegiip:save")
public R save(@RequestBody TimescodeRegiIpEntity timescodeRegiIp){
timescodeRegiIpService.save(timescodeRegiIp);
return R.ok();
}
/**
* 修改
*/
@RequestMapping("/update")
// @RequiresPermissions("manage:timescoderegiip:update")
public R update(@RequestBody TimescodeRegiIpEntity timescodeRegiIp){
timescodeRegiIpService.updateById(timescodeRegiIp);
return R.ok();
}
/**
* 删除
*/
@RequestMapping("/delete")
// @RequiresPermissions("manage:timescoderegiip:delete")
public R delete(@RequestBody String[] ips){
timescodeRegiIpService.removeByIds(Arrays.asList(ips));
return R.ok();
}
}
package io.office.modules.manage.dao;
import io.office.modules.manage.entity.TimescodeRegiEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* ${comments}
*
* @author wudi
* @email
* @date 2021-12-07 16:52:34
*/
@Mapper
public interface TimescodeRegiDao extends BaseMapper<TimescodeRegiEntity> {
}
package io.office.modules.manage.dao;
import io.office.modules.manage.entity.TimescodeRegiIpEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* ${comments}
*
* @author wudi
* @email
* @date 2021-12-07 17:34:57
*/
@Mapper
public interface TimescodeRegiIpDao extends BaseMapper<TimescodeRegiIpEntity> {
}
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;
/**
* 验证码实体
*
* @author tgl
* @email
* @date 2021-12-07 16:52:34
*/
@Data
@TableName("timescode_regi")
public class TimescodeRegiEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 手机号
*/
@TableId
private String phone;
/**
* 时间
*/
private String date;
/**
*获取短信验证码次数
*/
private Integer times;
/**
* 输入短信验证码验证次数
*/
private Integer timesValidate;
public TimescodeRegiEntity() {
}
public TimescodeRegiEntity(String phone, String date, Integer times, Integer timesValidate) {
this.phone = phone;
this.date = date;
this.times = times;
this.timesValidate = timesValidate;
}
}
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-07 17:34:57
*/
@Data
@TableName("timescode_regi_ip")
public class TimescodeRegiIpEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* $column.comments
*/
@TableId
private String ip;
/**
* $column.comments
*/
private String date;
/**
* $column.comments
*/
private Integer timesIp;
public TimescodeRegiIpEntity(String ip, String date, Integer timesIp) {
this.ip = ip;
this.date = date;
this.timesIp = timesIp;
}
}
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.TimescodeRegiIpEntity;
import java.util.Map;
/**
* ${comments}
*
* @author wudi
* @email
* @date 2021-12-07 17:34:57
*/
public interface TimescodeRegiIpService extends IService<TimescodeRegiIpEntity> {
PageUtils queryPage(Map<String, Object> params);
}
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.TimescodeRegiEntity;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
/**
* ${comments}
*
* @author tgl
* @email
* @date 2021-12-07 16:52:34
*/
public interface TimescodeRegiService extends IService<TimescodeRegiEntity> {
PageUtils queryPage(Map<String, Object> params);
String getVerificationCode(Map<String, Object> params, HttpServletRequest request, HttpServletResponse response);
}
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.TimescodeRegiIpDao;
import io.office.modules.manage.entity.TimescodeRegiIpEntity;
import io.office.modules.manage.service.TimescodeRegiIpService;
@Service("timescodeRegiIpService")
public class TimescodeRegiIpServiceImpl extends ServiceImpl<TimescodeRegiIpDao, TimescodeRegiIpEntity> implements TimescodeRegiIpService {
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<TimescodeRegiIpEntity> page = this.page(
new Query<TimescodeRegiIpEntity>().getPage(params),
new QueryWrapper<TimescodeRegiIpEntity>()
);
return new PageUtils(page);
}
}
\ No newline at end of file
package io.office.modules.manage.service.impl;
import cn.hutool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.office.common.exception.RRException;
import io.office.modules.manage.dao.TimescodeRegiIpDao;
import io.office.modules.manage.entity.TimescodeRegiIpEntity;
import io.office.modules.manage.utils.DateUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
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.TimescodeRegiDao;
import io.office.modules.manage.entity.TimescodeRegiEntity;
import io.office.modules.manage.service.TimescodeRegiService;
import org.springframework.util.StringUtils;
import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Service("timescodeRegiService")
public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, TimescodeRegiEntity> implements TimescodeRegiService {
@Autowired
private TimescodeRegiDao timescodeRegiDao;
@Autowired
private TimescodeRegiIpDao timescodeRegiIpDao;
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<TimescodeRegiEntity> page = this.page(
new Query<TimescodeRegiEntity>().getPage(params),
new QueryWrapper<TimescodeRegiEntity>()
);
return new PageUtils(page);
}
public static void main(String[] args) {
System.out.println((int)((Math.random()*9+1)*100000)+"");
}
@Override
public String getVerificationCode(Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
//判断手机号码格式
//判断手机号码是否在黑名单内
//TODO tgl 验证码随机6位数字(验证码)
String pass=(int)((Math.random()*9+1)*100000)+"";
//获取IP(可能不是真实IP)
String ip = request.getRemoteAddr();
//手机号
String phone = String.valueOf(params.get("phone")==null?"":params.get("phone"));
// 以下添加同一个手机号短信发送次数验证
int flag = 1;
TimescodeRegiEntity timescodeRegiEntity = timescodeRegiDao.selectById(phone);
if (timescodeRegiEntity!=null){ // 该手机号码是否发送过短信
Integer times = timescodeRegiEntity.getTimes();
Integer times_validate = timescodeRegiEntity.getTimesValidate();
String date = String.valueOf(params.get("date"));
String date_1 = DateUtils.getDay();
if (date_1.equals(date)){
times = times + 1;
date = date_1;
//更新数据
timescodeRegiDao.updateById(new TimescodeRegiEntity(phone,date,times,times_validate));
if (times <= 3){
flag = 1;
}else{
flag = 0;
}
}else{
//不是同一天修改 数据并初始化数据
times = 1;
date = date_1;
timescodeRegiDao.updateById(new TimescodeRegiEntity(phone,date,times,times_validate));
flag = 1;
}
}else {
//没有手机号,新增数据
String date = DateUtils.getDay();
int times = 1;
int times_validate = 0;
//新增数据
TimescodeRegiEntity timescodeRegiEntity1 = new TimescodeRegiEntity();
timescodeRegiEntity1.setPhone("phone");
timescodeRegiEntity1.setDate("date");
timescodeRegiEntity1.setTimes(times);
timescodeRegiEntity1.setTimesValidate(times_validate);
timescodeRegiDao.insert(timescodeRegiEntity1);
flag = 1;
}
// 同一个手机号短信发送次数验证end
// 以下添加同一个ip短信发送次数验证
int flag_ip = 1;
TimescodeRegiIpEntity timescodeRegiIpEntity = timescodeRegiIpDao.selectById(ip);
if (timescodeRegiIpEntity!=null){
Integer times_ip = timescodeRegiIpEntity.getTimesIp();
String date = timescodeRegiIpEntity.getDate();
String date_1 = DateUtils.getDay();
if (date_1.equals(date)){
times_ip = times_ip + 1;
date = date_1;
timescodeRegiIpDao.updateById(new TimescodeRegiIpEntity(ip,date,times_ip));
if (times_ip <= 10){
flag_ip = 1;
}else{
flag_ip = 0;
}
}else {
times_ip = 1;
date = date_1;
flag_ip = 1;
}
}else {
String date = DateUtils.getDay();
int times_ip = 1;
timescodeRegiIpDao.insert(new TimescodeRegiIpEntity(ip, date, times_ip));
flag_ip = 1;
}
// 同一个ip短信发送次数验证end
if (flag == 0){
throw new RRException("同一手机号每日最多操作三次!");
}else if(flag_ip == 0){
throw new RRException("同一ip每日最多操作十次!");
} else { //flag=1时 ,发送短信
// //********* 以下调用手机发短信接口 ********* //
String res = "res";
String account = "gs1cn"; // 此处按接口文档填写
String password = "test@2021"; // 此处按接口文档填写
String userid = "9220"; // 此处按接口文档填写
String content = "您在中国物品编码中心网站注册的手机验证码为:" + pass + ",五分钟内有效,请正确输入!同一手机号每日最多操作三次!同一个ip每日最多操作十次!【中国物品编码中心】";
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");
String post = sendPost("http://39.106.204.178:8888/sms.aspx", map2Url(params_aspx), "application/x-www-form-urlencoded;charset=UTF-8", "POST");
JSONObject jsonObject = JSON.parseObject(post);
String returnstatus = jsonObject.getString("returnstatus");
if (returnstatus.equals("Success")){
return "Success";
}else if (returnstatus.equals("Faild")){
throw new RRException("验证码发送失败:"+ jsonObject.getString("message"));
}else {
throw new RRException("验证码发送失败,请稍后再试!");
}
}catch (Exception e){
throw new RRException("网络错误,无法连接到服务器!");
}
}
}
/**
* 发送post 数据
* @param urls
* @return
*/
public static String sendPost(String urls, String param, String contentType, String method) {
StringBuffer sb=new StringBuffer();
DataOutputStream out = null;
BufferedReader responseReader = null;
InputStream in1 = null;
try {
// 创建url资源
URL url = new URL(urls);
// 建立http连接
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
// 设置不用缓存
conn.setUseCaches(false);
// 设置允许输出
conn.setDoOutput(true);
// 设置允许输入
conn.setDoInput(true);
// 设置传递方式
conn.setRequestMethod(method);
System.out.println(conn.getRequestMethod());
// 设置维持长连接
conn.setRequestProperty("Connection", "Keep-Alive");
// 设置文件字符集:
conn.setRequestProperty("Charset", "UTF-8");
// 转换为字节数组
// byte[] data = (param).getBytes();
// // 设置文件长度
// conn.setRequestProperty("Content-Length", String.valueOf(data.length));
// 设置文件类型:
conn.setRequestProperty("Content-Type", contentType);
// 开始连接请求
conn.connect();
out = new DataOutputStream(conn.getOutputStream());
// 写入请求的字符串
out.writeBytes(param);
out.flush();
System.out.println(conn.getResponseCode());
// 请求返回的状态
if (HttpURLConnection.HTTP_OK == conn.getResponseCode()) {
System.out.println("连接成功");
// 请求返回的数据
in1 = conn.getInputStream();
String readLine;
responseReader = new BufferedReader(new InputStreamReader(in1,"UTF-8"));
while((readLine=responseReader.readLine()) != null){
sb.append(readLine).append("\n");
}
} else {
System.out.println("error++");
}
} catch (Exception e) {
} finally {
try {
if (null != responseReader)
responseReader.close();
if (null != in1)
in1.close();
} catch(Exception e) {}
try {
out.close();
} catch(Exception e) {}
}
return sb.toString();
}
/**
* map转url参数
*/
public static String map2Url(Map<String, String> paramToMap) {
if (null == paramToMap || paramToMap.isEmpty()) {
return null;
}
StringBuffer url = new StringBuffer();
boolean isfist = true;
for (Map.Entry<String, String> entry : paramToMap.entrySet()) {
if (isfist) {
isfist = false;
} else {
url.append("&");
}
url.append(entry.getKey()).append("=");
String value = entry.getValue();
if (!StringUtils.isEmpty(value)) {
try {
url.append(URLEncoder.encode(value, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
return url.toString();
}
}
\ No newline at end of file
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