Пример #1
0
def register():
    '''注册'''
    # 1获取参数
    param_dict = request.json
    username = param_dict.get('username')
    email = param_dict.get('email')
    password = param_dict.get('password')
    repeatpassword = param_dict.get('repeatpassword')
    # 2校验参数
    if not all([email, username, password, repeatpassword]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不能为空')

    # 判断邮箱是否存在
    result = None
    try:
        result = User.query.filter(User.email == email).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据查询错误')
    if result:
        return jsonify(errno=RET.DATAEXIST, errmsg='数据已存在')
    # 判断用户名是否存在
    result = None
    try:
        result = User.query.filter(User.username == username).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据查询错误')
    if result:
        return jsonify(errno=RET.DATAEXIST, errmsg='数据已存在')

    # 初始化User对象添加到数据
    user = User()
    user.username = username
    user.password = password
    user.email = email
    user.mobile = username
    user.gender = 0  # 0代表male,1代表female

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

    # 5.保存用户的登录状态
    session['user_id'] = user.id
    session['username'] = user.username
    # 6返回相应
    return jsonify(errno=RET.OK, errmsg='注册成功')
Пример #2
0
def registeV2():
    params_dict = request.json
    username, email, email_code, password = params_dict.get(
        "username", "email", "email_code", "password")
    current_app.loger.info(params_dict)

    if not all([username, email, email_code, password]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")
    if not re.match(r'[0-9a-zA-Z]{8,16}', username):
        return jsonify(errno=RET.PARAMERR, errmsg="用户名格式错误")
    if not re.match(r'^[0-9a-zA-Z)]{0,19}@[0-9]{1,13}\.com', email):
        return jsonify(errno=RET.PARAMERR, errmsg="邮箱格式错误")
    if not re.match(r'[0-9a-zA-Z_]{6,15}', password):
        return jsonify(errno=RET.PARAMERR, errmsg="密码格式错误")
    try:
        real_email_code = redis_store.get("EmailCode:" + email)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg="数据查询失败")
    if not real_email_code:
        return jsonify(errno=RET.NODATA, errmsg="邮箱验证码已过期")
    if real_email_code != email_code:
        return jsonify(errno=RET.PARAMERR, errmsg="邮箱验证码输入错误")

    user = User()
    user.nick_name = username
    user.email = email
    user.password = password
    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()

    session['user_id'] = user.id
    session['nick_name'] = user.nick_name
    session['email'] = user.email
    return jsonify(errno=RET.OK, msg="注册成功")
Пример #3
0
def register():
    """
    用户注册
    1、获取参数,mobile,sms_code,password
    2、检查参数完整性
    3、检查手机号的格式
    4、尝试从redis中获取真实的短信验证码
    5、判断获取结果是否存在
    6、先比较短信验证码是否正确
    7、删除redis中存储的短信验证码
    8、构造模型类对象,存储用户信息
    9、提交数据到数据库中
    10、缓存用户信息到redis数据库中
    11、返回结果
    :return:
    """
    # 获取参数
    mobile = request.json.get('mobile')
    email = request.json.get("email")
    sms_code = request.json.get('sms_code')
    password = request.json.get('password')
    # 检查参数的完整性
    if not all([email, sms_code, password]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数缺失')
    # 使用正则校验手机号格式
    # if not re.match(r'1[3456789]\d{9}$', mobile):
    #     return jsonify(errno=RET.PARAMERR, errmsg='手机号格式错误')
    if not re.match('^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$', email):
        return jsonify(errno=RET.PARAMERR, errmsg='email格式错误')
    # 从redis中获取真实的短信验证码
    try:
        real_sms_code = redis_store.get('SMSCode_' + email)
    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 != str(sms_code):
        return jsonify(errno=RET.DATAERR, errmsg='短信验证码不一致')
    # 删除短信验证码
    try:
        redis_store.delete('SMSCode_' + email)
    except Exception as e:
        current_app.logger.error(e)
    # 判断手机号是否已注册
    try:
        user = User.query.filter_by(email=email).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='邮箱已注册')
    # 保存用户信息
    user = User()
    # user.mobile = '18772845363'
    user.email = email
    user.nick_name = email
    # 实际上调用了模型类中的password方法,实现了密码加密存储,generate_password_hash
    user.password = password
    # 提交数据到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='保存数据失败')
    # 缓存用户信息
    session['user_id'] = user.id
    session['email'] = email
    session['nick_name'] = email
    # 返回结果
    return jsonify(errno=RET.OK, errmsg='OK')