예제 #1
0
def send_sms_code():
    data = request.json
    mobile=data.get('mobile')
    image_code =data.get('image_code')
    image_code_id = data.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[3-9][0-9]{9}',mobile):
        return jsonify(errno = RET.PARAMERR,errmsg ='手机格式不正确')
    count =User.query.filter(User.mobile == mobile).count()
    if count > 0:
        return jsonify(errno=RET.DATAEXIST,errmsg='手机号已经注册')
    try:
        redis_code =redis_store.get('img_'+image_code_id)
        if redis_code:
            redis_code =redis_code.decode()
            redis_store.delete('img_'+image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR,errmsg = 'redis数据有问题')
    if not redis_code:
        return jsonify(errno=RET.DATAERR,errmsg = '图片验证码以及过期')
    if image_code.lower() != redis_code.lower():
        return jsonify(errno=RET.DATAERR,errmsg = '验证码不一致')
    from random import randint
    sms_code = '%06d'%randint(0,999999)
    result = CCP().send_template_sms(mobile,[sms_code,constants.SMS_CODE_REDIS_EXPIRES/60],1)
    # if result !=0:
        #return jsonify(errno=RET.DATAERR,errmsg = '发送失败')
    try:
        redis_store.setex('sms_'+mobile,constants.SMS_CODE_REDIS_EXPIRES,sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return  jsonify(errno=RET.DATAERR,errmsg = '发送失败')
    return  jsonify(errno=RET.OK,errmsg = 'OK')
예제 #2
0
파일: views.py 프로젝트: ltfred/flask_news
def 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("1[356789]\d{9}", mobile):
        return jsonify(errno=RET.PARAMERR, errmsg="请输入手机号码")

    real_image_code = redis_store.get("image_code_" + image_code_id)
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg="图片验证码已过期")

    if real_image_code.lower() != image_code.lower():
        return jsonify(errno=RET.PARAMERR, errmsg="请输入正确的验证码")

    result = random.randint(0, 999999)
    sms_code = "%06d" % result
    print("短信验证码 = " + sms_code)
    redis_store.set("sms_code_" + mobile, sms_code,
                    constants.SMS_CODE_REDIS_EXPIRES)

    statusCode = CCP().send_template_sms(mobile, [sms_code, 5], 1)
    if statusCode != 0:
        return jsonify(errno=RET.THIRDERR, errmsg="短信发送失败")

    return jsonify(errno=RET.OK, errmsg="发送短信成功")
예제 #3
0
def 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_id, image_code]):
        # 参数不全
        return jsonify(errno=RET.PARAMERR, errmsg="参数不全")

    if not re.match(r"1[3456789]\d{9}", mobile):
        return jsonify(errno=RET.DATAERR, errmsg="手机号不正确")
    real_image_code = redis_store.get("image_code_" + image_code_id)
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg="redis已经过期")
    if image_code.lower() != real_image_code.lower():
        return jsonify(errno=RET.PARAMERR, errmsg="请输入正确的验证码")
    # 编辑发送的短信内容
    result = random.randint(0, 999999)
    sms_code_random = "%06d" % result
    # 存储短信验证码
    redis_store.set("sms_code_" + mobile, sms_code_random,
                    constants.SMS_CODE_REDIS_EXPIRES)
    print("短信内容是:" + sms_code_random)
    statusCode = CCP().send_template_sms(mobile, [sms_code_random, 5], 1)
    if statusCode != 0:
        return jsonify(errno=RET.THIRDERR, errmsg="短信发送失败")

    return jsonify(errno=RET.OK, errmsg="成功")
예제 #4
0
def sms_passport():
    """
    1. 接收参数并判断是否有值
    2. 校验手机号是正确
    3. 通过传入的图片编码去redis中查询真实的图片验证码内容
    4. 进行验证码内容的比对
    5. 生成发送短信的内容并发送短信
    6. redis中保存短信验证码内容
    7. 返回发送成功的响应
    :return:
    """

    # 1接受请求值中的内容
    params_dict = request.json
    mobile = params_dict.get("mobile")
    input_text = params_dict.get("image_code")
    image_code_id = params_dict.get("image_code_id")

    # 检查参数是否全部传递了
    if not all([mobile, input_text, image_code_id]):
        return jsonify(errno=RET.DATAERR, errmsg="参数不全,请重新输入")

    # 检测输入的手机格式是否正确
    if not re.match(r"1[3-9]\d{9}", mobile):
        return jsonify(error=RET.DATAERR, errmsg="手机号码格式不正确,请重新输入")
    try:
        # redis_store.set("sms_" + mobile, mobile)
        result_image = redis_store.get("ImageCode_" + image_code_id)
        if result_image:
            redis_store.delete("ImageCode_" + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="没有查询到数据")

    # 判断验证码是否正确
    if input_text.upper() != result_image.upper():
        return jsonify(errno=RET.DATAERR, errmsg="图片验证码错误")

    # 发送短信验证码
    import random
    psms_code = random.randint(100000, 999999)
    print("sms_code", psms_code)
    current_app.logger.debug("短信验证码的内容是: %s" % psms_code)
    result = CCP().send_template_sms(
        mobile, [psms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1)
    try:
        redis_store.set("SMS_" + str(mobile), psms_code,
                        constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(errno=RET.DATAERR, errmsg="数据保存异常")
        return jsonify(errno=RET.DBERR, errmsg="短信验证码保存失败")
    return jsonify(errno=RET.OK, errmsg="发送成功")
예제 #5
0
def get_sms_code():
    # 获取参数  手机号码 用户输入的验证码 图片key
    mobile = request.json.get("mobile")
    img_code = request.json.get("img_code")
    img_code_id = request.json.get("img_code_id")
    # 校验参数
    if not all([mobile, img_code_id, img_code]):
        return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR])

    # 校验手机号码的格式
    if not re.match(r"1[35678]\d{9}$", mobile):
        return jsonify(errno=RET.DATAERR, errmsg=error_map[RET.DATAERR])

    # 校验图片验证码是否过期
    try:
        real_img_code = sr.get("img_code_id" + img_code_id)
    except BaseException as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR])

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

    # 判断用户输入的验证码是否正确
    if real_img_code != img_code.upper():
        return jsonify(errno=RET.PARAMERR, errmsg="验证码输入错误!")

    # 判断用户输入的手机号码是否注册过
    user = User.query.filter_by(mobile=mobile).first()
    if user:
        return jsonify(errno=RET.DATAEXIST, errmsg=error_map[RET.DATAEXIST])

    # 获取4位随机数字
    sms_code = "%04d" % random.randint(0, 9999)
    current_app.logger.error("短信验证码为:%s" % sms_code)
    # 发送短信
    res_code = CCP().send_template_sms(mobile, [sms_code, 5], 1)
    # 判断短信是否发送成功
    if res_code == -1:
        return jsonify(errno=RET.THIRDERR, errmsg=error_map[RET.THIRDERR])

    # 将短信验证码保存到redis,并设置有效期为一分钟
    try:
        sr.set("sms_code_id" + mobile, sms_code, ex=60)
    except BaseException as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR])

    return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
예제 #6
0
def sms_code():
    # 获取前端传递过来的json数据
    mobile = request.json.get("mobile")

    # 参数表示图片验证码的内容
    image_code = request.json.get("image_code")

    # 表示图片验证码的id
    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中的图片验证码
    real_image_code = redis_store.get("image_code" + image_code_id)

    # 判断redis中的验证码是否过期
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg="当前验证码已过期")

    # 到了这里说明验证码还在有效期,判断用户输入的验证码是否和redis中保存的相同,都转换为小写,方便用户体验
    if image_code.lower() != real_image_code.lower():
        return jsonify(errno=RET.PARAMERR, errmsg="请输入正确的验证码")

    # 通过随机数生成一个6位的验证码,并用0占位补齐六位
    random_sms_code = "%06d" % random.randint(0, 999999)

    # 将产生的随机数短信内容储存在redis中,方便注册的时候进行验证
    # 第一个参数表示key,  第二个参数表示六位随机数字,  第三个参数表示数据有效期,单位是秒
    redis_store.set("sms_code_" + mobile, random_sms_code, constants.SMS_CODE_REDIS_EXPIRES)

    print("短信内容 = " + random_sms_code)

    # 发送短信到指定的手机号码>>括号中数据1>手机号码2>随机数字,5分钟有效,返回状态码为1

    statuCode = CCP().send_template_sms(mobile, [random_sms_code, 5], 1)
    print(statuCode)
    if statuCode != 0:
        return jsonify(errno=RET.THIRDERR, errmsg="短信发送失败")

    return jsonify(errno=RET.OK, errmsg="发送短信成功")
