views.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # Create your views here.
  2. import json
  3. import base64
  4. import re
  5. from django import http
  6. from django.shortcuts import render,redirect
  7. # Create your views here.
  8. from django.views import View
  9. from django.conf import settings
  10. from django.db import connection, transaction
  11. import datetime
  12. from django.contrib.auth import authenticate, login,logout
  13. from utils.sqlinjection import sqlinjections
  14. from utils.cust_data import customer_data
  15. from utils.et_CACC_NO import et_CACC_NO
  16. from utils.examine_ood import examine_ood
  17. from utils.if_account import if_account
  18. from utils.usr_data import usr_data
  19. from utils.monthly_odd import monthly_odd
  20. from utils.executeQuery import executeQuery
  21. # 用户注册
  22. from users.models import Customer
  23. # 用户注册
  24. class RegisterView(View):
  25. def get(self,request):
  26. return render(request,'register.html')
  27. def post(self,request):
  28. #1,获取参数
  29. user_name = request.POST.get("user_name")
  30. pwd = request.POST.get("pwd")
  31. #2,校验参数
  32. #2,1 为空校验
  33. if not all([user_name,pwd]):
  34. return http.HttpResponseForbidden("参数不全")
  35. #3,创建用户对象,保存到数据库中
  36. user = Customer.objects.create(username=user_name,password=pwd)
  37. #4,返回响应
  38. response = redirect("http://www.taobao.com")
  39. return response
  40. # 用户登录处理 初始化密码后台设置为:MTEx 表示密码是:111
  41. class Login(View):
  42. def get(self, request):
  43. return render(request, 'login.html')
  44. def post(self, request):
  45. sqlinjection = sqlinjections().ifsql(request.POST)
  46. if sqlinjection:
  47. return http.HttpResponseForbidden("参数非法")
  48. user_1 = request.POST.get("user_name").split('->')[0]
  49. # print(request.POST)
  50. pswd = request.POST.get("pwd").lower()
  51. # 校验参数
  52. if not all([user_1,pswd]):
  53. return http.HttpResponseForbidden("请输入账号密码")
  54. # 判断用户密码正确性
  55. try:
  56. user = Customer.objects.get(user_no=user_1)
  57. except Exception:
  58. return http.HttpResponseForbidden("存在重复的账号,请联系管理员")
  59. # 对数据库密码进行解码
  60. try:
  61. sql_password = bytes(base64.b64decode(user.password.encode(encoding='utf-8'))).decode()
  62. except Exception:
  63. return http.HttpResponseForbidden("密码被后台操作过请联系管理员")
  64. print(sql_password)
  65. # 校验密码是否正确
  66. if user.user_no !=user_1 or sql_password!=pswd:
  67. return http.HttpResponseForbidden("账号或者密码错误")
  68. # 3,状态保持
  69. # login(request, user)
  70. # 3,1设置状态保持的时间
  71. # if request.session.get('username'):
  72. request.session['username'] = user_1
  73. request.session.set_expiry(3600 * 24 * 2) # 两天有效
  74. response = redirect('/index')
  75. response.set_cookie("username", user.username, 3600 * 24 * 2)
  76. return response
  77. def put(self,request):
  78. req_data = json.loads(request.body.decode())["params"]
  79. user_2 = req_data.get("user_2").split('->')[0]
  80. print(user_2)
  81. pswd_old = req_data.get("pswd_old").lower()
  82. pswd_new = req_data.get("pswd_new").lower()
  83. print(req_data)
  84. try:
  85. user = Customer.objects.get(user_no=user_2)
  86. except Exception:
  87. return http.HttpResponseForbidden("存在重复的账号,请联系管理员")
  88. # 对数据库密码进行解码
  89. try:
  90. sql_password = bytes(base64.b64decode(user.password.encode(encoding='utf-8'))).decode()
  91. except Exception:
  92. return http.HttpResponseForbidden("密码被后台操作过请联系管理员")
  93. # 判断输入的密码是否正确
  94. if user.user_no !=user_2 or sql_password!=pswd_old:
  95. return http.HttpResponseForbidden("旧密码与账号不匹配")
  96. pswd_new = bytes(base64.b64encode(pswd_new.encode())).decode()
  97. user.password = pswd_new
  98. user.save()
  99. context = {
  100. "message":"修改成功",
  101. "code":200
  102. }
  103. return http.JsonResponse(context)
  104. def delete(self, request):
  105. del request.session['username']
  106. response = redirect('/login/')
  107. response.delete_cookie("username")
  108. return response
  109. #获取用户登录信息
  110. class LoginInformation(View):
  111. def get(self, request):
  112. search_no = request.GET["search_no"]
  113. if search_no != '':
  114. search_no = ' USR LIKE ' + "'" + '%%' + search_no + '%%' + "'" + ' OR ' + '[NAME] LIKE ' + "'" + '%%' + search_no + '%%' + "'"
  115. # search_no = ' PRD_NO LIKE '+"'"+'%%'+search_no+'%%'+"'"
  116. sql = """SELECT USR,NAME FROM PSWD WHERE {0}""".format(search_no)
  117. else:
  118. sql = """SELECT USR,NAME FROM PSWD """
  119. data = []
  120. print(sql)
  121. with connection.cursor() as cursor:
  122. try:
  123. dep_data = cursor.execute(sql, []).fetchall()
  124. except Exception:
  125. context = {
  126. "data": data,
  127. "errmsg": "没有获取到数据",
  128. "code": 510
  129. }
  130. return http.JsonResponse(context)
  131. if len(dep_data) != 0:
  132. for i in dep_data:
  133. data.append({"id": i[0], "text": str(i[0]) + '->' + str(i[1])})
  134. # print(data)
  135. context = {
  136. "data": data,
  137. "errmsg": "获取用户信息成功",
  138. "code": 200
  139. }
  140. return http.JsonResponse(context)