【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代码:

<div class="form_group">
    <input type="text" name="smscode" id="smscode" class="code_pwd">
    <div class="input_tip">手机验证码</div>
    <a href="javascript:;" class="get_code" onclick="sendSMSCode()">点击获取验证码</a>
    <div id="register-sms-code-err" class="error_tip">验证码不能为空</div>
</div>

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);
            }
        }
    });
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值