Ejemplo n.º 1
0
def check_email():
    """
    验证邮箱(用于更换邮箱)
    :return:
    """
    email = request.json.get('email')
    email_code = request.json.get('email_code')

    # 对比邮箱验证码
    try:
        real_email_code = redis_store.get('email_code_' + email).decode()
    except Exception as e:
        logging.error(e)
        return jsonify(message="数据库错误")

    if not real_email_code:
        return jsonify(message="短信验证码已过期或者手机号填写错误")

    if real_email_code.lower() != email_code.lower():
        return jsonify(message="验证码填写错误")

    try:
        redis_store.delete('email_code_' + email)
    except Exception as e:
        logging.error(e)
        return jsonify(message="删除redis数据库错误")

    sql = """update user set email=%s where id=%s"""
    db.update(sql, (email, id))

    return jsonify(message='邮箱绑定成功')
Ejemplo n.º 2
0
def check_mobile():
    """
    验证手机号,用于绑定新手机号
    :return:
    """
    # 获取前端数据
    mobile = request.json.get('mobile')
    sms_code = request.json.get('sms_code')

    # 对比短信验证码
    try:
        real_sms_code = redis_store.get('sms_code_' + mobile).decode()
    except Exception as e:
        logging.error(e)
        return jsonify(message="获取验证码错误")

    if not real_sms_code:
        return jsonify(message="短信验证码已过期")

    if real_sms_code != sms_code:
        return jsonify(message="验证码有误,请重新输入")

    try:
        redis_store.delete('sms_code_' + mobile)
    except Exception as e:
        logging.error(e)
        return jsonify(message="删除redis数据库错误")

    return jsonify(message='验证码验证成功')
Ejemplo n.º 3
0
def register():
    """
    注册
    :return:
    """
    # 获取前端数据
    mobile = request.json.get('mobile')
    sms_code = request.json.get('sms_code')
    password = request.json.get('password')

    # 验证数据完整性
    if not all([mobile, sms_code, password]):
        return jsonify(message='参数不全')

    # 密码长度验证
    if not re.match(r'^\w{6,20}$', password):
        return jsonify(message='密码长度为6-20个字符')

    # 判断是否注册
    sql = """select id from user where mobile=%s"""
    res, result = db.fetch_one(sql, mobile)

    if res:
        return jsonify(message="手机号已注册")

    # 对比短信验证码
    try:
        real_sms_code = redis_store.get('sms_code_' + mobile).decode()
    except Exception as e:
        logging.error(e)
        return jsonify(message="获取验证码错误")

    if not real_sms_code:
        return jsonify(message="短信验证码已过期")

    if real_sms_code != sms_code:
        return jsonify(message="验证码有误,请重新输入")

    try:
        redis_store.delete('sms_code_' + mobile)
    except Exception as e:
        logging.error(e)
        return jsonify(message="删除redis数据库错误")

    # 将密码进行加密
    password = generate_password_hash(password)

    # 将数据保存到数据库中
    sql = """insert into user (mobile, password) VALUES (%s, %s)"""

    db.insert(sql, (mobile, password))

    return jsonify(message='注册成功')
Ejemplo n.º 4
0
def mobile_login():
    """
    手机号登录
    :return:
    """
    # 获取参数
    mobile = request.json.get('mobile')
    code = request.json.get('code')

    if not all([mobile, code]):
        return jsonify(message='请填写手机号或验证码')

    # 判断是否注册
    sql = """select mobile from user where mobile=%s"""
    res, result = db.fetch_one(sql, mobile)

    if res == None:
        return jsonify(message='请先注册')

    # 对比短信验证码
    try:
        real_sms_code = redis_store.get('sms_code_' + mobile).decode()
    except Exception as e:
        logging.error(e)
        return jsonify(message="数据库错误")

    if not real_sms_code:
        return jsonify(message="短信验证码已过期")

    if real_sms_code != code:
        return jsonify(message="验证码填写错误")

    try:
        redis_store.delete('sms_code_' + mobile)
    except Exception as e:
        logging.error(e)
        return jsonify(message="删除redis数据库错误")

    # 设置session
    session['name'] = mobile

    # 返回用户信息
    sql = """select id, mobile from user where mobile=%s"""
    res, result = db.fetch_one(sql, mobile)

    return jsonify(id=result[0], mobile=result[1], message='登录成功')
