目录
一、登陆认证
''' MyAuth.py ''' from rest_framework.authentication import BaseAuthentication from LearnOnline import models from rest_framework.exceptions import AuthenticationFailed class LoginAuth(BaseAuthentication): def authenticate(self, request): ''' authenticate方法重新写 -主要逻辑:获取GET内token,若UserToken内存在,则返回两个值证明通过 ''' token = request.GET.get('token') ret = models.UserToken.objects.filter(token=token).first() if ret: return ret.user, ret else: raise AuthenticationFailed('认证失败,没有登录')
二、 购物车添加
class ShoppingCart(APIView): ''' 购物车的增删改查 - 基于Redis数据库操作 ''' authentication_classes = [MyAuth.LoginAuth] conn = get_redis_connection() def post(self, request, *args, **kwargs): ''' 购物车添加 - 前端传输数据 - course_id:课程id - policy_id:价格策略id - 逻辑处理 - course:课程对象 - policy_price_all:所有价格策略 - shopping_byte:当前用户在redis中的购物车(bytes格式) = shopping_cart:当前用户在redis中的购物车(json格式) -{course_id - 课程id:{ 'title': course.name - 课程名字, 'img': course.course_img - 课程图片, 'default_policy': policy_id - 默认价格策略id, 'policy': policy - 价格策略字典},} - policy:所有价格策略(字典格式) -{'1':{ "period":1, "period_display":"3天", "price":200 },} -{'价格策略id':{ "period":价格策略表pk(id), "period_display":价格策略周期(天数),