Exemplo n.º 1
0
def addOrder(request):
    orderids = request.session.get('orderids')
    order = OrderInfo()
    oOder = OrderInfo.objects.all().order_by('-oid')[0:1]
    if len(oOder) == 0:
        order.oid = 1
    else:
        order.oid = int(oOder[0].oid) + 1
    order.odate = datetime.now()
    order.oIsPay = 0
    order.ototal = request.POST.get('total')
    order.oaddress = request.POST.get('address')
    order.user_id = request.session.get('user_id')
    order.zhifu = request.POST.get('zhifu')
    order.save()
    for oid in orderids:
        cartInfo = CartInfo.objects.get(id=oid)
        good = GoodsInfo.objects.get(id=cartInfo.goods_id)
        if cartInfo.count > good.gkucun:
            return JsonResponse({'status': 2})

        detail = OrderDetailInfo()
        detail.price = good.gprice
        detail.count = cartInfo.count
        detail.goods_id = good.id
        detail.order_id = order.oid
        detail.save()
    return JsonResponse({'status': 1})
Exemplo n.º 2
0
def addOrder(request):#先获取下单的所有购物车的id
    orderids = request.session.get('orderids')
    #构建一个订单对象
    order = OrderInfo()
    #查询订单最大的id
    oOder = OrderInfo.objects.all().order_by('oid')[0:1]
    if len(oOder) == 0:
        order.oid = 1
    else:
        print(int(oOder[0].oid))
        order.oid = int(oOder[0].oid)+1
    #增加订单时间
    order.odate = datetime.now()
    #是否付款
    order.oIsPay =0
    order.ototal=request.POST.get('totle')
    order.oaddress= request.POST.get('address')
    order.user_id= request.session.get('user_id')
    order.zhifu= request.POST.get('zhifu')
    order.save()
    #增加订单明细的商品洗信息
    for oid in orderids:#获取购物车(会有多条数据)
        cartInfo = CartInfo.objects.get(id=oid)
        good = GoodInfo.objects.get(id=cartInfo.goods_id)
        #如果返回2表示库存不够
        if cartInfo.count>good.gkucun:
            return JsonResponse({'status':2})

        #定义一个明细订单
        detail = OrderDetailInfo()
        detail.price = good.gprice
        detail.count = cartInfo.count
        detail.goods_id = good.id
        detail.price = order.oid
        detail.save()
        #如果返回1表示增加成功
    return JsonResponse({'status':1})
Exemplo n.º 3
0
def do_order(request):
    # {'cid': cids,'pay_style': pay_style},
    pay_style = request.GET.get('pay_style')  # cash  zfb
    cids = request.GET.getlist('cid')  # 购物车的id
    uid = request.session.get('user_id')  # 用户id
    user = UserInfo.objects.get(id=uid)  # 用户

    # 开启事务
    sid = transaction.savepoint()  #
    # 创建订单
    order = OrderInfo()
    order.oid = str(int(time.time() * 1000)) + str(uid)  # 订单编号
    order.user_id = uid
    order.ototal = 0  # 订单金额

    order.oaddr = UserAddressInfo.objects.get(user=user).uaddress  # 订单地址
    #address = order.oaddr.objects.get("uaddress")
    order.save()
    cart_list = CartInfo.objects.filter(id__in=cids)  # 查找到所有购物车对象
    total_price = 0
    total_count = 0
    isOk = True
    for cart in cart_list:
        #
        if cart.count <= cart.goods.gkucun:
            # 库存充足
            detail = OrderDetailInfo()
            detail.order = order
            detail.goods = cart.goods
            detail.price = cart.goods.gprice
            detail.count = cart.count
            detail.save()

            # 更改库存
            cart.goods.gkucun -= cart.count
            cart.goods.save()

            # 计算总价
            total_price += detail.count * detail.price
            total_count += detail.count

            # 删除购物车对象
            cart.delete()
        else:
            # 库存不够
            isOk = False
            break

    if isOk:
        # 最终提交订单
        order.ototal = total_price
        # 订单成功
        if pay_style == 'cash':
            order.oIsPay = 1
            order.save()
            transaction.savepoint_commit(sid)  # 提交事务
            return JsonResponse({'res': 1})

        else:
            order_id = order.oid  # 订单编号
            # total_pay = order.ototal
            total_pay = order.ototal
            order_string = alipay.api_alipay_trade_page_pay(
                out_trade_no=order_id,
                total_amount=str(total_pay),
                subject=f'粤嵌科技{order_id}',
                return_url='http://127.0.0.1:8000/order/check_pay/?order_id=' +
                order_id,
                notify_url='http://127.0.0.1:8000/order/check_pay/?order_id=' +
                order_id,
            )
            alipay_url = settings.ALIPAY_URL + '?' + order_string
            return JsonResponse({'res': 3, 'pay_url': alipay_url})
    else:
        # 订单失败
        transaction.savepoint_rollback(sid)
        return JsonResponse({'res': 0})