示例#1
0
def send_sms_code():
    mobile = request.json.get('mobile')  # 手机号
    image_code = request.json.get('image_code')  # 短信码
    image_code_id = request.json.get('image_code_id')  # uuid
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数缺少')

    if not re.match(r'^1[345678][0-9]{9}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号码格式不对')

    try:
        real_image_code = redis_store.get("ImageCode_" + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据库异常')

    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg='图片验证码已经过期')
    try:
        redis_store.delete('ImageCode_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)

    if real_image_code.lower() != image_code.lower():
        return jsonify(errno=RET.PARAMERR, errmsg='图片验证码不一致')
    try:
        user = User.query.filter(User.mobile == mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询数据库异常')
    else:
        if user:
            return jsonify(errno=RET.DATAEXIST, errmsg='手机号码已注册')

    # 生成随机数
    sms_code = ''
    for i in range(6):
        sms_code += str(random.randint(0, 9))
    print(sms_code)
    # 生成短信验证码
    try:
        redis_store.setex('SMSCode_' + mobile,
                          constants.SMS_CODE_REDIS_EXPIRES, sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='短信验证码存储失败')
    # 第三方接口发送信息
    try:
        ccp = sms.CCP()
        # 手机号,【验证码 , 有效时间/分】,模版1
        result = ccp.send_template_sms(
            mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信失败')

    if 0 == result:
        return jsonify(errno=RET.OK, errmsg='发送成功')
    else:
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信失败')
示例#2
0
def send_sms():

    mobile = request.json.get('mobile')
    image_code = request.json.get('image_code')
    image_code_id = request.json.get('image_code_id')
    if not all({mobile, image_code, image_code_id}):
        return jsonify(errno=RET.PARAMERR, errmsg='提交参数有误')

    if not re.match(r'1[3456789]\d{9}$',mobile):
        return jsonify(errno=RET.DATAERR, errmsg='手机号输入有误')

    # 数据库查找用户信息,确认该手机号是否已注册
    try:
        find_user = User.query.filter(User.mobile == mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="获取用户信息失败")

    if find_user:
        return jsonify(errno=RET.DATAEXIST, errmsg="用户已存在")

    try:
        redis_image_code = redis_store.get('ImageCode_'+image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='获取验证码数据失败')

    if not redis_image_code:
        return jsonify(errno=RET.NODATA, errmsg="验证码已过期")

    try:
        redis_store.delete('ImageCode_'+image_code_id)
    except Exception as e:
        current_app.logger.error(e)

    if redis_image_code.lower() != image_code.lower():
        return jsonify(errno=RET.DATAERR, errmsg="验证码输入错误")

    smsCode = '%06d' % random.randint(0, 999999)

    try:
        redis_store.setex('SMS_'+mobile, constants.SMS_CODE_REDIS_EXPIRES, smsCode)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="sms验证码写入错误")

    try:
        cpp = sms.CCP()
        resp = cpp.send_template_sms(mobile, [smsCode, constants.SMS_CODE_REDIS_EXPIRES/60], 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg="第三方接口错误")

    if resp == 0:
        return jsonify(errno=RET.OK)
    else:
        return jsonify(errno=RET.THIRDERR, errmsg='发送失败')
示例#3
0
def send_sms_code():
    mobile = request.json.get('mobile')
    image_code = request.json.get('image_code')
    image_code_id = request.json.get('image_code_id')
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数缺失2')
    if not re.match(r'^1[3456789]\d{9}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号格式错误')
    try:
        real_image_code = redis_store.get('ImageCode_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='获取图片验证码异常')
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg='图片验证码已过期')
        # 因为图片验证码只能让用户比较一次,redis只能get一次,所以要立即删除
    try:
        redis_store.delete('ImageCode_' + 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='图片验证码错误')
        # 判断手机号是否已注册
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(enrro=RET.DATAERR, errmsg='查询数据库异常')
    else:
        if user is not None:
            return jsonify(enrro=RET.DATAEXIST, errmsg='手机号已经注册过')

        # 构造短信随机码,format()
    sms_code = '%06d' % random.randint(0, 999999)
    # 把短信随机数保存到redis数据库中
    try:
        redis_store.setex('SMSCode_' + mobile,
                          constants.SMS_CODE_REDIS_EXPIRES, sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存短信验证码异常')
    # 调用云通讯,发送短信随机数
    try:
        ccp = sms.CCP()
        # 调用云通讯的模板方法,发送短信,需要保存发送结果
        result = ccp.send_template_sms(
            mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信异常')
    # 判断发送结果
    if result == 0:
        return jsonify(errno=RET.OK, errmsg='发送成功')
    else:
        return jsonify(errno=RET.THIRDERR, errmsg='发送失败')
示例#4
0
def send_sms_code():
    mobile = request.json.get('mobile')
    image_code = request.json.get('image_code')
    image_code_id = request.json.get('image_code_id')
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不完整')
    if not re.match(r'1[3456789]\d{9}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号不符合格式')
    try:
        real_image_code = redis_store.get('ImageCode_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='获取图片验证码失败')
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg='图片验证码已过期')
    try:
        # 删除数据
        redis_store.delete('ImageCode_' + 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='图片验证码不一致')
    # 查询 mysql 数据库,判断手机号是否已注册
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询用户信息失败')
    else:
        if user is not None:
            return jsonify(errno=RET.DATAEXIST, errmsg='该手机号已注册')

        sms_code = '%06d' % random.randint(0, 999999)
        # sms_code = 666666
    try:
        redis_store.setex('SMSCode_' + mobile,
                          constants.SMS_CODE_REDIS_EXPIRES, sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存数据失败')
    # 调用云通讯
    try:
        ccp = sms.CCP()
        result = ccp.send_template_sms(
            mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信异常')
    if result == 0:
        return jsonify(errno=RET.OK, errmsg='发送成功')
    else:
        return jsonify(errno=RET.THIRDERR, errmsg='发送失败')
示例#5
0
文件: views.py 项目: 18203356710/ts
def sendSMSCode():
    mobile = request.json.get('mobile')
    image_code = request.json.get('image_code')
    image_code_id = request.json.get('image_code_id')
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数缺失')
    if not re.match(r'1[3456789]\d{9}', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号码格式不正确')
    try:
        real_image_code = redis_store.get('ImageCode_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='获取数据失败')
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg='数据已失效')
    try:
        redis_store.delete('ImageCode_' + 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='图片验证码错误')
    try:
        user = User.query.filter(User.mobile == mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询数据失败')
    else:
        if user:
            return jsonify(errno=RET.DATAEXIST, errmsg='手机号码已经注册')
    sms_code = '%06d' % random.randint(0, 999999)
    print(sms_code)
    try:
        redis_store.setex('SMSCode_' + mobile,
                          constants.SMS_CODE_REDIS_EXPIRES, sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存数据已失效')
    # 使用云通讯发送短信
    try:
        ccp = sms.CCP()
        result = ccp.send_template_sms(
            mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信异常')
    if result == 0:
        return jsonify(errno=RET.OK, errmsg='发送成功')
    else:
        return jsonify(errno=RET.THIRDERR, errmsg='发送失败')
示例#6
0
文件: views.py 项目: TuringEmmy/News
def send_sms_code():
    """
    发送短信:写接口、调接口
    获取参数-----检查参数-----业务处理-----返回结果
    1、获取参数mobile,image_code,image_code_id
    2、检查参数的完整性
    3、检查手机号的格式,正则
    4、尝试从redis数据库中获取真实的图片验证码
    5、判断获取结果是否存在,图片验证码有可能过期
    6、删除图片验证码
    7、比较图片验证码是否一致
    8、构造短信随机码,6位数
    9、调用云通讯发送短信方法
    10、判断发送结果是否成功
    11、返回结果

    :return:
    """
    # 获取参数
    mobile = request.json.get("mobile")
    image_code = request.json.get("image_code")
    image_code_id = request.json.get("image_code_id")
    # 检查参数的完整性
    # if mobile and image_code and image_code_id:
    if not all([mobile,image_code,image_code_id]):
        return jsonify(errno=RET.PARAMERR,errmsg='参数不完整')
    # 检查手机号的格式
    if not re.match(r'1[3456789]\d{9}$',mobile):
        return jsonify(errno=RET.PARAMERR,errmsg='手机号格式错误')
    # 获取redis数据库中存储的真实图片验证码
    try:
        real_image_code = redis_store.get('ImageCode_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg='获取数据异常')
    # 判断数据是否过期
    if not real_image_code:
        return jsonify(errno=RET.NODATA,errmsmg='数据已过期')
    # 删除redis数据库中存储的图片验证码,因为图片验证码只能比较一次,本质是只能读取redis数据库一次
    try:
        redis_store.delete('ImageCode_' + 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='图片验证码错误')
    # 判断用户是否已注册
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg='查询数据失败')
    else:
        if user is not None:
            return jsonify(errno=RET.DATAEXIST,errmsg='用户已存在')

    # 构造短信随机数,保存到redis数据库中
    sms_code = '%06d' % random.randint(0, 999999)
    print(sms_code)
    try:
        redis_store.setex('SMSCode_' + mobile,constants.SMS_CODE_REDIS_EXPIRES,sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg='存储数据异常')
    # 调用云通讯接口,发送短信
    try:
        ccp = sms.CCP()
        results = ccp.send_template_sms(mobile,[sms_code,constants.SMS_CODE_REDIS_EXPIRES/60],1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR,errmsg='发送短信异常')
    # 判断发送结果
    if results == 0:
        return jsonify(errno=RET.OK,errmsg='发送成功')
    else:
        return jsonify(errno=RET.THIRDERR,errmsg='发送失败')
示例#7
0
def send_sms_code():
    """
    发送短信:1.获取参数,2.检查参数3.业务处理4.返回结果
    1.获取三个参数,手机号,uuid,输入验证码
    2.判断参数是否齐全
    3.校验手机号格式,是否正确
    4.获取本地的redis数据库中存储真实图片验证码;只能取一次,无论用户输入是否正确,都需要删除图片验证码
    5.判断获取结果,如果不存在,直接中止程序的执行,图片验证码过期
    6.删除内存中图片验证码
    7.比较图片验证码是否一致,因为图片验证码只能比较一次
    8.判断手机号是否注册
    9.比较一致,构造短信验证码,随机数六位,random。randint()
    11.在redis数据库中存储短信验证码
    12.调用云通讯发送短信
    13.判断是否发送成功

    :return:
    """
    # 获取post请求的参数
    mobile = request.json.get('mobile')
    image_code = request.json.get('image_code')
    image_code_id = request.json.get('image_code_id')
    # 判断参数的完整性
    if not all([mobile,image_code,image_code_id]):
        return jsonify(erron=RET.PARAMERR,errmsg='参数不完整')
    # 检查手机号的格式
    if not re.match(r'^1[3456789]\d{9}$',mobile):
        return jsonify(erron=RET.PARAMERR,errmsg='手机号格式错误')
    # 从redis根据uuid获取图片验证码内容,查询依然使用try
    try:
        text_img = redis_store.get('ImageCode' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(erron=RET.DBERR,errmsg='获取图片验证码失败')
    # 判断获取结果是否存在
    if not text_img:
        return jsonify(erron=RET.NODATA,errmsg='图片验证码过期')
    #删除图片验证码,因为图片验证码只能get一次
    try:
        redis_store.delete('ImageCode' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)

    #比较图片验证码是否一致,lower 忽略大小写
    if text_img.lower() != image_code.lower():
        return jsonify(erron=RET.DATAERR, errmsg='图片验证码错误')
    # 判断手机是否已经注册
    try:
        user = User.query.filter(User.mobile==mobile).first()
    except Exception as a:
        current_app.logger.error(e)
        return jsonify(erron=RET.DBERR, errmsg='查询数据库异常')
    else:
        #判断查询结果是否有数据
        if user:
            return jsonify(erron=RET.DATAERR, errmsg='注册用户已存在')







    # 随机生成六位数,%06d 占位符,必须六位,前面为用0
    sms_code = '%06d' % random.randint(0,999999)
    # 将随机的生成的验证码,保存到redis中,为了比较验证码
    try:
        redis_store.setex('SMScode_' + mobile,constants.SMS_CODE_REDIS_EXPIRES,sms_code)
    except Exception as e:
        current_app.logger.error(e)# 将错误信息保存在日志里
        return jsonify(erron=RET.DBERR, errmsg='保存短信验证码失败')
    # 调运第三方接口云通讯发送信息,有网络连接的地方也用try
    try:
        # 实例化云通信对象
        ccp = sms.CCP()
        # 调用云通讯的模板方法发送短息
        result = ccp.send_template_sms(mobile,[sms_code,constants.SMS_CODE_REDIS_EXPIRES/60],1)
    except Exception as e:
        return jsonify(erron=RET.THIRDERR, errmsg='发送短信异常')
    # 判断发送结果
    if result == 0:
        return jsonify(erron=RET.OK, errmsg='发送成功')
    else:
        return jsonify(erron=RET.THIRDERR, errmsg='发送失败')
示例#8
0
def send_sms_code():
    """
    发送短信:web开发:写接口、调接口
    获取参数----检查参数----业务处理----返回结果
    1、获取参数mobile,image_code,image_code_id
    2、检查参数的完整性
    3、检查手机号的格式,正则
    4、尝试从redis中获取真实的图片验证码
    image_code = redis_store.get(imagecode)
    5、判断获取结果,如果不存在,说明图片验证码已过期
    6、删除redis中存储的图片验证码,因为图片验证码无论正确与否,只能比较一次,
    7、比较图片验证码是否正确
    8、构造短信随机码,6位数
    9、使用云通讯发送短信,保存发送结果
    10、返回结果
    :return:
    """
    # 获取参数
    mobile = request.json.get('mobile')
    image_code = request.json.get('image_code')
    image_code_id = request.json.get('image_code_id')
    # 检查参数的完整性
    # if mobile and image_code and image_code_id:
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不完整')
    # 使用正则校验手机号格式
    if not re.match(r'1[3456789]\d{9}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号格式错误')
    # 手机号是否注册可以

    # 获取redis中存储的真实图片验证码
    try:
        real_image_code = redis_store.get('ImageCode_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='获取数据失败')
    # 判断获取结果
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg='图片验证码已过期')
    # 删除redis中的图片验证码
    try:
        redis_store.delete('ImageCode_' + 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='图片验证码不一致')
    # 判断手机号是否已注册
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询用户数据失败')
    else:
        if user:
            return jsonify(errno=RET.DATAEXIST, errmsg='手机号已注册')

    # 构造六位数的短信随机数
    sms_code = '%06d' % random.randint(0, 999999)
    print(sms_code)
    # 存入到redis数据库中
    try:
        redis_store.setex('SMSCode_' + mobile,
                          constants.SMS_CODE_REDIS_EXPIRES, sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存数据异常')

    # 使用云通讯发送短信
    try:
        ccp = sms.CCP()
        results = ccp.send_template_sms(
            mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信异常')
    # 判断发送结果
    if results == 0:
        return jsonify(errno=RET.OK, errmsg='发送成功')
    else:
        return jsonify(errno=RET.THIRDERR, errmsg='发送失败')
示例#9
0
文件: views.py 项目: sensendada/flask
def send_sms_code():
    """
    发送短信:web开发:写接口,调接口
    获取参数-----检查参数-----业务处理 ----返回结果
    1.获取参数就mobile,image_code, image_code_id
    2.检查参数的完整性
    3.检查手机号的格式,正则
    4.尝试从redis数据库中获取正确的图片验证码
    image_code = redis_store.get(imagecode)
    5.判断获取结果,如果不存在,书名图片验证码已过期
    6.删除redis数据库中存储的图片验证码,因为无论图片验证码正确与否,只能比较一次
    7.比较图片验证码是否正确
    8.构造短信随机码,6位数
    9.使用云通讯发送短信,保存发送结果
    10.返回结果
    :return:
    """
    # 从前端获取参数
    mobile = request.json.get('mobile')
    image_code = request.json.get('image_code')
    image_code_id = request.json.get('image_code_id')
    # 检查参数的完整性
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不完整')
    # 使用正则校验手机号格式
    if not re.match(r'1[3456789]\d{9}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号格式错误')
    # 检查手机号是否可以注册
    # 获取redis中存储的真实的图片验证码
    try:
        real_image_code = redis_store.get('ImageCode_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='获取数据失败')
    # 判断获取结果
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg='图片验证码已经过期')
    # 删除redis中的图片验证码
    try:
        redis_store.delete('ImageCode' + 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='图片验证码不一致')
    # 判断 手机号是否可以注册
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询用户数据失败')
    else:
        if user:
            return jsonify(errno=RET.DATAERR, errmsg='手机号已经注册了')

    # 构造六位数的短信验证码
        sms_code = '%06d' % random.randint(0, 999999)
        print(sms_code)
        # 存入redis数据库中
        try:
            redis_store.setex('SMSCode_' + mobile,
                              constants.SMS_CODE_REDIS_EXPIRES, sms_code)
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.DBERR, errmsg='保存数据异常')
        # 使用云通讯发送短信
        try:
            ccp = sms.CCP()
            results = ccp.send_template_sms(
                mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1)
        except Exception as e:
            current_app.looger.error(e)
            return jsonify(errno=RET.THIRDERR, errmsg='发送短信异常')
        # 判断发送结果
        if results == 0:
            return jsonify(errno=RET.OK, errmsg='发送成功')
        else:
            return jsonify(errno=RET.THIRDERR, errmsg='发送失败')
示例#10
0
def send_sms_code():
    """
    发送短信
    获取参数---检查参数---业务处理---返回结果
    1、获取参数,mobile,image_code(用户输入的图片验证码内容),image_code_id(UUID)
    2、判断参数的完整性
    3、检查手机号的格式
    4、尝试从redis中获取真实的图片验证码
    5、都要删除redis中存储的图片验证码,因为图片验证码只能比较一次,比较一次的本质是只能对redis数据库get一次
    6、判断获取结果,如果图片验证码已过期,直接结束程序
    7、比较图片验证码是否正确
    8、生成短信验证码
    9、存储在redis数据库中,ImageCode_UUID,SMSCode_mobile
    10、调用云通讯,发送短信
    11、保存发送结果,判断发送是否成功
    12、返回结果

    :return:
    """
    # 获取参数
    mobile = request.json.get('mobile')
    image_code = request.json.get('image_code')
    image_code_id = request.json.get('image_code_id')
    # 检查参数的完整性
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数缺失')
    # 检查手机号格式
    if not re.match(r'1[3456789]\d{9}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号格式错误')

    # 从redis中获取图片验证码
    try:
        real_image_code = redis_store.get('ImageCode_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询数据失败')
    # 判断获取结果
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg='数据已过期')
    # 删除redis中存储的图片验证码
    try:
        redis_store.delete('ImageCode_' + 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='图片验证码不一致')
    # 根据手机号查询数据库,确认用户未注册,如果用户未注册,才发送短信
    try:
        user = User.query.filter(User.mobile == mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询用户数据失败')
    else:
        # 如果用户已存在
        if user:
            return jsonify(errno=RET.DATAEXIST, errmsg='手机号已注册')

    # 生成六位数的短信验证码
    sms_code = '%06d' % random.randint(0, 999999)
    print(sms_code)
    # 存储短信验证码到redis数据库
    try:
        redis_store.setex('SMSCode_' + mobile,
                          constants.SMS_CODE_REDIS_EXPIRES, sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存数据失败')
    # 调用云通讯发送短信
    try:
        ccp = sms.CCP()
        result = ccp.send_template_sms(
            mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信异常')
    # 判断发送的结果
    if result == 0:
        return jsonify(errno=RET.OK, errmsg='发送成功')
    else:
        return jsonify(errno=RET.THIRDERR, errmsg='发送失败')
示例#11
0
def send_sms_code():
    """
    发送短信
    1、获取参数,mobile,image_code,image_code_id
    2、判断参数的完整性
    3、验证手机号格式,正则
    4、校验图片验证码,从redis读取真实的图片验证码
    real_image_code = redis_store.get(key)
    5、判断获取结果是否存在,如果不存在,表示已过期
    6、如果图片验证码存在,需要删除图片验证码,本质是任意一个图片验证码,只能读取一次;
    7、比较图片验证码内容是否一致;
    8、查询mysql数据库,确认手机号是否已经注册;
    9、生成短信随机数;六位
    10、把短信随机数保存到redis数据库中,设置有效期
    11、调用云通讯扩展,来发送短信,保存发送结果
    12、判断发送结果是否成功。
    :return:
    """
    # 获取手机号、用户输入的图片验证码内容,图片验证码编号
    mobile = request.json.get('mobile')
    image_code = request.json.get('image_code')
    image_code_id = request.json.get('image_code_id')
    print(image_code)
    # 检查参数的完整性
    # if mobile and image_code and image_code_id
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不完整')
    # 检查手机号格式
    if not re.match(r'^1[3456789]\d{9}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号格式错误')
    # 检查图片验证码,首先从redis获取真实的图片验证码
    try:
        real_image_code = redis_store.get('ImageCode_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='获取图片验证码失败')
    # 判断获取结果是否存在
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg='图片验证码已经过期')
    # 删除图片验证码,因为图片验证码只能校验一次
    try:
        redis_store.delete('ImageCode_' + 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='图片验证码不一致')
    # 查询mysql数据库,确认用户是否注册
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.loggere.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询用户信息失败')
    else:
        # 判断查询结果是否存在
        if user is not None:
            return jsonify(errno=RET.DATAEXIST, errmsg='手机号已注册')
    # 构造六位数的短信随机数
    sms_code = '%06d' % random.randint(0, 999999)
    print(sms_code)
    # 保存到redis数据库中
    try:
        redis_store.setex('SMSCode_' + mobile,
                          constants.SMS_CODE_REDIS_EXPIRES, sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存数据失败')
    # 调用云通讯发送短信验证码
    try:
        ccp = sms.CCP()
        # 调用模板方法,发送短信,保存发送结果
        result = ccp.send_template_sms(
            mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信异常')
    # 判断发送结果是否成功
    if result == 0:
        return jsonify(errno=RET.OK, errmsg='发送成功')
    else:
        return jsonify(errno=RET.THIRDERR, errmsg='发送失败')
示例#12
0
def send_sms_code():
    '''
    发送短信验证码:

    参数名:mobile,手机号,str
           image_code,图片验证码编号,str
           image_code_id,图片验证码内容,str
    获取参数——>校验参数——>业务处理——>返回结果

    1.  从前端获取用户输入的手机号mobile,图片验证码编号image_code,用户输入的图片验证码的内容image_code_id
        var params = {
            "mobile": mobile,
            "image_code": imageCode,
            "image_code_id": imageCodeId
        }
    2.  检查参数是否全部存在
    3.  检查手机号格式,正则
    4.  根据image_code从redis数据库中查询真正的图片验证码内容
        4.1  判断是否获取到图片验证码内容,没有就是图片验证码过期
        4.2  删除redis中真正的图片验证码
        4.3  判断用户输入的图片验证码和真正的图片验证码是否一致
        4.4  判断手机号是否注册
    5.  生成随机短信验证码4位,
    6.  短信验证码存入到redis数据库中
    7.  通过云通讯将短信验证码发送
    8.  判断是否发送成功
    9.  返回结果

    :return:
    ''' ''
    mobile = request.json.get('mobile')
    image_code = request.json.get('image_code')  # 用户输入的图片验证码
    image_code_id = request.json.get(
        "image_code_id")  # 前端在发送请求获取图片验证码时,生成的uuid
    # 检查参数的完整性
    # if mobile and image_code_id and image_code:
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不完整')
    # 使用正则,校验手机号格式
    # 13112345678
    if not re.match(r'1[3456789]\d{9}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号格式错误')
    # 图片验证码的校验:必须是先删除,再比较
    # 尝试从redis中获取真实的图片验证码
    try:
        real_image_code = redis_store.get('ImageCode_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询数据异常')
    # 判断图片验证码是否过期
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg='图片验证码已过期')
    # 把redis中存储的图片验证码删除
    try:
        redis_store.delete('ImageCode_' + 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='图片验证码错误')
    # 检查手机号是否注册!!!补充
    try:
        user = User.query.filter(User.mobile == mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询用户数据失败')
    else:
        if user is not None:
            return jsonify(errno=RET.DATAEXIST, errmsg='手机号已注册')

    # 业务处理
    # 首先生成短信随机数,4位数的字符串
    sms_code = '%04d' % random.randint(0, 9999)
    print(sms_code)
    # 把短信随机数存入redis数据库
    try:
        redis_store.setex("SMSCode_" + mobile,
                          constants.SMS_CODE_REDIS_EXPIRES, sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存数据失败')
    # 调用云通讯接口,发送短信,需要保存发送结果
    try:
        ccp = sms.CCP()
        # 第一个参数表示手机号,第二个参数为列表:短信随机码和过期时间,第三个参数表示模板id,免费测试为1
        result = ccp.send_template_sms(
            mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信异常')
    # 判断发送结果
    if result == 0:
        return jsonify(errno=RET.OK, errmsg='发送成功')
    else:
        return jsonify(errno=RET.THIRDERR, errmsg='发送失败')