Flask项目--发送短信验证码

1.后端代码

在这里插入图片描述具体代码如下:

# GET /api/v1.0/sms_codes/<mobile>?image_code=xxxx&image_code_id=xxxx
@api.route("/sms_codes/<re(r'1[34578]\d{9}'):mobile>")
def get_sms_code(mobile):"""获取短信验证码"""# 获取参数image_code = request.args.get("image_code")image_code_id = request.args.get("image_code_id")# 校验参数if not all([image_code_id, image_code]):# 表示参数不完整return jsonify(errno=RET.PARAMERR, errmsg="参数不完整")# 业务逻辑处理# 从redis中取出真实的图片验证码try:real_image_code = redis_store.get("image_code_%s" % image_code_id)except Exception as e:current_app.logger.error(e)return jsonify(errno=RET.DBERR, errmsg="redis数据库异常")# 判断图片验证码是否过期if real_image_code is None:# 表示图片验证码没有或者过期return jsonify(errno=RET.NODATA, errmsg="图片验证码失效")# 删除redis中的图片验证码,防止用户使用同一个图片验证码验证多次try:redis_store.delete("image_code_%s" % image_code_id)except Exception as e:current_app.logger.error(e)# 与用户填写的值进行对比if real_image_code.lower() != image_code.lower():# 表示用户填写错误return jsonify(errno=RET.DATAERR, errmsg="图片验证码错误")# 判断对于这个手机号的操作,在60秒内有没有之前的记录,如果有,则认为用户操作频繁,不接受处理try:send_flag = redis_store.get("send_sms_code_%s" % mobile)except Exception as e:current_app.logger.error(e)else:if send_flag is not None:# 表示在60秒内之前有过发送的记录return jsonify(errno=RET.REQERR, errmsg="请求过于频繁,请60秒后重试")# 判断手机号是否存在try:user = User.query.filter_by(mobile=mobile).first()except Exception as e:current_app.logger.error(e)else:if user is not None:# 表示手机号已存在return jsonify(errno=RET.DATAEXIST, errmsg="手机号已存在")# 如果手机号不存在,则生成短信验证码sms_code = "%06d" % random.randint(0, 999999)# 保存真实的短信验证码try:redis_store.setex("sms_code_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code)# 保存发送给这个手机号的记录,防止用户在60s内再次出发发送短信的操作redis_store.setex("send_sms_code_%s" % mobile, constants.SEND_SMS_CODE_INTERVAL, 1)except Exception as e:current_app.logger.error(e)return jsonify(errno=RET.DBERR, errmsg="保存短信验证码异常")# 发送短信# 使用celery异步发送短信, delay函数调用后立即返回(非阻塞)# send_sms.delay(mobile, [sms_code, int(constants.SMS_CODE_REDIS_EXPIRES/60)], 1)# 返回异步任务的对象result_obj = send_sms.delay(mobile, [sms_code, int(constants.SMS_CODE_REDIS_EXPIRES/60)], 1)print(result_obj.id)# 通过异步任务对象的get方法获取异步任务的结果, 默认get方法是阻塞的ret = result_obj.get()print("ret=%s" % ret)# 返回值# 发送成功return jsonify(errno=RET.OK, errmsg="发送成功")

2.前端html代码

在这里插入图片描述具体代码如下:

<!DOCTYPE html>
<html>
<head> <meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"><title>爱家-注册</title><link href="/static/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet"><link href="/static/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet"><link href="/static/css/reset.css" rel="stylesheet"><link href="/static/css/ihome/main.css" rel="stylesheet"><link href="/static/css/ihome/register.css" rel="stylesheet">
</head>
<body><div class="container"><div class="logo-bar"><a href="/"><img src="/static/images/logo@128x59.png"></a></div><form class="form-register"><div class="form-group form-group-lg"><div class="input-group"><div class="input-group-addon"><i class="fa fa-mobile fa-2x fa-fw"></i></div><input type="number" class="form-control" name="mobile" id="mobile" placeholder="手机号" required></div></div><div class="error-msg" id="mobile-err"><i class="fa fa-exclamation-circle"></i><span></span></div><div class="form-group form-group-lg"><div class="input-group"><div class="input-group-addon"><i class="fa fa-image fa-lg fa-fw"></i></div><input type="text" class="form-control" name="imagecode" id="imagecode" placeholder="图片验证码" required><div class="input-group-addon image-code" onclick="generateImageCode();"><img src=""></div></div></div><div class="error-msg" id="image-code-err"><i class="fa fa-exclamation-circle"></i><span></span></div><div class="form-group form-group-lg"><div class="input-group"><div class="input-group-addon"><i class="fa fa-envelope-o fa-lg fa-fw"></i></div><input type="text" class="form-control" name="phonecode" id="phonecode" placeholder="短信验证码" required><div class="phonecode input-group-addon"><a class="phonecode-a" href="javascript:;" onclick="sendSMSCode();">获取验证码</a></div></div></div><div class="error-msg" id="phone-code-err"><i class="fa fa-exclamation-circle"></i><span></span></div><div class="form-group form-group-lg"><div class="input-group"><div class="input-group-addon"><i class="fa fa-lock fa-lg fa-fw"></i></div><input type="password" class="form-control" name="password" id="password" placeholder="密码" required></div></div><div class="error-msg" id="password-err"><i class="fa fa-exclamation-circle"></i><span></span></div><div class="form-group form-group-lg"><div class="input-group"><div class="input-group-addon"><i class="fa fa-lock fa-lg fa-fw"></i></div><input type="password" class="form-control" name="password2" id="password2" placeholder="确认密码" required></div></div><div class="error-msg" id="password2-err"><i class="fa fa-exclamation-circle"></i><span></span></div><button type="submit" class="btn btn-lg btn-theme btn-block">立即注册</button><p class="login-a">已有账号,<a href="/login.html">立即登陆</a></p></form></div><script src="/static/js/jquery.min.js"></script><script src="/static/plugins/bootstrap/js/bootstrap.min.js"></script><script src="/static/js/ihome/register.js"></script>
</body>
</html>

