Esempio n. 1
0
def register():
    mobile = request.json.get('mobile')
    smscode = request.json.get('sms_code')
    password = request.json.get('password')
    if not all([mobile, smscode, password]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不全')
    try:
        redis_sms_code = redis_store.get('sms_' + mobile)
        if redis_sms_code.decode() == smscode:
            redis_store.delete('sms' + mobile)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='redis错误')
    if not redis_sms_code:
        return jsonify(errno=RET.NODATA, errmsg='验证码过期')
    if redis_sms_code.decode() != smscode:
        return jsonify(errno=RET.DATAERR, errmsg='验证码不一致 ')

    user = User()
    user.mobile = mobile
    user.nick_name = mobile
    user.password = password
    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg='数据保存失败')
    session['user_id'] = user.id
    session['mobile'] = mobile
    session['nick_name'] = mobile
    return jsonify(errno=RET.OK, errmsg='注册成功')
Esempio n. 2
0
def send_sms():
    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="参数不全")

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

    real_image_code = redis_store.get("ImageCode_" + image_code_id)
    if real_image_code:
        real_image_code = real_image_code
        redis_store.delete("ImageCode_" + image_code_id)
    else:
        return jsonify(errno=RET.NODATA, errmsg="验证码已过期")

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

    user = User.query.filter_by(mobile=mobile).first()
    if user:
        return jsonify(errno=RET.DATAEXIST, errmsg="该手机已被注册")
    result = random.randint(0, 999999)
    sms_code = "%06d" % result
    current_app.logger.debug("短信验证码的内容:%s" % sms_code)
    redis_store.set("SMS_" + mobile, sms_code,
                    constants.SMS_CODE_REDIS_EXPIRES)
    return jsonify(errno=RET.OK, errmsg="发送成功验证码为%s" % sms_code)
