Exemplo n.º 1
0
def changecart(request, flag):
    num = 1
    if flag == "1":
        num = -1

    # 判断是否登录
    tokenValue = request.COOKIES.get("token")
    if not tokenValue:
        print("nonononono")
        # 说明没登录
        return JsonResponse({"error": 1})
    try:
        user = User.objects.get(tokenValue=tokenValue)
    except User.DoesNotExist as e:
        return JsonResponse({"error": 2})

    gid = request.POST.get("gid")
    cid = request.POST.get("cid")
    pid = request.POST.get("pid")

    product = Product.objects.filter(categoryId=gid,
                                     childId=cid).get(productId=pid)

    try:
        cart = Cart.objects.filter(user__tokenValue=tokenValue).filter(
            product__categoryId=gid).filter(product__childId=cid).get(
                product__productId=pid)
        if flag == "2":
            if product.storeNums == "0":
                return JsonResponse({"error": 0, "num": cart.num})
        #以买过该商品,得到了该购物车数据
        cart.num = cart.num + num
        product.storeNums = str(int(product.storeNums) - num)
        product.save()
        if cart.num == 0:
            cart.delete()
        else:
            cart.save()

    except Cart.DoesNotExist as e:
        if flag == "1":
            return JsonResponse({"error": 0, "num": 0})
        #找一个可用的订单  isDelete为False,flag为0,在当前用户中的所有订单中最多只能有一个订单为0
        try:
            order = Order.orders2.filter(user__tokenValue=tokenValue).get(
                flag=0)
        except Order.DoesNotExist as e:
            #没有可用订单
            orderId = str(uuid.uuid4())
            address = Address.objects.get(pk=1)
            order = Order.create(orderId, user, address, 0)
            order.save()
        #没有购买过该商品,需要创建该条购物车数据
        cart = Cart.create(user, product, order, 1)
        cart.save()
        product.storeNums = str(int(product.storeNums) - num)
        product.save()
    # 告诉客户端添加成功
    return JsonResponse({"error": 0, "num": cart.num})
Exemplo n.º 2
0
def changecart(request, flag):
    num = 1
    if flag == "1":
        num = -1
    tokenValue = request.COOKIES.get("token")
    if not tokenValue:
        return JsonResponse({"error": 1})
    try:
        # 找到用户
        user = User.objects.get(tokenValue=tokenValue)
    except User.DoesNotExist as e:
        return JsonResponse({"error": 2})

    gid = request.POST.get("gid")
    cid = request.POST.get("cid")
    pid = request.POST.get("pid")
    # 找到商品
    product = Product.objects.filter(categoryId=gid, childId=cid).get(productId=pid)
    # 找到该商品的购物车数据
    try:
        cart = Cart.objects.filter(user__tokenValue=tokenValue).filter(product__categoryId=gid).filter(
            product__childId=cid).get(product__productId=pid)
        # 添加商品时,如果商品库存为0,就不在添加,数据(num)返回
        if flag == "2":
            if product.storeNums == "0":
                return JsonResponse({"error": 0, "num": cart.num})
        # 买过商品,把购物车数据加上
        cart.num = cart.num + num
        # 减该商品库存
        product.storeNums = str(int(product.storeNums) - num)
        product.save()
        # 当商品数量为0时,删除购物车数据。不为0保存数据
        if cart.num == 0:
            cart.delete()
        else:
            cart.save()
    except Cart.DoesNotExist as e:
        # 没有该商品购物车数据,点击减商品时,不会有变化。
        if flag == "1":
            return JsonResponse({"error": 0, "num": 0})
        # 数据不存在说明没有买过该商品,创建数据添加到购物车。
        # 找到一个可用的订单flag为0的
        try:
            order = Order.orders2.filter(user__tokenValue=tokenValue).get(flag=0)
        except Order.DoesNotExist as e:
            # 没有可用订单,创建订单
            orderId = str(uuid.uuid4())
            address = Address.objects.get(pk=1)
            order = Order.create(orderId, user, address, 0)
            order.save()
        # 没有购买过该商品,创建购物车数据
        cart = Cart.create(user, product, order, 1)
        cart.save()
        # 减该商品库存
        product.storeNums = str(int(product.storeNums) - num)
        product.save()
        # 把数据返回
    return JsonResponse({"error": 0, "num": cart.num})
Exemplo n.º 3
0
def changecart(request,flag):
    num = 1
    if flag == '1':
        num = -1
    #判断是否登陆
    tokenValue=request.COOKIES.get('token')
    if not tokenValue:
        return JsonResponse({'error':1})
    try:
        user = User.objects.get(tokenValue=tokenValue)
    except User.DoesNotExist as e:
        return JsonResponse({'error':2})

    gid = request.POST.get('gid')
    cid = request.POST.get('cid')
    pid = request.POST.get('pid')
    product = Product.objects.filter(categoryId=gid,childId=cid).get(productId=pid)
    try:
        cart = Cart.objects.filter(user__tokenValue=tokenValue).filter(product__categoryId=gid).filter(product__childId=cid).get(product__productId=pid)
        if flag == '2':
            if product.storeNums == '0':
                return JsonResponse ({'error':0,'num':cart.num})
        cart.num = cart.num + num
        product.storeNums = str(int(product.storeNums) - num)
        product.save()
        if cart.num == 0:
            cart.delete()
        else:
            cart.save()

    except Cart.DoesNotExist as e:
        if flag == '1':
            return  JsonResponse({'error':0,'num':0})
        try:
            order = Order.orders2.filter(user__tokenValue=tokenValue).get(flag=0)
        except Order.DoesNotExist as e:
            orderId = str(uuid.uuid4())
            address = Address.objects.get(pk=3)
            order = Order.create(orderId,user,address,0)
            order.save()
        cart = Cart.create(user,product,order,1)
        cart.save()
        product.storeNums = str(int(product.storeNums) - num)
        product.save()

    return  JsonResponse({'error':0,'num':cart.num})
