2020-09-03 04:03:19 +00:00
|
|
|
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
|
|
|
|
|
2020-09-24 06:51:39 +00:00
|
|
|
from polls.models import Notice, VerifyCode
|
2020-09-03 04:03:19 +00:00
|
|
|
from polls.utils import generate_code, sent_sms_code
|
2020-09-03 09:46:04 +00:00
|
|
|
from dashboard.models import Organization, Userprofile
|
2020-09-03 04:03:19 +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(username=phone).first()
|
|
|
|
if not user:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '用户名或密码错误'})
|
2020-09-23 09:47:05 +00:00
|
|
|
profile = user.userprofile_set.first()
|
2020-09-22 07:39:33 +00:00
|
|
|
if not profile or profile.status == 0:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '用户在审核中'})
|
2020-09-03 04:03:19 +00:00
|
|
|
|
|
|
|
u = authenticate(request, username=phone, password=password)
|
|
|
|
if u is not None:
|
|
|
|
login(request, u)
|
2020-09-03 09:46:04 +00:00
|
|
|
token, created = Token.objects.get_or_create(user=u)
|
2020-09-03 04:03:19 +00:00
|
|
|
result = dict()
|
|
|
|
profile = u.userprofile_set.first()
|
2020-09-04 21:57:48 +00:00
|
|
|
result['phone'] = u.username
|
2020-09-03 09:46:04 +00:00
|
|
|
result['token'] = token.key
|
2020-09-03 04:03:19 +00:00
|
|
|
if profile:
|
|
|
|
result['name'] = profile.name
|
|
|
|
result['gender'] = profile.sex
|
2020-09-04 21:57:48 +00:00
|
|
|
result['thumbnail'] = request.build_absolute_uri(
|
|
|
|
profile.image.url) if profile.image else None
|
2020-09-03 04:03:19 +00:00
|
|
|
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': '手机号不正确'})
|
|
|
|
|
|
|
|
exist_code = VerifyCode.objects.filter(
|
|
|
|
phone=phone, category=category).first()
|
2020-09-03 09:46:04 +00:00
|
|
|
if exist_code and exist_code.is_in_progress():
|
2020-09-03 04:03:19 +00:00
|
|
|
return JsonResponse({'status': 'error', 'message': '验证码使用中'})
|
|
|
|
|
|
|
|
code = generate_code()
|
|
|
|
now = datetime.datetime.now()
|
2020-09-03 09:46:04 +00:00
|
|
|
after_1min = now + datetime.timedelta(minutes=1)
|
2020-09-03 04:03:19 +00:00
|
|
|
response = sent_sms_code(phone, code)
|
|
|
|
result = response.decode('utf8')
|
2020-09-17 08:23:27 +00:00
|
|
|
print(phone,result)
|
2020-09-03 04:03:19 +00:00
|
|
|
if "OK" in result:
|
2020-09-03 09:46:04 +00:00
|
|
|
VerifyCode.objects.create(
|
|
|
|
code=code, phone=phone, category=category, timeouted=after_1min)
|
2020-09-03 04:03:19 +00:00
|
|
|
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')
|
2020-09-03 09:46:04 +00:00
|
|
|
category = request.POST.get('category', 0)
|
|
|
|
if not phone:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '手机号不正确'})
|
2020-09-03 04:03:19 +00:00
|
|
|
|
|
|
|
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(
|
2020-09-03 09:46:04 +00:00
|
|
|
phone=phone, code=code, category=category).first()
|
|
|
|
if verify_code and verify_code.is_in_progress():
|
2020-09-03 04:03:19 +00:00
|
|
|
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')
|
2020-09-03 09:46:04 +00:00
|
|
|
if not phone:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '手机号不正确'})
|
2020-09-03 04:03:19 +00:00
|
|
|
name = request.POST.get('name')
|
2020-09-03 09:46:04 +00:00
|
|
|
if not name:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '姓名不能为空'})
|
2020-09-03 04:03:19 +00:00
|
|
|
password = request.POST.get('password')
|
2020-09-03 09:46:04 +00:00
|
|
|
if not password:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '密码不能为空'})
|
2020-09-22 07:39:33 +00:00
|
|
|
unit = request.POST.get('unit')
|
|
|
|
if not unit:
|
2020-09-03 09:46:04 +00:00
|
|
|
return JsonResponse({'status': 'error', 'message': '单位不能为空'})
|
|
|
|
|
2020-09-22 07:39:33 +00:00
|
|
|
department = request.POST.get('department')
|
|
|
|
if not department:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '处/科室不能为空'})
|
|
|
|
|
|
|
|
post = request.POST.get('post')
|
|
|
|
if not post:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '职务不能为空'})
|
|
|
|
|
2020-09-03 04:03:19 +00:00
|
|
|
UserModel = get_user_model()
|
2020-09-03 09:46:04 +00:00
|
|
|
is_exists = UserModel.objects.filter(username=phone).exists()
|
|
|
|
if is_exists:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '该手机号已经使用'})
|
|
|
|
|
|
|
|
u = UserModel.objects.create_user(username=phone, password=password)
|
2020-09-22 07:39:33 +00:00
|
|
|
profile = Userprofile.objects.create(name=name, user=u, status=0, unit=unit, department=department, post=post)
|
2020-09-24 06:51:39 +00:00
|
|
|
admins = Userprofile.objects.filter(organization__level__level=1)
|
|
|
|
for a in admins:
|
|
|
|
content = '用户%s注册了账号,请审核' % (name,)
|
|
|
|
Notice.create_reply_notice(a.user.id, content, 'dashboard', 'Userprofile', 'status', profile.id, '1')
|
2020-09-03 04:03:19 +00:00
|
|
|
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')
|
2020-09-03 09:46:04 +00:00
|
|
|
if not phone:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '手机号不正确'})
|
2020-09-03 04:03:19 +00:00
|
|
|
code = request.POST.get('code')
|
2020-09-03 09:46:04 +00:00
|
|
|
if not code:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '验证码不正确'})
|
2020-09-03 04:03:19 +00:00
|
|
|
verify_code = VerifyCode.objects.filter(
|
|
|
|
phone=phone, code=code, category=1).first()
|
2020-09-03 09:46:04 +00:00
|
|
|
if verify_code and verify_code.is_in_progress():
|
2020-09-03 04:03:19 +00:00
|
|
|
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')
|
2020-09-03 09:46:04 +00:00
|
|
|
if not phone:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '手机号不正确'})
|
2020-09-03 04:03:19 +00:00
|
|
|
password = request.POST.get('password')
|
2020-09-03 09:46:04 +00:00
|
|
|
if not password:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '密码不能为空'})
|
2020-09-03 04:03:19 +00:00
|
|
|
password_confirm = request.POST.get('password_confirm')
|
2020-09-03 09:46:04 +00:00
|
|
|
if not password_confirm:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '密码确认不正确'})
|
2020-09-03 04:03:19 +00:00
|
|
|
if password != password_confirm:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '两次密码输入不一致'})
|
2020-09-04 21:57:48 +00:00
|
|
|
print(phone, password, password_confirm)
|
2020-09-03 04:03:19 +00:00
|
|
|
UserModel = get_user_model()
|
|
|
|
user = UserModel.objects.filter(username=phone).first()
|
|
|
|
if not user:
|
|
|
|
return JsonResponse({'status': 'error', 'message': '用户名不存在'})
|
|
|
|
user.set_password(password)
|
2020-09-03 09:46:04 +00:00
|
|
|
user.save()
|
2020-09-04 21:57:48 +00:00
|
|
|
return JsonResponse({'status': 'success', 'message': '密码修改成功'})
|