Esempio n. 3
0
def sms_code():
    user = None
    mobile = request.json.get('mobile')
    real_image_code = request.json.get('image_code')
    id_image_code = request.json.get('image_code_id')
    if not all([mobile, real_image_code, id_image_code]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数错误')
    # print('验证手机好')
    if not re.match('1[3456789]\d{9}', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='请输入正确手机号')
    user = User.query.filter(User.mobile == mobile).first()
    print(user)
    # if not user is None:
    #     return jsonify(error=RET.DATAEXIST, errmsg='此用户已注册,请直接登陆')
    image_code = redis_store.get('image_code_' + id_image_code)
    if not image_code:
        return jsonify(errno=RET.NODATA, errmsg='图片验证码过期')
    if real_image_code.lower() != image_code.lower():
        return jsonify(errno=RET.PARAMERR, errmsg='请输入正确的验证码.')
    redis_store.delete('image_code_' + id_image_code)
    random_sms_code = '%06d' % random.randint(0, 999999)
    redis_store.set('sms_code_' + mobile, random_sms_code, 300)
    print(mobile, real_image_code, id_image_code, random_sms_code)
    # 发送短信
    # statuCode = CCP().send_template_sms(mobile, [random_sms_code, 5], 1)
    # if statuCode != 0:
    #     return jsonify(errno = RET.THIRDERR,errmsg = "短信发送失败")
    return jsonify(errno=RET.OK, errmsg='发送成功.')
Esempio n. 4
0
def get_code():
    """
        1.获取参数
        2.校验参数,cur_id
        3.判断是否有上个pre_id,如果有则删除redis中上次图片验证码
        4.生成图片验证码,并存储到redis中
        5.返回图片验证码
        :return:
    """
    # redis_store.set()
    cur_code_id = request.args.get("cur_id")
    per_code_id = request.args.get("pre_id")

    if not cur_code_id:
        return "图片编码不能为空"
    try:
        if per_code_id:
            redis_store.delete("image_code:%s"%per_code_id)
    except Exception as e:
        current_app.logger.error(e)

    try:
        image_code_id, text, image_data = captcha.captcha.generate_captcha()
        redis_store.set("image_code:%s"%cur_code_id,text,constants.IMAGE_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return ("存储图片失败")

    response = make_response(image_data)
    response.headers['Content-Type'] = "image/jpg"

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

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

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

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

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

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

    if 0 == result:
        return jsonify(errno=RET.OK, errmsg='发送成功')
    else:
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信失败')
Esempio n. 6
0
def get_image_code():
    """
    获取请求参数
    生成图片验证码
    保存到redis
    返回图片验证码
    :return: 
    """""
    # 获取用户利用get请求发送的数据cur_id
    cur_id = request.args.get('cur_id')
    prcur_id = request.args.get('prcur_id')
    name, text, image_data = captcha.generate_captcha()
    # 将图片验证码写入到redis中
    try:
        redis_store.set('image_code:%s'%cur_id, text, constants.IMAGE_CODE_REDIS_EXPIRES)

        if prcur_id:
            redis_store.delete('image_code:%s'%prcur_id)
    except Exception as e:
        current_app.logger.error(e)

    response = make_response(image_data)
    response.headers['Content-Type'] = 'image/jpg'

    return response
Esempio n. 7
0
def get_image_code():
    # 1,获取参数
    # 2.生成验证码
    # 3.保存到redis
    # 5.返回图片验证码

    # 1.获取当前验证码图片的编号id
    code_id = request.args.get('code_id')

    pre_id = request.args.get('pre_id')
    # 2.生成验证码
    name, text, image_data = captcha.generate_captcha()
    # 3.保存到redis
    try:
        redis_store.set('image_code:%s' % code_id, text,
                        constants.IMAGE_CODE_REDIS_EXPIRES)

        if pre_id:
            redis_store.delete('image_code:%s' % pre_id)
    except Exception as e:
        current_app.logger.error(e)

    # 4.返回验证码,设置返回数据格式
    response = make_response(image_data)
    response.headers['Content-Type'] = 'image/jpg'
    return response
Esempio n. 8
0
def get_image_code():
    """
    1.获取请求参数
    2.生成图片验证码
    3.保存到redis
    4.返回图片验证码
    :return:
    """
    # 1.获取请求参数,args是获取?后面的参数
    cur_id = request.args.get("cur_id")
    pre_id = request.args.get("pre_id")
    # 2.生成图片验证码
    name, text, image_data = captcha.generate_captcha()
    # 3.保存到redis
    try:
        redis_store.set("image_code:%s" % cur_id, text,
                        constants.IMAGE_CODE_REDIS_EXPIRES)
        if pre_id:
            redis_store.delete("image_code:%s" % pre_id)
    except Exception as e:
        current_app.logger.error(e)
    # 4.返回图片验证码
    response = make_response(image_data)
    response.headers['Content-Type'] = 'image/jpg'
    return response
Esempio n. 9
0
def get_image_code():
    """
    1.获取请求参数
    2.生成图片验证码
    3.保存到reids
    4.返回图片验证码
    :return:
    """
    # 1.获取请求参数,args是获取?后面的参数  www.baidu.com?name=zhangsan&age=13
    cur_id = request.args.get("cur_id")
    pre_id = request.args.get("pre_id")

    # 2.生成图片验证码
    name, text, image_data = captcha.generate_captcha()

    # 3.保存到reids
    try:
        # key + value +time
        redis_store.set("image_code:%s" % cur_id, text,
                        constants.IMAGE_CODE_REDIS_EXPIRES)

        #判断是否有上一个uuid,如果存在则删除
        if pre_id:
            redis_store.delete("image_code:%s" % pre_id)

    except Exception as e:
        current_app.logger.error(e)

    # 4.返回图片验证码
    response = make_response(image_data)
    response.headers["Content-Type"] = "image/jpg"

    return response
Esempio n. 10
0
def getImage():
    from info import redis_store

    image_code_id = request.args.get('image_code_id')
    pre_image_code_id = request.args.get('pre_image_code_id')

    try:
        redis_store.delete('iamge_code_id:' + pre_image_code_id)
    except Exception as err:
        current_app.logger.error(err)
        return jsonify(errno=RET.DBERR, errmsg='数据库操作故障')

    try:
        name, code_text, image = captcha.captcha.generate_captcha()
    except Exception as err:
        current_app.logger.error(err)
        return jsonify(errno=RET.SERVERERR, errmsg='生成验证码失败')

    # 存储图片验证码内容和识别id;
    try:
        redis_store.setex('iamge_code_id:' + image_code_id,
                          constants.IMAGE_CODE_REDIS_EXPIRES, code_text)
    except Exception as err:
        current_app.logger.error(err)
        return make_response(jsonify(errno=RET.DBERR, errmsg='验证码保存失败'))

    # 给浏览器返回验证码图片;
    ret = make_response(image)
    ret.headers['content_type'] = 'image/jpg'
    return ret
Esempio n. 11
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')
Esempio n. 12
0
def register():
    mobile = request.json.get('mobile')
    sms_code = request.json.get('sms_code')
    password = request.json.get('password')
    print(mobile, sms_code, password)
    if not all([mobile, sms_code, password]):
        return jsonify(errno=RET.PARAMERR, errmsg='请输入正确参数')
    sms_code_redis_store = redis_store.get('sms_code_' + mobile)
    if not sms_code_redis_store:
        return jsonify(errno=RET.PARAMERR, errmsg='短信验证码已经过期')
    if sms_code_redis_store != sms_code:
        return jsonify(errno=RET.DATAERR, errmsg='请输入正确的短信校验码')
    try:
        redis_store.delete('sms_code_' + mobile)
    except Exception as e:
        current_app.logger.error(e)

    user = User()
    user.mobile = mobile
    user.nick_name = mobile
    user.password = password
    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg='参数错误')
    # 注册便登陆
    session['user_id'] = user.id
    session['nick_name'] = user.nick_name
    session['mobile'] = user.mobile
    user.last_login = datetime.now()
    return jsonify(errno=RET.OK, errmsg='注册成功')
Esempio n. 13
0
def register():
    # 1.获取参数
    # json_data = request.data
    # dict_data = json_data.loads(json_data)

    # 上面两句话可以写成一句话
    # request.get_json()
    # request.json,等价于上面一句话
    dict_data = request.json
    mobile = dict_data.get("mobile")
    sms_code = dict_data.get("sms_code")
    password = dict_data.get("password")

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

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

    # 4.根据手机号,去redis中取出短信验证码
    try:
        redis_sms_code = redis_store.get("sms_code:%s" % mobile)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="获取短信验证码异常")

    # 5.判断短信验证码是否过期
    if not redis_sms_code:
        return jsonify(errno=RET.NODATA, errmsg="短信验证码已过期")

    # 6.删除redis的短信验证码
    try:
        redis_store.delete("sms_code:%s" % mobile)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="删除短信验证码异常")

    # 7.判断传入的短信验证码和redis中取出的是否一致
    if sms_code != redis_sms_code:
        return jsonify(errno=RET.DATAERR, errmsg="短信验证码错误")

    # 8.创建用户对象,设置属性
    user = User()
    user.nick_name = mobile
    user.mobile = mobile
    # user.password_hash = jiami(password)    user.mobile = mobile
    # user.password_hash = password(password)    user.mobile = mobile
    user.password = password
    # 9.保存用户到数据库mysql
    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="用户注册失败")

    # 10.返回响应
    return jsonify(errno=RET.OK, errmsg="注册成功")