Exemplo n.º 4
0
def carts(request):
    num = int(request.GET.get("num"))

    #已知用户
    # request.user和request.token 已在中间件check..定义其为当前用户和用户的tokenValue值
    user = request.user

    #获取要添加到购物车的商品
    gid = request.GET.get('gid')
    pid = request.GET.get('pid')
    product = Product.objects.filter(category_id=gid).get(product_id=pid)
    if product.store_nums == 0 and num == 1:
        return JsonResponse({'error': 2})

    #当前用户的所有没进入订单的购物车信息
    usercarts = Cart.objects.filter(user__tokenValue=request.token)
    try:
        productCart = usercarts.get(product__product_id=pid)
        #添加过
        productCart.num += num
        #库存
        product.store_nums -= num
        product.save()
        if productCart.num == 0:
            productCart.delete()
        else:
            productCart.isChose = not productCart.isChose
            productCart.save()
    except Cart.DoesNotExist as e:
        #没添加过
        if num == 1:
            productCart = Cart.create(user, None, product, 1)
            productCart.save()
            # 库存
            product.store_nums -= num
            product.save()
        elif num == -1:
            return JsonResponse({'error': 0, "num": 0})

    return JsonResponse({
        'error': 0,
        "num": productCart.num,
        "isChose": productCart.isChose
    })
Exemplo n.º 5
0
def changecart(request,flag):
    tokenValue = request.COOKIES.get('token')
    print('----------------购物车页面用户token',tokenValue)

    try:
        user_motto,phone = find_user_data(request.session.get(str(tokenValue)))
    except:
        print('------------通过token获取用户信息出错')
        return JsonResponse({'login':True})
    if user_motto and phone:
        print('--------------第一阶段认证通过')
        try:
            user = User.objects.get(token=tokenValue)
            print('--------------第二阶段认证通过')
            print('获取当前请浏览器的token---------------',tokenValue)
            print('获取当前请求的用户phone------------------', phone)
        except:
            print('--------------------用户Token异常',tokenValue)
            return JsonResponse({'Error': True})

        pid = request.POST.get("pid")
        gid = request.POST.get("gid")
        ppid = request.POST.get("ppid")
        product = Product.objects.filter(categoryId=gid).get(productId=pid)
        try:
            # 根据用户手机号码,试图找到收货地址
            user_motto, phone = find_user_data(request.session.get(str(tokenValue)))
            Address.objects.get(phoneNum=phone)
            print('用户有收货地址')
        except:
            # 如果没有找到用户地址,提醒用户去添加地址
            print('-------------用户没有收货地址')
            return JsonResponse({"None": 3})
        try:
            # 如果没报错,证明用户购买过商品   定位用户
            user_motto, phone = find_user_data(request.session.get(str(request.COOKIES.get('token'))))
            cart = Cart.objects.filter(user__phoneNum=phone).get(product__id=ppid)
            # yibu(flag, cart, product, ppid)
            if flag == '1':
                print('--------------用户增加商品')
                if int(product.storeNums) == 0:
                    print('------------商品库存为空')
                    return JsonResponse({'Null': True})
                else:
                    cart.num = str(int(cart.num) + 1)
                    pdt = Product.objects.get(id=ppid)
                    pdt.storeNums = str(int(pdt.storeNums) - 1)
                    pdt.save()
                    cart.save()
                    print('------------增加购物车的数量')
                    return JsonResponse({'Succeed': cart.num})
            else:
                if cart.num == 0:
                    # cart.delete()

                    return JsonResponse({'reload': True})
                cart.num = str(int(cart.num) - 1)
                pdt = Product.objects.get(pk=ppid)
                pdt.storeNums = str(int(pdt.storeNums) + 1)
                pdt.save()
                cart.save()
                print('------------去掉购物车的数量')
                return JsonResponse({'Succeed': cart.num})
        except:
            print('--------------没有找到用户的购物车订单')
            orderId = str(uuid.uuid4())
            # 获取用户的           收货地址
            address = Address.objects.get(user__phoneNum=phone)
            # 创建                订单数据
            order = Order.create(orderId, user, address, product.price)
            # 保存数据
            order.save()
            # 再次找到            订单对象
            oder = Order.orders2.get(pk=orderId)
            # 创建                 购物车数据
            cart = Cart.create(user=user, product=product, order=oder, num = 1)
            cart.save()
            product.storeNums = str(int(product.storeNums) - 1)
            product.save()
            # 告诉客户端添加成功
            print('----------------新用户/新商品数据增加完毕')
            return JsonResponse({'Succeed': cart.num})
    else:
        print('用户身份认证失败')
        return render(request, 'mine/login.html')