Ejemplo n.º 5
0
def reset_mobile():
    """
    绑定新手机号
    :return:
    """
    # 获取前端数据
    id = request.json.get('id')
    new_mobile = request.json.get('new_mobile')
    sms_code = request.json.get('sms_code')

    if not all([new_mobile, sms_code]):
        return jsonify(message='手机号或验证码不能为空')

    # 检测新手机号是否注册
    sql = """select mobile from user where mobile=%s"""
    res, result = db.fetch_one(sql, new_mobile)

    if res:
        return jsonify(message='该手机号已注册')

    # 对比短信验证码
    try:
        real_sms_code = redis_store.get('sms_code_' + new_mobile).decode()
    except Exception as e:
        logging.error(e)
        return jsonify(message="获取验证码错误")

    if not real_sms_code:
        return jsonify(message="短信验证码已过期")

    if real_sms_code != sms_code:
        return jsonify(message="验证码有误,请重新输入")

    try:
        redis_store.delete('sms_code_' + new_mobile)
    except Exception as e:
        logging.error(e)
        return jsonify(message="删除redis数据库错误")

    # 保存新手机号
    sql = """update user set mobile=%s where id=%s"""

    db.update(sql, (new_mobile, id))

    return jsonify(message='修改手机号成功')
Ejemplo n.º 6
0
def pass_info():
    """
    修改密码
    :return:
    """

    # 获取参数
    mobile = request.json.get('mobile')
    sms_code = request.json.get('sms_code')
    new_password = request.json.get('new_password')

    # 验证参数
    if not all([mobile, sms_code, new_password]):
        return jsonify(message='参数不全')

    # 对比短信验证码
    try:
        real_sms_code = redis_store.get('sms_code_' + mobile).decode()
    except Exception as e:
        logging.error(e)
        return jsonify(message="获取验证码错误")

    if not real_sms_code:
        return jsonify(message="短信验证码已过期")

    if real_sms_code != sms_code:
        return jsonify(message="验证码有误,请重新输入")

    try:
        redis_store.delete('sms_code_' + mobile)
    except Exception as e:
        logging.error(e)
        return jsonify(message="删除redis数据库错误")

    # 将新密码加密插入数据库中
    new_password = generate_password_hash(new_password)

    sql = """update user set password=%s where mobile=%s"""
    db.update(sql, (new_password, mobile))

    return jsonify(message='修改密码成功')
Ejemplo n.º 7
0
def reset_email():
    """
    用户重置邮箱
    :return:
    """
    # 获取前端数据
    id = request.json.get('id')
    new_email = request.json.get('new_email')
    email_code = request.json.get('email_code')

    if not all([new_email, email_code]):
        return jsonify(message='手机号或验证码不能为空')

    # 对比邮件验证码
    try:
        real_email_code = redis_store.get('email_code_' + new_email).decode()
    except Exception as e:
        logging.error(e)
        return jsonify(message="获取验证码错误")

    if not real_email_code:
        return jsonify(message="短信验证码已过期")

    if real_email_code.lower() != email_code.lower():
        return jsonify(message="验证码有误,请重新输入")

    try:
        redis_store.delete('email_code_' + new_email)
    except Exception as e:
        logging.error(e)
        return jsonify(message="删除redis数据库错误")

    # 保存新手机号
    sql = """update user set email=%s where id=%s"""

    db.update(sql, (new_email, id))

    return jsonify(message='修改手机号成功')
Ejemplo n.º 8
0
def set_mobile():
    """
    设置手机号(老用户使用邮箱登录,未绑定手机号)
    :return:
    """
    # 获取前端数据
    id = request.json.get('id')
    mobile = request.json.get('mobile')

    # 完整性判断
    if not mobile:
        return jsonify(message='手机号不能为空')

    # 对比短信验证码
    try:
        real_sms_code = redis_store.get('sms_code_' + mobile).decode()
    except Exception as e:
        logging.error(e)
        return jsonify(message="获取验证码错误")

    if not real_sms_code:
        return jsonify(message="短信验证码已过期")

    if real_sms_code != sms_code:
        return jsonify(message="验证码有误,请重新输入")

    try:
        redis_store.delete('sms_code_' + mobile)
    except Exception as e:
        logging.error(e)
        return jsonify(message="删除redis数据库错误")

    # 将手机号保存到数据库中
    sql = """update user set mobile=%s where id=%s"""
    db.update(sql, (mobile, id))

    return jsonify(message='手机号绑定成功')