def put(self, request, *args, **kwargs): response = MyResponse() # 0 取出课程id,价格策略id course_id = str(request.data.get('course_id')) policy_id = str(request.data.get('policy_id')) try: shopping_byte = self.conn.get('shoppingcart_%s' % request.user.pk) if shopping_byte: shopping_cart = json.loads(shopping_byte) else: shopping_cart = {} if course_id not in shopping_cart: raise CommonException(102, '要修改的课程不存在') course_detail = shopping_cart.get(course_id) if policy_id not in course_detail['policy']: raise CommonException(103, '价格策略不合法') course_detail['default_policy'] = policy_id response.msg = '修改成功' self.conn.set('shoppingcart_%s' % request.user.pk, json.dumps(shopping_cart)) except ObjectDoesNotExist as e: response.status = 101 response.msg = '该课程不存在,你可能是爬虫' except CommonException as e: response.status = e.status response.msg = e.msg except Exception as e: response.status = 400 response.msg = '未知错误' print(str(e)) return Response(response.get_dic)
def get_detail(self, request, pk, *args, **kwargs): response = MyResponse() try: # 因为查的是课程详情表,传过来的pk 是课程表的主键 course_detail = models.CourseDetail.objects.get(course_id=pk) course_detail_ser = CourseDetailSerializer(instance=course_detail, many=False) response.data = course_detail_ser.data except ObjectDoesNotExist as e: response.status = 101 response.msg = '您要查询的课程不存在' except Exception as e: response.status = 105 if settings.DEBUG: response.msg = str(e) else: response.msg = '未知错误' return Response(response.get_dic)
def get(self, request, *args, **kwargs): response = MyResponse() try: shopping_byte = self.conn.get('shoppingcart_%s' % request.user.pk) if shopping_byte: shopping_cart = json.loads(shopping_byte) else: shopping_cart = {} response.data = shopping_cart except Exception as e: response.status = 400 response.msg = '未知错误' print(str(e)) return Response(response.get_dic)
def delete(self, request, *args, **kwargs): response = MyResponse() course_id = request.data.get('course_id') try: shopping_byte = self.conn.get('shoppingcart_%s' % request.user.pk) if shopping_byte: shopping_cart = json.loads(shopping_byte) else: shopping_cart = {} shopping_cart.pop(course_id, None) self.conn.set('shoppingcart_%s' % request.user.pk, json.dumps(shopping_cart)) except Exception as e: response.status = 400 response.msg = '未知错误' print(str(e)) return Response(response.get_dic)
def post(self, request, *args, **kwargs): response = MyResponse() name = request.data.get('name') pwd = request.data.get('pwd') user = models.UserInfo.objects.filter(name=name, pwd=pwd).first() if user: # 得去UserToken表中存数据 # 生成一个随机字符串,不会重复 token = uuid.uuid4() models.UserToken.objects.update_or_create( user=user, defaults={'token': token}) response.token = token response.name = name response.msg = '登录成功' else: response.msg = '用户名或密码错误' response.status = '101' return Response(response.get_dic)
def post(self, request, *args, **kwargs): response = MyResponse() # 课程id,价格策略id # {"course_id": "1", "policy_id": "1"} # 放到redis中key值 shoppingcart_userid_courseid # 0 取出课程id,价格策略id course_id = str(request.data.get('course_id')) policy_id = str(request.data.get('policy_id')) # 1 校验课程是否合法 try: course = models.Course.objects.get(pk=course_id) # 2 获取所有价格策略(通过课程拿出所有价格策略) policy_price_all = course.price_policy.all() # 3 从redis中取出当前登录用户的购物车 shopping_byte = self.conn.get('shoppingcart_%s' % request.user.pk) if shopping_byte: shopping_cart = json.loads(shopping_byte) else: shopping_cart = {} # 循环构造出价格策略大字典 policy = {} for policy_price in policy_price_all: ''' { "period":3, "period_display":"3天", "price":200 }, ''' policy_one = { 'period': policy_price.pk, 'period_display': policy_price.get_valid_period_display(), 'price': policy_price.price } policy[str(policy_price.pk)] = policy_one # 判断价格策略是否合法,不再字典中,就不合法 if policy_id not in policy: # 不合法 raise CommonException(102, '价格策略不合法,你不是人') # 判断传入的课程id是否在购物车中 if course_id in shopping_cart: # 更新一下默认价格策略 shopping_cart[course_id]['default_policy'] = policy_id response.msg = '更新成功' else: shopping_course = { 'title': course.name, 'img': course.course_img, 'default_policy': policy_id, 'policy': policy } # 添加到购物车 shopping_cart[course_id] = shopping_course response.msg = '添加成功' # 写入redis self.conn.set('shoppingcart_%s' % request.user.pk, json.dumps(shopping_cart)) except ObjectDoesNotExist as e: response.status = 101 response.msg = '该课程不存在,你可能是爬虫' except CommonException as e: response.status = e.status response.msg = e.msg except Exception as e: response.status = 400 response.msg = '未知错误' print(str(e)) return Response(response.get_dic)
def post(self, request, *args, **kwargs): response = MyResponse() # {"course_list": [{"course_id": "1", "policy_id": "1"}, {"course_id": "2", "policy_id": "2"}]} # 取出前端传过来的数据 course_list = request.data.get('course_list') try: # 1 构造出结算中心和全局优惠券 大字典 payment_dic = {} global_coupon = { 'coupon': {}, 'default_coupon': 0 } # 2 拿出购物车 shoppingcart = self.conn.get('shoppingcart_%s' % request.user.pk) # 三元表达式 shoppingcart_dic = json.loads(shoppingcart) if shoppingcart else {} for course_in in course_list: # 判断这个id是否在购物车中,如果不在,直接抛异常 course_in_id = course_in['course_id'] if course_in_id not in shoppingcart_dic: raise CommonException(101, '当前结算的课程不在购物车中') # 定义一个空的课程详情字典,内部放了个一个空的优惠券字典 course_detail = { 'coupon': {}, 'default_coupon': 0 } course_detail.update(shoppingcart_dic[course_in_id]) # 把刚刚构造的课程详情字典,放到结算中心大字典中 payment_dic[course_in_id] = course_detail # 一次性查出当前用户的所有优惠券信息 import datetime ctime = datetime.datetime.now() coupon_list = models.CouponRecord.objects.filter(user=request.user, status=0, coupon__valid_end_date__gte=ctime, coupon__valid_begin_date__lte=ctime ) for coupon in coupon_list: # 所有优惠券: # 全站3张 # django3张--obj_id=1 # python3张--obj_id=2 # 拿出优惠券类型数字 coupon_type = coupon.coupon.coupon_type # 拿出优惠券类型文字描述 coupon_type_display = coupon.coupon.get_coupon_type_display() object_id = coupon.coupon.object_id coupon_detail = { "coupon_type": coupon_type, "coupon_display": coupon_type_display, } # coupon_detail={ # "coupon_type":1, # "coupon_display":"满减券", # "money_equivalent_value":10, # "minimum_consume":100 # } if coupon_type == '0': # 立减 coupon_detail['money_equivalent_value'] = coupon.coupon.money_equivalent_value elif coupon_type == '1': # 满减 coupon_detail['money_equivalent_value'] = coupon.coupon.money_equivalent_value coupon_detail['minimum_consume'] = coupon.coupon.minimum_consume else: # 折扣 coupon_detail['off_percent'] = coupon.coupon.off_percent # 全站优惠券 if not object_id: global_coupon['coupon'][str(coupon.pk)] = coupon_detail else: # 课程优惠券 # if payment_dic.get(str(object_id), None): payment_dic[str(object_id)]['coupon'][str(coupon.pk)] = coupon_detail # 存到redis self.conn.set('payment_%s' % request.user.pk, json.dumps(payment_dic)) self.conn.set('globalcoupon_%s' % request.user.pk, json.dumps(global_coupon)) response.msg = '加入成功' except CommonException as e: response.status = e.status response.msg = e.msg # except Exception as e: # response.status = 400 # response.msg = '未知错误' # print(str(e)) return Response(response.get_dic)
def post(self, request, *args, **kwargs): response = MyResponse() # { # "price": 600 # "bely": 100 # } # 取出前端传过来的数据 price_in = request.data.get('price') bely = request.data.get('bely') try: # 取出结算中心的字典,全局优惠券大字典 payment_bytes = self.conn.get('payment_%s' % request.user.pk) payment_dic = json.loads(payment_bytes) if payment_bytes else {} global_coupon_bytes = self.conn.get('globalcoupon_%s' % request.user.pk) global_coupon = json.loads(global_coupon_bytes) if global_coupon_bytes else {} # 定义一个列表存储所有课程优惠完成之后的价格,以后计算总价格直接sum(列表) list_price=[] for course_id, course_detail in payment_dic.items(): # 取出默认价格策略,取出默认价格,取出默认优惠券id default_policy = course_detail['default_policy'] default_price = course_detail['policy'][str(default_policy)]['price'] default_coupon_id = course_detail['default_coupon'] # 如果是0 ,没有选择优惠券,if内的不需要走 if default_coupon_id != '0': default_coupon_detail = course_detail['coupon'][str(default_coupon_id)] default_price=self.account(default_price,default_coupon_detail) list_price.append(default_price) #计算总价格,也有优惠券 global_coupon_id=global_coupon['default_coupon'] global_coupon_detail=global_coupon['coupon'][str(global_coupon_id)] final_price=self.account(sum(list_price),global_coupon_detail) # 判断贝利是否合法 if int(bely) >request.user.bely: raise CommonException(103,'贝利数不合法') final_price=final_price-int(bely)/10 request.user.bely=request.user.bely-int(bely) request.user.save() if not final_price==float(price_in): raise CommonException(104,'传入的价格不合法') # 生成订单,往订单表插入一条数据 if final_price<=0: final_price=0 else: # 构造支付宝链接 # 拼凑支付宝url alipay = ali() import time # 生成支付的url query_params = alipay.direct_pay( subject="路飞学成课程", # 商品简单描述 out_trade_no="x2" + str(time.time()), # 商户订单号 total_amount=final_price, # 交易金额(单位: 元 保留俩位小数) ) pay_url = "https://openapi.alipaydev.com/gateway.do?{}".format(query_params) response.url = pay_url except CommonException as e: response.status = e.status response.msg = e.msg except Exception as e: response.status = 400 response.msg = '未知错误' print(str(e)) return Response(response.get_dic)