def confirmOrder(request): tokenValue = request.COOKIES.get("token") #找到当前可用的订单 order = Order.objects2.filter(user__tokenValue=tokenValue).get(flag=0) order.flag = 1 order.save() #属于该订单的购物城选中数据的isOrder置为False carts = Cart.objects.filter(user__tokenValue=tokenValue).filter( order=order).filter(isCheck=True) for cart in carts: cart.isOrder = False cart.save() #将没有被选中的数据添加到新的订单中 newOrder = Order.create(str(uuid.uuid4()), User.objects.get(tokenValue=tokenValue), Address.objects.get(pk=1), 0) newOrder.save() oldCarts = Cart.objects.filter(user__tokenValue=tokenValue) for cart in oldCarts: cart.order = newOrder cart.save() return JsonResponse({"error": 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})
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})
def generateorder(request): token = request.session.get('token') user = User.objects.get(token=token) # 生成订单 order = Order() order.user = user order.identifier = str(int(time.time())) + str( random.randrange(10000, 100000)) order.save() # 订单商品 carts = Cart.objects.filter(user=user).filter(isselect=True) for cart in carts: orderGoods = OrderGoods() orderGoods.order = order orderGoods.goods = cart.goods orderGoods.number = cart.number orderGoods.save() # 从购物车移除 cart.delete() responseData = { 'msg': '订单生成成功', 'status': 1, 'identifier': order.identifier } return JsonResponse(responseData)
def generateorder(request): #确定用户身份 token = request.session.get('token') userid = cache.get(token) # print(userid) user = User.objects.get(pk=userid) # print(user) #生成订单 order = Order() order.user = user #表明订单用户 属于谁 order.identifier = generate_identifier() #生成订单号 order.save() #订单中的商品 从购物车中循环拿出 carts = user.cart_set.filter(isselect=True) for cart in carts: orderGoods = OrderGoods() orderGoods.order = order orderGoods.goods = cart.goods orderGoods.number = cart.goodsnumber orderGoods.save() #加入订单的商品从购物车中移除 cart.delete() return render(request, 'order/orderdetail.html', context={'order': order})
def saveorder(request): token = request.session.get('token') if token == None: return JsonResponse({'data': -2, 'status': 'error'}) user = User.objects.get(userToken=token) carts = Cart.obj1.filter(isChose=True) if carts.count == 0: return JsonResponse({'data': -2, 'status': 'error'}) oid = random.randrange(1, 10000) oid = "%d" % oid o = Order.createorder(oid, user.userAccount, 0) o.save() for item in carts: item.isDelete = True item.orderid = oid item.save() return JsonResponse({"status": "success"})
def qOrder(request): tokenValue = request.COOKIES.get('token') order = Order.orders2.filter(user__tokenValue=tokenValue).get(flag=False) order.flag = 1 order.save() carts = Cart.objects.filter(user__tokenValue=tokenValue).filter(order=order).filter(isCheck=True) for cart in carts: cart.isOrder = False cart.save() newOrder = Order.create(str(uuid.uuid4()),User.objects.get(tokenValue=tokenValue),Address.objects.get(pk=3),0) newOrder.save() oldCarts = Cart.objects.filter(user__tokenValue=tokenValue) for cart in oldCarts: cart.order = newOrder cart.save() return JsonResponse({'error':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})
def order(request): token = request.COOKIES.get("token") #用户 user = User.objects.get(tokenValue=token) #创建订单 order_id = str(uuid.uuid4()) userOrder = Order.create(order_id, user, 10) userOrder.save() # 带下订单的购物车数据 userCarts = Cart.objects.filter(user__tokenValue=token).filter( isChose=True) for userCart in userCarts: userCart.order = userOrder userCart.isOrder = True userCart.save() return JsonResponse({"error": 0})
def Orders(request): tokenValue = request.COOKIES.get("token") # 找到可用订单,状态给成不可用 order = Order.orders2.filter(user__tokenValue=tokenValue).get(flag=0) order.flag = 1 order.save() # 找到属于该订单,被选中的购物车数据,设置为False carts = Cart.objects.filter(user__tokenValue=tokenValue).filter(order=order).filter(isCheck=True) for cart in carts: cart.isOrder = False cart.save() # 没有被选中的商品生产一个新订单 neworder = Order.create(str(uuid.uuid4()), User.objects.get(tokenValue=tokenValue), Address.objects.get(pk=1), 0) neworder.save() # 找到该用户所有没被选中的购物车数据 oldcart = Cart.objects.filter(user__tokenValue=tokenValue) for cart in oldcart: cart.order = neworder cart.save() return JsonResponse({"error": 0})
def generateorder(request): token = request.session.get('token') user = User.objects.get(token=token) carts = user.cart_set.filter(isselect=True) if carts.first() == None: return redirect('axf:cart') # 订单 order = Order() order.user = user order.identifier = generate_identifier() order.save() # 订单商品(购物车中选中) for cart in carts: orderGoods = OrderGoods() orderGoods.order = order orderGoods.goods = cart.goods orderGoods.number = cart.number orderGoods.save() # 购物车中移除 cart.delete() # response_data = { # 'msg': '生成订单', # 'status': 1, # 'identifier': order.identifier # } # # return JsonResponse(response_data) sum = 0 for orderGoods in order.ordergoods_set.all(): sum += orderGoods.goods.price * orderGoods.number return render(request, 'order/orderdetail.html', context={ 'order': order, 'sum': sum })
def gen_order(request): cartids = request.GET.get('cartids') cartids = cartids.split('=') print(cartids) order = Order() username = request.session.get('username') user = User.objects.get(u_name=username) order.o_user = user order.o_status = 1 order.save() # 修改商品属于哪张表 for cartid in cartids: car = Cart.objects.get(pk=cartid) car.c_belong = True car.c_order = order car.save() return JsonResponse({'msg': 'ok', 'orderid': order.id})
def genorder(request): cartids = request.GET.get('cartids') # 列表拆分 cartids = cartids.split('#') # print(cartids) # 生成订单 将要购买的商品转换到订单表中,生成订单信息,将购买的商品的订单信息关联 order = Order() username = request.session.get('username') user = User.objects.get(u_name=username) # order.o_user = user # 定义状态 0,默认生成状态 , 1 ,已下单未付款 , 2 已付款 ,3 已付款并已发货.... order.o_status = 1 order.save() # order 在存储之后就有id了 for item in cartids: car = Cart.objects.get(pk=item) # 修改属于那张表 car.c_belong = True car.c_order = order car.save() return JsonResponse({'msg': 'ok', 'orderid': order.id})
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')