3.前端js代码

在这里插入图片描述具体代码如下:

function sendSMSCode() {// 点击发送短信验证码后被执行的函数$(".phonecode-a").removeAttr("onclick");var mobile = $("#mobile").val();if (!mobile) {$("#mobile-err span").html("请填写正确的手机号!");$("#mobile-err").show();$(".phonecode-a").attr("onclick", "sendSMSCode();");return;} var imageCode = $("#imagecode").val();if (!imageCode) {$("#image-code-err span").html("请填写验证码!");$("#image-code-err").show();$(".phonecode-a").attr("onclick", "sendSMSCode();");return;}// 构造向后端请求的参数var req_data = {image_code: imageCode, // 图片验证码的值image_code_id: imageCodeId // 图片验证码的编号,(全局变量)};// 向后端发送请求$.get("/api/v1.0/sms_codes/"+ mobile, req_data, function (resp) {// resp是后端返回的响应值,因为后端返回的是json字符串,// 所以ajax帮助我们把这个json字符串转换为js对象,resp就是转换后对象if (resp.errno == "0") {var num = 60;// 表示发送成功var timer = setInterval(function () {if (num >= 1) {// 修改倒计时文本$(".phonecode-a").html(num + "秒");num -= 1;} else {$(".phonecode-a").html("获取验证码");$(".phonecode-a").attr("onclick", "sendSMSCode();");clearInterval(timer);}}, 1000, 60)} else {alert(resp.errmsg);$(".phonecode-a").attr("onclick", "sendSMSCode();");}});
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/452682.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Jenkins系列之五——通过Publish over SSH插件实现远程部署

Jenkins通过Publish over SSH插件实现远程部署 步凑一、配置ssh免秘钥登录 部署详情地址&#xff1a;http://www.cnblogs.com/Dev0ps/p/8259099.html 步凑二、安装Publish over SSH插件 插…

Java高级应用开发之Servlet

学习路径&#xff1a; 1.Servlet简介 2.Servlet基础 3.表单处理 4.请求头信息 5.响应头信息 6.cookie 7.session 8.scope: Servlet Context 全局变量 Http Session 会话变量 Http Servlet Request 请求变量 9.Filter Filter是一种特殊的Servlet&#xff0c;其核心函数doFilter(…

typedef 数组使用详解

typedef到处都是&#xff0c;但是能够真正懂得typedef使用的不算太多。对于初学者而言&#xff0c;看别人的源码时对到处充斥的typedef往往不知所错&#xff0c;而参考书又很少&#xff0c;所以在此给出一个源码&#xff0c;供大家参考。 懂得这些&#xff0c;基本上是 对typed…

php柱状图实现年龄分布,考官雅思写作小作文满分范文 柱状图:年龄分布

考官雅思写作小作文满分范文 柱状图:年龄分布2017年06月12日14:48 来源&#xff1a;小站教育作者&#xff1a;小站雅思编辑参与(40)阅读(63981)摘要&#xff1a;为大家分享前考官simon演示的9分雅思小作文。考官亲笔&#xff0c;用最正统的4段式写作&#xff0c;本文主题-柱状图…

Flask项目--注册

0.效果展示 1.后端代码 # coding:utf-8from . import api from flask import request, jsonify, current_app, session from ihome.utils.response_code import RET from ihome import redis_store, db, constants from ihome.models import User from sqlalchemy.exc import I…

图片处理

//图片处理public function img(){//读取图片$imageImage::open(./img/02.jpg);//dump($image);//获取图片的信息// 返回图片的宽度$width $image->width();// 返回图片的高度$height $image->height();// 返回图片的类型$type $image->type();// 返回图片的mime类…

react 项目总结

前言 最近在写一个项目,在写react的过程中遇到过很多坑,现在总结一下,为以后的项目作参考.另外借此项目来比较一下 vue.js 和 react.js 之间的一些异同. 先说说组件 刚刚开始写组件的时候&#xff0c;感觉难度不大&#xff08;跟vue差不多&#xff09;。最有意思的应该是jsx语法…

现代数字影视 电影使用标准

1、国际数字电影标准1&#xff09;DCI&#xff08;Digital Cinema Initiatives数字影院系统规范&#xff09;美国好莱坞七大制片公司——Disney、MGM、Fox、Paramount Pictures、Sony Pictures Entertainment、Universal Studios和Warner Bros于2002年联合成立了DCI机构&#x…

数据流图 系统流程图 程序流程图 系统结构图联系与区别

1.数据流图&#xff08;Data Flow Diagram&#xff09;&#xff0c;简称DFD&#xff0c;它从数据传递和加工角度&#xff0c;以图形方式来表达系统的逻辑功能、数据在系统内部的逻辑流向和逻辑变换过程&#xff0c;是结构化系统分析方法的主要表达工具及用于表示软件模型的一种…

Linux--安装yum源

linux配置yum源 一、修改yum的配置文件 /etc/yum.repos.d/xxx.repo 1、进入yum配置文件目录 # cd /etc/yum.repos.d 2、删除全部原有的文件 # rm -rf * 3、新建一个yum的配置文件 # vi my.repo [myrepo] 标识配置文件名称&#xff08;名字随意&#xff09; namemyrepo 标识yum …

在 Confluence 6 中禁用 workbox 应用通知

如果你选择 不提供应用通知&#xff08;does not provide in-app notifications&#xff09;&#xff1a; Confluence workbox 图标将不会可见同时用户也不能在这个服务器上访问 workbox。这个 Confluence 服务器将不会发送消息到 workbox 中&#xff0c;同时也不会发送消息到其…

迄今为止最快的 JSON 序列化工具 Jil

2019独角兽企业重金招聘Python工程师标准>>> 迄今为止最快的 JSON 序列化工具 Jil https://github.com/kevin-montrose/Jil 转载于:https://my.oschina.net/xainghu/blog/1621365

mysql数据库访问编程,mysql 连接数据库

1、首先启动mysql 并链接数据 小意思吧&#xff01;都会了是吧mysql -uroot -p //连接数据net start mysql // 启动mysql2、查询当前 服务器里有哪些数据show databases;3、创建数据库create database jddb -------数据库名字(jddb)4、 使用数据库use jddb;5、查询当前数据库下…

linux多线程 pthread用法

#include int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr, void *(*start_rtn)(void),void *restrict arg); Returns: 0 if OK, error number on failure 第一个参数为指向线程标识符的指针。 第二个参数用来设置线程属性。 第三个参数是…

什么是数据字典

数据字典是指对数据的数据项、数据结构、数据流、数据存储、处理逻辑、外部实体等进行定义和描述&#xff0c;其目的是对数据流程图中的各个元素做出详细的说明。 数据字典最重要的作用是作为分析阶段的工具。任何字典最重要的用途都是供人查询对不了解的条目的解释&#xff0c…

Flsak爱家租房--个人信息

0.页面展示效果 1.设置用户头像–后端代码 # coding:utf-8from . import api from ihome.utils.commons import login_required from flask import g, current_app, jsonify, request, session from ihome.utils.response_code import RET from ihome.utils.image_storage impo…

json返回值为null显示key值的设置

使用的是阿里的json ----------com.alibaba.fastjson.JSONObject; Map<String,Object> map new HashMap<String,Object>(); return JSONObject.toJSONString(map); --------转义的时候&#xff0c;map中值是null的字段会被忽略掉&#xff0c;转义的json没有带n…

C++ string::size_type

从逻辑上讲&#xff0c;size()成员函数应该似乎返回整型数值&#xff0c;但事实上&#xff0c;size操作返回是string::size_type类型的值。string类类型和其他许多库类型都定义了一些配套类型(companion type)。通过这些配套类型&#xff0c;库函数的使用就与机器无关(machine-…

需求分遵循的准则

•必须理解并描述问题的信息域&#xff0c;根据这条准则应该建立数据模型。 •必须定义软件应完成的功能&#xff0c;这条准则要求建立功能模型。 •必须描述作为外部事件结果的软件行为&#xff0c;这条准则要求建立行为模型 •必须对描述信息、功能和行为的模型进行分解&…

MATLAB显示错误使用untitled,新手,用gui界面画李萨如图,出错,求解答

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼function varargout untitled1(varargin)% UNTITLED1 MATLAB code for untitled1.fig% UNTITLED1, by itself, creates a new UNTITLED1 or raises the existing% singleton*.%% H UNTITLED1 returns the handle to a new UNTITL…