def mine(request): print('-------------当前token',request.COOKIES.get('token')) try: user_motto,phone = find_user_data(request.session.get(request.COOKIES.get('token'))) return render(request, "mine/mine.html", {"phone": phone}) except: return render(request, "mine/mine.html")
def dizhi(request): IS_LOGIN = request.session.get("islogin", False) if IS_LOGIN: user_motto, phone = find_user_data(request.session.get(str(request.COOKIES.get('token')))) addresses = Address.objects.filter(user__phoneNum = phone) return render(request,"mine/dizhi.html",{"addresses":addresses}) else: return redirect('/login/')
def particulars(request): status = request.COOKIES.get('islogin') if status == 'True': print('--------------status?', status) user_motto,phone = find_user_data(request.session.get(request.COOKIES.get('token'))) users = User.objects.get(phoneNum=phone) return render(request,'mine/particulars.html',{'users':users}) else: return render(request,'mine/login.html')
def cart(request): sessionid = request.COOKIES.get('token') if sessionid and request.session.get('islogin'): print('------------------------------sessionid', sessionid) try: user_motto, phone = find_user_data(request.session.get(sessionid)) print('--------------------------------------2购物车手机号', phone) carts = Cart.objects.filter(user__phoneNum=phone) print('正常状态录状态进入') return render(request, "cart/cart.html", {"carts": carts}) except: print('------------------没有token值') return render(request,'cart/cart.html') print('未登录状态进入') return render(request,'cart/cart.html')
def changecart2(request,flag): if request.COOKIES.get('token'): pid = request.POST.get('pid') uesr_motto,phone = find_user_data(request.session.get(str(request.COOKIES.get('token')))) #首先找到商品对象 print('--------------------------商品id',pid) print('--------------------------用户手机号',phone) print('--------------------------购买状态',flag) product = Product.objects.get(id=pid) #在此找到购物车对象 cart = Cart.objects.filter(user__phoneNum=phone).get(product_id=pid) if flag == '1': print('---------------------------进入添加状态?') if int(product.storeNums) < int(cart.num): # print('---------------------------库存不足?',cart.num,product.storeNums) return JsonResponse({'Null': True}) else: # print('--------------------------进入数据更改分支?', cart.num, product.storeNums) cart.num = str(int(cart.num) + 1) pdt = Product.objects.get(id=pid) pdt.storeNums = str(int(pdt.storeNums) - 1) pdt.save() cart.save() print('--------------------------数据修改完毕!', cart.num, product.storeNums) print('_-'*50) return JsonResponse({'Num': cart.num}) else: if cart.num == 1: # print('---------------------------进入删除Cart状态?') cart.delete() return JsonResponse({"Del":True}) else: print('---------------------------进入删除状态?') # print('--------------------------数据修改前!', cart.num, product.storeNums) cart.num = str(int(cart.num) - 1) pdt = Product.objects.get(id=pid) pdt.storeNums = str(int(pdt.storeNums) + 1) pdt.save() cart.save() print('--------------------------数据修改完毕!', cart.num, product.storeNums) print('_-' * 50) return JsonResponse({'Num': cart.num}) else: return redirect('login')
def xiangqing(request,gid, cid, sid,pid): IS_LOGIN = request.session.get("islogin", False) if IS_LOGIN: try: print('-----------------',request.COOKIES.get('token')) user_motto, phone = find_user_data(request.session.get(str(request.COOKIES.get('token')))) carts = Cart.objects.filter(user__phoneNum=phone) market = request.get_host()+ ":8000/market/{}/{}/".format(gid,cid) url_pid = request.get_full_path().rsplit("/")[::-1][1] pid_obj = Product.objects.filter(productId=url_pid).first() print('---------------Islogin') return render(request, "market/xiangqing.html", {"pid_obj": pid_obj, "market": market,'carts':carts}) except: print('---------------Nologin') market = request.get_host() + ":8000/market/{}/{}/".format(gid, cid) url_pid = request.get_full_path().rsplit("/")[::-1][1] pid_obj = Product.objects.filter(productId=url_pid).first() return render(request, "market/xiangqing.html",{"pid_obj": pid_obj, "market": market}) else: return redirect('/login/')
def add(request): IS_LOGIN = request.session.get("islogin", False) user_motto, phone = find_user_data(request.session.get(str(request.COOKIES.get('token')))) if IS_LOGIN and phone: if request.method == "GET": return render(request,'mine/add.html') else: username = request.POST.get("name") phone = request.POST.get("phoneNum") sex = request.POST.get("sex") if sex == "男": sex = 0 sex = 1 postCode = request.POST.get("postCode") province = request.POST.get("province") city = request.POST.get("city") county = request.POST.get("county") street = request.POST.get("street") detailAddress = str(province)+str(city)+str(county)+str(street) user1 = User.objects.get(phoneNum=phone) saves = Address.create(name=username,sex=sex,phoneNum=phone,postCode=postCode,address=detailAddress,province=province,city=city,county=county,street=street,detailAddress=detailAddress,user=user1) saves.save() return redirect("/dizhi/") return render(request,"mine/add.html")
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')