2020-08-20 07:38:42 +00:00
|
|
|
from django.shortcuts import render
|
|
|
|
from django.http import HttpResponse, JsonResponse
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2020-08-20 10:34:14 +00:00
|
|
|
from django_token.models import Token
|
2020-08-20 07:38:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def index(request):
|
|
|
|
username = request.GET.get('username')
|
|
|
|
password = request.GET.get('password')
|
|
|
|
print(username, password)
|
|
|
|
return JsonResponse({'status': 'error', 'message': '用户名或密码错误'})
|
|
|
|
|
|
|
|
|
|
|
|
def status_500(request):
|
|
|
|
return HttpResponse(status=500)
|
|
|
|
|
|
|
|
|
|
|
|
def status_401(request):
|
|
|
|
return HttpResponse(status=401)
|
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def polls_login(request):
|
|
|
|
if request.method == 'POST':
|
|
|
|
phone = request.POST.get('phone')
|
|
|
|
password = request.POST.get('password')
|
|
|
|
UserModel = get_user_model()
|
|
|
|
user = UserModel.objects.filter(first_name=phone).first()
|
|
|
|
if not user:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '用户名或密码错误'})
|
|
|
|
|
|
|
|
u = authenticate(request, username=user.username, password=password)
|
|
|
|
if u is not None:
|
|
|
|
login(request, u)
|
2020-08-20 10:34:14 +00:00
|
|
|
token = Token.objects.get_or_create(user=u)
|
2020-08-20 07:38:42 +00:00
|
|
|
result = dict()
|
2020-08-20 10:34:14 +00:00
|
|
|
profile = u.userprofile_set.first()
|
|
|
|
result['phone'] = u.first_name
|
|
|
|
result['token'] = token
|
|
|
|
if profile:
|
|
|
|
result['name'] = profile.name
|
|
|
|
result['gender'] = profile.sex
|
|
|
|
result['thumbnail'] = profile.image.path
|
|
|
|
result['organization'] = profile.organization.name
|
|
|
|
return JsonResponse({'status': 'success', 'message': result})
|
2020-08-20 07:38:42 +00:00
|
|
|
else:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '用户名或密码错误'})
|
|
|
|
else:
|
2020-08-20 10:34:14 +00:00
|
|
|
return HttpResponse(status=405)
|
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def send_code(request):
|
|
|
|
if request.method == 'GET':
|
|
|
|
return HttpResponse(status=405)
|
|
|
|
phone = request.POST.get('phone')
|
|
|
|
category = request.POST.get('category', 0)
|
|
|
|
if not phone:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '手机号不正确'})
|
|
|
|
|
|
|
|
exist_code = VerifyCode.objects.filter(phone=phone, category=category).first()
|
|
|
|
if exist_code and exist_code.in_progress():
|
|
|
|
return JsonResponse({'status': 'error', 'message': '验证码使用中'})
|
|
|
|
|
|
|
|
code = generate_code()
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
after_10mins = now + datetime.timedelta(minutes=10)
|
|
|
|
VerifyCode.objects.create(code=code, phone=phone, category=category)
|
|
|
|
return JsonResponse({'status': 'success'})
|
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def register_step_one(request):
|
|
|
|
if request.method == 'GET':
|
|
|
|
return HttpResponse(status=405)
|
|
|
|
phone = request.POST.get('phone')
|
|
|
|
code = request.POST.get('code')
|
|
|
|
exist_code = VerifyCode.objects.filter(phone=phone, code=code, category=0).exist()
|
|
|
|
if exist_code.in_progress():
|
|
|
|
return JsonResponse({'status': 'success', 'message': {phone: phone}})
|
|
|
|
else:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '验证码超时,请重发'})
|
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def register_step_two(request):
|
|
|
|
if request.method == 'GET':
|
|
|
|
return HttpResponse(status=405)
|
|
|
|
phone = request.POST.get('phone')
|
|
|
|
name = request.POST.get('name')
|
|
|
|
gender = request.POST.get('gender')
|
|
|
|
organization = request.POST.get('organization')
|
|
|
|
# create user
|
|
|
|
return JsonResponse({'status': 'success', 'message': '注册成功'})
|
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def password_recover_step_one(request):
|
|
|
|
if request.method == 'GET':
|
|
|
|
return HttpResponse(status=405)
|
|
|
|
phone = request.POST.get('phone')
|
|
|
|
code = request.POST.get('code')
|
|
|
|
exist_code = VerifyCode.objects.filter(phone=phone, code=code, category=1).exist()
|
|
|
|
if exist_code.in_progress():
|
|
|
|
return JsonResponse({'status': 'success', 'message': {phone: phone}})
|
|
|
|
else:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '验证码超时,请重发'})
|
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def password_recover_step_two(request):
|
|
|
|
if request.method == 'GET':
|
|
|
|
return HttpResponse(status=405)
|
|
|
|
phone = request.POST.get('phone')
|
|
|
|
password = request.POST.get('password')
|
|
|
|
password_confirm = request.POST.get('password_confirm')
|