Commit f1662b05 by 唐功亮

【新增】 发送验证码

parent 989ae640
...@@ -252,6 +252,19 @@ ...@@ -252,6 +252,19 @@
<version>1.1.2</version> <version>1.1.2</version>
</dependency> </dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -3,6 +3,8 @@ package io.office.modules.manage.dao; ...@@ -3,6 +3,8 @@ package io.office.modules.manage.dao;
import io.office.modules.manage.entity.TimescodeRegiEntity; import io.office.modules.manage.entity.TimescodeRegiEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
/** /**
* ${comments} * ${comments}
...@@ -13,5 +15,7 @@ import org.apache.ibatis.annotations.Mapper; ...@@ -13,5 +15,7 @@ import org.apache.ibatis.annotations.Mapper;
*/ */
@Mapper @Mapper
public interface TimescodeRegiDao extends BaseMapper<TimescodeRegiEntity> { public interface TimescodeRegiDao extends BaseMapper<TimescodeRegiEntity> {
@Select("select * from timescode_regi where phone=#{phone} ")
TimescodeRegiEntity findByPhone(@Param("phone") String phone);
} }
...@@ -3,6 +3,8 @@ package io.office.modules.manage.dao; ...@@ -3,6 +3,8 @@ package io.office.modules.manage.dao;
import io.office.modules.manage.entity.TimescodeRegiIpEntity; import io.office.modules.manage.entity.TimescodeRegiIpEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
/** /**
* ${comments} * ${comments}
...@@ -13,5 +15,7 @@ import org.apache.ibatis.annotations.Mapper; ...@@ -13,5 +15,7 @@ import org.apache.ibatis.annotations.Mapper;
*/ */
@Mapper @Mapper
public interface TimescodeRegiIpDao extends BaseMapper<TimescodeRegiIpEntity> { public interface TimescodeRegiIpDao extends BaseMapper<TimescodeRegiIpEntity> {
@Select("select * form timescode_regi_ip where ip = #{ip} ")
public TimescodeRegiIpEntity findByIp(@Param("ip") String ip);
} }
package io.office.modules.manage.entity; package io.office.modules.manage.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
import lombok.Data; import lombok.Data;
import org.springframework.data.annotation.Id;
/** /**
* 验证码实体 * 验证码实体
...@@ -22,7 +24,7 @@ public class TimescodeRegiEntity implements Serializable { ...@@ -22,7 +24,7 @@ public class TimescodeRegiEntity implements Serializable {
/** /**
* 手机号 * 手机号
*/ */
@TableId(type = IdType.INPUT)
private String phone; private String phone;
/** /**
* 时间 * 时间
......
package io.office.modules.manage.entity; package io.office.modules.manage.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
...@@ -22,7 +23,7 @@ public class TimescodeRegiIpEntity implements Serializable { ...@@ -22,7 +23,7 @@ public class TimescodeRegiIpEntity implements Serializable {
/** /**
* $column.comments * $column.comments
*/ */
@TableId @TableId(type = IdType.INPUT)
private String ip; private String ip;
/** /**
* $column.comments * $column.comments
......
package io.office.modules.manage.service.impl; package io.office.modules.manage.service.impl;
import cn.hutool.log.Log;
import com.alibaba.fastjson.JSON; 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.modules.manage.dao.TimescodeRegiIpDao; import io.office.modules.manage.dao.TimescodeRegiIpDao;
import io.office.modules.manage.entity.TimescodeRegiIpEntity; import io.office.modules.manage.entity.TimescodeRegiIpEntity;
import io.office.modules.manage.utils.DateUtils; import io.office.modules.manage.utils.DateUtils;
import org.joda.time.DateTime; import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
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.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
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;
...@@ -26,9 +25,7 @@ import io.office.common.utils.Query; ...@@ -26,9 +25,7 @@ import io.office.common.utils.Query;
import io.office.modules.manage.dao.TimescodeRegiDao; import io.office.modules.manage.dao.TimescodeRegiDao;
import io.office.modules.manage.entity.TimescodeRegiEntity; import io.office.modules.manage.entity.TimescodeRegiEntity;
import io.office.modules.manage.service.TimescodeRegiService; 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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
...@@ -51,17 +48,14 @@ public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, Time ...@@ -51,17 +48,14 @@ public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, Time
return new PageUtils(page); return new PageUtils(page);
} }
public static void main(String[] args) {
System.out.println((int)((Math.random()*9+1)*100000)+"");
}
@Override @Override
public String getVerificationCode(Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) { public String getVerificationCode(Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
//判断手机号码格式 //判断手机号码格式
//判断手机号码是否在黑名单内 //判断手机号码是否在黑名单内
//TODO tgl 验证码随机6位数字(验证码) //验证码随机6位数字(验证码)
String pass=(int)((Math.random()*9+1)*100000)+""; String pass=(int)((Math.random()*9+1)*100000)+"";
//获取IP(可能不是真实IP) //获取IP(可能不是真实IP)
String ip = request.getRemoteAddr(); 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"));
// 以下添加同一个手机号短信发送次数验证 // 以下添加同一个手机号短信发送次数验证
...@@ -70,7 +64,7 @@ public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, Time ...@@ -70,7 +64,7 @@ public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, Time
if (timescodeRegiEntity!=null){ // 该手机号码是否发送过短信 if (timescodeRegiEntity!=null){ // 该手机号码是否发送过短信
Integer times = timescodeRegiEntity.getTimes(); Integer times = timescodeRegiEntity.getTimes();
Integer times_validate = timescodeRegiEntity.getTimesValidate(); Integer times_validate = timescodeRegiEntity.getTimesValidate();
String date = String.valueOf(params.get("date")); String date = timescodeRegiEntity.getDate();
String date_1 = DateUtils.getDay(); String date_1 = DateUtils.getDay();
if (date_1.equals(date)){ if (date_1.equals(date)){
times = times + 1; times = times + 1;
...@@ -95,12 +89,7 @@ public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, Time ...@@ -95,12 +89,7 @@ public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, Time
int times = 1; int times = 1;
int times_validate = 0; int times_validate = 0;
//新增数据 //新增数据
TimescodeRegiEntity timescodeRegiEntity1 = new TimescodeRegiEntity(); timescodeRegiDao.insert(new TimescodeRegiEntity(phone,date,times,times_validate));
timescodeRegiEntity1.setPhone("phone");
timescodeRegiEntity1.setDate("date");
timescodeRegiEntity1.setTimes(times);
timescodeRegiEntity1.setTimesValidate(times_validate);
timescodeRegiDao.insert(timescodeRegiEntity1);
flag = 1; flag = 1;
} }
...@@ -126,6 +115,7 @@ public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, Time ...@@ -126,6 +115,7 @@ public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, Time
}else { }else {
times_ip = 1; times_ip = 1;
date = date_1; date = date_1;
timescodeRegiIpDao.updateById(new TimescodeRegiIpEntity(ip,date,times_ip));
flag_ip = 1; flag_ip = 1;
} }
}else { }else {
...@@ -147,7 +137,7 @@ public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, Time ...@@ -147,7 +137,7 @@ 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<>(); /*Map<String, String> params_aspx = new HashMap<>();
params.put("action", "send"); params.put("action", "send");
params.put("userid", userid); params.put("userid", userid);
params.put("account",account); params.put("account",account);
...@@ -157,18 +147,33 @@ public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, Time ...@@ -157,18 +147,33 @@ public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, Time
params.put("sendTime",DateUtils.getDay()); params.put("sendTime",DateUtils.getDay());
params.put("mobilenumber","2"); params.put("mobilenumber","2");
params.put("countnumber","2"); params.put("countnumber","2");
params.put("telephonenumber","0"); 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"); NameValuePair[] data = {
JSONObject jsonObject = JSON.parseObject(post); new NameValuePair("action", "send"),
String returnstatus = jsonObject.getString("returnstatus"); new NameValuePair("userid", userid),
new NameValuePair("account", account),
new NameValuePair("password", password),
new NameValuePair("mobile", phone),
new NameValuePair("content", content),
new NameValuePair("sendTime", DateUtils.getDay()),
new NameValuePair("mobilenumber", "2"),
new NameValuePair("countnumber", "2"),
new NameValuePair("telephonenumber", "0"),
};
String post = sendPost("http://39.106.204.178:8888/sms.aspx", data);
Document document = DocumentHelper.parseText(post);
// 获取returnstatus节点对象
String returnstatus = document.selectSingleNode("//returnstatus").getText();
if (returnstatus.equals("Success")){ if (returnstatus.equals("Success")){
return "Success"; return "Success";
}else if (returnstatus.equals("Faild")){ }else if (returnstatus.equals("Faild")){
throw new RRException("验证码发送失败:"+ jsonObject.getString("message")); throw new RRException("验证码发送失败:"+ document.selectSingleNode("//message").getText());
}else { }else {
throw new RRException("验证码发送失败,请稍后再试!"); throw new RRException("验证码发送失败,请稍后再试!");
} }
}catch (Exception e){ }catch (Exception e){
e.printStackTrace();
throw new RRException("网络错误,无法连接到服务器!"); throw new RRException("网络错误,无法连接到服务器!");
} }
} }
...@@ -180,101 +185,19 @@ public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, Time ...@@ -180,101 +185,19 @@ public class TimescodeRegiServiceImpl extends ServiceImpl<TimescodeRegiDao, Time
* @param urls * @param urls
* @return * @return
*/ */
public static String sendPost(String urls, String param, String contentType, String method) { public static String sendPost(String urls, NameValuePair[] data ) {
StringBuffer sb=new StringBuffer();
DataOutputStream out = null;
BufferedReader responseReader = null;
InputStream in1 = null;
try { try {
// 创建url资源 PostMethod postMethod = null;
URL url = new URL(urls); postMethod = new PostMethod(urls) ;
// 建立http连接 postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ;
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); //参数设置,需要注意的就是里边不能传NULL,要传空字符串
// 设置不用缓存 postMethod.setRequestBody(data);
conn.setUseCaches(false); org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
// 设置允许输出 int response = httpClient.executeMethod(postMethod); // 执行POST方法
conn.setDoOutput(true); String result = postMethod.getResponseBodyAsString() ;
// 设置允许输入 return result;
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) { } catch (Exception e) {
throw new RuntimeException(e.getMessage());
} 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
<?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="io.office.modules.manage.dao.TimescodeRegiIpDao">
<!-- 可根据自己的需求,是否要使用 -->
<resultMap type="io.office.modules.manage.entity.TimescodeRegiIpEntity" id="timescodeRegiIpMap">
<result property="ip" column="ip"/>
<result property="date" column="date"/>
<result property="timesIp" column="times_ip"/>
</resultMap>
</mapper>
\ 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