def get(self, request, classid=0, sortgoods=0): # 接收当前分类 classid = int(classid) # 接收排序索引 sortgoods = int(sortgoods) # 自定义排序列表 sortlist = ['id', '-sales', 'price', '-price', 'add_time'] # 查询分类表 goodsclass = GoodsClassModel.objects.filter(is_delete=False) total = 0 # 查询对应的商品 if classid == 0: goods = GoodsSKUModel.objects.filter(goodsclass_id=1).order_by( sortlist[sortgoods]) # 获取购物车中的总的商品的数量 user_id = request.session.get("id") cnn = get_redis_connection("default") car_key = get_car_key(user_id) car_values = cnn.hvals(car_key) # 保存到redis中的数据是 二进制编码了, 需要解码才能使用 for v in car_values: total += int(v) else: goods = GoodsSKUModel.objects.filter( goodsclass_id=classid).order_by(sortlist[sortgoods]) context = { "goods": goods, # 对应的商品 "goodsclass": goodsclass, # 分类表 "classid": classid, # 当前分类 "sortgoods": sortgoods, # 当前排序索引 "total": total, # 购物车总数量 } return render(request, 'goods/category.html', context=context)
def post(self, request): # 1. 判断用户是否登录 user_id = request.session.get("id") if user_id is None: return JsonResponse({"error": 1, "msg": "没有登录,请登录!"}) # 2. 接收请求参数 sku_id = request.POST.get("sku_id") count = request.POST.get("count") # 判断参数合法性 # 1.都要是整数 try: sku_id = int(sku_id) count = int(count) except: return JsonResponse({"error": 2, "msg": "参数错误!"}) # 2.商品必须要存在 try: goods_sku = GoodsSKUModel.objects.get(pk=sku_id) except GoodsSKUModel.DoesNotExist: return JsonResponse({"error": 3, "msg": "商品不存在!"}) # 3.库存判断 if goods_sku.stock < count: return JsonResponse({"error": 4, "msg": "库存不足!"}) # 将购物车数据添加到redis中 # 链接redis cnn = get_redis_connection("default") # 操作redis # 准备key car_key = get_car_key(user_id) # 添加 # 获取购物车中已经存在的数量 加 上 需要添加 与 库存进行比较 old_count = cnn.hget(car_key, sku_id) # 二进制 if old_count is None: old_count = 0 else: old_count = int(old_count) if goods_sku.stock < old_count + count: return JsonResponse(json_msg(5, "库存不足!")) rs_count = cnn.hincrby(car_key, sku_id, count) if rs_count <= 0: # 删除field cnn.hdel(car_key, sku_id) # 获取购物车中的总的商品的数量 car_values = cnn.hvals(car_key) # 保存到redis中的数据是 二进制编码了, 需要解码才能使用 total = 0 for v in car_values: total += int(v) return JsonResponse({"error": 0, "msg": "添加成功", "total": total})
def get(self, request): # 在redis中查找 user_id = request.session.get("id") cnn = get_redis_connection("default") car_key = get_car_key(user_id) allcar = cnn.hgetall(car_key) # 把查找到的数据放到列表中 car = [] for goodsid, count in allcar.items(): goodcars = GoodsSKUModel.objects.get(pk=goodsid) goodcars.count = count car.append(goodcars) context = {"car": car} return render(request, 'shoppingcarts/shopcart.html', context=context)
def post(self, request): """ 保存订单数据 1. 订单基本信息表 和 订单商品表 """ # 1. 接收参数 transport_id = request.POST.get('transport') sku_ids = request.POST.getlist('sku_ids') address_id = request.POST.get('address') # 接收用户的id user_id = request.session.get("id") user = Register.objects.get(pk=user_id) # 验证数据的合法性 try: transport_id = int(transport_id) address_id = int(address_id) sku_ids = [int(i) for i in sku_ids] except: return JsonResponse(json_msg(2, "参数错误")) # 验证收货地址和运输方式存在 try: address = UserAddress.objects.get(pk=address_id) except UserAddress.DoesNotExist: return JsonResponse(json_msg(3, "收货地址不存在!")) try: transport = Transport.objects.get(pk=transport_id) except Transport.DoesNotExist: return JsonResponse(json_msg(4, "运输方式不存在!")) # 2. 操作数据 # >>>1 . 操作订单基本信息表 order_sn = "{}{}{}".format(datetime.now().strftime("%Y%m%d%H%M%S"), user_id, random.randrange(10000, 99999)) address_info = "{}{}{}-{}".format(address.hcity, address.hproper, address.harea, address.brief) order = Order.objects.create(user=user, order_sn=order_sn, transport_price=transport.price, transport=transport.name, username=address.username, phone=address.phone, address=address_info) # 操作订单商品表 # 操作redis r = get_redis_connection() cart_key = get_car_key(user_id) # 准备个变量保存商品总金额 goods_total_price = 0 for sku_id in sku_ids: # 获取商品对象 try: goods_sku = ShopSKU.objects.get(pk=sku_id, is_delete=False) except ShopSKU.DoesNotExist: return JsonResponse(json_msg(5, "商品不存在")) # 获取购物车中商品的数量 # redis 基于内存的存储,有可能数据会丢失 try: count = r.hget(cart_key, sku_id) count = int(count) except: return JsonResponse(json_msg(6, "购物车中数量不存在!")) # 判断库存是否足够 if goods_sku.stock < count: return JsonResponse(json_msg(7, "库存不足!")) # 保存订单商品表 order_goods = OrderGoods.objects.create(order=order, goods_sku=goods_sku, price=goods_sku.price, count=count) # 添加商品总金额 goods_total_price += goods_sku.price * count # 扣除库存, 销量增加 goods_sku.stock -= count goods_sku.sales += count goods_sku.save() # 3. 反过头来操作订单基本信息表 商品总金额 和 订单总金额 # 订单总金额 order_price = goods_total_price + transport.price order.goods_total_price = goods_total_price order.order_price = order_price order.save() # 4. 清空redis中的购物车数据(对应sku_id) r.hdel(cart_key, *sku_ids) # 3. 合成响应 return JsonResponse(json_msg(0, "创建订单成功!", data=order_sn))
def get(self, request): """ 1. 如果没有收货地址显示添加收货地址 如果有收货地址, 显示默认收货地址, 如果没有默认显示第一个 2. 展示商品信息 a. 获取商品sku_ids 如何获取多个参数的值 getlist() get()只能获取一个 b. 根据商品id获取商品信息 并且 获取购物车redis中的数量 """ user_id = request.session.get("id") # 收货地址处理 address = UserAddress.objects.filter( user_id=user_id).order_by("-isDefault").first() # 处理商品信息 sku_ids = request.GET.getlist("sku_ids") # 准备空列表 装商品 goods_skus = [] # 准备商品总计 goods_total_price = 0 # 准备redis r = get_redis_connection() # 准备键 cart_key = get_car_key(user_id) # 遍历 for sku_id in sku_ids: # 商品信息 try: goods_sku = ShopSKU.objects.get(pk=sku_id) except ShopSKU.DoesNotExist: # 商品不存在就回到购物车 return redirect("cart:展示购物车") # 获取对应商品的数量 try: count = r.hget(cart_key, sku_id) count = int(count) except: # 商品数量不存在就回到购物车 return redirect("cart:展示购物车") # 保存到商品对象上 goods_sku.count = count # 装商品 goods_skus.append(goods_sku) # 统计商品总计 goods_total_price += goods_sku.price * count # 获取运输方式 transports = Transport.objects.filter( is_delete=False).order_by('price') # 渲染数据 context = { 'address': address, 'goods_skus': goods_skus, 'goods_total_price': goods_total_price, 'transports': transports, } return render(request, 'order/tureorder.html', context=context)