Esempio n. 1
0
 def post(self, request):
     a = json.loads(request.body.decode())
     bianma = a['bianma']
     code = a['code']
     if rds.get("image_code_%s" % bianma):
         if code == rds.get("image_code_%s" % bianma).decode():
             rds.delete("image_code_%s" % bianma)
             return JsonResponse({"msg": "ok"})
         else:
             return JsonResponse({"msg": "no"})
     else:
         return JsonResponse({"msg": "cuowu"})
Esempio n. 2
0
def area(request):
    # 尝试从redis中读取数据
    try:
        resp_json = rds.get("area_info")
    except Exception as e:
        print(e)
    else:
        if resp_json is not None:
            return JsonResponse({
                "data": json.loads(resp_json.decode()),
                "is": "yes"
            })

    try:
        area_li = Area.objects.all()
    except Exception as e:
        return JsonResponse({"err": "数据库异常"})

    area_dict_li = []
    for area in area_li:
        area_dict_li.append(area.to_dict())

    # 将数据保存到redis中

    areas = json.dumps(area_dict_li)
    try:
        rds.setex("area_info", 300, areas)
    except Exception as e:
        print(e)
    return JsonResponse({"data": area_dict_li, "is": "no"})
Esempio n. 3
0
def register(request):
    req = json.loads(request.body.decode())

    mobile = req['mobile']
    sms_code = req['sms_code']
    password = req['password']
    password2 = req['password2']

    # 校验参数
    if not all([mobile, sms_code, password, password2]):
        return JsonResponse({"err": "参数不完整"})

    # 判断手机号格式
    if not re.match(r"1[345678]\d{9}", mobile):
        return JsonResponse({"err": "手机号格式错误"})

    if password != password2:
        return JsonResponse({"err": "两次密码不一致"})

    # 从redis中取出短信验证码
    try:
        real_sms_code = rds.get("sms_code_%s" % mobile)
    except Exception as e:
        return JsonResponse({"err": "读取真实短信验证码异常"})

    # 判断短信验证码是否过期
    if real_sms_code is None:
        return JsonResponse({"err": "短信验证码失效"})

    # # 删除redis中的短信验证码,防止重复使用校验
    # try:
    #     rds.delete("sms_code_%s" % mobile)
    # except Exception as e:
    #     print(e)

    #

    # # 判断用户的手机号是否注册过
    # try:
    #     user = User.objects.filter(mobile=mobile).first()
    # except Exception as e:
    #     return JsonResponse({"err": "数据库异常"})
    # else:
    #     if user is not None:
    #         # 表示手机号已存在
    #         return JsonResponse({"err": "手机号已存在"})

    try:
        user = User(name=mobile, mobile=mobile)
        user.password = password
        user.save()
    except IntegrityError as e:
        return JsonResponse({"err": "手机号已存在"})
    except Exception as e:
        return JsonResponse({"err": "查询数据库异常"})

    return JsonResponse({"err": "注册成功"})
Esempio n. 4
0
def login(request):
    """
    登录
    :param request: 手机号,密码
    :return: 成功,失败
    """
    req = json.loads(request.body.decode())
    mobile = req['mobile']
    password = req['password']
    if not all([mobile, password]):
        # 参数不完整
        return JsonResponse({"err": "参数不完整"})

    # 手机号的格式
    if not re.match(r'1[34567]\d{9}', mobile):
        return JsonResponse({"err": "手机号格式错误"})

    # 判断错误次数是否超过限制,如果超过限制,则返回
    # redis记录: "access_nums_请求ip":"次数"
    user_ip = request.META['REMOTE_ADDR']
    try:
        access_nums = rds.get("access_num_%s" % user_ip).decode()
        print(type(access_nums), access_nums)
    except Exception as e:
        print(e)
    else:
        if access_nums is not None and int(access_nums) >= 5:
            return JsonResponse({"err": "错误次数过多,请稍后重试"})

    try:
        user = User.objects.filter(mobile=mobile).first()
    except Exception as e:
        print(e)
        return JsonResponse({"err": "获取用户信息失败"})

    # 用数据库的密码与用户填写的密码进行对比验证
    if user is None or not user.check_password(password):
        # 如果验证失败,记录错误次数,返回信息
        try:
            rds.incr("access_num_%s" % user_ip)
            rds.expire("access_num_%s" % user_ip, 300)
        except Exception as e:
            print(e)
        return JsonResponse({"err": "用户名或密码错误"})

    # 如果验证相同成功,保存登录状态,再session中
    request.session["islogin"] = True
    return JsonResponse({"msg": "登录成功"})
Esempio n. 5
0
def get_sms_code(request):
    """
    获取短信验证码
    :param request: mobile,image_code,image_code_id
    :return: code:0 成功 -1失败
    """
    image_code = request.GET.get('image_code')
    image_code_id = request.GET.get('image_code_id')
    mobile = request.GET.get('mobile')

    if not all([image_code, image_code_id, mobile]):
        # 参数不完整
        return JsonResponse({"err": "参数不完整"})

    # 业务逻辑处理
    # 从redis中取出真实的图片验证码
    try:
        real_image_code = rds.get('image_code_%s' % image_code_id)
    except Exception as e:
        return JsonResponse({"err": "redis数据库异常"})

    # 判断图片验证码是否过期
    if real_image_code is None:
        return JsonResponse({"err": "图片验证码失效"})

    # 删除图片验证码
    try:
        rds.delete("image_code_%s" % image_code_id)
    except Exception as e:
        print(e)

    # 与用户填写的值进行对比
    if real_image_code.decode().lower() != image_code.lower():
        return JsonResponse({"err": "图片验证码错误"})

    # 判断60s内是否有发送短信
    try:
        send_flag = rds.get("send_sms_code_%s" % mobile)
    except Exception as e:
        print(e)
    else:
        if send_flag is not None:
            return JsonResponse({"err": "请求过于频繁"})

    # 判断手机号是否存在
    # try:
    #     user = User.objects.filter(mobile=mobile).first()
    # except Exception as e:
    #     print(e)
    # else:
    #     if user is not None:
    #         # 表示手机号已存在
    #         return JsonResponse({"err": "手机号已存在"})

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

    # 保存真实的短信验证码
    try:
        # rds.setex("sms_code_%s" % mobile, 300, sms_code)
        rds.set("sms_code_%s" % mobile, sms_code)
        # 保存发送给这个手机号码的记录,防止用户再60s内再次发送短信的操作
        rds.setex("send_sms_code_%s" % mobile, 60, 1)
    except Exception as e:
        print(e)
        return JsonResponse({"err": "保存短信验证码异常"})
    # 发送短信
    send_sms.delay(mobile, [sms_code, 5], 1)
    return JsonResponse({"msg": "发送成功", "code": 0})