Esempio n. 14
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")
    print(mobile, 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("image_code_" + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg="获取验证码失败")

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

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

    redis_store.delete("image_code_" + image_code_id)  # 删除验证码

    generate_sms_code = "%06d" % random.randint(1, 999999)
    redis_store.set("sms_code_" + mobile, generate_sms_code,
                    constants.SMS_CODE_REDIS_EXPIRES)
    print("短信验证码:", generate_sms_code)
    # result = CCP().send_template_sms(mobile, [generate_sms_code, 5], 1)
    # if result != 0:
    #     return jsonify(errno=RET.PARAMERR, errmsg="第三方错误")
    print("短信验证码发送成功")
    return jsonify(errno=RET.OK, errmsg="短信验证码发送成功")
Esempio n. 15
0
def image_code():
    """
    1.获取参数
    2.校验参数(为空校验)
    3.生成图片验证码
    4.保存图片验证码到redis
    5.判断是否由上个图片验证码编号,有则删除
    6.返回图片验证码即可
    :return:
    """
    # 1.获取参数
    cur_id = request.args.get("cur_id")
    pre_id = request.args.get("pre_id")
    # 2.校验参数(为空校验)
    if not cur_id:
        return jsonify(errno=RET.NODATA, errmsg="图片验证码编号不存在")
    # 3.生成图片验证码
    name, text, image_data = captcha.generate_captcha()
    try:
        # 4.保存图片验证码到redis constants验证码有效期
        redis_store.set("image_code:%s" % cur_id, text,
                        constants.IMAGE_CODE_REDIS_EXPIRES)
        # 5.判断是否由上个图片验证码编号,有则删除
        if pre_id:
            redis_store.delete("image_code:%s" % pre_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="图片验证码异常")

    # 6.返回图片验证码即可
    response = make_response(image_data)
    response.headers["Content-Type"] = "image/jpg"
    return response
Esempio n. 16
0
def register():
    """
    注册
    :return:
    """
    # 1、获取参数
    param_dict = request.json
    mobile = param_dict.get("mobile", "")
    smscode = param_dict.get("smscode", "")
    password = param_dict.get("password", "")

    # 2、检验
    # 参数是否齐全
    if not all([mobile, smscode, password]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不全")
    # 短信验证码是否正确
    try:
        # 获取短信验证码
        real_sms_code = redis_store.get("SMS_" + mobile)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="获取本地短信验证码失败!")

    if not real_sms_code:
        # 短息验证码过期
        return jsonify(errno=RET.NODATA, errmsg="短信验证码失效")

    # 校验
    real_sms_code = real_sms_code.decode()
    if smscode != real_sms_code:
        return jsonify(errno=RET.DATAERR, errmsg="短信验证码错误")

    # 删除短息验证码
    try:
        redis_store.delete("SMS_" + mobile)
    except Exception as e:
        current_app.logger.error(e)

    # 3、保存数据
    user = User()
    user.mobile = mobile
    user.nick_name = mobile
    # 在User模型中对密码加密处理
    # hash_password = generate_password_hash(password)
    user.password = password
    db.session.add(user)
    try:
        db.session.commit()
    except Exception as e:
        db.session.rollback()  # 回滚
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg="保存数据失败")

    # 保持用户登录状态
    session["user_id"] = user.id
    # session["nick_name"] = user.nick_name
    # session["mobile"] = user.mobile

    # 4、返回响应
    return jsonify(errno=RET.OK, errmsg="OK")
