urls.py
router = SimpleRouter()
router.register('', views.LoginAPIView, 'login')
urlpatterns = [
path('', include(router.urls)),
]
views.py
class LoginAPIView(ViewSet):
@action(methods=('post',), detail=False)
def login(self, request, *args, **kwargs):
"""用户多方式登录"""
ser = serializer.UserModelSerilaizer(data=request.data)
ser.is_valid(raise_exception=True)
token = ser.context['token']
user = ser.context['user']
return utils.APIResponse(token=token, username=user.username)
@action(detail=False)
def verify_telephone(self, request, *args, **kwargs):
"""校验手机号码"""
telephone = request.query_params.get('telephone')
if re.search(r'^1[3-9][0-9]{9}$', telephone):
models.User.objects.get(telephone=telephone)
return utils.APIResponse()
return utils.APIResponse(code=0, msg='手机号不合法!')