Exemplo n.º 1
0
def sms_code():
    # 获取参数
    dict_data = request.json
    mobile = dict_data.get("mobile")
    image_code = dict_data.get("image_code")
    image_code_id = dict_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[35789]\d{9}", mobile):
        return jsonify(errno=RET.DATAERR, errmsg="手机号格式错误")
    # 通过验证码编号取出,redis中的图片验证码A
    try:
        redis_image_code = redis_store.get("image_code:%s" % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="获取图片验证码异常")
    # 判断验证码A是否过期
    if not redis_image_code:
        return jsonify(errno=RET.NODATA, errmsg="图片验证码已过期")
    try:
        # 未过期,就删除redis中的图片验证码
        redis_store.delete("image_code:%s" % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="图片验证码操作异常")
    # 判断验证码A和传入进来的验证码B是否相等
    if image_code.lower() != redis_image_code.lower():
        return jsonify(errno=RET.DATAERR, errnsg="图片验证码填写错误")
    # 生成验证码
    sms_code = "%06d" % random.randint(0, 999999)
    current_app.logger.debug("短信验证码:%s" % sms_code)

    # 发送短信验证码,调用ccp
    ccp = CCP()
    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:
        return jsonify(errno=RET.DATAERR, errmsg="短信发送失败")

    # 储存短信验证码到redis中
    try:
        redis_store.set("sms_code:%s" % 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="发送成功")