Esempio n. 17
0
def sms_code():
    params_dict = request.json
    # 1.get mobile number infomation
    mobile = params_dict.get('mobile')
    # 2.get image code infomation
    image_code = params_dict.get('image_code')
    # 3.get code id infomation
    image_code_id = params_dict.get('image_code_id')
    # 4.verify all infomations
    if not all([mobile, image_code, image_code_id]):
        return jsonify(error=RET.PARAMERR, errmsg='参数不全')
    # 5.verify mobile number is true or false
    if not re.match(r'^1[3-9][0-9]{9}', mobile):
        return jsonify(error=RET.DATAERR, errmsg='手机号码不正确')
    # 6.verify image code
    real_image_code = None
    try:
        real_image_code = redis_store.get('image_code_id_' + image_code_id)
        if real_image_code:
            # to decode infomations from redis
            real_image_code = real_image_code.decode()
            redis_store.delete('image_code_id_' + 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='验证码已过期')

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

    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(error=RET.DBERR, errmsg='数据查询错误')
    if user:
        return jsonify(error=RET.DATAEXIST, errmsg='用户已存在,不能重复注册')
    # 7.create sms and send it to the mobile num
    result = random.randint(0, 999999)
    current_app.logger.info(result)
    sms_code = '%06d' % result
    current_app.logger.debug('sms image code content %s' % sms_code)

    # result = CCP().send_template_sms(mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], '1')
    # if result != 0:
    #     return jsonify(error=RET.THIRDERR, errmsg='send sms failed')

    # 8.save sms to 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(error=RET.DBERR, errmsg='报错短信验证码失败')

    # 9.reutrn response
    return jsonify(error=RET.OK, errmsg='ok')
