add user login/password

This commit is contained in:
baoliang 2020-08-28 00:26:34 +08:00
parent df4aaf1c17
commit a9d3f3e6d4
1 changed files with 28 additions and 9 deletions

View File

@ -8,6 +8,7 @@ import datetime
from .models import VerifyCode from .models import VerifyCode
from .utils import generate_code, send_sms_code from .utils import generate_code, send_sms_code
def index(request): def index(request):
username = request.GET.get('username') username = request.GET.get('username')
password = request.GET.get('password') password = request.GET.get('password')
@ -29,11 +30,11 @@ def polls_login(request):
phone = request.POST.get('phone') phone = request.POST.get('phone')
password = request.POST.get('password') password = request.POST.get('password')
UserModel = get_user_model() UserModel = get_user_model()
user = UserModel.objects.filter(first_name=phone).first() user = UserModel.objects.filter(username=phone).first()
if not user: if not user:
return JsonResponse({'status': 'error', 'message': '用户名或密码错误'}) return JsonResponse({'status': 'error', 'message': '用户名或密码错误'})
u = authenticate(request, username=user.username, password=password) u = authenticate(request, username=phone, password=password)
if u is not None: if u is not None:
login(request, u) login(request, u)
token = Token.objects.get_or_create(user=u) token = Token.objects.get_or_create(user=u)
@ -62,7 +63,8 @@ def send_code(request):
if not phone: if not phone:
return JsonResponse({'status': 'error', 'message': '手机号不正确'}) return JsonResponse({'status': 'error', 'message': '手机号不正确'})
exist_code = VerifyCode.objects.filter(phone=phone, category=category).first() exist_code = VerifyCode.objects.filter(
phone=phone, category=category).first()
if exist_code and exist_code.in_progress(): if exist_code and exist_code.in_progress():
return JsonResponse({'status': 'error', 'message': '验证码使用中'}) return JsonResponse({'status': 'error', 'message': '验证码使用中'})
@ -77,15 +79,21 @@ def send_code(request):
return JsonResponse({'status': 'error', 'message': '验证码发送失败'}) return JsonResponse({'status': 'error', 'message': '验证码发送失败'})
@csrf_exempt @csrf_exempt
def register_step_one(request): def register_step_one(request):
if request.method == 'GET': if request.method == 'GET':
return HttpResponse(status=405) return HttpResponse(status=405)
phone = request.POST.get('phone') phone = request.POST.get('phone')
code = request.POST.get('code') code = request.POST.get('code')
exist_code = VerifyCode.objects.filter(phone=phone, code=code, category=0).exist()
if exist_code.in_progress(): UserModel = get_user_model()
is_exists = UserModel.objects.filter(username=phone).exists()
if is_exists:
return JsonResponse({'status': 'error', 'message': '该手机号已经使用'})
verify_code = VerifyCode.objects.filter(
phone=phone, code=code, category=0).first()
if verify_code and verify_code.in_progress():
return JsonResponse({'status': 'success', 'message': {phone: phone}}) return JsonResponse({'status': 'success', 'message': {phone: phone}})
else: else:
return JsonResponse({'status': 'error', 'message': '验证码超时,请重发'}) return JsonResponse({'status': 'error', 'message': '验证码超时,请重发'})
@ -98,8 +106,10 @@ def register_step_two(request):
phone = request.POST.get('phone') phone = request.POST.get('phone')
name = request.POST.get('name') name = request.POST.get('name')
gender = request.POST.get('gender') gender = request.POST.get('gender')
password = request.POST.get('password')
organization = request.POST.get('organization') organization = request.POST.get('organization')
# create user UserModel = get_user_model()
UserModel.objects.create_user(username=phone, password=password)
return JsonResponse({'status': 'success', 'message': '注册成功'}) return JsonResponse({'status': 'success', 'message': '注册成功'})
@ -109,8 +119,9 @@ def password_recover_step_one(request):
return HttpResponse(status=405) return HttpResponse(status=405)
phone = request.POST.get('phone') phone = request.POST.get('phone')
code = request.POST.get('code') code = request.POST.get('code')
exist_code = VerifyCode.objects.filter(phone=phone, code=code, category=1).exist() verify_code = VerifyCode.objects.filter(
if exist_code.in_progress(): phone=phone, code=code, category=1).first()
if verify_code and verify_code.in_progress():
return JsonResponse({'status': 'success', 'message': {phone: phone}}) return JsonResponse({'status': 'success', 'message': {phone: phone}})
else: else:
return JsonResponse({'status': 'error', 'message': '验证码超时,请重发'}) return JsonResponse({'status': 'error', 'message': '验证码超时,请重发'})
@ -123,3 +134,11 @@ def password_recover_step_two(request):
phone = request.POST.get('phone') phone = request.POST.get('phone')
password = request.POST.get('password') password = request.POST.get('password')
password_confirm = request.POST.get('password_confirm') password_confirm = request.POST.get('password_confirm')
if password != password_confirm:
return JsonResponse({'status': 'error', 'message': '两次密码输入不一致'})
UserModel = get_user_model()
user = UserModel.objects.filter(username=phone).first()
if not user:
return JsonResponse({'status': 'error', 'message': '用户名不存在'})
user.set_password(password)
return JsonResponse({'status': 'success', 'message': {phone: phone}})