码迷,mamicode.com
首页 > 其他好文 > 详细

验证码

时间:2016-01-22 16:58:19      阅读:422      评论:0      收藏:0      [点我收藏+]

标签:

1.jsp

<div class="yycontent" id="yybtn">
					<span>验证码</span>
					<input type="text" id="scode" name="scode" style="width: 79%; border: 0; height: 3.5%; font-size: 0.75em; outline: none;" placeholder="请输入手机验证码">
						<input type="button" name="btnsendsms" id="btnsendsms" class="yybtn1" type="button" value="获取验证" onclick="headsendSms()" />
				</div>


<script type="text/javascript">
		var headInterValObj; //timer变量,控制时间
		var headcount = 60; //间隔函数,1秒执行
		var headcurCount;//当前剩余秒数
		
		//timer处理函数
		function headSetRemainTime() {
			
			if (headcurCount == 0) {
				window.clearInterval(headInterValObj);//停止计时器
				$("#btnsendsms").val("获取验证");//启用按钮
				$("#btnsendsms").attr("onclick", "headsendSms()");
			} else {
				headcurCount--;
				$("#btnsendsms").attr("onclick", "");
				$("#btnsendsms").val("重新发送(" + headcurCount + ")");
			}
		}

		function headsendSms() {
			
			if (trim($(‘#cellphone‘).val()) == "") {
				showmsg(0, "手机号错误。");
				return;
			}
			
			headcurCount = headcount;
			$("#btnsendsms").attr("onclick", "");
			headInterValObj = window.setInterval(headSetRemainTime, 1000); //启动计时器,1秒执行一次
			$.ajax({
				url : "${contextPath}/web/sendsms.htm",
				data : $("#headForm").serialize(),
				cache : false,
				success : function(xmlHttp) {
					var res = xmlHttp;
					if (res.errorMessage == ‘105‘) {
						showmsg(0, "操作过快,请等10s以后再操作。");
					} else if (res.errorMessage == ‘0‘) {
						showmsg(1, "验证码发送成功。");
					} else {
						showmsg(0, "获取验证码出错。");
					}
				}
			});
		}
	</script>

  2.controller

@RequestMapping("/sendsms.htm")
	@ResponseBody
	public Response sendsms(Model model, RequestParameter param) {
		Response res = new Response();

		SessionUser suser = null;
		if (httpSession.getAttribute(AoConstants.SESSION_USER_KEY) != null) {
			suser = new SessionUser();
			suser = (SessionUser) httpSession.getAttribute(AoConstants.SESSION_USER_KEY);
		}

		int code = (int) (Math.random() * 9000 + 1000);
		int ttcode = (int) (Math.random() * 9000 + 1000);
		
		String scode = String.valueOf(code);
		String tcode = String.valueOf(ttcode);
		
		String cellphone = "15267287105";
		//String scode = "1111";

		if (suser == null) {
			FenXiao fenXiao = new FenXiao();
			fenXiao.setCellphone(param.getCellphone());
			fenXiao.setScode(scode);
			fenXiao.setTcode(tcode);

			String key = AoConstants.SESSION_USER_KEY + "_" + param.getCellphone();
			if (httpSession.getAttribute(key) != null)
				httpSession.removeAttribute(key);
			httpSession.setAttribute(key, fenXiao);

			HttpSenderTest Esms = new HttpSenderTest();
			//res.setErrorMessage("0");
			res.setErrorMessage(Esms.sms_send(param.getCellphone(), scode, cellphone, tcode)); // 注册
		}

		return res;
	}


@RequestMapping("/register.htm")
	@ResponseBody
	public Response register(Model model, RequestParameter param) {
		Response res = new Response();

		String key = AoConstants.SESSION_USER_KEY + "_" + param.getCellphone();
		if (httpSession.getAttribute(key) == null) {
			res.setErrorMessage("验证码错误。");
			res.setResult(2);
			return res;
		}
		FenXiao fenXiao = (FenXiao) httpSession.getAttribute(key);
		if (StringUtils.isBlank(fenXiao.getScode()) || !fenXiao.getScode().equals(param.getScode())) {
			res.setErrorMessage("验证码错误。");
			res.setResult(2);
			return res;
		}
		
		if (StringUtils.isBlank(fenXiao.getTcode()) || !fenXiao.getTcode().equals(param.getTcode())) {
			res.setErrorMessage("推荐码错误。");
			res.setResult(2);
			return res;
		}
		// httpSession.removeAttribute(key); // 及时清理 等完善信息后 清理

		// #0: 正常;
		// #1: 手机号重复;
		Integer ret = 0;

		ret = webService.checkCellphone(fenXiao);
		if (ret == 1) {
			httpSession.removeAttribute(key); // 及时清理
			res.setErrorMessage("注册失败,手机号重复。");
			res.setResult(1);

		}
		/*
		 * else{
		 * res.setLlong(fenXiao.getId());
		 * }
		 */

		fenXiao.setPassword(param.getPassword());
		if (httpSession.getAttribute(key) != null)
			httpSession.removeAttribute(key);
		httpSession.setAttribute(key, fenXiao);

		return res;

	}

  3.java类  http.jar

import com.bcloud.msg.http.HttpSender;

public class HttpSenderTest {

	public String sms_send(String phonenum1, String scode, String phonenum2, String tcode) {
		String uri = "http://120.26.69.248/msg/HttpSendSM";//应用地址
		String account = "002006";//账号
		String pswd = "Sy123456";//密码
		String mobiles1 = phonenum1;//手机号码,多个号码使用","分割
		String content1 = "您好,您的验证码是:" + scode;//短信内容
		
		String mobiles2 = phonenum2;//手机号码,多个号码使用","分割
		String content2 = "您好,您的验证码是:" + tcode;//短信内容
		
		boolean needstatus = true;//是否需要状态报告,需要true,不需要false
		String product = "";//产品ID(不用填写)
		String extno = "";//扩展码(不用填写)
		
		String str = "";
		try {
			String returnString1 = HttpSender.send(uri, account, pswd, mobiles1, content1, needstatus, product, extno);
			//String returnString2 = HttpSender.send(uri, account, pswd, mobiles2, content2, needstatus, product, extno);
			//System.out.println(needstatus);
			System.out.println(returnString1);
			Integer in = returnString1.indexOf(",");
			Integer end = returnString1.indexOf("\n");
			str = returnString1.substring(in+1, end);
			//TODO 处理返回值,参见HTTP协议文档
		} catch (Exception e) {
			//TODO 处理异常
			e.printStackTrace();
		}
		return str;
	}
}

  

验证码

标签:

原文地址:http://www.cnblogs.com/wzz1020/p/5151259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!