Esempio n. 18
0
def send_sms():
    param_dict = request.json
    mobile = param_dict.get('mobile')
    image_code_id = param_dict.get('image_code_id')
    image_code = param_dict.get('image_code')
    if not all([mobile, image_code_id, image_code]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不足')
    if not re.match(r'^1[345678][0-9]{9}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号格式错误')
    # print('params =', param_dict)
    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='数据库查询失败')

    if real_image_code:
        try:
            redis_store.delete('imageCodeId_%s' % image_code_id)
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.DBERR, errmsg='数据库查询失败')
    else:
        return jsonify(errno=RET.NODATA, errmsg='验证码已过期')

    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='数据库查询失败')

    if user:
        return jsonify(errno=RET.DATAEXIST, errmsg='手机号已注册')

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

    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='数据库添加短信验证码失败')

    # result = CCP().send_template_sms(mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES], 1)
    # if result == 0:
    #     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='数据库添加短信验证码失败')
    # else:
    #     return jsonify(errno=RET.THIRDERR, errmsg='发送短信验证码失败')
    return jsonify(errno=RET.OK, errmsg='发送短信验证成功')
Esempio n. 19
0
def register():
    """
    1.获取参数
    2.校验参数,为空校验
    3.手机号格式校验
    4.根枯手机号,去redis中取出短信验证码
    5.判断短信验证码是否过期
    6.删除reids的短信验证码
    7.判断传入的短信验证码和redis中取出的是否一致
    8.创建用户对象,设置属性
    9.保存用户到数据库mysql
    10.返回响应
    :return:
    """
    # 1.获取参数
    # json_data = request.data
    # dict_data = json.loads(json_data)
    dict_data = request.json
    # 2.校验参数,为空校验
    mobile = dict_data.get("mobile")
    sms_code = dict_data.get("sms_code")
    password = dict_data.get("password")
    # 3.手机号格式校验
    if not re.match("1[35789]\d{9}", mobile):
        return jsonify(errno=RET.PARAMERR, errmsg="手机号格式错误")
    # 4.根枯手机号,去redis中取出短信验证码
    try:
        redis_sms_code = redis_store.get("sms_code:%s" % mobile)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="获取短信验证码异常")
    # 5.判断短信验证码是否过期
    if not redis_sms_code:
        return jsonify(error=RET.DBERR, errmsg="短信验证码已过期")
    # 6.删除reids的短信验证码
    try:
        redis_store.delete("sms_code:%s" % mobile)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg="删除短信验证码异常")
    # 7.判断传入的短信验证码和redis中取出的是否一致
    if sms_code != redis_sms_code:
        return jsonify(error=RET.DATAERR, errmsg="验证码输入错误")
    # 8.创建用户对象,设置属性
    user = User()
    user.nick_name = mobile
    # user.password_hash = password  # 需要加密处理
    user.password = password
    user.mobile = mobile
    # 9.保存用户到数据库mysql
    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg="注册用户失败")
    # 10.返回响应
    return jsonify(errno=RET.OK, errmsg="注册成功")