예제 #7
0
def send_sms_code():
    # 2. 后端要接收数据(json)
    # 接收 json数据
    data = request.json
    mobile = data.get('mobile')
    image_code = data.get('image_code')
    image_code_id = data.get('image_code_id')
    # 判断是否全部有值
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.NODATA, errmsg='参数不全')
    # 判断手机号格式
    if not re.match(r'1[3-9]\d{9}', mobile):
        return jsonify(errno=RET.DATAERR, errmsg='手机号格式错误')
    # 判断手机号是否已经注册
    user_count = User.query.filter_by(mobile=mobile).count()
    if user_count > 0:
        return jsonify(errno=RET.DATAEXIST, errmsg='手机号已经注册过')
    try:
        redis_code = redis_store.get('img_' + image_code_id)
        if redis_code:
            redis_code = redis_code.decode()
            # redis 中认证后删除该记录
            redis_store.delete('img_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.NODATA, errmsg='redis数据错误')
    if not redis_code:
        return jsonify(errno=RET.NODATA, errmsg='验证码过期')

    if image_code.lower() != redis_code.lower():
        return jsonify(errno=RET.DATAERR, errmsg='验证码不一致')

    # 生成6位验证码
    # base_code = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    # sms_code = ''.join(random.sample(base_code, 6))
    sms_code = '%06d' % random.randint(0, 999999)
    result = CCP().send_template_sms(
        mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1)
    current_app.logger.info(sms_code)
    try:
        redis_store.setex('sms_' + mobile, constants.SMS_CODE_REDIS_EXPIRES,
                          sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg='发送失败')
    return jsonify(errno=RET.OK, errmsg='发送成功')
예제 #8
0
파일: views.py 프로젝트: HHBCODE/hehangbo
def get_sms_code():
    # 获取参数 (手机号 图片key 图片验证码)
    mobile = request.json.get('mobile')
    img_code_id = request.json.get('img_code_id')
    img_code = request.json.get('img_code')
    # 校验参数
    if not all([mobile, img_code_id, img_code]):
        return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR])
    if not re.match(r"1[345678]\d{9}$", mobile):
        return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR])
    # 根据图片key 取出验证码文字
    try:
        real_img_code = sr.get('img_code_id_' + img_code_id)
    except BaseException as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR])
    # 检验验证码是否过期 是否正确
    if not real_img_code:
        return jsonify(errno=RET.PARAMERR, errmsg='验证码已过期')
    if img_code.upper() != real_img_code:
        return jsonify(errno=RET.PARAMERR, errmsg="验证码错误")
    # 校验成功 发送短信
    sms_code = "%04d" % random.randint(0, 9999)
    current_app.logger.info('短信验证码为%s' % sms_code)
    res_code = CCP().send_template_sms(18715221675, [sms_code, 5], 1)
    print(res_code)
    if res_code == -1:
        return jsonify(errno=RET.THIRDERR, errmsg=error_map[REI.RET.THIRDERR])
    # 将短信验证码保存到数据库中
    try:
        sr.set('sms_code_id_' + mobile, sms_code, ex=60)
    except BaseException as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR])
    # 根据短信结果使用json 返回
    return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
예제 #9
0
def send_sms_code():
    """发送短信验证后端接口"""
    """
    1.获取参数
        1.1 mobile:手机号码 , image_code:用户填写的图片验证码 , image_code_id:UUID编码
    2.参数校验
        2.1 非空判断
        2.2 正则校验手机号码格式
    3.逻辑处理
        3.1 根据image_code_id编号去redis数据库获取真是的图片验证码值real_image_code
            3.1.1 real_image_code没有值:图片验证码过期
            3.1.2 real_image_code有值:将图片验证码从redis中删除(防止多次适用同一个验证码来进行多次验证码验证)
        3.2 对比用户添加的图片验证码值和真实的图片验证码值
            3.2.1 不相等:提示图片验证码填写错误
            3.2.2 相等:发送短信验证码

        TODO:判断用户填写的手机号码是否注册(提高用户体验)

        3.3 发送短信验证码具体流程
            3.3.1 生成6位的随机短信验证码值
            3.3.2 调用ccp类中方法发送短信验证码
            3.3.3 发送短信验证码失败:提示前端重新发送
            3.3.4 将6位的短信验证码使用redis数据库保存起来,设置有效时长(方便注册接口获取真实的短信验证值)
    4.返回值
        4.1 发送短信验证码成功
    """

    # 1.1 mobile: 手机号码, image_code: 用户填写的图片验证码, image_code_id: UUID编码(格式:json)
    # 将前端发送的json数据转换成python对象
    param_dict = request.json
    mobile = param_dict.get("mobile")
    image_code = param_dict.get("image_code")
    image_code_id = param_dict.get("image_code_id")

    # 2.1非空判断
    if not all([mobile, image_code, image_code_id]):
        err_dict = {"errno": RET.PARAMERR, "errmsg": "参数不足"}
        """
        {
        "errno":4103,
        "errmsg":"参数不足"
        }
        """
        return jsonify(err_dict)

    # 2.2正则校验手机号码格式
    if not re.match("1[3456789][0-9]{9}", mobile):
        current_app.logger.error("手机格式错误")
        return jsonify(errno=RET.PARAMERR, errmsg="手机格式错误")

    # 3.1根据image_code_id编号去redis数据库获取真是的图片验证码值real_image_code
    try:
        real_image_code = redis_store.get("CODEID_%s" % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="获取真实的图片验证码错误")

    # 3.1.1 real_image_code没有值: 图片验证码过期
    if not real_image_code:
        current_app.logger.error("图片验证码过期了")
        return jsonify(errno=RET.NODATA, errmsg="图片验证码过期了")
    # 3.1.2 real_image_code有值: 将图片验证码从redis中删除(防止多次适用同一个验证码来进行多次验证码验证)
    else:
        try:
            redis_store.delete("CODEID_%s" % image_code_id)
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.DBERR, errmsg="删除真实的图片验证码异常")
    """
    细节:1.忽略大小写 2.对比时数据格式要一致,将从redis中获取的真实图片验证码值转换成字符串
    """
    if image_code.lower() != real_image_code.lower():
        # 3.2.1不相等: 提示图片验证码填写错误
        return jsonify(errno=RET.DATAERR, errmsg="图片验证码填写错误")

    # 3.2.2相等: 发送短信验证码

    # TODO: 判断用户填写的手机号码是否注册(提高用户体验)
    try:
        user = User.query.filter(User.mobile == mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询用户对象异常")

    # 表示用户已经注册过
    if user:
        return jsonify(errno=RET.DATAEXIST, errmsg="用户已经注册过")
    # 3.3.1生成6位的随机短信验证码值
    import random
    sms_code = random.randint(0, 999999)
    # 不足6位在前面补0
    sms_code = "%06d" % sms_code

    # 3.3.2调用ccp类中方法发送短信验证码
    # 参数1:手机号码 参数2:{6位短信验证码,5分钟过期时长} 参数3:模板编号
    result = CCP().send_template_sms(
        mobile, {sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60}, 1)

    if result == -1:
        # 3.3.3发送短信验证码失败: 提示前端重新发送
        return jsonify(errno=RET.THIRDERR, errmsg="云通讯发送短信验证码失败")
    elif result == 0:
        # 3.3.4将6位的短信验证码使用redis数据库保存起来, 设置有效时长(方便注册接口获取真实的短信验证值)
        #                        手机号码      短信内容       过期时长
        redis_store.setex("SMS_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES,
                          sms_code)
        # 4.1发送短信验证码成功
        return jsonify(errno=RET.OK, errmsg="发送短信验证码成功")
예제 #10
0
def send_sms_code():
    """点击发送短信验证码后端接口"""
    """
    1.获取参数
        1.1 用户账号手机号码mobile, 用户填写的图片验证码值:image_code, 编号UUID:image_code_id

    2.校验参数
        2.1 非空判断 mobile,image_code,image_code_id 是否有空
        2.2 手机号码格式的正则判断

    3.逻辑处理
        3.1 根据编号去redis数据库获取图片验证码的真实值(正确值)
             3.1.1 真实值有值: 将这个值从redis中删除(防止他人多次拿着同一个验证码值来验证)
             3.1.2 真实值没有值: 图片验证码真实值过期了

        3.2 拿用户填写的图片验证码值和Redis中获取的真实值进行比较

        3.3 不相等 告诉前端图片验证码填写错误

        TODO: 判断用户是否注册过,如果注册过,就不在发送短信验证码引导到登录页(提高用户体验)

        3.4 相等, 生成6位随机短信验证码,发送短信验证码

        3.4 将 生成6位随机短信验证码存储到redis数据库

    4.返回值
        4.1 发送短信验证码成功
    """
    # 上传数据是json类型
    #1.1 用户账号手机号码mobile, 用户填写的图片验证码值:image_code, 编号UUID:image_code_id
    #json.loads(request.data)
    # 可以接受前端上传的json格式数据,json字符串转换成python对象
    param_dict = request.json
    # 手机号码
    mobile = param_dict.get("mobile", "")
    # 用户填写的图片验证码值
    image_code = param_dict.get("image_code", "")
    # uuid编号
    image_code_id = param_dict.get("image_code_id", "")
    # 2.1 非空判断 mobile,image_code,image_code_id 是否有空
    if not all([mobile, image_code, image_code_id]):
        # 记录日志
        current_app.logger.error("参数不足")
        # 给调用者返回json格式的错误信息
        return jsonify({"errno":RET.PARAMERR, "errmsg": '参数不足'})

    # 2.2 手机号码格式的正则判断
    if not re.match('1[35789][0-9]{9}', mobile):
        current_app.logger.error("手机格式错误")
        return jsonify(errno=RET.PARAMERR, errmsg="手机格式错误")

    #3.1 根据编号去redis数据库获取图片验证码的真实值(正确值)
    try:
        real_image_code = redis_store.get("imageCodeId_%s" % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="从redis中获取图片真实值异常")
    #3.1.1 真实值有值: 将这个值从redis中删除(防止他人多次拿着同一个验证码值来验证)
    if real_image_code:
        redis_store.delete("imageCodeId_%s" % image_code_id)
    # 3.1.2 真实值没有值: 图片验证码真实值过期了
    else:
        return jsonify(errno=RET.NODATA, errmsg="图片验证码过期")

    #3.2 拿用户填写的图片验证码值和Redis中获取的真实值进行比较
    # 细节1:全部按照小写格式进行比较(忽略大小写)
    # 细节2:redis对象创建的时候设置decode_responses=True
    if real_image_code.lower() != image_code.lower():
        #3.3 不相等 告诉前端图片验证码填写错误
        return jsonify(errno=RET.DATAERR, errmsg="填写图片验证码错误")

    #TODO: 判断用户是否注册过,如果注册过,就不在发送短信验证码引导到登录页(提高用户体验)
    try:
        user = User.query.filter(User.mobile == mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="从mysql中查询用户异常")
    # 注册过就不在发送短信验证码引导到登录页
    if user:
        return jsonify(errno=RET.DATAEXIST, errmsg="手机号码已经注册")

    #3.4 相等, 生成6位随机短信验证码,发送短信验证码
    # 生成6位随机短信验证码
    sms_code = random.randint(0, 999999)
    # 补足6位前面补0
    sms_code = "%06d" % sms_code

    current_app.logger.debug(sms_code)

    try:
        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.THIRDERR, errmsg="云通讯发送短信失败")

    #3.4 将 生成6位随机短信验证码存储到redis数据库
    try:
        # SMS_CODE_18520340804 每个用户这个key都不一样
        redis_store.setex("SMS_CODE_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="存储短信验证码异常")

    #4.返回值
    return jsonify(errno=RET.OK, errmsg="发送短信验证码成功,注意查收")
예제 #11
0
def send_sms():
    """点击发送短信验证码接口"""
    """
    1.获取参数
        1.1 手机号码,用户填写的图片验证码真实值,编号
    2.校验参数
        2.1 判断 手机号码,用户填写的图片验证码真实值,编号是否为空
        2.2 手机号码格式校验
    3.逻辑处理
        3.1 根据编号去获取redis中存储的图片验证码真实值
            3.1.1 image_code_id有值,删除
            3.1.2 image_code_id没有值,表示编号过期
        3.2 对比用户填写的真实值和后端获取的验证码真实值是否一致
        一致:发送短信验证码
        不一致:验证码填写错误
        3.3 保存短信验证码到redis
    4.返回值处理
    """
    #1.获取参数(json类型参数)
    #1.1 手机号码,用户填写的图片验证码真实值,编号
    #request.json 获取到数据会自动转换成python对象(dict or list)
    param_dict = request.json
    mobile = param_dict.get("mobile")
    image_code = param_dict.get("image_code")
    image_code_id = param_dict.get("image_code_id")

    #2.校验参数
    #2.1 判断 手机号码,用户填写的图片验证码真实值,编号是否为空
    if not all([mobile, image_code, image_code_id]):
        # 返回错误给前端展示
        return jsonify(errno=RET.PARAMERR, errmsg="提交参数不足")
    #2.2 手机号码格式校验
    if not re.match('1[3578][0-9]{9}', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg="手机号码格式错误")

    # 3.逻辑处理
    try:
        #3.1 根据编号去获取redis中存储的图片验证码真实值
        real_image_code = redis_store.get("imagecode_%s" % image_code_id)
        # 3.1.1 image_code_id有值,删除 防止下次多次使用同一个real_image_code来访问
        if real_image_code:
            redis_store.delete(real_image_code)
    except Exception as e:
        # 记录日志
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg="查询图片验证码异常")

    #3.1.2 image_code_id没有值,表示编号过期
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg="验证码真实值过期")

    #3.2 对比用户填写的真实值和后端获取的验证码真实值是否一致
    if image_code.lower() != real_image_code.lower():
        return jsonify(errno=RET.DATAERR, errmsg="图片验证码填写错误")

    # 一致:发送短信验证码
    # 查看手机号码是否注册过
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        return jsonify(errno=RET.DATAERR, errmsg="查询用户手机号码是否存在异常")
    # 已经注册过了
    if user:
        return jsonify(errno=RET.DATAEXIST, errmsg="用户已经注册过")

    #1. 生成短信验证码随机值
    sms_code = random.randint(0, 999999)
    sms_code = "%06d" % sms_code
    #2. 调用云通讯发送短信验证码
    result = CCP().send_template_sms(
        mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1)
    # 发送短信验证码失败
    if result != 0:
        return jsonify(errno=RET.THIRDERR, errmsg="发送短信验证码失败")

    #3.发送短信验证码成功
    try:
        redis_store.set("SMS_%s" % mobile,
                        sms_code,
                        ex=constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="存储短信验证码真实值失败")

    # 返回值处理
    return jsonify(errno=RET.OK, errmsg="发送短信验证码成功,请注意查收")
