Exemple #1
0
def register(request):
    # 判断参数是否正确
    data = request.data
    code = data.get("code")
    if not code:
        return Response({"status": 400, "msg": "验证码不能为空"})
    validate_code = None
    kwargs = {}
    if data.get("mobile"):
        kwargs["mobile"] = data["mobile"]
        passport = passportRepository.load(**kwargs)
        if passport:
            return Response({"status": 400, "msg": "用户已存在"})
        validate_code = cache.get(data.get("mobile"))
    elif data.get("email"):
        kwargs["email"] = data["email"]
        passport = passportRepository.load(**kwargs)
        if passport:
            return Response({"status": 400, "msg": "用户已存在"})
        validate_code = cache.get(data.get("email"))
    else:
        return Response({"status": 400, "msg": "参数错误"})

    kwargs = {}
    kwargs["name"] = data["name"]
    passport_list = passportRepository.search(**kwargs)
    if passport_list:
        return Response({"status": 400, "msg": "用户名已存在"})

    if validate_code != str(code):
        return Response({"status": 400, "msg": "验证码错误"})

    password = data.get("password")
    if not password:
        return Response({"status": 400, "msg": "密码不能为空"})
    if not Validate.password_validate(password):
        return Response({"status": 400, "msg": "密码格式不正确"})

    # 随机生成盐值加密
    salt = CryptogramGenerate.get_salt().decode()
    password = CryptogramGenerate.md5(password + salt)
    password = password + '|' + salt
    created = TimeUtil.get_utc_time()
    avert = user_settings.defaultAvert
    passport = Passport(password=password, created=created,
                        email=data.get("email"), mobile=data.get("mobile"), name=data.get("name"), avert=avert)
    passport.save()

    return Response({"status": 200, "msg": ""})
Exemple #2
0
def modify_password(request):
    data = request.data
    code = data.get("code")
    if not code:
        return Response({"status": 400, "msg": "参数错误"})
    kwargs = {}
    if data.get("email"):
        kwargs["email"] = data["email"]
        passport = passportRepository.load(**kwargs)
        if not passport:
            return Response({"status": 404, "msg": "用户不存在"})
        validate_code = cache.get(data.get("email"))
        if not validate_code:
            return Response({"status": 404, "msg": "验证码过期"})
    elif request.get("mobile"):
        kwargs["mobile"] = data["mobile"]
        passport = passportRepository.load(**kwargs)
        if not passport:
            return Response({"status": 404, "msg": "用户不存在"})
        validate_code = cache.get(data.get("mobile"))
        if not validate_code:
            return Response({"status": 404, "msg": "验证码过期"})
    else:
        return Response({"status": 400, "msg": "参数错误"})

    if str(code) != validate_code:
        return Response({"status": 400, "msg": "验证码错误"})

    password = data.get("password")
    if not password:
        return Response({"status": 400, "msg": "密码不能为空"})
    if not Validate.password_validate(password):
        return Response({"status": 400, "msg": "密码格式不正确"})

    salt = CryptogramGenerate.get_salt().decode()
    password = CryptogramGenerate.md5(password + salt)
    password = password + '|' + salt
    passport = passport[0]
    passport.password = password
    passport.save()

    return Response({"status": 200, "msg": "密码更新成功"})