Esempio n. 20
0
def register():
    # 获取手机号,短信验证码,密码
    json_data = request.json
    mobile = json_data.get('mobile')
    sms_code = json_data.get('smscode')
    password = json_data.get('password')
    # 校验参数是否完整
    if not all([mobile, sms_code, password]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不全")

    try:
        real_sms_code = redis_store.get('SMS_' + mobile)  # 数据类型:bytes
        real_sms_code = real_sms_code.decode()  # 数据类型:str
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='获取本地验证码失败')

    # 校验短信验证码是否过期
    if not real_sms_code:
        return jsonify(errno=RET.NODATA, errmsg='验证码过期')

    # 校验短信验证码是否正确
    if sms_code != real_sms_code:
        return jsonify(errno=RET.DATAERR, errmsg='验证码错误')

    try:
        # 删除奇景获取了的验证码
        redis_store.delete('SMS_' + mobile)
    except Exception as e:
        current_app.logger.error(e)

    # 保存用户信息
    user = User()  # 创建模型类对象
    # 添加数据
    user.nick_name = mobile
    user.mobile = mobile
    user.password = password

    try:
        # 插入一条新对象
        db.session.add(user)
        db.session.commit()  # 提交到数据库
    except Exception as e:
        current_app.loggert(e)
        return jsonify(errno=RET.DBERR, errmsg='数据库保存错误')

    # 记录最后一次登陆时间
    try:
        user.last_login = datetime.now()
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)

    # 用session保存用户状态
    session['user_id'] = user.id
    session['nick_name'] = user.nick_name
    session['mobile'] = user.mobile

    return jsonify(errno=RET.OK, errmsg='OK')
Esempio n. 21
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="验证码已发送,请注意查收")
Esempio n. 22
0
def register():
    """
    点击注册按钮,发起注册请求
    :return: 返回注册结果, errno 和 errmsg
    """
    # 1. 获取参数
    params_dict = request.json
    if not params_dict:
        return jsonify(errno=RET.REQERR, errmsg="请求错误")
    mobile = params_dict.get("mobile")
    sms_code = params_dict.get("smscode")
    password = params_dict.get("password")

    # 2. 参数校验
    # 非空校验
    if not all([mobile, sms_code, password]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

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

    # 3. 从redis中获取短信验证码, 进行核对
    try:
        real_sms_code = redis_store.get("SMS_" + mobile)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据库查询错误")
    if not real_sms_code:
        return jsonify(errno=RET.NODATA, errmsg="短信验证码过期")
    if real_sms_code != sms_code:
        return jsonify(errno=RET.DATAERR, errmsg="短信验证码输入错误")

    # 验证通过, 删除redis中的短信验证码
    try:
        redis_store.delete("SMS_" + mobile)
    except Exception as e:
        current_app.logger.error(e)

    # 4. 初始化user模型, 并保存到数据库中
    user = User()
    user.nick_name = mobile
    user.mobile = mobile
    # 保存密码, 自动调用propert装饰后的方法, 加密为passwor_hash
    user.password = password
    # 记录最近登录的时间
    user.last_login = datetime.now()

    # 配置teardown后自动commit属性, 操作数据库后自动commit
    db.session.add(user)

    # 5. 记录用户登录状态到session
    session["user_id"] = user.id
    session["user_mobile"] = user.mobile
    session["user_nick_name"] = user.nick_name

    # 6. 返回注册结果
    return jsonify(errno=RET.OK, errmsg="注册成功")
Esempio n. 23
0
def register():
    """
    register user to database
    we need three parmas mobile numble,sms code and password
    """
    data = request.json
    # get mobile , sms code and password
    mobile = data.get('mobile')
    sms_code = data.get('smscode')
    password = data.get('password')

    # if not all params
    if not all([mobile, sms_code, password]):
        return jsonify(error=RET.PARAMERR, errmsg='参数不全')

    # get real sms code from redis
    try:
        real_sms_code = redis_store.get('SMS_' + mobile)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(error=RET.DATAERR, errmsg='获取短信验证码失败')
    # determine sms whether None
    if not real_sms_code:
        return jsonify(RET.NODATA, errmsg='短信验证码已过期')
    # verify sms code whether true or false
    if sms_code != real_sms_code.decode():
        return jsonify(RET.DATAERR, errmsg='短信验证码错误')

    # verify true
    try:
        redis_store.delete('SMS_' + mobile)
    except Exception as e:
        current_app.logger.error(e)

    # init user model
    user = User()
    user.nick_name = mobile
    user.mobile = mobile
    # password encryption
    user.password = password

    # storage user
    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        current_app.logger.error(e)
        return jsonify(RET.DATAERR, errmsg='用户保存错误')

    # keep login in web clent
    session['user_id'] = user.id
    session['nick_name'] = user.nick_name
    session['mobile'] = user.mobile

    # return response
    return jsonify(error=RET.OK, errmsg='ok')
Esempio n. 24
0
def send_sms():

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

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

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

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

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

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

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

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

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

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

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

    if resp == 0:
        return jsonify(errno=RET.OK)
    else:
        return jsonify(errno=RET.THIRDERR, errmsg='发送失败')
Esempio n. 25
0
def sms_code():
    # 1. 获取参数
    # dict_data = request.json
    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="参数验证不全")
    # 3. 验证手机号参数
    if not re.match("1[35789]\d{9}", mobile):
        return jsonify(errno=RET.DATAERR, errmsg="手机号码格式错误")
    # 4. 通过验证码编号,取出图片验证码
    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.DATAERR, errmsg="获取验证码失败")
    # 5. 判断验证码是否过期
    if not redis_image_code:
        return jsonify(erron=RET.NODATA, errmsg="验证码不存在")
    # 6. 删除redis中的验证码
    try:
        redis_store.delete("image_code:%s" % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PWDERR, errmsg="获取验证码异常")

    # 7. 判断验证码和redis中的是否相等
    if redis_image_code.lower() != image_code.lower():
        return jsonify(errno=RET.DATAERR, errmsg="图片验证码不相等")
    # 8. 生成验证码
    sms_code = "%06d" % random.randint(0, 999999)
    # 9. 通过ccp发送验证码
    current_app.logger.debug(sms_code)
    # ccp = CCP()
    # try:
    #     result = ccp.send_template_sms(mobile,[sms_code, constants.IMAGE_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="短信发送失败")
    # # 10. 存储验证码到redis中
    try:
        redis_store.set("sms_code:%s" % mobile, sms_code,
                        constants.IMAGE_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="返回成功")
