217 lines
8.5 KiB
Python
217 lines
8.5 KiB
Python
from django.shortcuts import render
|
|
from django.http import HttpResponse, JsonResponse
|
|
from django.contrib.auth import get_user_model, authenticate, login
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
from django_token.models import Token
|
|
import datetime
|
|
|
|
from polls.models import Notice, VerifyCode
|
|
from polls.utils import generate_code, sent_sms_code
|
|
from dashboard.models import Organization, Userprofile
|
|
|
|
|
|
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(username=phone).first()
|
|
if not user:
|
|
return JsonResponse({'status': 'error', 'message': '账号不存在,请联系管理员'})
|
|
profile = user.userprofile_set.first()
|
|
if not profile or profile.status == 0:
|
|
return JsonResponse({'status': 'error', 'message': '用户尚未激话'})
|
|
if not profile or profile.admin_status == 0:
|
|
return JsonResponse({'status': 'error', 'message': '用户已删除,请联系管理员'})
|
|
|
|
u = authenticate(request, username=phone, password=password)
|
|
if u is not None:
|
|
login(request, u)
|
|
token, created = Token.objects.get_or_create(user=u)
|
|
result = dict()
|
|
profile = u.userprofile_set.first()
|
|
result['phone'] = u.username
|
|
result['token'] = token.key
|
|
if profile:
|
|
result['name'] = profile.name
|
|
result['department'] = profile.department
|
|
result['post'] = profile.post
|
|
result['thumbnail'] = request.build_absolute_uri(
|
|
profile.image.url) if profile.image else None
|
|
result['organization'] = profile.organization.name
|
|
return JsonResponse({'status': 'success', 'message': result})
|
|
else:
|
|
return JsonResponse({'status': 'error', 'message': '用户名或密码错误'})
|
|
else:
|
|
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': '手机号不正确'})
|
|
UserModel = get_user_model()
|
|
is_exists = UserModel.objects.filter(username=phone).exists()
|
|
if not is_exists:
|
|
return JsonResponse({'status': 'error', 'message': '账号不存在,请联系管理员'})
|
|
|
|
exist_code = VerifyCode.objects.filter(
|
|
phone=phone, category=category).first()
|
|
if exist_code and exist_code.is_in_progress():
|
|
return JsonResponse({'status': 'error', 'message': '验证码使用中'})
|
|
|
|
code = generate_code()
|
|
now = datetime.datetime.now()
|
|
after_1min = now + datetime.timedelta(minutes=1)
|
|
response = sent_sms_code(phone, code)
|
|
result = response.decode('utf8')
|
|
print(phone, result)
|
|
if "OK" in result:
|
|
VerifyCode.objects.create(
|
|
code=code, phone=phone, category=category, timeouted=after_1min)
|
|
return JsonResponse({'status': 'success'})
|
|
return JsonResponse({'status': 'error', 'message': '验证码发送失败'})
|
|
|
|
|
|
@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')
|
|
category = request.POST.get('category', 0)
|
|
if not phone:
|
|
return JsonResponse({'status': 'error', 'message': '手机号不正确'})
|
|
|
|
UserModel = get_user_model()
|
|
is_exists = UserModel.objects.filter(username=phone).exists()
|
|
if not is_exists:
|
|
return JsonResponse({'status': 'error', 'message': '账号不存在,请联系管理员'})
|
|
|
|
verify_code = VerifyCode.objects.filter(
|
|
phone=phone, code=code, category=category).first()
|
|
if verify_code and verify_code.is_in_progress():
|
|
profile = Userprofile.objects.filter(user__username=phone).first()
|
|
return JsonResponse({'status': 'success', 'message': {'id': profile.user_id, 'phone': phone, 'name': profile.name, 'organization': profile.organization.name}})
|
|
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')
|
|
if not phone:
|
|
return JsonResponse({'status': 'error', 'message': '账号不正确'})
|
|
department = request.POST.get('department')
|
|
if not department:
|
|
return JsonResponse({'status': 'error', 'message': '处(科)室不能为空'})
|
|
password = request.POST.get('password')
|
|
if not password:
|
|
return JsonResponse({'status': 'error', 'message': '密码不能为空'})
|
|
if len(password) < 6:
|
|
return JsonResponse({'status': 'error', 'message': '密码长度至少6位'})
|
|
post = request.POST.get('post')
|
|
if not post:
|
|
return JsonResponse({'status': 'error', 'message': '职务不能为空'})
|
|
|
|
profile = Userprofile.objects.filter(user__username=phone).first()
|
|
if not profile:
|
|
return JsonResponse({'status': 'error', 'message': '账号不存在'})
|
|
profile.department = department
|
|
profile.post = post
|
|
profile.status = 1
|
|
profile.save()
|
|
UserModel = get_user_model()
|
|
user = UserModel.objects.get(username=phone)
|
|
user.set_password(password)
|
|
user.save()
|
|
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')
|
|
if not phone:
|
|
return JsonResponse({'status': 'error', 'message': '手机号不正确'})
|
|
code = request.POST.get('code')
|
|
print(code)
|
|
if not code:
|
|
return JsonResponse({'status': 'error', 'message': '验证码不正确'})
|
|
verify_code = VerifyCode.objects.filter(
|
|
phone=phone, code=code, category=1).first()
|
|
if verify_code and verify_code.is_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')
|
|
if not phone:
|
|
return JsonResponse({'status': 'error', 'message': '手机号不正确'})
|
|
password = request.POST.get('password')
|
|
if not password:
|
|
return JsonResponse({'status': 'error', 'message': '密码不能为空'})
|
|
password_confirm = request.POST.get('password_confirm')
|
|
if not password_confirm:
|
|
return JsonResponse({'status': 'error', 'message': '密码确认不正确'})
|
|
if password != password_confirm:
|
|
return JsonResponse({'status': 'error', 'message': '两次密码输入不一致'})
|
|
print(phone, password, password_confirm)
|
|
UserModel = get_user_model()
|
|
user = UserModel.objects.filter(username=phone).first()
|
|
if not user:
|
|
return JsonResponse({'status': 'error', 'message': '用户名不存在'})
|
|
user.set_password(password)
|
|
user.save()
|
|
return JsonResponse({'status': 'success', 'message': '密码修改成功'})
|
|
|
|
|
|
@csrf_exempt
|
|
def reset_password(request):
|
|
if request.method == 'GET':
|
|
return HttpResponse(status=405)
|
|
password = request.POST.get('password')
|
|
if not password:
|
|
return JsonResponse({'status': 'error', 'message': '原密码不能为空'})
|
|
new_password = request.POST.get('newPassword')
|
|
if not new_password:
|
|
return JsonResponse({'status': 'error', 'message': '密码不能为空'})
|
|
if len(new_password) < 6:
|
|
return JsonResponse({'status': 'error', 'message': '密码长度不能少于6位'})
|
|
user_id = request.user.id
|
|
UserModel = get_user_model()
|
|
user = UserModel.objects.get(id=user_id)
|
|
if not user.check_password(password):
|
|
return JsonResponse({'status': 'error', 'message': '原密码不正确'})
|
|
user.set_password(new_password)
|
|
user.save()
|
|
return JsonResponse({'status': 'success', 'message': '密码修改成功'})
|