user/views.py
from django.shortcuts import render
from rest_framework import viewsets, mixins
from django.core.cache import cache
# Create your views here.
from rest_framework.decorators import list_route
from rest_framework.response import Response
from user.models import AXFUser
from user.serializers import UserSerializer, UserRegisterSerializer, UserLoginSerializer
from utils.error import PramsException
class UserView(viewsets.GenericViewSet,
mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.ListModelMixin,
):
#用户相关数据
queryset = AXFUser.objects.all()
#序列化
serializer_class = UserSerializer
def list(self, request, *args, **kwargs):
token = request.query_params.get('token')
user_id = cache.get(token)
user = AXFUser.objects.filter(id=user_id).first()
serializer = self.get_serializer(user)
#TODO: 订单数量的筛选
res = {
'user_info':serializer.data
}
return Response(res)
@list_route(methods=['POST'], serializer_class=UserRegisterSerializer)
def register(self, request):
# /api/user/user/register/ POST
# 校验参数
serializers = self.get_serializer(data=request.data)
#如果校验失败, 直接往外抛出异常
serializers.is_valid(raise_exception=False)
if not request:
raise PramsException({'code':1004, 'msg': '参数有误'})
# 注册功能
data = serializers.register_data(serializers.data)
return data
@list_route(methods=['POST'], serializer_class=UserLoginSerializer)
def login(self, request):
serializers = self.get_serializer(data=request.data)
result = serializers.is_valid(raise_exception=False)
if not result:
raise PramsException({'code':1007, 'msg':'参数出错'})
#登录功能
res = serializers.login_date(serializers.data)
return Response(res)
user/serializers.py
import uuid
from django.contrib.auth.hashers import make_password, check_password
from rest_framework import serializers
from rest_framework.response import Response
from django.core.cache import cache
from user.models import AXFUser
from utils.error import PramsException
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = AXFUser
#序列化所有字段
fields = '__all__'
class UserRegisterSerializer(serializers.Serializer):
# 只是用于校验字段
# 和前段人员沟通好需要校验字段的名称
u_username = serializers.CharField(required=True,
max_length=32,
min_length=4,
error_messages={
'required':'注册账号必填',
'max_length':'注册账号不能超过32位',
'min_length': '注册账号不能短于4位',
})
u_password = serializers.CharField(required=True,
max_length=10,
min_length=3,
error_messages={
'required': '密码必填',
'max_length': '密码不能超过10位',
'min_length': '密码不能短于3位',
})
u_password2 = serializers.CharField(required=True,
max_length=10,
min_length=3,
error_messages={
'required': '确认密码必填',
'max_length': '确认密码不能超过10位',
'min_length': '确认密码不能短于3位',
})
u_email = serializers.CharField(required=True, )
def validate(self, attrs):
# 判断用户是否注册,
username = attrs.get('u_username')
if AXFUser.objects.filter(u_username=username).exists():
raise PramsException({'code': 1001, 'msg':'注册账号已经存在'})
# 判断密码是否一致,
if attrs.get('u_password')!= attrs.get('u_password2'):
raise PramsException({'code': 1002, 'msg':'密码不一致'})
# 判断邮箱是否存在
if AXFUser.objects.filter(u_email=attrs.get('u_email')).exists():
raise PramsException({'code': 1003, 'msg':'邮箱已经存在'})
return attrs
def register_data(self, validata_data):
#实现注册功能
u_password = make_password(validata_data['u_password'])
user = AXFUser.objects.create(u_username=validata_data['u_username'],
u_password=u_password,
u_email=validata_data['u_email'])
res = {
'code': 200,
'msg': '注册成功',
'user_id': user.id
}
return Response(res)
class UserLoginSerializer(serializers.Serializer):
u_username = serializers.CharField(required=True,
max_length=32,
min_length=4,
error_messages={
'required': '注册账号必填',
'max_length': '注册账号不能超过32位',
'min_length': '注册账号不能短于4位',
})
u_password = serializers.CharField(required=True,
max_length=10,
min_length=3,
error_messages={
'required': '密码必填',
'max_length': '密码不能超过10位',
'min_length': '密码不能短于3位',
})
def validate(self, attrs):
# 判断账号是否存在
if not AXFUser.objects.filter(u_username=attrs.get('u_username')).exists():
raise PramsException({'code':1005, 'msg':'账号不存在登录失败'})
#判断密码是否正确
user = AXFUser.objects.filter(u_username=attrs.get('u_username')).first()
if not check_password(attrs.get('u_password'), user.u_password): #判断传过来的密码attrs.get('u_password'和数据库的密码user.u_password是否一致
raise PramsException({'code':1006, 'msg':'密码错误,登录失败'})
return attrs
#是否登录
def login_date(self, validate_date):
#登录
token = uuid.uuid4().hex
user = AXFUser.objects.filter(u_username = validate_date['u_username']).first()
#cache使用redis进行存储
#set存储的是字符串类型的值
cache.set(token, user.id, timeout=60*60*7)
res = {
'token':token
}
return res