views.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # Create your views here.
  2. from datetime import datetime
  3. from .models import User, LoginToken
  4. from utils.crypto import secure_transport
  5. from utils.http import make_json_response
  6. from .models import get_user
  7. from django.views.decorators.http import require_POST
  8. from .decorators import login_required
  9. from .utils import auth_with_username_or_email, check_password
  10. @secure_transport
  11. @require_POST
  12. def register(request):
  13. username = request.POST.get('username', '')
  14. password = request.POST.get('password', '')
  15. email = request.POST.get('email', '')
  16. if not username or not password or not email:
  17. return make_json_response(code=303, error='用户名/密码/邮箱不能为空')
  18. if User.objects.filter(username=username):
  19. return make_json_response(code=301, error='用户名已存在')
  20. if User.objects.filter(email=email):
  21. return make_json_response(code=302, error='邮箱已存在')
  22. if not check_password(password):
  23. return make_json_response(code=304, error='密码长度不能小于8, 必须包含数字和英文字符')
  24. try:
  25. User.objects.create(username=username, password=password, email=email)
  26. print('注册成功')
  27. return make_json_response()
  28. except Exception as e:
  29. return make_json_response(code=500, error=str(e))
  30. @secure_transport
  31. @require_POST
  32. def login(request):
  33. username = request.POST.get('username', '')
  34. password = request.POST.get('password', '')
  35. old_token = request.POST.get('token', '')
  36. try:
  37. user = auth_with_username_or_email(username, password)
  38. print(user)
  39. except:
  40. return make_json_response(code=303, error='用户名或密码错误')
  41. print(f'token = {old_token}')
  42. if user.check_token(old_token):
  43. try:
  44. user_token = user.tokens.get(token=old_token)
  45. print('已登录')
  46. user_token.delete()
  47. # return JsonResponse({'code': 303, 'msg': '已登录'}, status=303)
  48. except Exception as e:
  49. print('token无效')
  50. else:
  51. print('token已过期')
  52. user.last_login = datetime.now()
  53. token = user.make_token()
  54. user_token = LoginToken()
  55. user_token.user = user
  56. user_token.token = token
  57. user_token.save()
  58. print('登录成功')
  59. print(f'token = {token}')
  60. return make_json_response(token=token)
  61. @secure_transport
  62. @login_required
  63. def logout(request):
  64. user = get_user(request)
  65. data = request.POST
  66. token = data.get('token')
  67. user_token = user.tokens.get(token=token)
  68. user_token.delete()
  69. return make_json_response()
  70. @secure_transport
  71. @require_POST
  72. def send_email_verification_code(request):
  73. data = request.POST
  74. username = data.get('username')
  75. email = data.get('email')
  76. try:
  77. user = User.objects.get(username=username)
  78. except:
  79. return make_json_response(code=302, error='用户不存在')
  80. if user.email != email:
  81. return make_json_response(code=301, error='邮箱错误')
  82. try:
  83. # 发送验证码
  84. token = user.make_token()
  85. print(f'发送验证码 email = {user.email} token = {token}')
  86. user.send_email('ST网盘重置密码验证码', token)
  87. return make_json_response()
  88. except Exception as e:
  89. print(e)
  90. return make_json_response(code=500, error='验证码发送失败')
  91. @secure_transport
  92. @require_POST
  93. def check_token(request):
  94. data = request.POST
  95. username = data.get('username')
  96. token = data.get('token')
  97. print(username)
  98. try:
  99. user = User.objects.get(username=username)
  100. except:
  101. return make_json_response(code=302, error='用户不存在')
  102. print(f'token={token}')
  103. if token and user.check_token(token):
  104. print('验证码有效')
  105. return make_json_response()
  106. else:
  107. return make_json_response(code=303, error='验证码无效')
  108. @secure_transport
  109. @require_POST
  110. def reset_password(request):
  111. data = request.POST
  112. username = data.get('username')
  113. password = data.get('password')
  114. token = data.get('token')
  115. if not check_password(password):
  116. return make_json_response(code=304, error='密码长度不能小于8, 必须包含数字和英文字符')
  117. try:
  118. user = User.objects.get(username=username)
  119. except:
  120. return make_json_response(code=302, error='用户不存在')
  121. print(f'token={token}')
  122. if token and user.check_token(token):
  123. # 重置密码
  124. print("验证码有效")
  125. user.password = password
  126. user.save()
  127. return make_json_response()
  128. else:
  129. return make_json_response(code=303, error='验证码无效')