栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【Flask项目】短信验证码的发送 # 7

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Flask项目】短信验证码的发送 # 7

第三方平台: 文档与下载:

云通讯

从中可以查看文档以获得帮助

点此下载云通讯SDK

配置接入信息:

(注册账号后,默认会赠送8大洋)

在info文件夹下新建libs目录,将解压得到的文件夹直接拖入libs目录即可。

 

 

配置sms.py文件中的三条信息:

 这三条信息可以从你注册的账号的管理台中看到:

依次对应复制粘贴填入即可。 

基本使用:
from info.libs.yuntongxun.sms import CCP

sms_res = CCP().send_template_sms("手机号", ["验证码", 时长], 模板)

# sms_res = CCP().send_template_sms("18011111111", ["2028", 5], 1)
效果:

 

发送短信的基本使用: 接口设计:

后端Flask代码:
import random
import re
from flask import request, jsonify, current_app
# from info.libs.yuntongxun.sms import CCP
from info import sr, constants
from info.response_code import RET
from . import blue_passport

@blue_passport.route("/sms_code", methods=["GET", "POST"])
def sms_code():
    """
        短信验证码
        1.接收参数(image_code_uuid, mobile, image_code)
        2.校验参数(image_code_uuid, mobile, image_code)
            2.1 是否都存在
            2.2 tel_number是否合法
            2.3 redis中是否存在image_code_uuid
            2.4 校验用户输入的图形验证码是否正确
        3.生成短信验证码(random)
        4.保存短信验证码到redis
        5.发送短信验证码
        6.返回结果
    :return:
    """
    # 1.接收参数(image_code_uuid, mobile, image_code)
    image_code_uuid = request.json.get("image_code_uuid")
    mobile = request.json.get("mobile")
    image_code = request.json.get("image_code")

    # 2.校验参数(image_code_uuid, mobile, image_code)
        # 2.1 是否都存在
    if not all([image_code_uuid, mobile, image_code]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数缺失")
        # 2.2 tel_number是否合法
    if not re.findall("^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])d{8}$", mobile.strip()):
        return jsonify(errno=RET.PARAMERR, errmsg="手机号有误")
        # 2.3 redis中是否存在image_code_uuid
    try:
        imageCode = sr.get("imageCodeUUID:"+image_code_uuid)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据库连接失败")
    if not sr:
        return jsonify(errno=RET.NODATA, errmsg="获取imageCodeUUID失败")
        # 2.4 校验用户输入的图形验证码是否正确
    if image_code != imageCode:
        return jsonify(errno=RET.PARAMERR, errmsg="验证码不正确")

    # 3.生成短信验证码(random)
    smsCode = "%04d" % random.randint(0, 9999)
    print("短信验证码:"+smsCode)

    # 4.保存短信验证码到redis
    try:
        sr.set("smsCodeTel:"+mobile, smsCode, ex=constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据库连接失败")

    # 5.发送短信验证码
    # try:
    #     sms_res = CCP().send_template_sms("18011111111", [smsCode, constants.SMS_CODE_REDIS_EXPIRES/60], 1)
    # except Exception as e:
    #     current_app.logger.error(e)
    #     return jsonify(errno=RET.THIRDERR, errmsg="短信发送失败")
    sms_res = 0
    if sms_res != 0:
        return jsonify(errno=RET.THIRDERR, errmsg="短信发送失败")

    # 6.返回结果
    return jsonify(errno=RET.OK, errmsg="短信发送成功")
HTML代码:
    
    手机验证码
    点击获取验证码
    验证码不能为空
Js代码:
// 发送短信验证码
function sendSMSCode() {
    // 校验参数,保证输入框有数据填写
    $(".get_code").removeAttr("onclick");
    var mobile = $("#register_mobile").val();
    if (!mobile) {
        $("#register-mobile-err").html("请填写正确的手机号!");
        $("#register-mobile-err").show();
        $(".get_code").attr("onclick", "sendSMSCode();");
        return;
    }
    var imageCode = $("#imagecode").val();
    if (!imageCode) {
        $("#image-code-err").html("请填写验证码!");
        $("#image-code-err").show();
        $(".get_code").attr("onclick", "sendSMSCode();");
        return;
    }

    var params = {
        'mobile':mobile,
        'image_code':imageCode,
        'image_code_uuid':imageCodeId
    };

    // TODO 发送短信验证码
    $.ajax({
        url:'/passport/sms_code',   // 请求地址
        type:'post',                // 请求方法
        data:JSON.stringify(params),// 请求参数
        contentType:'application/json',// 数据类型
        success:function (response) {  // 回调函数
            if (response.errno == '0') {
                // 发送短信验证码成功
                alert(response.errmsg);
            } else {
                alert(response.errmsg);
            }
        }
    });
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/323792.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号