Exemplo n.º 2
0
def sms_code():
    # 1.获取参数
    req_data = request.data
    json_data = json.loads(req_data, encoding="utf-8")
    mobile = json_data["mobile"]
    image_code = json_data["image_code"]
    image_code_id = json_data["image_code_id"]

    # 2.参数的为空校验
    if not all([mobile, image_code, image_code_id]):
        err_rsp(RET.PARAMERR, "参数错误!")

    # 3.校验手机的格式
    if not re.match('1[3-9]\d{9}', mobile):
        err_rsp(RET.DATAERR, "手机号格式错误!")

    # 4.通过图片验证码编号获取,图片验证码
    try:
        pic_code = redis_store.get("image_code:%s" % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        err_rsp(RET.DBERR, "操作redis失败")

    # 5.判断图片验证码是否过期
    if not pic_code:
        err_rsp(RET.NODATA, "验证码过期")

    # 6.判断图片验证码是否正确
    if pic_code.upper() != image_code.upper():
        err_rsp(RET.DATAERR, "图片验证码不正确")

    # 7.核验成功,删除redis中的图片验证码,短信验证码的层级更高,就不需要图片验证码对用户注册进行验证
    try:
        redis_store.delete("image_code:%s" % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        err_rsp(RET.DBERR, "操作redis失败")

    # 8.生成一个随机的短信验证码,调用cCp发送短信,判断是否成功
    rd_sms_code = "%06d" % random.randint(0, 999999)
    ccp = CCP()
    statue_code = ccp.send_template_sms('18332672733', [rd_sms_code, 5], 1)
    if statue_code == -1:
        err_rsp(RET.DBERR, "验证码发送失败")

    # 9.将短信保存到redis中
    try:
        redis_store.set("rd_sms_code:%s" % mobile, rd_sms_code,
                        constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        err_rsp(RET.DBERR, "操作redis失败")

    # 10.返回
    return err_rsp(RET.OK, "短信发送成功!")
Exemplo n.º 3
0
def get_msg_code():
    dict_data = request.json
    modile, image_code, image_code_id = dict_data.get("modile"), dict_data.get(
        "image_code"), dict_data.get("image_code_id")
    if not all([modile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不全")

    if not re.match(r"1[35678]\d{9}", modile):
        return jsonify(erron=RET.PARAMERR, errmsg="手机号格式不正确")

    try:
        real_image_code = redis_store.get("ImageCodeId_" + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(erron=RET.DBERR, errmsg="数据库查询失败")

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

    if image_code.upper() != real_image_code.upper():
        return jsonify(errno=RET.DATAERR, errmsg="图片验证码输入错误")

    sms_code_str = "%06d" % random.randint(0, 999999)
    result = CCP().send_template_sms(modile, [sms_code_str, 5], 1)
    if result != 0:
        return jsonify(errno=RET.THIRDERR, errmsg="第三方错误")
    try:
        redis_store.setex("sms_" + modile, constants.SMS_CODE_REDIS_EXPIRES,
                          sms_code_str)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="手机验证码保存失败")

    return jsonify(errno=RET.OK, errmsg="发送成功")
Exemplo n.º 4
0
def sms_code():
    #"{'mobile':'13829494822','image_code':'asdf','image_code_id':uuid}"
    json_str = request.data.decode('utf-8')
    json_dict = json.loads(json_str)
    mobile = json_dict['mobile']
    image_code_client = json_dict['image_code']
    image_code_id = json_dict['image_code_id']

    if not all([mobile, image_code_client, image_code_id]):
        return jsonify(errno=response_code.RET.PARAMERR, errmsg='缺少参数')
    if not re.match(r'^1[345678][0-9]{9}$', mobile):
        return jsonify(errno=response_code.RET.PARAMERR, errmsg='手机格式错误')
    try:
        image_code_server = redis_store.get('imageCoedId:' + image_code_id)
    except Exception as e:
        current_app.logger.erro(e)
        return jsonify(errno=response_code.RET.DBERR, errmsg='查询验证码失败')
    if not image_code_server:
        return jsonify(error=response_code.RET.NODATA, errmsg='图片验证码不存在')

    if image_code_server.lower() != image_code_client.lower():
        return jsonify(error=response_code.RET.PARAMERR, errmsg='输入验证码错误')
    sms_code = '%06d' % random.randint(0, 999999)
    current_app.logger.debug(sms_code)
    result = CCP().send_template_sms(mobile, [sms_code, 5], 1)
    if result != 0:
        return jsonify(error=response_code.RET.THIRDERR, errmsg='发送验证码错误')

    try:
        redis_store.set('SMS:' + mobile, sms_code,
                        constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        return jsonify(error=response_code.RET.DBERR, errmsg='保存验证码失败')

    return jsonify(error=response_code.RET.OK, errmsg='发送验证码成功')
Exemplo n.º 5
0
def send_sms_code():
    '''
    发送短信的逻辑
    1 获取参数:手机号,图片验证码内容,图片验证码的编号
    2  校验参数(参数是否符合规则,判断是否有值
    3   先从redis中取出验证码内容,与用户的验证码内容对比.
    4   如果对比不一致,那么验证码输入错误.
    5   如果一直,生成验证码内容
    6   发送验证码
    7   告知发送结果.
    :return:
    '''
    # 1 获取参数:手机号,图片验证码内容,图片验证码的编号
    params_dict = request.json

    mobile = params_dict.get("mobile", None)
    image_code = params_dict.get('image_code', None)
    image_code_id = params_dict.get("image_code_id", None)
    # 2  校验参数(参数是否符合规则,判断是否有值
    if not all([mobile, image_code, image_code_id]):
        #没有值就返回错误
        #{"errno":4100,"errmsg":"参数有无'}
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")
    #检测手机是欧服正确
    if not re.match(r'1[35678]\d{9}$', 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, errmag="查询数据失败")
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg="图片验证码过期")

    # 4   如果对比不一致,那么验证码输入错误.
    if real_image_code.upper() != image_code.upper():
        return jsonify(errno=RET.DBERR, errmsg="验证码输入错误")
    # 5   如果一直,生成验证码内容
    #随机数字,保证数字长度为6为3,不够再前面不上0.
    sms_code_str = "%06d" % random.randint(0, 999999)
    current_app.logger.debug("短信验证码是%s" % sms_code_str)

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

    #保存验证码到redis中
    try:
        redis_store.set('SMS_' + mobile, sms_code_str)
        redis_store.expire('SMS_' + mobile, 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="发送成功")
Exemplo n.º 6
0
def get_sms_code():
    """
    '{"mobile": "11111111111", "image_code": "AAAA", "image_code_id": "sfownegwefwfwefs"}'
    1. 获取手机号| 图片验证码编号 | 图片验证码内容
    2. 检验参数(是否符合规则, 是否存在)
    3. 从redis中获取 真实值
    4. 比对
    5. 生成短信验证码或者错误提示
    6. 告知发送结果
    :return: json 数据
    """
    # 接收到的是 json 格式的字符串
    # 1.获取参数
    # params_dict = json.loads(request.data)  # 但是这里不这么操作
    params_dict = request.json  # type: dict

    mobile = params_dict.get("mobile")
    image_code = params_dict.get("image_code")
    image_code_id = params_dict.get("image_code_id")

    # 2.校验参数
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数有误")
    # 检验手机号是否正确
    if not re.match(r"1[35678]\d{9}", mobile):
        return jsonify(errno=RET.DBERR, errmsg="手机号格式不正确")

    try:
        real_image_code = redis_store.get(
            "ImageCodeID_" + image_code_id)  # StrictRedis: redis_store
    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 real_image_code.upper() != image_code.upper():
        return jsonify(errno=RET.DATAERR, errmsg="验证码输入错误")
    # 3.生成短信验证码内容, 调用三方平台发送短信
    sms_code_str = "{:06d}".format(random.randint(0, 999999))
    current_app.logger.debug("短信验证码内容是: %s" % 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.setex("SMS_" + mobile, constants.SMS_CODE_REDIS_EXPIRES,
                          sms_code_str)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据操作失败")

    # 4.返回提示信息
    return jsonify(errno=RET.OK, errmsg="发送成功")
Exemplo n.º 7
0
def sms_code():
    """发送短信验证码
    1.接受参数(手机号,图片验证码,图片验证码编号)
    2.校验参数(判断参数是否存在,手机号是否合法)
    3.查询服务器存储的图片验证码
    4.跟客户端传入的图片验证码对比
    5.如果对比成功,生成短信验证码数字
    6.调用CCP()单例类封装的发送短信的方法,发送短信
    7.将短信验证码存储到服务器(将来注册时要判断短信验证码是否正确)
    8.响应发送短信验证码的结果
    """
    # 1.接受参数(手机号,图片验证码,图片验证码编号)
    # '{'mobile':'17600992168','image_code':1234,'image_code_id':'uuid'}'
    # json_str = request.data
    # json_dict = json.loads(json_str)
    json_dict = request.json
    mobile = json_dict.get('mobile')
    image_code_clinet = json_dict.get('image_code')
    image_code_id = json_dict.get('image_code_id')

    # 2.校验参数(判断参数是否存在,手机号是否合法)
    if not all([mobile, image_code_clinet, image_code_id]):
        # '{'errno':'0', 'errmsg':'OK'}'
        return jsonify(errno=response_code.RET.PARAMERR, errmsg='缺少参数')
    if not re.match(r'^1[345678][0-9]{9}$', mobile):
        return jsonify(errno=response_code.RET.PARAMERR, errmsg='手机号格式错误')

    # 3.查询服务器存储的图片验证码
    try:
        image_code_server = redis_store.get('ImageCode:' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=response_code.RET.DBERR, errmsg='查询图片验证码失败')
    if not image_code_server:
        return jsonify(errno=response_code.RET.NODATA, errmsg='图片验证码不存在')

    # 4.跟客户端传入的图片验证码对比
    if image_code_server.lower() != image_code_clinet.lower():
        return jsonify(errno=response_code.RET.PARAMERR, errmsg='输入验证码有误')

    # 5.如果对比成功,生成短信验证码数字
    # '%06d' : 不够6位补0;比如,34--》000034
    sms_code = '%06d' % random.randint(0, 999999)

    # 6.调用CCP()单例类封装的发送短信的方法,发送短信
    result = CCP().send_template_sms(mobile, [sms_code, 5], 1)
    if result != 0:
        return jsonify(errno=response_code.RET.THIRDERR, errmsg='发送短信验证码失败')

    # 7.将短信验证码存储到服务器(将来注册时要判断短信验证码是否正确)
    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=response_code.RET.DBERR, errmsg='存储短信验证码失败')

    #  8.响应发送短信验证码的结果
    return jsonify(errno=response_code.RET.OK, errmsg='发送短信验证码成功')
Exemplo n.º 8
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 = request.json
    mobile = params_dict.get("mobile")
    image_code = params_dict.get("image_code")
    image_code_id = params_dict.get("image_code_id")
    # 2
    # 判断参数是否有值
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数有误")
    # 检验手机号是否正确
    if not re.match('1[35678]\\d{9}', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg="手机号格式不正确")
    # 3
    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
    sms_code_str = "%06d" % random.randint(0, 9999)
    current_app.logger.debug("贪玩蓝月提醒您,您的验证码是 %s" % sms_code_str)

    # 6
    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="发送成功")
Exemplo n.º 9
0
def sms_code():
    """发送短信
    1.接受参数(手机号,图片验证码,uuid)
    2.校验参数是否齐全,手机号是否合法
    3.查询服务器存储的图片验证码
    4.跟客户端传入的图片验证码对比
    5.如果对比成功,生成短信验证码,并发送短信
    6.存储短信验证码到redis,方便注册时比较
    7.响应短信验证码发送的结果
    """
    # 1.接受参数(手机号,图片验证码,uuid)
    # '{'mobile':'17600992168','image_code':'asdc','image_code_id':'uuid'}'
    json_dict = request.json
    mobile = json_dict.get('mobile')
    image_code_client = json_dict.get('image_code')
    image_code_id = json_dict.get('image_code_id')
    # 2.校验参数是否齐全,手机号是否合法
    if not all([mobile, image_code_client, image_code_id]):
        return jsonify(errno=response_code.RET.PARAMERR, errmsg='缺少参数')
    if not re.match(r'^1[345678][0-9]{9}$', mobile):
        return jsonify(errno=response_code.RET.PARAMERR, errmsg='手机号格式错误')
    # 3.查询服务器存储的图片验证码
    try:
        image_code_server = redis_store.get('ImageCode:' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=response_code.RET.DBERR, errmsg='查询图片验证码失败')
    if not image_code_server:
        return jsonify(errno=response_code.RET.NODATA, errmsg='图片验证码不存在')
    # 4.跟客户端传入的图片验证码对比
    if image_code_server.lower() != image_code_client.lower():
        return jsonify(errno=response_code.RET.PARAMERR, errmsg='输入验证码有误')
        # 4.1 校验该手机是否已经注册
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=response_code.RET.DBERR, errmsg="数据库查询错误")
    if user:
        # 该手机已被注册
        return jsonify(errno=response_code.RET.DATAEXIST, errmsg="该手机已被注册")

    # 5.如果对比成功,生成短信验证码,并发送短信
    sms_code = '%06d' % random.randint(0, 999999)
    current_app.logger.debug(sms_code)
    result = CCP().send_template_sms(mobile, [sms_code, 5], 1)
    if result != 0:
        return jsonify(errno=response_code.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=response_code.RET.DBERR, errmsg='保存短信验证码失败')

    # 7.响应短信验证码发送的结果
    return jsonify(errno=response_code.RET.OK, errmsg='发送短信验证码成功')
Exemplo n.º 10
0
def get_sms_code():
    """
    生产并发送短信验证码
    1.获取参数:手机号,图片验证码id,图片验证码内容
    2.判断是否有值,校验手机号
    3.从redis中取出图片验证码内容,与用户传过来的图片验证码内容进行校验
    4.生成短信验证码
    5.发送短信验证码
    6.将短信验证码内容存到redis中,记录验证码
    7.返回响应
    :return:
    """

    # 1.获取参数:手机号,图片验证码id,图片验证码内容
    params_dict = request.json
    mobile = params_dict["mobile"]
    image_code = params_dict["image_code"]
    image_code_id = params_dict["image_code_id"]

    # 2.判断是否有值,校验手机号
    if not all([mobile, image_code_id, image_code]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不全")

    # 校验手机号
    if not re.match(r"^1[345678]\d{9}$", mobile):
        return jsonify(errno=RET.DATAERR, errmsg="手机号不正确")

    # 3.从redis中取出图片验证码内容,与用户传过来的图片验证码内容进行校验
    try:
        real_image_code = redis_store.get("imageCodeId" + image_code_id)
        # 如果取出图片验证码内容, 删除redis中的缓存
        if real_image_code:
            real_image_code = real_image_code.decode()
            redis_store.delete("imageCodeId" + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.NODATA, errmsg="验证码已过期")
    # 校验
    if real_image_code.upper() != image_code.upper():
        return jsonify(errno=RET.DATAERR, errmsg="验证码错误")

    # 生成短信验证码
    sms_code = "%06d" % random.randint(0, 999999)

    # 5.发送短信验证码
    result = CCP().send_template_sms(
        mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES // 60], 2)
    if result != 0:
        return jsonify(errno=RET.THIRDERR, errmsg="验证码发送失败")

    # 6.将短信验证码内容存到redis中
    redis_store.set("SMS_" + mobile, sms_code,
                    constants.SMS_CODE_REDIS_EXPIRES)
    # 记录验证码
    current_app.logger.info("SMS_%s,---短信验证码%s" % (mobile, sms_code))

    # 7.返回响应
    return jsonify(errno=RET.OK, errmsg="验证码已发送,请注意查收")
Exemplo n.º 11
0
def send_sms_code():
    """
    发送短信验证码
    0、获取参数(非表单提交,从request.data获取)
    1、判断参数是否为空、是否符合规则
    2、从redis读取图片内容及编号进行比对
    3、图码不符合返回提示信息
    4、图码符合--生成短信码内容、发送短信码、提示发送结果
    """
    # 测试、默认成功
    # return jsonify(errno=RET.OK, errmsg="发送成功")

    # 0、获取参数字典(手机号、图码内容、图码编号-所有参数以json格式传送)
    # print(request)
    params_dict = json.loads(request.data)
    # params_dict = request.json
    mobile = params_dict.get('mobile')
    image_code = params_dict.get('image_code')
    # print('1',image_code)
    image_code_id = params_dict.get('image_code_id')
    # 1、校验参数(有值、符合规则)
    # 是否有值
    if not all([mobile, image_code, image_code_id]):
        # {"xx:xx,yy:yy"}
        return jsonify(errno=RET.PARAMERR, errmsg="参数不能为空")
    # 手机号规则校验
    if not re.match('^1[35678]\\d{9}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg="手机号不合法")
    # 2、从redis中读取保存的图码内容
    try:
        real_image_code = redis_store.get('imageCodeId' + image_code_id).decode('utf-8')
        # print('2',real_image_code)
    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="图片验证码已过期")
    # 与用户输入参数进行比对(这里为了避免大小写造成的影响,将参数和读取的结果都转换大写再比较)
    # 3、不符合,提示错误
    if real_image_code.upper() != image_code.upper():
        return jsonify(errno=RET.DATAERR, errmsg="验证码输入错误")

    # 4、符合则生成短信码(随机数,确保6位,不足0补)
    sms_code_str = "%06d" % random.randint(0, 999999)
    current_app.logger.debug("短信码内容是%s" % sms_code_str)
    # 发送,过期时间单位为分钟
    result = CCP().send_template_sms(mobile, [sms_code_str, constants.SMS_CODE_REDIS_EXPIRES / 60], "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="数据库保存失败")
    # 提示结果(之前需要保存短信码在redis)
    return jsonify(errno=RET.OK, errmsg="发送成功")
Exemplo n.º 12
0
def send_sms_code():
    """
    Realized send sms code
    :return:
    """
    # Get json to dict with request json method
    params_dict = request.json
    print('用户请求的参数: %s' % params_dict)
    # Get params from front-end
    mobile = params_dict.get('mobile')
    image_code = params_dict.get('image_code')
    image_code_id = params_dict.get('image_code_id')

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

    # Judge mobile format
    if not re.match(r'1[356789]\d{9}', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg="手机号格式不正确")

    try:
        # Get image code from redis
        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="图片验证码已过期")

    # User input image_code compare to real_image_code
    if image_code.upper() != real_image_code.upper():
        return jsonify(errno=RET.DATAERR, errmsg="图片验证码输入错误")

    # Generate sms_code
    sms_code_str = '%06d' % random.randint(0, 999999)
    current_app.logger.debug("生成的短信验证码是: %s" % sms_code_str)

    # Send sms_code
    result = CCP().send_template_sms(
        mobile, [sms_code_str, constants.SMS_CODE_REDIS_EXPIRES / 300], 1)

    # Save sms_code to redis
    if result != 0:
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信失败')

    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='数据保存失败')

        # 返回发送结果
    return jsonify(errno=RET.OK, errmsg='发送成功')
Exemplo n.º 13
0
def send_sms_code():
    """发送短信验证码"""
    # 1. 获取参数(手机号,图片验证码,图片验证码id)并进行参数校验
    req_dict = request.json
    if not req_dict:
        return jsonify(errno=RET.PARAMERR, errmsg='缺少参数')

    mobile = req_dict.get('mobile')
    image_code = req_dict.get('image_code')
    image_code_id = req_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[3578]\d{9}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号格式不正确')

    # 2. 根据image_code_id获取对应的图片验证码文本
    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 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. 产生随机6位的短信验证码内容
    sms_code = '%06d' % random.randint(0, 999999)
    current_app.logger.info("短信验证码内容为:%s" % sms_code)

    # 5. 在redis中保存短信验证码
    try:
        redis_store.set('smscode:%s' % mobile, sms_code,
                        constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.looger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存图片验证码失败')

    # 6. 给用户的手机发送短信验证码
    try:
        res = 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 res != 0:
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信验证码失败')

    # 7. 返回应答,发送短信成功
    return jsonify(errno=RET.OK, errmsg='发送短信验证码成功')
Exemplo n.º 14
0
def get_sms_code():
    """
    短信验证功能
    在点击获取验证码时
    通过main.js的sendSMSmsg方法中的ajax请求
    """
    msg_dict = request.json
    mobile = msg_dict.get('mobile')
    image_code = msg_dict.get('image_code')
    image_code_id = msg_dict.get('image_code_id')
    try:
        session_img_code = redis_store.get('image_code: %s' % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR])
    if session_img_code:
        if image_code.lower() == session_img_code.lower():
            redis_store.delete(image_code_id)
            ccp = CCP()
            msg_info = '%06d' % random.randint(0, 999999)
            print(msg_info)
            try:
                result = ccp.send_template_sms(
                    mobile, [msg_info, SMS_CODE_REDIS_EXPIRES / 60], 1)
            except Exception as e:
                current_app.logger.error(e)
                return jsonify(errno=RET.THIRDERR,
                               erromsg=error_map[RET.THIRDERR])
            if result == -1:
                return jsonify(errno=RET.DATAERR, errmsg='短信发送失败')
            else:
                try:
                    redis_store.set('sms_code: %s' % mobile, msg_info,
                                    SMS_CODE_REDIS_EXPIRES)
                    return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
                except Exception as e:
                    current_app.logger.error(e)
                    return jsonify(errno=RET.DATAERR,
                                   errmsg=error_map[RET.DATAERR])
        else:
            return jsonify(errno=RET.DATAERR, errmsg='验证码输入错误')
    else:
        return jsonify(errno=RET.DATAEXIST, errmsg='验证码已失效')
Exemplo n.º 15
0
def send_sms_code():
    """
    发送短信的逻辑
    1. 获取参数:手机号,图片验证码内容,图片验证码的编号 (随机值)
    2. 校验参数(参数是否符合规则,判断是否有值)
    3. 先从redis中取出真实的验证码内容
    4. 与用户的验证码内容进行对比,如果对比不一致,那么返回验证码输入错误
    5. 如果一致,生成验证码的内容(随机数据)
    6. 发送短信验证码,并保存验证码,以便校验
    7. 告知发送结果
    """
    # 本质:最后调用loads函数
    parameters = flask.request.json
    # parameters = flask.json.loads(flask.request.data)
    mobile_phone = parameters.get('mobile')
    image_code = parameters.get('image_code')
    image_code_id = parameters.get('image_code_id')

    if not all([mobile_phone, image_code, image_code_id]):
        return flask.jsonify(errno=RET.PARAMERR, errmsg='参数有误')
    if not re.match('1[35678]\\d{9}', mobile_phone):
        return flask.jsonify(errno=RET.PARAMERR, errmsg='手机号格式不正确')

    try:
        real_image_code = redis_db.get('imageCodeId' + image_code_id)
    except Exception as e:
        flask.current_app.logger(e)
        return flask.jsonify(errno=RET.DBERR, errmsg='数据查询失败')
    else:
        # 过期
        if not real_image_code:
            return flask.jsonify(errno=RET.NODATA, errmsg='图片验证码过期')
        if image_code.upper() != real_image_code:
            return flask.jsonify(error=RET.DATAERR, errmsg='输入验证码错误')
        # 验证通过,可以发送手机验证码了
        sms_code_str = "%06d" % random.randint(0, 999999)
        # 不去搞和这个编码问题,直接变量
        flask.current_app.logger.debug(sms_code_str)
        # bug:验证码和过期时间以数组形式传给第三方,属于文档阅读不仔细
        result = CCP().send_template_sms(
            mobile_phone, [sms_code_str, constants.SMS_CODE_REDIS_EXPIRES / 5],
            '1')

        if result != 0:
            return flask.jsonify(errno=RET.THIRDERR, errmsg='发送短信失败')
        else:
            try:
                # 手机号作为key
                redis_db.set(mobile_phone, sms_code_str,
                             constants.SMS_CODE_REDIS_EXPIRES)
            except Exception as e:
                flask.current_app.logger.error(e)
                return flask.jsonify(errno=RET.DBERR, errmsg='数据保存失败')
        return flask.jsonify(errno=RET.OK, errmsg='发送成功')
Exemplo n.º 16
0
def send_sms_code():
    """
    发送短信的逻辑
    1.获取参数:手机号、图片验证码内容、图片验证码编号
    2.校验参数
    3.先从redis中取出真实的验证码内容
    4.与用户的验证码内容进行对比,如果对比不一致,那么返回验证码输入错误
    5.如果一致,生成验证码的内容
    6.发送短信验证码
    7.告知发送结果
    :return:
    """
    # 1.获取参数:手机号、图片验证码内容、图片验证码编号
    # params_dict = json.loads(request.data)
    params_dict = request.json

    mobile = params_dict.get("mobile")
    image_code = params_dict.get("image_code")
    image_code_id = params_dict.get("image_code_id")
    # 2.校验参数
    if not all([mobile, image_code_id, image_code]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数有误")
    if not re.match('1[35678]\\d{9}', 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():
        print(real_image_code, "---", image_code)
        return jsonify(errno=RET.DATAERR, errmsg="验证码输入错误")
    # 5.如果一致,生成验证码的内容
    sms_code_str = "%06d" % random.randint(0, 999999)
    current_app.logger.debug("短信验证码内容是%s" % sms_code_str)
    # 6.发送短信验证码
    result = CCP().send_template_sms(mobile, [sms_code_str, constants.SMS_CODE_REDIS_EXPIRES], 1)
    # TODO 这里没有真正的发送短信
    # if result != 0:
    #     # 代表发送不成功
    #     return jsonify(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="发送成功")
Exemplo n.º 17
0
def sms_code():
    """
    步骤:
        1.收集手机号码 验证码image_code 验证码图片中的内容image_code_id
        2.校验手机 验证码 验证码过期
        3.比对验证码和 redis是否相同       手机号码是否存在(放一下)
        4.通过云通讯发送验证码给手机
        5.并把验证码保存一下
        6.告诉页面发送成功
    """
    '''
    mobile	string	是	手机号
    image_code	string	是	用户输入的图片验证码内容
    image_code_id	string	是	真实图片验证码编号
    '''
    #  1.收集手机号码 验证码image_code 验证码图片中的内容image_code_id
    # 这种方式比较繁琐 request.data.get['mobile'] request.data.get['image_code']
    req_dic = request.json
    # 将获取好的参数接受一下
    mobile = req_dic['mobile']
    image_code = req_dic['image_code']
    image_code_id = req_dic['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='手机格式不对')
    # 3比对redis
    # 获取redis存储的验证码 获取result_image_code_id(格式输出的问题)
    #  decode_responses=True 保证redis获取数据的时候是一个字符串
    result_image_code_id = redis_store.get('image_code:%s' % image_code_id)
    # 判断数据是否过期
    if not result_image_code_id:
        return jsonify(errno=RET.PARAMERR, errmsg='验证码过期重新设置验证码')
    # 判断验证码是否和redis的相同
    # result_image_code_id image_code
    if result_image_code_id.upper() != image_code.upper():
        return jsonify(errno=RET.PARAMERR, errmsg='验证码过期重新设置验证码')
    # 4 将验证码发送给手机 1 2   0004  0010
    sms_rand_id = '%04d' % random.randint(0, 9999)
    # 将生成好的sms_rand_id 通过运通讯发送出去
    # mobile= 手机号码 sms_rand_id 验证码 SMS_CODE_REDIS_EXPIRES 过期时间 1以短信的形式发送过去
    res = CCP().send_template_sms(mobile,
                                  [sms_rand_id, SMS_CODE_REDIS_EXPIRES], 1)
    # 判断res的参数 0 成功 -1 失败
    if res != 0:
        return jsonify(errno=RET.PARAMERR, errmsg='手机发送验证码失败')
    # sms_rand_id存储到数据库中redis mysql
    redis_store.set('sms_code:%s' % mobile, sms_rand_id,
                    SMS_CODE_REDIS_EXPIRES)
    # 6将成功的数据发给浏览器
    return jsonify(errno=RET.OK, errmsg='发送短信成功')
Exemplo n.º 18
0
Arquivo: views.py Projeto: ZangZ/info8
def send_sms_code():
    """发送短信的逻辑"""
    # TODO 以伪代码默认发送成功
    # return jsonify(erron=RET.OK, errmsg="发送成功")
    '{"mobiel": "18811111111", "image_code": "AAAA", "image_code_id": "u23jksdhjfkjh2jh4jhdsj"}'

    # 1获取数据
    params_dict = request.json
    # print(params_dict) # TODO 1

    mobile = params_dict.get("mobile")
    image_code = params_dict.get("image_code")
    image_code_id = params_dict.get("image_code_id")

    # 2校验数据
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数有误")
    if not re.match("1[35678]\\d{9}", 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(erron=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发送短信验证码
    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="发送短信成功")
Exemplo n.º 19
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='图片验证码错误')
    sms_code='%06d' % random.randint(0,999999)
    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=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='发送失败')
Exemplo n.º 20
0
def sms_code():
    """发送短信"""
    """
    1.接受参数(手机号,图片验证码,uuid)
    2.校验参数是否齐全,手机号是否合法
    3.查询服务器存储的图片验证码
    4.跟客户端传入的图片验证码对比
    5.如果对比成功,生成短信验证码,并发送短信
    6.存储短信验证码到redis,方便注册时比较
    7.响应短信验证码发送的结果
    """
    #1.接受参数(手机号,图片验证码,uuid)
    json_str = request.data.decode()
    json_dict = json.loads(json_str)
    mobile = json_dict.get('mobile')
    image_code_client = json_dict.get('image_code')
    image_code_id = json_dict.get('image_code_id')
    #2.校验参数是否齐全,手机号是否合法
    if not all([mobile, image_code_client]):
        return jsonify(errno=response_code.RET.PARAMERR, errmsg='缺少参数')
    if not re.match(r'^1[345678][0-9]{9}$', mobile):
        return jsonify(errno=response_code.RET.PARAMERR, errmsg='手机号格式错误')
    #3.查询服务器存储的图片验证码
    try:
        image_code_server = redis_store.get('imageCodeId:' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=response_code.RET.DBERR, errmsg='查询图片验证码失败')
    if not image_code_server:
        return jsonify(errno=response_code.RET.NODATA, errmsg='图片验证码不存在')
    #4.跟客户端传入的图片验证码对比
    if image_code_server.lower() != image_code_client.lower():
        return jsonify(errno=response_code.RET.PARAMERR, errmsg='输入验证码有误')
    #5.如果对比成功,生成短信验证码,并发送短信
    # '%06d' : 如果不够6位,补0.比如:28-->000028
    sms_code = '%06d' % random.randint(0, 999999)
    current_app.logger.debug(sms_code)
    result = CCP().send_template_sms(mobile, [sms_code, 5], 1)
    if result != 0:
        return jsonify(errno=response_code.RET.THIRDERR, errmsg='发送短信验证码失败')
    #6.存储短信验证码到redis,方便注册时比较
    try:
        redis_store.set('SMS:' + mobile, sms_code,
                        constants.IMAGE_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=response_code.RET.DBERR, errmsg='保存短信验证码失败')
    #7.响应短信验证码发送的结果
    return jsonify(errno=response_code.RET.OK, errmsg='发送短信验证码成功')
Exemplo n.º 21
0
def sms_code():
    #     获取手机号
    # 获取验证码
    # 第一次不用存手机号
    # 验证码对比失败,应该是验证码的值取的不对,检查获取的验证码是否正确,检查从server中获取的值是否正确
    json_dict = request.json
    mobile = json_dict.get("mobile")
    image_code_client = json_dict.get("image_code")
    image_code_id = json_dict.get("image_code_id")
    print('id1-----------------------', image_code_id)
    # 从客户端获取的验证码应该和从服务器获取的验证码一致,在前端在获取值的时候,会绑定uuid
    # 校验参数是否存在
    if not all([mobile, image_code, image_code_client]):
        return jsonify(errno=response_code.RET.PARAMERR, errmsg='缺少参数')
# 判断手机格式是否正确
    if not re.match(r'^1[345678][0-9]{9}$', mobile):
        return jsonify(errno=response_code.RET.PARAMERR, errmsg='手机号格式错误')
# 校验手机验证码是否和redis中的匹配,在这之前先判断在服务器中是否存在这个验证码?,(不存在的原因是已经失效了)如果不存在就不用往下判断了
    try:
        image_code_id_server = redis_store.get('ImageCode:' + image_code_id)
        print('id------------------------', image_code_id_server)
    #     这是空的
    except Exception as e:
        current_app.logger(e)
        return jsonify(errno=response_code.RET.DBERR, errmsg='查询图片验证码失败')
        # 如果数据库中获取失败,给客户友好提示
    if not image_code_id_server:
        return jsonify(errno=response_code.RET.DBERR, errmsg='查询图片验证码失败')
    if image_code_client.lower() != image_code_id_server.lower():
        return jsonify(errno=response_code.RET.PARAMERR, errmsg='输入验证码有误')
# 如果存在,给手机号发送短信
    sms_code = '%06d' % random.randint(0, 99999)
    result = CCP().send_template_sms(mobile, [sms_code, 5], 1)
    if result != 0:
        # if not result:
        return jsonify(errno=response_code.RET.THIRDERR, errmsg='发送短信验证码失败')
#     反之发送成功,成功后将手机号作为建,将手机验证码存储到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=response_code.RET.DBERR, errmsg='保存短信验证码失败')

# 7.响应短信验证码发送的结果
    return jsonify(errno=response_code.RET.OK, errmsg='发送短信验证码成功')
Exemplo n.º 22
0
def send_sms_code():

    # return jsonify(errno=RET.OK, errmsg="发送成功")  # 打开此行,测试手机验证码发送后填写时间倒计时功能是否奏效。

    # 1. 获取参数:用户手机号、用户输入的图片验证码、图片验证码的id
    params_dict = request.json  # 等价于 josn.loads(request.data)
    mobile, image_code, image_code_id = params_dict["mobile"], params_dict["image_code"], params_dict["image_code_id"]

    # 2. 校验是否有空值,以及可能的手机号格式错误
    if not all ([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数有误")
    if not re.match("^1[35678][0-9]{9}$", 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位数字符串
    sms_code_str = "%06d" % random.randint(0, 999999)
    # print(sms_code_str)  # 确保容联云代码无误后,开发时用这三行,可跳过容联云仅能给指定手机发短信的限制,测试注册成功后自动跳转
    # redis_store.set('SMS_' + mobile, sms_code_str, constants.SMS_CODE_REDIS_EXPIRES)
    # return jsonify(errno=RET.OK, errmsg="发送成功")

    # 6. 发送短信验证码
    result = CCP().send_template_sms(mobile, [sms_code_str, constants.SMS_CODE_REDIS_EXPIRES//60], 1)
    if result != 0:
        return jsonify(errno=RET.THIRDERR, errmsg="发送短信失败")

    # 7. 保存验证码内容到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="数据保存失败")

    # 8. 告知结果
    return jsonify(errno=RET.OK, errmsg="发送成功")
Exemplo n.º 23
0
def send_sms():
    # params_dict=json.loads(request.data)
    params_dict=request.json
    mobile = params_dict.get("mobile")
    image_code = params_dict.get("image_code")
    image_code_id = params_dict.get("image_code_id")

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

    if not re.match("^1[3578][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="验证码已过期")

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


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

    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="发送成功")
Exemplo n.º 24
0
def send_sms_code():
    # 1.获取参数:手机号, 图片验证码内容, 图片验证码的编号
    # params_dict = json.loads(request.data)
    params_dict = request.json
    mobile = params_dict.get("mobile")
    image_code = params_dict.get("image_code")
    image_code_id = params_dict.get("image_code_id")

    # 2.校验参数
    # 判断参数是否有值
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数有误")
    if not re.match('1[35678]\\d{9}]', 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. 发送短信验证码
    result = CCP().send_template_sms(
        mobile, [sms_code_str, constants.SMS_CODE_REDIS_EXPIRES])
    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="发送成功")
Exemplo n.º 25
0
def sms_code():
    """发送短信"""
    # 1.接受参数
    json_str = request.data
    json_dict = json.loads(json_str)
    mobile = json_dict.get('mobile')
    image_code_client = json_dict.get('image_code')
    image_code_id = json_dict.get('image_code_id')

    # 2.校验参数
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=response_code.RET.PARAMERR, errmsg='缺少参数')
    if not re.match(r'^1[345678][0-9]{9}$', mobile):
        return jsonify(errno=response_code.RET.PARAMERR, errmsg='手机号码不正确')

    # 3.查询服务器存储的图片验证码
    try:
        image_code_server = redis_store.get('ImageCode:' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=response_code.RET.DBERR, errmsg='查询图片验证码失败')
    if not image_code_id:
        return jsonify(errno=response_code.RET.NODATA, errmsg='图片验证码不存在')
    print(image_code_server, image_code_client)
    # 4.跟客户端传入的图片验证码对比
    if image_code_server.lower() != image_code_client.lower():
        return jsonify(errno=response_code.RET.PARAMERR, errmsg='输入验证码有误')

    # 5.如果对比成功,生成短信验证码,并发送短信
    sms_code = '%06d' % random.randint(0, 999999)
    current_app.logger.debug(sms_code)
    result = CCP().send_template_sms(mobile, [sms_code, 5], 1)
    if result != 0:
        return jsonify(errno=response_code.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=response_code.RET.DBERR, errmsg='保存短信验证码失败')

    # 7.响应短信验证码发送的结果
    return jsonify(errno=response_code.RET.OK, errmsg='发送短信验证码成功')
Exemplo n.º 26
0
def sms_code():
    """短信验证码后端实现"""
    # 1.取值(图片验证码编号,图片验证码内容,手机号)
    print('1111')
    params = request.json
    mobile = params.get('mobile')
    image_code_id = params.get('image_code_id')
    image_code = params.get('image_code')
    print(params)
    # 2.判断是否为空
    if not all([mobile, image_code_id, image_code]):
        return jsonify(errno=RET.DATAERR, errmsg='缺少参数')
    # 3.校验手机号
    if not re.match(r'1[35678][0-9]{9}', mobile):
        return jsonify(error=RET.DATAERR, errmsg='手机号不正确')
    # 4.用图片验证码编号从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 image_code.upper() != real_image_code.upper():
        print(image_code)
        print(real_image_code)
        return jsonify(errno=RET.DATAERR, errmsg='验证码错误')
    # 5.生成短信验证码
    messcode = "%06d" % random.randint(0, 999999)
    # 6.将手机号和短信验证码给第三方平台,让其发送
    result = CCP().send_template_sms(
        mobile, [messcode, constants.SMS_CODE_REDIS_EXPIRES / 60], '1')
    print(result)
    if result != 0:
        return jsonify(errno=RET.DATAERR, errmsg='发送失败')

    # 7.将手机号与短信验证码存入redis中
    try:
        redis_store.set("mobile_" + mobile, messcode)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg='数据保存失败')
    # 8.发送成功
    return jsonify(errno=RET.OK, errmsg='发送成功')
Exemplo n.º 27
0
def send_code():
    """验证短信"""
    # 1.获取前端发过来的json数据
    mobile = request.json.get('mobile')
    # 图片验证码内容
    image_code = request.json.get('image_code')
    # 图片验证码uuid
    image_code_id = request.json.get('image_code_id')
    print("%s %s %s" % (mobile, image_code, image_code_id))
    # 2.校验前端传过来的参数是否有值
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg='请输入齐全的参数')
    # 3.校验手机号码
    if not re.match(r"^1[34578]\d{9}$", mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号码不正确')
    # 4.获取redis数据库存储的验证码
    real_image_code = redis_store.get('image_code_' + image_code_id)
    # 5.判断验证码是否过期
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg="图片验证码已过期")
    # 6.判断验证码是否正确,转换小写(可忽略大小写)
    if image_code.lower() != real_image_code.lower():
        return jsonify(errno=RET.DATAERR, errmsg="请输入正确的验证码")

    # 7.生成随机验证码
    result = random.randint(0, 9999)
    sms_code = "%06d" % result
    print("短信验证码:" + sms_code)
    # 8.通过云通讯工具发送短信
    statusCode = CCP().send_template_sms(
        mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES], 1)
    if statusCode != 0:
        return jsonify(errno=RET.THIRDERR, errmsg="发送短信失败")
    # 9.将短信验证码存储到redis数据库
    try:
        redis_store.set('SMS_code' + mobile, sms_code,
                        constants.IMAGE_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="短信验证码存储失败")
    return jsonify(errno=RET.OK, errmsg="发送短信成功")
Exemplo n.º 28
0
def get_sms_code():
    """
    生成短信验证码
    :return:
    """
    params_dict = request.json
    mobile = params_dict["mobile"]
    image_code = params_dict["image_code"]
    image_code_id = params_dict["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.PARAMERR, errmsg="手机号码输入格式不正确")
    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.PARAMERR, errmsg="验证码过期")
    if image_code.upper() != real_image_code.upper():
        return jsonify(errno=RET.PARAMERR, errmsg="验证码输入不正确")
    sms_code_id = "%06d" % random.randint(0, 999999)
    print(sms_code_id)
    result = CCP().send_template_sms(
        mobile, [sms_code_id, constants.SMS_CODE_REDIS_EXPIRES], 1)
    if result:
        return jsonify(errno=RET.DATAERR, errmsg="短信验证码发送失败")
    if not result:
        try:
            redis_store.set("SMS" + mobile, sms_code_id,
                            constants.SMS_CODE_REDIS_EXPIRES)
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(RET.DBERR, errmsg="数据存储错误")
    return jsonify(errno=RET.OK, errmsg="验证成功")
Exemplo n.º 29
0
def get_sms_code():
    """
    思路分析:
    1.获取参数
    2.校验参数,为空检验,格式校验
    3.取出redis中的图片验证码
    4.判断是否过期
    5.删除redis中图片验证码
    6.正确性判断
    7.生成短信验证码
    8.发送短信
    9.判断是否发送成功
    10.保存短信验证码到redis
    11.返回响应
    """
    # 1.获取参数
    json_data = request.data
    dict_data = json.loads(json_data)
    mobile = dict_data.get('mobile')
    image_code = dict_data.get('image_code')
    image_code_id = dict_data.get('image_code_id')

    # 2.校验参数,为空检验,格式校验
    if not all([mobile,image_code,image_code_id]):
        return jsonify(errno=RET.PARAMERR,errmsg="参数不全")

    if not re.match('1[35789]\d{9}',mobile):
        return jsonify(errno=RET.DATAERR,errmsg="手机号格式不正确")

    # 3.取出redis中的图片验证码
    try:
        redis_image_code = redis_store.get('image_code:%s'%image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="数据获取失败")

    # 4.判断是否过期
    if not redis_image_code:
        return jsonify(errno=RET.NODATA,errmsg="图片验证码过期")

    # 5.删除redis中图片验证码
    try:
        redis_store.delete('image_code:%s'%image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="获取失败")

    # 6.正确性判断
    if image_code.upper() != redis_image_code.upper():
        return jsonify(errno=RET.DATAERR,errmsg="图片验证码错误")

    # # 7.生成短信验证码
    sms_code = '%06d'%random.randint(0,999999)
    current_app.logger.debug('短信验证码 = %s'%sms_code )
    # 8.发送短信
    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="云通讯发送失败")

    # 9.判断是否发送成功
    if result == -1:
        return jsonify(errno=RET.DATAERR,errmsg="发送短信失败")

    # 10.保存短信验证码到redis
    try:
        redis_store.set('sms_code:%s'%mobile,sms_code,constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="短信保存失败")

    # 11.返回响应
    return jsonify(errno=RET.OK,errmsg="发送成功")
Exemplo n.º 30
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([image_code, mobile]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数缺失')
    if not re.match(r'1[3456789]\d{9}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号格式错误')
    try:
        text = redis_store.get('Imagecode_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='验证码已过期')
    try:
        redis_store.delete('Imagecode_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='获取数据失败')
    # 判断获取结果
    if not text:
        return jsonify(errno=RET.NODATA, errmsg='图片验证码已过期')
    # 判断图片验证码输入是否正确而
    if image_code.lower() != text.lower():
        return jsonify(errno=RET.PARAMERR, errmsg='图片验证码错误')
    # 删除图片验证码
    try:
        redis_store.delete('Imagecode_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
    # 判断手机是否注册
    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='手机号已注册')
    # 调用第三方工具发送短信
    sms_data = "%06d" % random.randint(1, 999999)
    print(sms_data)
    # 存储验证码信息
    try:
        redis_store.setex('smscode_' + mobile,
                          constants.SMS_CODE_REDIS_EXPIRES, sms_data)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据异常')
    # 调用云通讯发送短信
    try:
        ccp = CCP()
        result = ccp.send_template_sms(
            mobile, [sms_data, 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='短信发送失败')