예제 #12
0
def send_sms():
    """
        1. 接收参数并判断是否有值
        2. 校验手机号是正确(因为数据是由前端发送过来的,因此,需要判断)
        3. 通过传入的图片编码去redis中查询真实的图片验证码内容
        4. 进行验证码内容的比对
        5. 生成发送短信的内容并发送短信
        6. redis中保存短信验证码内容
        7. 返回发送成功的响应
        :return:
    """
    # 以下两种方式均是将咱们的 json 字符串转化为 字典的形式(便于操作)
    # args_data = json.loads(request.data)
    args_data = request.json
    # 获取参数
    mobile = args_data.get('mobile')
    image_code = args_data.get('image_code')
    image_code_id = args_data.get('image_code_id')
    # 校验参数 (参数是否有值,参数内容是否正确)
    if not all([mobile, image_code, image_code_id]):
        #  参数不全
        return jsonify(errno=RET.PARAMERR, errmsg='参数不全')
    # 因为前段给咱们传过来的数据我们无法控制
    if not re.match("^1[3578][0-9]{9}$", mobile):
        return jsonify(errno=RET.DATAERR, errmsg='手机号不正确')

    try:
        real_image_code = redis_store.get("ImageCode_" + image_code_id)
        if real_image_code:
            real_image_code = real_image_code.decode()
            redis_store.delete("ImageCode_" + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(error=RET.DBERR, errmsg="获取图片验证码失败")

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

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

    try:
        user = User.query.filter_by(mobile).first()
    except Exception as e:
        user = None
        current_app.logger.error(e)

    if user:
        return jsonify(errno=RET.DATAEXIST, errmsg="该手机已经被注册")

    # 5.生成短信的内容并发送短信
    result = random.randint(0, 999999)
    # 保证生成一个6位数
    sms_code = "%06d" % result
    current_app.logger.debug('短信验证码的内容:%s' % sms_code)
    result = CCP().send_template_sms(
        mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], "1")
    if result != 0:
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信失败')

    try:
        redis_store.set("SMS_" + mobile, sms_code,
                        constants.SMS_CODE_REDIS_EXPIRES)

    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="保存短信验证码失败")

    return jsonify(errno=RET.OK, errmsg="发送成功")
예제 #13
0
def send_sms():
    """发送短信验证的接口"""

    # 1.获取参数
    # a.手机号码,用户填写的验证码,UUID随机编号

    # parem_dict = json.loads(request.data)

    parem_dict = request.json

    mobile = parem_dict.get("mobile")

    image_code = parem_dict.get("image_code")

    image_code_id = parem_dict.get("image_code_id")

    # 2.校验参数

    # a.手机号码,用户填写的验证码,UUID随机编号判断空校验

    if not all([mobile, image_code, image_code_id]):
        # 返回错误的json字符串
        return jsonify(errno=RET.PARAMERR, errmsg="参数不足")

    # b.手机格式校验

    if not re.match("1[356789][0-9]{9}", mobile):
        # 返回错误的json字符串

        return jsonify(errno=RET.PARAMERR, errmsg="手机格式错误")

    # 3.逻辑处理

    # a.根据image_code__id编号去redis中获取验证码的真实的值

    try:
        # 在redis创建的时候设置decode_response = True ,转字符串,不然是byte

        real_image_code = redis_store.get("imagecode_%s" % image_code_id)

        # b.real_image_code验证码是否有数值:删除redis在中储存的值,防止多次利用real_image_code验证

        if real_image_code:
            redis_store.delete(real_image_code)

    except Exception as e:

        current_app.logger.error(e)

        return jsonify(errno=RET.DATAERR, errmsg="查询验证码异常")

    # c.没有数值代表redis中的验证码过期-----调用前端的再次生成图片

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

    # d.比较用户填写的image_code和后端真是的real_image_code进行对比

    if image_code.lower() != real_image_code.lower():
        # f.验证码填写错误

        return jsonify(errno=RET.DATAERR, errmsg="验证码填写错误")

    # g.根据手机号码验证查询手机号码是否已经注册
    try:

        user = User.query.filter_by(mobile=mobile).first()

        if user:
            # 表示已经注册过了

            return jsonify(errno=RET.DATAEXIST, errmsg="手机号已经注册过了")

    except Exception as e:

        # current_app.logger 记录日志

        current_app.logger.error(e)

        return jsonify(errno=RET.DATAERR, errmsg="数据库查询用户对象异常")

    # h.调用云通讯的SDK发送短信验证码

    # i.生成一个6个数字的随机短信内容

    sms_code = random.randint(0, 999999)

    sms_code = "%06d" % sms_code

    result = CCP().send_template_sms(
        mobile, [sms_code, constants.IMAGE_CODE_REDIS_EXPIRES / 60], 1)

    # e.成功:发送短信验证

    if result != 0:
        return jsonify(errno=RET.THIRDERR, errmsg="发送短信验证码失败")

    # j.发送短信验证码成功,存储短信验证码到redis

    try:

        redis_store.set("SMS_%s" % mobile,
                        sms_code,
                        ex=constants.SMS_CODE_REDIS_EXPIRES)

    except Exception as e:

        current_app.logger.error(e)

        return jsonify(errno=RET.DATAERR, errmsg="保存短信验证码到数据库异常")

    # 4.返回值处理
    # a.告诉前端发送短信验证码成功,注意查收

    return jsonify(errno=RET.OK, errmsg="发送短信验证码成功")
