Пример #1
0
def send_sms_code(mobile):
    #获取参数,除了传递的mobile外,还有图片验证码(内容数据)text,图片验证码id(模板id)--id
    image_code = request.args.get('text')
    image_code_id = request.args.get('id')
    #校验是否缺少参数,mobile,text,id
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不完整')
    #校验mobile手机号格式
    if not re.match(r'1[3456789]\d{9}', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机格式错误')
    #获取本地存储redis中存储的真实图片验证码
    try:
        real_image_code = redis_store.get('ImageCode_' + image_code_id)
    #判断获取结果,放在try中,图片验证码是否过期
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询图片验证码失败')
    #判断是否过期
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg='图片验证码过期')
    #删除图片验证码,图片验证码只能操作一次
    try:
        redis_store.delete('ImageCode' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
    #比较真实的图片验证码与获取的是否一致,不区分大小写
    if real_image_code.lower() != image_code.lower():
        return jsonify(errno=RET.DATAERR, errmsg='图片验证码错误')
    #查询数据库,看手机号是否已经注册,也就是是否存在这个手机的用户
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询数据库异常')
    else:
        if user:
            return jsonify(errno=RET.DATAEXIST, errmsg='手机号已注册')
    #构造短信随机码并保存
    sms_code = '%06d' % random.randint(0, 999999)
    try:
        redis_store.setex('SMSCode_' + mobile,
                          constants.SMS_CODE_REDIS_EXPIRES, sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存短信验证码失败')
    #调用云通讯扩展包发送短信,模板方法
    try:
        ccp = sms.CCP()
        result = ccp.send_template_sms(
            mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信异常')
    #通过返回结果判断是否成功
    if 0 == result:
        return jsonify(errno=RET.OK, errmsg='发送成功')
    else:
        return jsonify(errno=RET.THIRDERR, errmsg='发送失败')
Пример #2
0
def send_sms_code(mobile):
    """
    发送短信:获取参数/校验参数/业务处理/返回结果
    1/获取参数,mobile,text,id
    2/校验参数的完整性all/any
    3/校验手机号,正则表达式
    4/校验图片验证码,获取本地存储的真实图片验证码
    5/判断获取结果
    6/删除图片验证码
    7/比较图片验证码是否一致
    8/判断手机号是否已注册
    9/构造短信随机数,random.randint()
    10/发送短信,调用云通讯接口
    11/需要保存发送结果
    12/判断发送结果是否成功
    13/返回结果
    :param mobile:
    :return:
    """
    # 获取get请求的参数,mobile,text,id
    image_code = request.args.get('text')
    image_code_id = request.args.get('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(1, 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:
        # 根据手机号进行查询,并判断查询结果
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询用户信息失败')
    else:
        # 判断用户存在
        if user is not None:
            return jsonify(errno=RET.DATAEXIST, errmsg='手机号已注册')
    # 调用云通讯接口,发送短信
    try:
        ccp = sms.CCP()
        # 保存发送结果
        result = ccp.send_template_sms(
            mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信异常')
    # 判断发送短信结果
    # if result == 0:
    if 0 == result:
        return jsonify(errno=RET.OK, errmsg='发送成功')
    else:
        return jsonify(errno=RET.THIRDERR, errmsg='发送失败')
Пример #3
0
def send_sms_code(mobile):
    """
    发送短信:获取参数/校验参数/查询数据/返回结果
    1/获取参数,mobile,text,id
    2/校验参数完整性
    3/校验手机号,正则校验手机号格式
    4/获取本地存储的真实图片验证码,校验查询结果
    5/删除图片验证码
    6/比较图片验证码是否一致
    7/构造短信验证码,random模块
    8/在redis中缓存短信验证码
    9/调用云通信接口,发送短信
    10/保存发送结果,返回前端结果
    :param mobile:
    :return:
    """
    # 获取参数,图片验证码和图片验证码编号
    image_code = request.args.get("text")
    image_code_id = request.args.get("id")
    # 校验参数完整性
    # if mobile and image_code and image_code_id:
    # any
    # 检验 所有参数 是否都获取到了
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数缺失")
    # 校验手机号
    if not re.match(r"^1[34578]\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.DATAERR, errmsg="图片验证码过期")
    # 删除图片验证码
    try:
        redis_store.delete("ImageCode_" + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
    # 比较图片验证码
    if real_image_code.lower() != image_code.lower():
        return jsonify(errno=RET.DATAERR, errmsg="图片验证码不一致")

    # 检验用户是否已经注册
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据库链接错误')
    else:
        if user is not None:
            return jsonify(errno=RET.DBERR, errmsg='手机号已注册')

    # 生成短信验证码内容
    sms_code = "%06d" % random.randint(0, 999999)
    # 保存短信验证码到redis中
    try:
        redis_store.setex("SMSCode_" + mobile,
                          constants.SMS_CODE_REDIS_EXPIRES, sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="保存短信验证码失败")

    # 调用云通信接口,准备发送短信
    try:
        ccp = sms.CCP()
        # 发送短信的模板方法
        result = ccp.send_template_sms(
            mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg="短信发送异常")
    # 判断发送短信结果
    # if result = 0:
    if 0 == result:
        return jsonify(errno=RET.OK, errmsg="短信发送成功")
    else:
        return jsonify(errno=RET.THIRDERR, errmsg="短信发送失败")
Пример #4
0
def send_sms_code(mobile):
    """
    发送短信:获取参数---检查参数---业务处理(查询数据)---返回结果
    1/获取参数,mobile,text(图片验证码),id(图片验证码编号)
    2/校验参数的完整性
    3/校验手机号,正则校验手机号
    4/获取本地存储的真实图片验证码
    5/判断获取结果是否存在
    6/删除已经读取过的图片验证码,图片验证码只能获取一次
    7/比较图片验证码是否一致
    8/发送短信,云通讯只能提供网络服务,短信内容需要自定义,随机数
    9/生成一个短信验证码,六位数的随机数,random.randint()
    10/存储短信验证码到redis数据库中
    11/准备发送短信,判断用户是否已注册,然后调用sms.CCP()
    12/保存发送结果,判断发送是否成功
    13/返回结果
    :param mobile:
    :return:
    """
    # 获取参数,get请求,text为用户输入的图片验证码内容,id为图片验证码编号
    image_code = request.args.get('text')
    image_code_id = request.args.get('id')
    # 校验参数的完整性,any判断一个
    if not all([mobile,image_code,image_code_id]):
        return jsonify(errno=RET.PARAMERR,errmsg='参数不完整')
    # 参数检查,首先检查手机号
    # a = compile()把具体的数据编译成一个对象,match()从前往后,search()搜索,findall()查询所有,sub()查询替换
    # \d,\D,\w(a-zA-Z0-9_)\W,.+*?
    if not re.match(r'1[3456789]\d{9}',mobile):
        return jsonify(errno=RET.PARAMERR,errmsg='手机号格式错误')
    # 获取真实的图片验证码
    try:
        real_image_code = redis_store.get('ImageCode_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg='获取图片验证码失败')
    # 判断获取结果
    if not real_image_code:
        return jsonify(errno=RET.NODATA,errmsg='图片验证码已过期')
    # 删除图片验证码,因为只能读取一次
    try:
        redis_store.delete('ImageCode_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
    # 比较图片验证码是否一致,需要忽略大小写
    if real_image_code.lower() != image_code.lower():
        return jsonify(errno=RET.DATAERR,errmsg='图片验证码不一致')
    # 判断手机号是否已注册
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg='查询数据库异常')
    else:
        # 判断查询结果,用户是否存在
        if user is not None:
            return jsonify(errno=RET.DATAEXIST,errmsg='手机号已注册')
    # 生成短信随机数,六位数
    sms_code = '%06d' % random.randint(1,999999)
    # 保存短信验证码,到redis中
    try:
        redis_store.setex('SMSCode_' + mobile,constants.SMS_CODE_REDIS_EXPIRES,sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg='保存数据异常')
    # 调用云通讯扩展,准备发送短信,调用第三方扩展一般都需要异常处理
    try:
        send_sms = sms.CCP()
        result = send_sms.send_template_sms(mobile,[sms_code,constants.SMS_CODE_REDIS_EXPIRES/60],1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR,errmsg='发送短信异常')
    # 判断返回结果,表达式中变量和数据比较,建议变量写后面;
    if 0 == result:
    # if result == 0:
        return jsonify(errno=RET.OK,errmsg='发送成功')
    else:
        return jsonify(errno=RET.THIRDERR,errmsg='发送失败')
Пример #5
0
def send_sms_code(mobile):
    """
    发送方式:GET
    1. 获取参数:手机号, 用户输入的验证码, image_code_id
    2. 校验参数存在
    3. 通过正则校验手机号是否为合法手机号
    4. 查询redis数据库获取图片验证码,校验验证码一致
    5. 删除图片验证码,因为验证码只能校验一次
    6. 查询用户手机号是否已注册过
    7. 生成验证码,
    8. 调用云通讯接口发送信息
    :return:
    """
    # 获取参数image_code, image_code_id
    image_code = request.args.get('text')
    image_code_id = request.args.get('id')
    # 校验参数完整
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errcode=RET.PARAMERR, errmsg='参数输入不完整')
    # 通过正则校验手机号合法
    if not re.match(r'^1[3-9]\d{9}$', mobile):
        return jsonify(errcode=RET.PARAMERR, errmsg='手机号不合法')

    # 查询redis数据库, 获取真实的imagecode
    try:
        real_image_code = redis_store.get('imagecode_' + image_code_id)
        print real_image_code
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errcode=RET.DBERR, errmsg='查询图片验证码异常')
    # 判断是否有数据,若无数据则表示已过期
    if not real_image_code:
        return jsonify(errcode=RET.DATAERR, errmsg='图片验证码已过期')

    # 清除redis数据库
    try:
        redis_store.delete('imagecode_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
    # 验证用户输入的验证码与真实验证码是否一致
    if image_code.lower() != real_image_code.lower():
        return jsonify(errcode=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='查询数据库错误')

    if user is not None:
        return jsonify(errno=RET.DATAERR, errmsg='该手机号已经注册过')

    # 当手机号和图片验证码验证通过后,开始生成短信验证码,然后发送短信验证码
    # 1.随机生成验证码
    sms_code = '%06d' % random.randint(0, 999999)
    # 2. 保存验证码到数据库
    try:
        redis_store.setex('smscode_' + mobile,
                          constants.SMS_CODE_REDIS_EXPIRES, sms_code)
    except Exception:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='存储短信验证码到数据库异常')

    # 3, 保存成功后,调用云通讯发送短信验证码
    try:
        # 实例化云通讯对象
        ccp = sms.CCP()
        result = ccp.send_template_sms(
            mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信验证码异常')
    if result == 0:
        return jsonify(errno=RET.OK, errmsg='发送成功')
    else:
        return jsonify(errno=RET.THIRDERR, errmsg='发送失败')
Пример #6
0
def send_sms_code(mobile):
    """
    发送短信:获取参数-校验参数-查询数据(业务处理)-返回结果
    1.获取参数,mobile,text,id(验证码编号)
    2.校验参数完整性,正则匹配手机号
    3.从本地中获取存储的图片验证码
    4.判断获取结果是否存在
    5.删除缓存中已经读取过的图片验证码,只能读取一次
    6.比较两次的验证码是否一致
    7.发送短信,云通讯只能提供网络服务,内容需要自定(随机数)
    8.生成一个短信验证码,六位的随机数
    9.存数生成的验证码到redis中
    10.准备发送短信,判断用户是否依据能够注册,
    11.保存发送结果,判断是否发送成功
    12.返回结果
    :param mobile:
    :return:
    """
    # 获取参数text为图片验证码的内容,id为图片验证码的编号
    image_code = request.args.get('text')
    image_code_id = request.args.get('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日志中
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="获取图片验证码失败")

    # 判断验证码是否存在
    if not real_image_code:
        return jsonify(errno=RET.PARAMERR, errmsg="验证码不存在")

    # 删除redis保存的已经读取过的验证码
    try:
        redis_store.delete("ImageCode_" + image_code_id)
    except Exception as e:
        current_app.logger.error(e)

    # 判断两次的验证码是否一致,忽略大小写
    if image_code.lower() != real_image_code.lower():
        return jsonify(errno=RET.DATAERR, errmsg="图片验证码不一致")

    # 生成验证码随机数
    sms_code = "%06d" % random.randint(1, 999999)

    # 保存验证码到redis中
    try:
        redis_store.setex("SMSCode_" + mobile,
                          constants.SMS_CODE_REDIS_EXPIRES, sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="验证码保存失败")

    # 判断用户是否已经注册
    try:
        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="手机号已经注册")

    # 调用云通讯发送验证码短信
    try:
        send_sms = sms.CCP()
        result = send_sms.send_template_sms(
            mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg="发送短信失败")

    # 判断返回结果,
    if 0 == result:
        return jsonify(errno=RET.OK, errmsg="发送成功")
    else:
        return jsonify(errno=RET.THIRDERR, errmsg="发送失败")
Пример #7
0
def send_sms_code(mobile):
    """
    发送短信:获取参数--校验参数--查询数据--返回结果
    1/获取参数,图片验证码和编号
    2/校验参数的完整性,mobile,text,id
    3/检查mobile手机号格式
    4/获取本地存储的真实图片验证码
    5/判断获取结果,图片验证码是否过期
    6/删除图片验证码
    7/比较图片验证码
    8/查询数据库,判断手机号是否已经注册
    9/生成短信内容,随机数
    10/保存短信内容到redis中
    11/调用云通讯发送短信
    12/保存返回结果,判断是否发送成功
    13/返回结果
    :param mobile:
    :return:
    """
    # 获取参数
    image_code = request.args.get("text")
    image_code_id = request.args.get("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="手机号格式错误")

    # 检查图片验证码,从redis中获取正确的图片验证码
    try:
        real_image_code = redis_store.get("ImageCode_" + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询图片验证码失败")
    # 校验验证码是否过期
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg="图片验证码过期")
    # 删除图片验证码,图片验证码只使用一次
    try:
        redis_store.delete("ImageCode_" + image_code_id)
    except Exception as e:
        current_app.logger.error(e)

    # 比较图片验证码是否一致,忽略大小写
    if real_image_code.lower() != image_code.lower():
        return jsonify(errno=RET.DATAERR, errmsg="图片验证码错误")

    # 查询数据库,获取user对象
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询数据库异常")
    else:
        # 判断手机号是否已存在
        if user:
            return jsonify(errno=RET.DATAEXIST, errmsg="手机号已注册")

    # 构造短信验证码,生成6位随机数
    sms_code = "%06d" % random.randint(0, 999999)
    # 保存短信验证码到redis中
    try:
        redis_store.setex("SMSCode_" + mobile,
                          constants.SMS_CODE_REDIS_EXPIRES, sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="保存短信验证码失败")

    # 调用云通讯拓展包,发送短信
    try:
        cpp = sms.CCP()
        result = cpp.send_template_sms(
            mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg="发送短信异常")
    # 判断是否发送成功
    if 0 == result:
        return jsonify(errno=RET.OK, errmsg="发送成功")
    else:
        return jsonify(errno=RET.THIRDERR, errmsg="发送失败")
Пример #8
0
def send_sms_code(mobile):
    """
    1/获取参数,mobile,text,id,request.args.get('text')
    2/校验参数的完整性
    3/校验手机号,正则表达式校验手机号格式
    4/校验图片验证码,操作redis数据库,获取本地存储的真实图片验证码
    5/校验获取结果,判断真实的图片验证码是否存在
    6/删除redis中的图片验证码
    7/比较图片验证码是否一致
    8/生成短信验证码,'%06d' % random.randint(1,999999)生成随机数
    9/在本地存储短信验证码,存redis中
    10/判断手机号是否已注册
    11调用云通讯接口,发送短信,send_template_sms(mobile,sms_coce,time,tempID)
    12/保存发送结果,判断发送是否成功
    13/返回结果
    :param mobile:
    :return:
    """
    # 获取参数
    image_code = request.args.get('text')
    image_code_id = request.args.get('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='手机号码格式不正确')
    # 校验图片验证码,从redis中获取
    try:
        real_image_code = redis_store.get('ImageCode_' + image_code_id)
    except Exception as e:
        # 写入日志文件
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='获取图片验证码失败')
    # 校验获取结果
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg='图片验证码失效')
    # 删除图片验证码
    try:
        redis_store.delete('ImageCode_' + image_code_id)
    except Exception as e:
        # 写入日志文件
        current_app.logger.error(e)

    # 比较图片验证码是否一致,并忽略大小写
    if real_image_code.lower() != image_code.lower():
        return jsonify(errno=RET.PARAMERR, errmsg='图片验证码错误')

    # 开始准备发送短信验证码
    sms_code = '%06d' % random.randint(1, 999999)
    # 把验证码保存到redis中
    try:
        redis_store.setex('SMSCode_' + mobile,
                          constants.SMS_CODE_REDIS_EXPIRES, sms_code)

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

    # 判断手机号是否已经注册
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='获取用户的信息失败')
    else:
        # 判断用户是否已经存在
        if user is not None:
            return jsonify(errno=RET.DATAEXIS, errmsg='用户已经存在')

    # 调用云通讯接口发送短信
    try:
        cpp = sms.CCP()
        # 发送短信的模板方法会有返回值,0 表示发送成功
        result = cpp.send_template_sms(
            mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信异常')

    # 判断发送结果
    if 0 == result:
        return jsonify(errno=RET.OK, errmsg='发送成功')
    else:
        return jsonify(errno=RET.THIRDERR, errmsg='发送失败')
Пример #9
0
def send_sms_code(mobile):
    """
    发送短信:获取参数--校验参数--查询数据--返回结果
    1/获取参数,图片验证码和编号
    2/校验参数的完整性,mobile,text,id
    3/检查mobile手机号格式
    4/获取本地存储的真实图片验证码
    5/判断获取结果,图片验证码是否过期
    6/删除图片验证码
    7/比较图片验证码
    8/生成短信内容,随机数
    9/查询数据库,判断手机号是否已经注册
    10/保存短信内容到redis中
    11/调用云通讯发送短信
    12/保存返回结果,判断是否发送成功
    13/返回结果
    :param mobile:
    :return:
    """
    # 获取参数
    image_code = request.args.get('text')
    image_code_id = request.args.get('id')
    # 检查参数的完整性,any--all
    if not all([mobile,image_code,image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不完整')
    # 校验手机号格式是否满足
    if not re.match(r'1[3456789]\d{9}',mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号格式错误')
    # 检查图片验证码,获取本地存储的真实图片验证码
    try:
        real_image_code = redis_store.get('ImageCode_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询图片验证码失败')
    # 校验获取结果
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg='图片验证码过期')
    try:
        redis_store.delete('ImageCode_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
    # 比较图片验证码是否一致,忽略大小写
    if real_image_code.lower() != image_code.lower():
        return jsonify(errno=RET.DATAERR, errmsg='图片验证码错误')
    # 查询数据库,判断手机号是否已经注册
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询数据库异常')
    else:
        # 判断查询结果
        if user:
            return jsonify(errno=RET.DATAEXIST, errmsg='手机号已注册')
    # 构造短信随机码
    sms_code = '%06d' % random.randint(0, 999999)
    # 保存短信随机码
    print mobile
    print sms_code
    try:
        redis_store.setex('SMSCode_' + mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存短信验证码失败')
    # 调用云通讯扩展,发送短信
    try:
        ccp = sms.CCP()
        # 调用云通讯的模板方法发送短信
        result = ccp.send_template_sms(mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES/60],1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信异常')
    # 判断result是否发送成功
    # result = 0
    if 0 == result:
        return jsonify(errno=RET.OK, errmsg='发送成功')
    else:
        return jsonify(errno=RET.THIRDERR, errmsg='发送失败')