用户认证
JWT_token 中的载体payload:
payload = {
'id': user.id,
'username': user.username,
'mobile': user.mobile
}
身份认证,有一些功能(加入购物车)必须是用户登录成功以后才能操作。
自定义中间件 在根目录创建middleware 创建middleware.py文件
from django.utils.deprecation import MiddlewareMixin
import jwt
from day2 import settings
class CheckUserMiddleware(MiddlewareMixin):
def process_request(self,request):
token=request.headers.get('token')
if not token:
request.user_info=None
else:
payload=jwt.decode(token,key=settings.SECRET_KEY,algorithms='HS256')
request.user_info=payload
API设计
功能 | 添加历史记录 |
---|
请求方式 | POST | 请求地址 | /goods/oneGood/history/ | POST参数 | productID: 商品id | headers参数 | token:jwt信息 | 成功响应 | {‘msg’:‘添加历史记录成功!’,‘code’:200} | 失败响应 | {‘msg’:‘添加历史记录失败!’,‘code’:204} |
from user.models import User
import redis
class History(APIView):
def post(self,request):
try:
user=User.objects.get(id=request.user_info.get('id'))
except Exception as e:
print(e)
return Response({'msg':'用户不存在或未登录','code':'204'})
good_id = request.data.get('productID')
try:
good_data = Goods.objects.get(id=good_id)
except Exception as e:
print(e)
return Response({'msg': '此商品不存在', 'code': 204})
history_key = f'history_{user.username}'
r = redis.Redis(host='localhost', port=6379, db=0)
r.lrem(history_key, 0, good_id)
r.lpush(history_key, good_id)
r.ltrim(history_key, 0, 4)
r.close()
return Response({'msg': '历史记录添加成功', 'code': 200})
收藏
模型类
收藏表字段:id 商品(外键) 用户(外键)
from user.models import User
class GoodsCollect(models.Model):
user=models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='用户')
goods=models.ForeignKey(Goods,on_delete=models.CASCADE,verbose_name='商品')
class Meta:
verbose_name_plural='收藏表'
db_table='good_collect'
def __str__(self):
return f'{self.user.username}:{self.goods.sku_name}'
API设计
功能 | 添加收藏 |
---|
请求方式 | POST | 请求地址 | /goods/user/collection/ | POST参数 | productID: 商品id | headers参数 | token:jwt信息 | 成功响应 | {‘msg’:‘收藏成功!’,‘code’:200} | 失败响应 | {‘msg’:‘添加收藏失败!’,‘code’:204} |
类试图
class Collection(APIView):
def post(self,request):
try:
user=User.objects.get(id=request.user_info.get('id'))
except Exception as e:
print(e)
return Response({'msg':'用户不存在或未登录','code':204})
good_id = request.data.get('productID')
collect_data=GoodsCollect.objects.filter(user=user,goods_id=good_id)
if collect_data.count()>0:
return Response({'msg':'收藏已存在','code':204})
try:
GoodsCollect.objects.create(user=user,goods_id=good_id)
except Exception as e:
print(e)
return Response({'msg': '添加收藏失败', 'code': 204})
return Response({'msg': '添加收藏成功', 'code': 200})
API设计
功能 | 查看收藏 |
---|
请求方式 | POST | 请求地址 | /goods/user/collection/ | headers参数 | token:jwt信息 | 成功响应 | {‘code’:200, ‘collectList’:[
{ “id”:i.good.id, “sku_name”:i.good.sku_name, “title”:i.good.title, “price”:i.good.price, “selling_price”:i.good.selling_price, “img”:i.good.img, } ,… ]} | 失败响应 | {‘msg’:‘添加收藏失败!’,‘code’:204} |
def get(self,request):
try:
user = User.objects.get(id=request.user_info.get('id'))
except Exception as e:
print(e)
return Response({'msg': '用户不存在或未登录', 'code': 204})
collect_data=GoodsCollect.objects.filter(user=user)
if collect_data.count()==0:
return Response({'msg': '用户没有任何收藏', 'code': 204})
collect_list = []
for i in collect_data:
collect_list.append({
'id':i.goods.id,
'sku_name': i.goods.sku_name,
'title': i.goods.title,
'price': i.goods.price,
'selling_price': i.goods.selling_price,
'img': i.goods.img,
})
return Response({'collectList': collect_list, 'code': 200})
API设计
功能 | 删除收藏 |
---|
请求方式 | DELETE | 请求地址 | /goods/user/collection/ | headers参数 | token:jwt信息 | 成功响应 | {‘code’:200, ‘msg’:‘删除收藏成功’} | 失败响应 | {‘msg’:‘删除收藏失败!’,‘code’:204} |
def delete(self,request):
try:
user = User.objects.get(id=request.user_info.get('id'))
except Exception as e:
print(e)
return Response({'msg': '用户不存在或未登录', 'code': 204})
good_id = request.data.get('productID')
try:
GoodsCollect.objects.get(user=user,goods_id=good_id).delete()
except Exception as e:
print(e)
return Response({'msg': '删除收藏失败', 'code': 204})
return Response({'msg': '删除收藏成功', 'code': 200})
路由
from django.urls import path,include
from . import views
urlpatterns = [
path('oneGood/history/', views.History.as_view()),
path('user/collection/', views.Collection.as_view()),
]
|