예제 #14
0
파일: views.py 프로젝트: shy2336155/learn
def send_smscode():
    """发送短信验证码后端接口"""
    """
    1.获取参数
        1.1 mobile: 手机号码  image_code_id:uuid唯一编号  image_code:用户填写图片验证码真实值
    2.校验参数
        2.1 非空判断
        2.2 正则校验手机号码格式
    3.逻辑处理
        3.1 根据image_code_id编号去redis数据库获取正确的图片验证码值,
            3.1.1 real_image_code有值: 从数据库删除(防止多次使用这个值进行校验)
            3.1.2 real_image_code没有有值: 图片验证码过期了
        3.2 然后进行对比校验、
            3.2.1 相等 :发送短信验证码    
            3.2.2 不相等: 告诉前端图片验证码填写错误(前端再次生成一张图片 )
             
        TODO: 查询手机号码是否有注册过,不要等到注册的时候在判断(提高用户体验)         
        3.3 送短信验证码细节
            3.3.1 生成6位的随机短信值 : 123456
            3.3.2 调用封装好ccp短信验证码工具类发送 
            3.3.3 发送短信验证码成功后,保存6位的随机短信值到redis数据库
    4.返回值 
        4.1 发送短信验证码成功
    """

    # import json
    # json.loads(request.data)
    # 1.1 mobile: 手机号码  image_code_id:uuid唯一编号  image_code:用户填写图片验证码真实值
    # 提取前端发送的json格式数据,并且将数据转换成python对象
    param_dict = request.json
    mobile = param_dict.get("mobile")
    image_code = param_dict.get("image_code")
    image_code_id = param_dict.get("image_code_id")
    # 2.1 非空判断
    if not all([mobile, image_code, image_code_id]):
        current_app.logger.error("参数错误")
        # return jsonify({"errno": RET.PARAMERR, "errmsg": "参数错误"})
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")
    # 2.2 正则校验手机号码格式

    if not re.match("1[35789][0-9]{9}", mobile):
        current_app.logger.error("手机格式错误")
        return jsonify(errno=RET.PARAMERR, errmsg="手机格式错误")

    # 3.1 根据image_code_id编号去redis数据库获取正确的图片验证码值,
    try:
        real_image_code = redis_store.get("ImageCode_%s" % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询验证码真实值异常")
    # 3.1.1 real_image_code有值: 从数据库删除(防止多次使用这个值进行校验)
    if real_image_code:
        try:
            redis_store.delete("ImageCode_%s" % image_code_id)
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.DBERR, errmsg="删除验证码真实值异常")
    # 3.1.2 real_image_code没有有值: 图片验证码过期了
    else:
        current_app.logger.error("图片验证码真实值过期了")
        return jsonify(errno=RET.NODATA, errmsg="图片验证码真实值过期了")

    # 3.2 然后进行对比校验
    """
    细节:1.忽略大小写  2.real_image_code如果直接获取是二进制形式 设置 decode_responses=True 
    """
    if image_code.lower() != real_image_code.lower():
        # 3.2.2 不相等: 告诉前端图片验证码填写错误(前端再次生成一张图片 )
        current_app.logger.error("填写图片验证码错误")
        return jsonify(errno=RET.DATAERR, errmsg="填写图片验证码错误")

    # 3.2.1 相等 :发送短信验证码
    # TODO: 查询手机号码是否有注册过,不要等到注册的时候在判断(提高用户体验)
    # try是会消耗性能的,在关键的代码使用
    try:
        user = User.query.filter(User.mobile == mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询用户数据异常")
    if user:
        # 用户已经注册
        return jsonify(errno=RET.DATAEXIST, errmsg="用户已经注册")

    import random
    # 3.3 送短信验证码细节
    # 3.3.1 生成6位的随机短信值 : 123456
    # 可能不足6位
    sms_code = random.randint(0, 999999)
    # 不足6位前面用0补足
    sms_code = "%06d" % sms_code

    # 3.3.2 调用封装好ccp短信验证码工具类发送
    """
    参数1: 手机号码
    参数2; {6位的短信验证码值,分钟}
    参数3: 模板id 1:您的验证码为{1},请于{2}内正确输入,如非本人操作,请忽略此短信。
    """
    try:
        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 == -1:
        current_app.logger.error("短信验证码发送失败")
        return jsonify(errno=RET.THIRDERR, errmsg="短信验证码发送失败")

    print(sms_code)
    # 3.3.3 发送短信验证码成功后,保存6位的随机短信值到redis数据库
    try:
        redis_store.setex("SMS_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES,
                          sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="保存短信验证码异常")

    # 4.返回值
    # 4.1 发送短信验证码成功
    return jsonify(errno=RET.OK, errmsg="发送短信验证码成功")
예제 #15
0
def send_sms_code():
    """点击发送短信验证码后端接口"""
    """
    1.获取参数
        1.1 手机号码mobile,用户填写的图片验证码值image_code,image_code_id全球唯一的UUID编号
    2.校验参数
        2.1 非空判断
        2.2 手机号码格式正则判断
    3.逻辑处理
        3.1 image_code_id编号去redis数据库取出图片验证码的真实值
            3.1.1 有值: 从redis数据库删除真实值(防止拿着相同的值多次验证)
            3.1.2 没有值:图片验证码值在redis中过期
        3.2 比较用户填写的图片验证码值和真实的验证码值是否一致
            TODO: 手机号码有了(用户是否已经注册的判断,用户体验最好),
            根据手机号码去查询用户是否有注册,有注册,不需要再注册,没有注册才去发送短信验证码
            一致:填写正确,生成6位的短信验证码值,发送短信验证码
            不一致:提示图片验证码填写错误
        3.3 将生成6位的短信验证码值 存储到redis数据库
    4.返回值
        4.1 发送短信验证码成功
    """
    #1.1 手机号码mobile,用户填写的图片验证码值image_code,image_code_id全球唯一的UUID编号
    # dict = json.loads(request.data) 能够将json字符串转换成dict
    # 能够获取前端发送过来的json数据,同时能够将json字符串转换成python的对象
    param_dict = request.json
    # 手机号码
    mobile = param_dict.get("mobile", "")
    # 用户填写的图片验证值
    image_code = param_dict.get("image_code", "")
    # uuid编号
    image_code_id = param_dict.get("image_code_id", "")

    #2.1 非空判断
    if not all([mobile, image_code, image_code_id]):
        current_app.logger.error("参数不足")
        # 返回json格式的错误信息
        return jsonify({"errno": RET.PARAMERR, "errmsg": "参数不足"})
    #2.2 手机号码格式正则判断
    if not re.match('1[3578][0-9]{9}', mobile):
        # 手机号码格式有问题
        current_app.logger.error("手机号码格式错误")
        return jsonify(errno=RET.PARAMERR, errmsg="手机号码格式错误")

    real_image_code = None
    try:
        # 3.1 image_code_id编号去redis数据库取出图片验证码的真实值
        real_image_code = redis_store.get("imageCodeId_%s" % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询图片验证码真实值异常")

    #3.1.1 有值: 从redis数据库删除真实值(防止拿着相同的值多次验证)
    if real_image_code:
        redis_store.delete("imageCodeId_%s" % image_code_id)
    # 3.1.2 没有值:图片验证码值在redis中过期
    else:
        return jsonify(errno=RET.NODATA, errmsg="图片验证码值在redis中过期")

    #3.2 比较用户填写的图片验证码值和真实的验证码值是否一致
    #细节1:全部转成小写
    #细节2:设置redis数据decode操作
    if real_image_code.lower() != image_code.lower():
        return jsonify(errno=RET.DATAERR, errmsg="图片验证码填写错误")
    """
        TODO: 手机号码有了(用户是否已经注册的判断,用户体验最好),
                根据手机号码去查询用户是否有注册,有注册,不需要再注册,没有注册才去发送短信验证码
    """
    try:
        user = User.query.filter(User.mobile == mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询用户数据异常")

    # 用户存在
    if user:
        return jsonify(errno=RET.DATAEXIST, errmsg="用户已经注册")

    #一致:填写正确,生成6位的短信验证码值,发送短信验证码
    # 生成6位的短信验证码值
    sms_code = random.randint(0, 999999)
    sms_code = "%06d" % sms_code
    # print(sms_code)

    try:
        ccp = 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.THIRDERR, errmsg="云通讯发送短信验证码失败")

    #3.3 将生成6位的短信验证码值 存储到redis数据库
    try:
        redis_store.setex("SMS_CODE_%s" % mobile,
                          constants.SMS_CODE_REDIS_EXPIRES, sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="保存短信验证码到数据库异常")

    #4.返回值
    return jsonify(errno=RET.OK, errmsg="发送短信验证码成功")
예제 #16
0
def send_sms():
    """
    点击发送短信验证码接口
    :return:
    1. 获取参数
       1.1  手机号码, 用户填写的图片验证码真实值,编号
    """
    param_dict = request.json
    moblie = param_dict.get("moblie")
    image_code = param_dict.get("image_code")
    image_code_id = param_dict.get("image_code_id")
    """
    2. 校验参数
       2.1  判断 手机号码,用户填写的图片验证码真实值,编号是否为空
       2.2  手机号码格式校验
    """
    if not all([moblie, image_code, image_code_id]):
        return jsonify(errmsg="提交参数不足", errno=RET.PARAMERR)
    if re.match("1[3578][0-9]{9}]", moblie):
        return jsonify(errno=RET.PARAMERR, errmsg="手机号码格式错误")

    """
    根据编号获取的redis中存储的图片验证码真实值
    3. 逻辑处理
      image_code_id如果有值,删除,防止下次使用image_code_id来多次问访问
      如果没有值,表示编号过期
    """
    try:
        real_image_code = redis_store.get("imagecode_%s" % image_code_id)
        if real_image_code:
            redis_store.delete(real_image_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg="查询图片验证码异常")
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg="验证码真实值的已经过期")

    """
    对比用户填写的真实值和后端获取的验证码肾实质是否一致    
    """
    if image_code.lower() != real_image_code.lower():
        return jsonify(errno=RET.DATAERR, errmsg="图片验证码填写错误")

    """
    一致的话:发送短信验证码
    查看手机号码是否注册
    """
    try:
        user = User.query.fliter_by(moblie=moblie).firsr()
    except Exception as e:
        return jsonify(errno=RET.DATAERR, errmsg="查询用户手机号码是否存在异常")
    # 已经注册过
    if user:
        return jsonify(errno=RET.DATAEXIST, errmsg="用户已经注册过了")
    """
    调用云通讯发送短信验证码
    """
    sms_code = random.randint(0, 999999)
    sms_code = "%06d" % sms_code

    result = CCP().send_template_sms(moblie, [sms_code, constants.SMS_CODE_REDIS_EXPIRES/60], 1)
    """
    逻辑判断
    """
    if result != 0:
        return  jsonify(errno=RET.THIRDERR, errmsg="发送短信验证码失败")

    try:
        redis_store.set("SMS_%s" % moblie, sms_code, ex=constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="存储短信验证码真实值失败")

    """
    返回值处理
    """
    return jsonify(errno=RET.OK, errmsg="发送短信验证码成功,请查收")
예제 #17
0
def send_ams():
    """
      1. 接收参数并判断是否有值
      2. 校验手机号是正确
      3. 通过传入的图片编码去redis中查询真实的图片验证码内容
      4. 进行验证码内容的比对
      5. 生成发送短信的内容并发送短信
      6. redis中保存短信验证码内容
      7. 返回发送成功的响应
      :return:
    """
    args_data = request.json
    mobile = args_data.get("mobile")
    image_code = args_data.get('image_code')
    image_code_id = args_data.get('image_code_id')
    # print(image_code_id)
    # 1.接收参数并判断是否有值
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不全")
    # 2.校验手机号是正确
    # print(mobile)
    if not re.match("^1[3578][0-9]{9}$", mobile):
        return jsonify(errno=RET.DATAERR, errmsg='手机号不正确')
    # 3.通过传入的图片编码去redis中查询真实的图片验证码内容
    try:
        real_image_code = redis_store.get('ImageCode_' + image_code_id)
        # 如果能够取出来值,删除redis中缓存的内容
        # if real_image_code:
        #     real_image_code = real_image_code.decode()
        #     redis_store.delete('ImageCode_' + image_code_id)
    except Exception as e:
        current_app.logger.errno(e)
        return jsonify(errno=RET.DBERR, errmsg="获取图片验证码失败")
    # 判断验证码是否存在,已过期
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg="验证码已过期")

    # 4. 进行验证码内容的比对

    if real_image_code.lower() != image_code.lower():
        return jsonify(errno=RET.DATAERR, errmsg="验证码输入错误")
        # 4.1 校验该手机是否已经注册
    try:
        use = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.errno(e)
        return jsonify(errno=RET.DBERR, errmsg="数据库查询错误")
    if use:
        return jsonify(errno=RET.DATAEXIST, errmsg="该手机已被注册")
    # 5. 生成发送短信的内容并发送短信
    result = random.randint(0, 999999)
    sms_code = '%06d' % result
    current_app.logger.debug("短信验证码的内容:%s" % sms_code)
    result = CCP().send_template_sms(mobile, [sms_code, 5], "1")
    print(sms_code)
    if result != 0:
        return jsonify(errno=RET.THIRDERR, errmsg="发送短信失败")

    # 6.redis中保存短信验证码内容
    try:
        redis_store.set("sms_" + mobile, sms_code,
                        constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.errno(e)
        return jsonify(errno=RET.DBERR, errmsg="保存短信验证码失败")
    # 7. 返回发送成功的响应
    return jsonify(errno=RET.OK, errmsg="发送成功")
예제 #18
0
def send_code():
    """发送短信验证码"""
    # 1,获取数据
    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,校验数据
    # 2.1 校验手机号是正确
    if not re.match(r'^1[3-9]\d{9}$', mobile):
        return jsonify(errno=RET.DATAERR, errmsg='手机号错误')
    # 2.2 通过传入的图片编码去redis中查询真实的图片验证码内容
    try:
        real_image_code = redis_store.get('ImageCode_' + image_code_id)
        # 如果能够取出来值,删除redis中缓存的内容
        if real_image_code:
            real_image_code = real_image_code.decode()
            redis_store.delete('ImageCode_' + 'image_code_id')
    except Exception as e:
        current_app.logger.error(e)
        # 获取图片验证码失败
        return jsonify(errno=RET.DBERR, errmsg='获取图片验证码失败')
    # 2.3 判断图片验证码是否过期
    if not real_image_code:
        # 验证码已过期
        return jsonify(errno=RET.NODATA, errmsg='验证码已过期')
    # 3,比较验证码是否一致
    if real_image_code.lower() != image_code.lower():
        return jsonify(errno=RET.DATAERR, errmsg='验证码错误')

    # 4,校验该手机是否已经注册
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询错误')
    # 判断手机号是否注册
    if user:
        return jsonify(errno=RET.DATAEXIST, errmsg='手机号已注册')

    # 5,生成短信验证码
    code = random.randint(0, 999999)
    sms_code = '%06d' % code
    # 5.1记录发送短信异常
    current_app.logger.debug("短信验证码的内容:%s" % sms_code)
    # 5.2 调用第三方工具发送短信验证码
    result = CCP().send_template_sms(
        mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES // 60], '1')
    if result != 0:
        return jsonify(errno=RET.THIRDERR, errmsg='短信发送失败')
    # 6,保存短信验证码
    try:
        redis_store.set('SMS_' + mobile, sms_code,
                        constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存短信验证码失败')

    # 7,返回响应内容
    return jsonify(errno=RET.OK, errmsg='短信发送成功')
예제 #19
0
def send_sms_code():
    """
    1获取参数
        获取三个参数:image_code_id,mobile,image_code
    2校验参数
    3逻辑处理
    4返回值
    """
    # request.data
    # 1获取参数
    param_dict = request.json
    mobile = param_dict["mobile"]
    image_code = param_dict["image_code"]
    image_code_id = param_dict["image_code_id"]
    # 2校验参数
    if not all([mobile, image_code, image_code_id]):
        current_app.logger.error("参数错误")
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")
        # return jsonify({"errno": RET.PARAMERR, "errmsg": "参数错误"})
    # print(1)
    if not re.match(r"1[3578][0-9]{9}", mobile):
        current_app.logger.error("手机号格式错误")
        return jsonify(errno=RET.PARAMERR, errmsg="手机号格式错误")
    # 3比对参数
    # 真实值对比,并删除redis中真实值
    try:
        real_image_id = redis_store.get("Image_%s" % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="真实值异常")
    if real_image_id:
        try:
            redis_store.delete("Image_%s" % image_code_id)
        except Exception as e:
            current_app.logger.error("删除真实值错误")
            return jsonify(errno=RET.DBERR, errmsg="真实值异常")
    else:
        current_app.logger.error("删除真实值异常")
        return jsonify(errno=RET.DBERR, errmsg="删除真实值异常")
    # 细节1,大小写2,redis取出的值需要解码,bytes类型
    if real_image_id.lower() != image_code.lower():
        current_app.logger.error("传入的值与图片不符")
        return jsonify(errno=RET.NODATA, errmsg="传入的值与图片不符")

    # 4相等,发送短信
    try:
        User.query.filter(User.mobile == mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAEXIST, errmsg="用户已存在")
    # 生成6位验证码
    import random
    sms_code = random.randint(0, 999999)
    sms_code = "%06d" % sms_code

    # 调用ccp发送短信
    """
    参数1: 手机号码
    参数2; {6位的短信验证码值,分钟}
    参数3: 模板id 1:您的验证码为{1},请于{2}内正确输入,如非本人操作,请忽略此短信。
    """
    try:
        result = CCP().send_template_sms(
            mobile, {constants.SMS_CODE_REDIS_EXPIRES / 60, sms_code}, 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg="短信验证码失败")
    if result == -1:
        current_app.logger.error("短信验证码失败")
        return jsonify(errno=RET.DATAEXIST, errmsg="短信验证码失败")
    print(sms_code)
    # 发送短信验证码成功,保存6位的随机短信值到redis数据库
    try:
        redis_store.setex("SMS_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES,
                          sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="保存短信验证码异常")
    # finally
    return jsonify(errno=RET.OK, errmsg="发送短信验证码成功")
예제 #20
0
def send_sms():
    """
    1. 接收参数并判断是否有值
    2. 校验手机号是正确
    3. 通过传入的图片编码去redis中查询真实的图片验证码内容
    4. 进行验证码内容的比对
    5. 生成发送短信的内容并发送短信
    6. redis中保存短信验证码内容
    7. 返回发送成功的响应
    :return:
    """
    # 1. 接收参数并判断是否有值
    # 取到请求值中的内容
    param_dict = request.json
    mobile = param_dict.get("mobile")
    image_code = param_dict.get("image_code")
    image_code_id = param_dict.get("image_code_id")

    if not all([mobile, image_code_id, image_code]):
        # 参数不全
        return jsonify(errno=RET.PARAMERR, errmsg="参数不全")

    # 2. 校验手机号是正确
    if not re.match("^1[3578][0-9]{9}$", mobile):
        # 提示手机号不正确
        return jsonify(errno=RET.DATAERR, errmsg="手机号不正确")

    # 3. 通过传入的图片编码去redis中查询真实的图片验证码内容
    try:
        real_image_code = redis_store.get("ImageCode_" + image_code_id)
        # 如果能够取出来值,删除redis中缓存的内容
        if real_image_code:
            real_image_code = real_image_code.decode()
            redis_store.delete("ImageCode_" + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        # 获取图片验证码失败
        return jsonify(errno=RET.DBERR, errmsg="获取图片验证码失败")
    # 3.1 判断验证码是否存在,已过期
    if not real_image_code:
        # 验证码已过期
        return jsonify(errno=RET.NODATA, errmsg="验证码已过期")

    # 4. 进行验证码内容的比对
    if image_code.lower() != real_image_code.lower():
        # 验证码输入错误
        return jsonify(errno=RET.DATAERR, errmsg="验证码输入错误")

    # 4.1 校验该手机是否已经注册
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据库查询错误")
    if user:
        # 该手机已被注册
        return jsonify(errno=RET.DATAEXIST, errmsg="该手机已被注册")

    # 5. 生成发送短信的内容并发送短信
    result = random.randint(0, 999999)
    sms_code = "%06d" % result
    current_app.logger.debug("短信验证码的内容:%s" % sms_code)
    result = CCP().send_template_sms(mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], "1")
    if result != 0:
        # 发送短信失败
        return jsonify(errno=RET.THIRDERR, errmsg="发送短信失败")

    # 6. redis中保存短信验证码内容
    try:
        redis_store.set("SMS_" + mobile, sms_code, constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        # 保存短信验证码失败
        return jsonify(errno=RET.DBERR, errmsg="保存短信验证码失败")

    # 7. 返回发送成功的响应
    return jsonify(errno=RET.OK, errmsg="发送成功")
예제 #21
0
def send_sms_code():
    """发送短信验证码后端接口"""
    """
    1.获取参数
        1.1 mobile: 手机号码,image_coed:用户填写的图形验证码值, image_code_id: UUID唯一编号
        1.2 数据是通过json格式上传的:request.json
    2.校验参数
        2.1 非空判断
        2.2 使用正则判断手机号码格式是否正确
    3.逻辑处理
        3.1 根据image_code_id编号提取redis数据库中,真实的图形验证码值real_image_code
        real_image_code有值:从redis数据库中删除(一个图形验证码只校验一次)
        real_image_code没有值:图形验证码过期了
        
        3.2 将用户填写的image_code和真实的图形验证码值进行比对
        #细节: 1.忽略大小写, 2.decode_responses=True,保持数据都是字符串类型
        
        3.3 不相等:提示前端图形验证码填写错误,前端:重新生成一张验证码图片
        
        TODO: 判断手机号码是否已经注册 【提高用户体验】
        
        3.4 相等:调用云通信工具类,发送短信验证码
        3.4.1 生成6位的随机短信
        3.4.2 调用CPP对象的send_template_sms发送短信验证码
        
        3.4.3 发送短信验证码失败:告知前端
        3.4.4 发送短信验证码成功:使用redis数据库保存正确的短信验证码值
        
    4.返回数据
        发送短信验证码成功 OK = 0
    """
    # 1.1 mobile: 手机号码,image_coed:用户填写的图形验证码值, image_code_id: UUID唯一编号
    param_dict = request.json
    mobile = param_dict.get("mobile")
    image_code = param_dict.get("image_code")
    image_code_id = param_dict.get("image_code_id")

    #  2.1 非空判断
    if not all([mobile, image_code, image_code_id]):
        # 参数不足
        current_app.logger.error("参数不足")
        # 返回错误信息
        err_dict = {"errno": RET.PARAMERR, "errmsg": "参数不足"}
        return jsonify(err_dict)

    #  2.2 使用正则判断手机号码格式是否正确
    if not re.match(r"1[3578][0-9]{9}", mobile):
        return jsonify(errno=RET.PARAMERR, errmsg="手机号码格式错误")

    # 3.1 根据image_code_id编号提取redis数据库中,真实的图形验证码值real_image_code
    try:
        real_image_code = redis_store.get("Iamge_Code_%s" % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询redis图形验证码异常")

    # real_image_code有值:从redis数据库中删除(一个图形验证码只校验一次)
    if real_image_code:
        # 只是redis数据库删除了数据,变量中还有
        redis_store.delete("Iamge_Code_%s" % image_code_id)

    # real_image_code没有值:图形验证码过期了
    else:
        return jsonify(errno=RET.NODATA, errmsg="图形验证码过期了")

    # 3.2 将用户填写的image_code和真实的图形验证码值进行比对
    # 细节: 1.忽略大小写, 2.decode_responses=True,保持数据都是字符串类型
    if image_code.lower() != real_image_code.lower():
        # # 3.3 不相等:提示前端图形验证码填写错误,前端:获取4004错误状态码,重新生成一张验证码图片
        return jsonify(errno=RET.DATAERR, errmsg="图形验证码填写错误")

    # TODO: 判断手机号码是否已经注册 【提高用户体验】
    try:
        user = User.query.filter(User.mobile == mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询用户数据异常")

    if user:
        # 当前手机号码已经注册
        return jsonify(errno=RET.DATAEXIST, errmsg="手机号码已经注册")

    # 3.4 相等:调用云通信工具类,发送短信验证码

    # 3.4.1 生成6位的随机短信
    real_sms_code = random.randint(0, 999999)
    # 不足6位,前面补零  000001
    real_sms_code = "%06d" % real_sms_code

    # 3.4.2 调用CPP对象的send_template_sms发送短信验证码
    # 参数1:发送到那个手机号码,参数2:发送短信的内容 参数3: 模板id
    try:
        result = CCP().send_template_sms(mobile, {real_sms_code, 5}, 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg="云通信发送短信验证码异常")

    # 3.4.3 发送短信验证码失败:告知前端
    if result == -1:
        return jsonify(errno=RET.THIRDERR, errmsg="云通信发送短信验证码异常")

    # 3.4.4 发送短信验证码成功:使用redis数据库保存正确的短信验证码值
    redis_store.setex("SMS_CODE_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES, real_sms_code)

    # 4.发送短信验证码成功
    return jsonify(errno=RET.OK, errmsg="发送短信验证码成功")
예제 #22
0
파일: views.py 프로젝트: aaLiuQ/Information
def send_sms_code():
    """
    /passport/sms_code
    传入参数:JSON格式:mobile,image_code,image_code_id(uuid)
    :return:errno,errmsg
    """
    param_dict = request.json
    mobile = param_dict.get("mobile")
    image_code = param_dict.get("image_code")
    image_code_id = param_dict.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[3-9]\d{9}', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg="手机号码格式错误")
    # 根据image_code_id去redis中获取图形验证码的值
    try:
        real_image_code = redis_store.get("imageCode_%s" % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据异常")
    # 图形验证码有值,将值删除,避免多次重复验证
    if real_image_code:
        redis_store.delete("imageCode_%s" % image_code_id)
    else:
        # 没有值即代表过期
        return jsonify(errno=RET.NODATA, errmsg="图形验证码过期")
    # 将图片验证码进行对比
    if image_code.lower() != real_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.DATAERR, errmsg="数据库查询异常")
    if user:
        return jsonify(errno=RET.DATAEXIST, errmsg="此号码已经注册过")
    # 发送短信
    # 生成随机的六位数
    real_sms_code = random.randint(0, 999999)
    real_sms_code = "%6d" % real_sms_code
    print(real_sms_code)
    # 调用CCP类发送短信验证码
    ccp = CCP()
    try:
        result = ccp.send_template_sms(
            mobile, [real_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 == -1:
        return jsonify(errno=RET.THIRDERR, errmsg="云通信发送短信验证码异常")
    # 将短信验证码保存到数据库以备后续
    try:
        redis_store.setex("SMS_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES,
                          real_sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="保存短信验证码值异常")

    # 4.1 发送短信验证码成功
    return jsonify(errno=RET.OK, errmsg="发送短信验证码成功")
예제 #23
0
def send_sms_code():
    """点击发送验证码后端端口"""
    """
    1. 获取参数
        1.1 用户账号手机号码mobile, 用户填写的图片验证码值:image_code, 编号UUID:image_code_id
    2. 校验参数
        2.1 非空判断 mobile, image_code, image_code_id是否有空
        2.2 手机号码格式的正则判断
    3. 逻辑处理
        3.1 根据编号取redis数据库获取图片验证码的真实值(正确值)
            3.1.1 真是值有值:将这个值从redis中删除(防止他人多次拿同一个验证码值来验证)
            3.1.2 真实值没有值:图片验证码真实值过期了
        3.2 拿用户填写的图片验证码值和redis中获取的真实值进行比较
        3.3 如果不相等 告诉前端图片验证码填写错误
        TODO: 判断用户是否注册过,如果注册过,就不在发送短信验证码直接引导到登录页(在这里做有助于提高用户的体验)
        3.4 如果相等 生成6位随机短信验证码,然后发送短信验证码
        3.4.1将生成的6位随机短信验证码存储到redis数据库中
    4. 返回值
        4.1 发送短信验证码成功
    """

    #  上传数据是json类型
    # 1.1用户账号手机号码mobile, 用户填写的图片验证码值:image_code, 编号UUID:image_code_id
    # json.loads(request.data)
    # 可以接受前端上传的json格式数据,json字符串转换成python对象
    param_dict = request.json
    # 手机号码
    mobile = param_dict.get("mobile", "")
    # 用户填写的图片验证码值
    image_code = param_dict.get("image_code", "")
    # UUID编号
    image_code_id = param_dict.get("image_code_id", "")

    # 2.1非空判断 mobile, image_code, image_code_id是否有空
    if not all([mobile, image_code, image_code_id]):
        # 记录日志
        current_app.logger.error("参数不足")
        # 给调用者返回json格式的错误信息
        return jsonify({"errno": RET.PARAMERR, "errmsg": "参数不足"})
    # 2.2 手机号码格式的正则判断
    if not re.match('1[35789][0-9]{9}', mobile):
        current_app.logger.error("手机格式错误")
        return jsonify(errno=RET.PARAMERR, errmsg="手机格式错误")
    # 3.1根据编号取redis数据库获取图片验证码的真实值(正确值)
    try:
        real_image_code = redis_store.get("imageCodeId_%s" % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="从redis中获取图片真实值异常")
    # 3.1.1真是值有值:将这个值从redis中删除(防止他人多次拿同一个验证码值来验证)
    if real_image_code:
        redis_store.delete("imageCodeId_%s" % image_code_id)
    # 3.1.2 真实值没有值:图片验证码真实值过期了
    else:
        return jsonify(errno=RET.NODATA, errmsg="图片验证码过期")

    # 3.2拿用户填写的图片验证码值和redis中获取的真实值进行比较
    # 细节1: 全部按照小写格式进行比较(忽略大小写)
    # 细节2: redis对象创建的时候设置decode_response=True(将二进制数据转换成字符串)
    if real_image_code.lower() != image_code.lower():
        # 3.3如果不相等告诉前端图片验证码填写错误
        return jsonify(errno=RET.DATAERR, errmsg="填写图片验证码错误")

    # TODO: 判断用户是否注册过,如果注册过,就不在发送短信验证码直接引导到登录页(在这里做有助于提高用户的体验)
    try:
        user = User.query.filter(User.mobile == mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="从mysql中查询用户异常")
    # 注册过就不在发送短信验证码直接引导到登录页
    if user:
        return jsonify(errno=RET.DATAEXIST, errmsg="手机号码已经注册过")

    # 3.4 如果相等 生成6位随机短信验证码,然后发送短信验证码
    # 生成6位的随机短信验证码
    sms_code = random.randint(0, 99999)
    # 不足6位在前面补0
    sms_code = "%06d" % sms_code
    try:  # SMS_CODE_REDIS_EXPIRES/60(短信验证码redis有效期) = 5分钟
        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.THIRDERR, errmsg="云通讯发送短信失败")

    # 3.4.1将生成的6位随机短信验证码存储到redis数据库中
    try:
        # SMS_CODE_18620593492 每个用户的key都不一样
        redis_store.setex("SMS_CODE_%s" % mobile,
                          constants.SMS_CODE_REDIS_EXPIRES, sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="存储短信验证码异常")

    # 返回值
    return jsonify(errno=RET.OK, errmsg="发送短信验证码成功,请主要查收")
예제 #24
0
def send_sms():
    """
    1. 接受参数并判断是否有值
    2. 校验手机号是否正确
    3. 通过传入的图片编码去redis中查询真是的图片验证码内容
    4. 进行验证码内容的比对
    5. 生成发送短信的内容并发送短信
    6. redis中保存短信验证码内容
    7. 返回发送成功的响应
    :return:
    """
    # 1. 接受参数并判断是否有值
    # 渠道请求值中的内容
    param_dict = request.json
    mobile = param_dict.get('mobile')
    image_code = param_dict.get('image_code')
    image_code_id = param_dict.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[3-9]\d{9}$', mobile):
        # 提示手机号不正确
        return jsonify(errno=RET.DATAERR, errmsg='手机号不正确')

    # 3. 通过传入的图片编码去redis中查询真实的图片验证码内容
    try:
        real_image_code = redis_store.get('ImageCode_' + image_code_id)
        # 如果能够取出来值,则删除redis中缓存的内容
        if real_image_code:
            real_image_code = real_image_code.decode()
            redis_store.delete('ImageCode_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        # 获取图片验证码失败
        return jsonify(errno=RET.DBERR, errmsg='获取图片验证码失败')

    # 3.1 判断验证码是否已过期
    if not real_image_code:
        # 验证码已过期
        return jsonify(errno=RET.NODATA, errmsg='验证码已过期')

    # 4. 进行验证码内容的比对
    if image_code.lower() != real_image_code.lower():
        # 验证码输入错误
        return jsonify(errno=RET.DATAERR, errmsg='验证码输入错误')

    # 4.1 校验该手机是否已经注册
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据库查询错误')
    if user:
        # 该手机已被注册
        return jsonify(errno=RET.DATAEXIST, errmsg='该手机已被注册')

    # 5. 生成发送短信的内容并发送短信
    result = random.randint(0, 999999)
    sms_code = "%06d" % result
    current_app.logger.debug("短信验证码的内容: %s" % sms_code)
    ccp = CCP()
    result = ccp.send_template_sms(mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES/60], "1")
    if result != 0:
        # 发送短信失败
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信失败')

    # 6. redis中保存短信验证码内容
    try:
        redis_store.set('SMS_' + mobile, sms_code, constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        # 保存短信验证码失败
        return jsonify(errno=RET.DBERR, errmsg='保存短信验证码失败')

    # 7. 返回发送成功的响应
    return jsonify(errno=RET.OK, errmsg='发送成功')
예제 #25
0
def send_sms():
    # 1. 获取数据
    param_dict = request.json
    mobile = param_dict.get("mobile")
    imagecode = param_dict.get("imageCode")
    imagecodeId = param_dict.get("imageCodeId")

    # 2. 数据判断
    # 2.1 判断值是否都已经输入了
    if not all([mobile, imagecode, imagecodeId]):
        return jsonify(erron=RET.PARAMERR, errmes="参数不足")
    # 2.2 验证手机号码格式
    if not re.match("^1[356789][0-9]{9}$", mobile):
        return jsonify(erron=RET.PARAMERR, errmes="手机格式有误")

    # 3. 逻辑处理

    try:
        # 3.1 根据image_code_id编号去redis中获取验证码的真实值
        # 注意一定要在redis创建的时候设置这个decode_responses=True属性
        real_image_code = redis_store.get("imageCode_%s" %
                                          imagecodeId)  # (上一个方法中设置了值的加密格式)
        # 3.2 如果值存在就把它删除
        if real_image_code:
            redis_store.delete("imageCode_%s" % imagecodeId)
    except Exception as e:
        current_app.logger.error(e)

    # 3.3 如果不存在,说明验证码已过期
    if not real_image_code:
        return jsonify(erron=RET.NODATA, errmes="验证码已过期")
    # 3.4 根据取出的验证码值与输入的进行比较.如果失败,报错。
    if imagecode.lower() != real_image_code.lower():
        return jsonify(erron=RET.DATAERR, errmes="验证码填写错误")

    try:
        # 3.5 比较成功,进行用户查询是否已经存在
        user = User.query.filter_by(mobile=mobile).first()
        # 3.6 如用户存在,报错。
        if user:
            return jsonify(erron=RET.DATAEXIST, errmes="用户已存在")
    except Exception as e:
        # 记录日志
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询用户对象异常")

    # 3.7 用户不存在,就准备调用第三方接口,生成6为随机数
    sms_code = random.randint(0, 999999)
    sms_code = "%06d" % sms_code

    print(sms_code)
    # 3.8 使用云通讯的发送短信接口进行发送
    result = CCP().send_template_sms(
        mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 5 / 60], 1)

    # 3.9 判断是否发送成功
    if result:
        return jsonify(erron=RET.THIRDERR, errmes="短信验证码发送失败")

    # 3.10 如果发送成功,就使用redis保存
    try:
        redis_store.set("sms_%s" % mobile,
                        sms_code,
                        ex=constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="保存短信验证码到数据库异常")

    # 4. 返回结果
    return jsonify(erron=RET.OK, errmes="短信验证码发送成功")
예제 #26
0
def send_sms_code():
    """发送短信验证码的后端接口"""
    """
    1.获取参数
        1.1 mobile: 手机号码, image_code:用户填写的图片验证码值, image_code_id: UUID编号
    2.校验参数
        2.1 非空判断
        2.2 手机号码格式校验
    3.逻辑处理
        3.1 根据image_code_id编号去redis中获取正确真实的图片验证码值
            3.1.1 真实的图片验证码有值:将值从redis数据库删除 [避免拿着这个值多次判断]
            3.1.2 真实的图片验证码没有值:图片验证码值过期了
        3.2 比对用户填写的图片验证码值 & 正确的图片验证码真实值
        3.3 不相等:返回错误状态码,提示图片验证码填写错误
        TODO: 提前判断手机号码是否注册过,数据库查询 [提高用户体验]
        3.4 发送短信验证码
        3.4.1 生成6位的随机短信验证码值
        3.4.2 调用CCP类发送短信验证码
        3.4.3 发送短信验证码成功后,保存6位的短信验证码值到redis数据库
    4.返回值
        4.1 发送短信验证码成功
    """
    # 1.1 mobile: 手机号码, image_code:用户填写的图片验证码值, image_code_id: UUID编号
    # 自动将json数据转换成字典
    param_dict = request.json
    mobile = param_dict.get("mobile")
    image_code = param_dict.get("image_code")
    image_code_id = param_dict.get("image_code_id")

    # 2.1 非空判断
    if not all([mobile, image_code, image_code_id]):

        # 参数不足 并且返回json格式数据
        # return jsonify({"errno": RET.PARAMERR, "errmsg": "参数不足"})
        return jsonify(errno=RET.PARAMERR, errmsg="参数不足")

    # 2.2 手机号码格式校验
    if not re.match(r"1[3546789][0-9]{9}", mobile):
        return jsonify(errno=RET.PARAMERR, errmsg="手机号码格式错误")

    # 3.1 根据image_code_id编号去redis中获取正确真实的图片验证码值
    try:
        real_image_code = redis_store.get("imageCode_%s" % image_code_id)
    except Exception as e:
        # 使用flask方式记录日志
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询图片验证码真实值数据异常")

    # 3.1.1 真实的图片验证码有值:将值从redis数据库删除 [避免拿着这个值多次判断]
    if real_image_code:
        redis_store.delete("imageCode_%s" % image_code_id)
    # 3.1.2 真实的图片验证码没有值:图片验证码值过期了
    else:
        return jsonify(errno=RET.NODATA, errmsg="图片验证码值过期了")

    # 3.2 比对用户填写的图片验证码值 & 正确的图片验证码真实值
    # 注意:忽略大小写 设置decode_responses=True
    if image_code.lower() != real_image_code.lower():
        # 4004 错误状态码,前端获取到后,需要重新生成一张图片
        # 3.3 不相等:返回错误状态码,提示图片验证码填写错误
        return jsonify(errno=RET.DATAERR, errmsg="图片验证码填写错误")

    # TODO: 提前判断手机号码是否注册过,数据库查询 [提高用户体验]
    try:
        user = User.query.filter(User.mobile == mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询用户对象异常")

    if user:
        # 用户已经注册
        return jsonify(errno=RET.DATAEXIST, errmsg="用户手机号码已经注册")

    # 3.4 发送短信验证码
    # 3.4.1 生成6位的随机短信验证码值
    real_sms_code = random.randint(0, 999999)
    # 不足6位前面补零
    real_sms_code = "%06d" % real_sms_code
    # 3.4.2 调用CCP类发送短信验证码
    ccp = CCP()
    try:
        result = ccp.send_template_sms(
            mobile, [real_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 == -1:
        return jsonify(errno=RET.THIRDERR, errmsg="云通信发送短信验证码异常")

    # 3.4.3 发送短信验证码成功后,保存6位的短信验证码值到redis数据库[方便注册接口使用]
    try:
        redis_store.setex("SMS_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES,
                          real_sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="保存短信验证码值异常")

    # 4.1 发送短信验证码成功
    return jsonify(errno=RET.OK, errmsg="发送短信验证码成功")
예제 #27
0
def send_sms_code():
    """
    发送短信的逻辑
    1. 获取参数:手机号,图片验证码内容,图片验证码的编号 (随机值)
    2. 校验参数(参数是否符合规则,判断是否有值)
    3. 先从redis中取出真实的验证码内容
    4. 与用户的验证码内容进行对比,如果对比不一致,那么返回验证码输入错误
    5. 如果一致,生成验证码的内容(随机数据)
    6. 发送短信验证码
    7. 告知发送结果
    :return:
    """
    '{"mobiel": "18811111111", "image_code": "AAAA", "image_code_id": "u23jksdhjfkjh2jh4jhdsj"}'
    # 1. 获取参数:手机号,图片验证码内容,图片验证码的编号 (随机值)
    # params_dict = json.loads(request.data)
    param_dict = request.json

    mobile = param_dict.get("mobile")
    image_code = param_dict.get("image_code").upper()

    image_code_id = param_dict.get("image_code_id")

    # 2. 校验参数(参数是否符合规则,判断是否有值)
    # 判断参数是否有值
    if not all([mobile, image_code, image_code_id]):
        # {"errno": "4100", "errmsg": "参数有误"}
        return jsonify(errno=RET.PARAMERR, errmsg="参数有误")

    # 校验手机号是否正确
    if not re.match('^(13\d|14[5|7]|15\d|166|17[3|6|7]|18\d)\d{8}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg="手机号格式不正确")

    # 3. 先从redis中取出真实的验证码内容
    try:
        real_image_code = redis_store.get("ImageCodeId_" + 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="图片验证码已过期")

    # 4. 与用户的验证码内容进行对比,如果对比不一致,那么返回验证码输入错误
    if real_image_code.upper() != image_code.upper():
        return jsonify(errno=RET.DATAERR, errmsg="验证码输入错误")

    # 5. 如果一致,生成短信验证码的内容(随机数据)
    # 随机数字 ,保证数字长度为6位,不够在前面补上0

    sms_code_str = "%06d" % random.randint(0, 999999)

    current_app.logger.debug("短信验证码内容是:%s" % sms_code_str)
    # 6. 发送短信验证码

    print(mobile, sms_code_str)
    result = CCP().send_template_sms(mobile, [sms_code_str, constants.SMS_CODE_REDIS_EXPIRES / 5], 1)
    if result != 0:
        # 代表发送不成功
        return jsonify(errno=RET.THIRDERR, errmsg="发送短信失败")

    # 保存验证码内容到redis
    try:
        redis_store.set("SMS_" + mobile, sms_code_str, constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据保存失败")

    # 7. 告知发送结果
    return jsonify(errno=RET.OK, errmsg="发送成功")
예제 #28
0
def send_sms():
    """

       3. 通过传入的图片编码去redis中查询真实的图片验证码内容
       4. 进行验证码内容的比对
       5. 生成发送短信的内容并发送短信
       6. redis中保存短信验证码内容
       7. 返回发送成功的响应
       :return:
    """
    # 1. 接收参数并判断是否有值
    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(error=RET.PARAMERR, errmsg='参数不全')

    # 2. 校验手机号是正确
    if not re.match("^1[3578][0-9]{9}$", mobile):
        # 提示手机号不正确
        return jsonify(errno=RET.DATAERR, errmsg="手机号不正确")

    # 3.通过传入的图片编码去redis中查询真实的图片验证码内容
    try:
        real_image_code = redis_client.get('ImageCode:' + image_code_id)
        if real_image_code:
            redis_client.delete('ImageCode:' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(error=RET.DBERR, errmsg='获取图片验证码失败')
    # 判断验证码是否已过期
    if not real_image_code:
        return jsonify(error=RET.NODATA, errmsg="验证码已过期")

    # 4, 检验该手机是否已注册
    try:
        user = User.query.filter(User.mobile==mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(error=RET.DBERR, errmsg="数据库查询错误")
    if user:
        return jsonify(errno=RET.DATAEXIST, errmsg="该手机已被注册")

    # 5,生成短信内容并验证
    result = random.randint(0, 999999)
    sms_code = '%06d' % result
    current_app.logger.debug('短信验证码的内容:%s' % sms_code)
    result = CCP().send_template_sms(mobile,  [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], "1")

    # TODO 没用容联云 先注释了保证验证码可以保存
    # if result != 0:
    #     return jsonify(errno=RET.THIRDERR, errmsg="发送短信失败")

    # 6. redis中保存短信验证码内容
    try:
        redis_client.set("SMS_" + mobile, sms_code, constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        # 保存短信验证码失败
        return jsonify(errno=RET.DBERR, errmsg="保存短信验证码失败")

    # 7. 返回发送成功的响应
    return jsonify(errno=RET.OK, errmsg="发送成功")
예제 #29
0
파일: views.py 프로젝트: FoginhaudAs/xjzx
def send_sms_code():
    """发送短信验证码接口"""

    # 1.获取参数
    # 1.1 mobile: 手机号码, image_code:用户填写的图片验证码值, image_code_id: UUID编号
    param_dict = request.json
    mobile = param_dict.get('mobile')
    image_code = param_dict.get('image_code')
    image_code_id = param_dict.get('image_code_id')
    # 2.校验参数
    # 2.1 非空判断
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不足')
    # 2.2 手机号码格式校验
    if not re.match(r'1[35678][0-9]{9}', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号码有误')

    # 3.逻辑处理
    # 3.1 根据image_code_id编号去redis中获取正确真实的图片验证码值
    try:
        real_image_code = redis_obj.get('imageCode_%s' % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询图片验证码真实值错误')

    if real_image_code:
        # 3.1.1 真实的图片验证码有值:将值从redis数据库删除 [避免拿着这个值多次判断]
        redis_obj.delete(real_image_code)
    else:
        # 3.1.2 真实的图片验证码没有值:图片验证码值过期了
        return jsonify(errno=RET.NODATA, errmsg='图片验证码已过期')
    # 3.2 比对用户填写的图片验证码值 & 正确的图片验证码真实值
    if real_image_code.lower() != image_code.lower():
        # 3.3 不相等:返回错误状态码,提示图片验证码填写错误
        return jsonify(errno=RET.DATAERR, errmsg='验证码填写错误')
    # TODO: 提前判断手机号码是否注册过,数据库查询 [提高用户体验]

    try:
        user = User.query.filter(User.mobile == mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='用户注册信息查询错误')
    if user:
        return jsonify(errno=RET.DATAEXIST, errmsg='手机号码已注册')
    # 3.4 发送短信验证码
    # 3.4.1 生成6位的随机短信验证码值
    real_sms_code = '%06d' % random.randint(0, 999999)
    # 3.4.2 调用CCP类发送短信验证码
    try:
        result = CCP().send_template_sms('18344015034', [real_sms_code, SMS_CODE_REDIS_EXPIRES/60], 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg='短信验证码发送失败')

    if result == -1:
        return jsonify(errno=RET.PARAMERR, errmsg='参数不足')
    # 3.4.3 发送短信验证码成功后,保存6位的短信验证码值到redis数据库
    redis_obj.setex('SMS_%s' % mobile, SMS_CODE_REDIS_EXPIRES, real_sms_code)
    # 4.返回值
    # 4.1 发送短信验证码成功
    return jsonify(errno=RET.OK, errmsg='短信验证码发送成功')
예제 #30
0
def send_sms():
    """
    1.接收参数判断是否为空
    2.校验手机号码格式
    3.通过id在redis查询验证码是否一致
    4.生成短信验证码进行发送
    5.redis存储短信验证码
    6.相应发送成功状态
    """
    # 获取参数和对应类型获取方式高版本才有json,其他是data
    param_dice = request.json
    mobile = param_dice.get('mobile')
    image_code = param_dice.get('image_code')
    image_code_id = param_dice.get('image_code_id')
    # 判断接收参数是否为空
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不全')
    # 判断手机号码格式
    if not re.match("^1[3-9][0-9]{9}$", mobile):
        return jsonify(errno=RET.DATAERR, errmsg="手机号不正确")
    #判断图片验证码
    try:
        real_image_code = redis_store.get("ImageCode_" + image_code_id)
        #取出则表示没有报错
        if real_image_code:
            # 进行解码赋值给变量
            real_image_code = real_image_code.decode()
            # 删除redis数据库中的编码
            redis_store.delete("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="验证码已过期")
    if image_code.lower() != real_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='该手机已被注册')

    # 生成随机短信验证码
    result = random.randint(0, 999999)
    global sms_code
    sms_code = "%06d" % result
    current_app.logger.debug('短信验证码内容为:%s' % sms_code)
    # 发送短信,参数为 电话,[内容,有效时间],模式
    result = CCP().send_template_sms(
        mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], "1")
    if result != 0:
        # 发送失败
        #发短信接口太贵改成显示验证码
        #return jsonify(errno=RET.THIRDERR, errmsg='发送短信失败')
        return jsonify(errno=RET.THIRDERR,
                       errmsg='短信接口太贵系列改为验证特征码:%s' % sms_code)

    #将验证码保存在redis中
    try:
        redis_store.set('SMS_' + mobile, sms_code,
                        constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        jsonify(errno=RET.DBERR, errmsg='保存验证码失败')

    return jsonify(errno=RET.OK, errmsg="发送成功")