Esempio n. 26
0
def send_sms_code():
    mobile = request.json.get('mobile')
    image_code = request.json.get('image_code')
    image_code_id = request.json.get('image_code_id')
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数缺失2')
    if not re.match(r'^1[3456789]\d{9}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号格式错误')
    try:
        real_image_code = redis_store.get('ImageCode_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='获取图片验证码异常')
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg='图片验证码已过期')
        # 因为图片验证码只能让用户比较一次,redis只能get一次,所以要立即删除
    try:
        redis_store.delete('ImageCode_' + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        # 比较图片验证码是否正确
    if real_image_code.lower() != image_code.lower():
        return jsonify(errno=RET.DATAERR, errmsg='图片验证码错误')
        # 判断手机号是否已注册
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(enrro=RET.DATAERR, errmsg='查询数据库异常')
    else:
        if user is not None:
            return jsonify(enrro=RET.DATAEXIST, errmsg='手机号已经注册过')

        # 构造短信随机码,format()
    sms_code = '%06d' % random.randint(0, 999999)
    # 把短信随机数保存到redis数据库中
    try:
        redis_store.setex('SMSCode_' + mobile,
                          constants.SMS_CODE_REDIS_EXPIRES, sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存短信验证码异常')
    # 调用云通讯,发送短信随机数
    try:
        ccp = sms.CCP()
        # 调用云通讯的模板方法,发送短信,需要保存发送结果
        result = ccp.send_template_sms(
            mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信异常')
    # 判断发送结果
    if result == 0:
        return jsonify(errno=RET.OK, errmsg='发送成功')
    else:
        return jsonify(errno=RET.THIRDERR, errmsg='发送失败')
Esempio n. 27
0
def register():
    """
    1. 获取参数和判断是否有值
    2. 从redis中获取指定手机号对应的短信验证码的
    3. 校验验证码
    4. 初始化 user 模型,并设置数据并添加到数据库
    5. 保存当前用户的状态
    6. 返回注册的结果
    :return:
    """
    # 1,获取参数,校验参数
    mobile = request.json.get('mobile')
    sms_code = request.json.get('smscode')
    password = request.json.get('password')
    print(sms_code)
    if not all([mobile, sms_code, password]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数缺失')
    # 2.从redis中获取指定手机号对应的短信验证码的
    try:
        # 从数据看获取的是二进制数据
        real_sms_code = redis_store.get('SMS_' + mobile).decode()
        print(real_sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据库查询失败')
    # 3.校验验证码
    # 3.1判断是否过期
    if not real_sms_code:
        return jsonify(errno=RET.NODATA, errmsg='验证码已过期')
    # 3.2判断验证码是否正确
    if sms_code != real_sms_code:
        return jsonify(errno=RET.DATAERR, errmsg='短信验证码错误')
    # 3.3删除短信验证码
    try:
        redis_store.delete('SMS_' + mobile)
    except Exception as e:
        current_app.logger.error(e)
    # 4. 初始化 user 模型,并设置数据并添加到数据库
    user = User()
    user.nick_name = mobile
    user.mobile = mobile
    user.password = password
    # 4.1 保存到数据库
    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        db.rollback()
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg='保存数据失败')
    # 5.保存当前用户的状态
    session['user_id'] = user.id
    session['nick_name'] = user.nick_name
    session['mobile'] = user.mobile
    # 6.返回注册的结果
    return jsonify(errno=RET.OK, errmsg='注册成功')
Esempio n. 28
0
def register():
    """
    1.接收参数:mobile,smscode,password
    2.整体效验参数的完整性
    3.手机号格式是否正确
    4.从redis中通过手机号码取出真实的短信验证码
    5.和用户输入的验证码比较
    6.初始化User()添加数据
    7.session保持用户登录状态
    8.返回响应
    :return:
    """
    # 1 接收参数
    dict_data = request.json
    mobile = dict_data.get("mobile")
    sms_code = dict_data.get("smscode")
    password = dict_data.get("password")
    # 2.整体效验参数的完整性
    if not all([mobile, sms_code, password]):
        return jsonify(errno=RET.PARAMERR, errmsg="有参数为空")
    # 3.手机格式正确
    if not re.match(r"1[35678][0-9]{9}$", mobile):
        return jsonify(errno=RET.DATAERR, errmsg="号码不符合")
    # 4.从redis中通过手机号码取出真实的短信验证码
    try:
        real_sms_code = redis_store.get("SMS_" + mobile)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg="获取本地验证码失败")
    if not real_sms_code:
        return jsonify(errno=RET.NODATA, errmsg="短信验证码过期")
    if sms_code != real_sms_code:
        return jsonify(errno=RET.DATAERR, errmsg="短信验证码不符合")
    # 删除短信验证码
    try:
        redis_store.delete("SMS_" + mobile)
    except Exception as e:
        current_app.logger.error(e)
    # 5.初始化User()添加数据

    user = User()
    user.nick_name = mobile
    # user.password_hash = password
    user.password = password  # 暗密码
    user.mobile = mobile
    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据库保持失败")
    # 7 session保持用户登录状态
    session["user_id"] = user.id
    return jsonify(errno=RET.OK, errmsg="注册成功")
Esempio n. 29
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="发送成功")
Esempio n. 30
0
def register():
    '''
    1. 获取参数
    2. 校验参数
    3. 通过手机号码取出验证码
    4. 判断验证码是否过期
    5. 删除redis中的短信验证码
    6. 判断验证码的正确性
    7. 创建用户对象
    8. 设置用户属性
    9. 保存到数据库
    10. 返回响应
    :return:
    '''
    dict_data = request.json
    mobile = dict_data.get('mobile')
    sms_code = dict_data.get('sms_code')
    password = dict_data.get('password')

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

    try:
        redis_sms_code = redis_store.get('sms_code:%s' % mobile)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="获取短信验证码异常")

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

    try:
        redis_store.delete('sms_code:%s' % mobile)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="删除短信验证码异常")

    if redis_sms_code != sms_code:
        return jsonify(errno=RET.DATAERR, errmsg="短信验证码错误")

    user = User()

    user.nick_name = mobile
    user.password = password
    user.mobile = mobile
    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg="用户注册失败")

    return jsonify(errno=RET.OK, errmsg="用户注册成功")