|
@@ -38,6 +38,10 @@ from Service.ModelService import ModelService
|
|
from Service.TemplateService import TemplateService
|
|
from Service.TemplateService import TemplateService
|
|
from django.views.generic import View
|
|
from django.views.generic import View
|
|
import base64
|
|
import base64
|
|
|
|
+import random
|
|
|
|
+from io import BytesIO
|
|
|
|
+from PIL import Image, ImageDraw, ImageFont
|
|
|
|
+from django.shortcuts import HttpResponse
|
|
|
|
|
|
|
|
|
|
# 获取验证码
|
|
# 获取验证码
|
|
@@ -1306,6 +1310,9 @@ class v3LoginView(TemplateView):
|
|
password = password.decode('utf-8')
|
|
password = password.decode('utf-8')
|
|
# 去前3位,后3位
|
|
# 去前3位,后3位
|
|
password = password[3:-3]
|
|
password = password[3:-3]
|
|
|
|
+
|
|
|
|
+ # print("------------%s"%password)
|
|
|
|
+ # return response.json(111)
|
|
data_valid = DataValid()
|
|
data_valid = DataValid()
|
|
if data_valid.email_validate(username):
|
|
if data_valid.email_validate(username):
|
|
return self.do_email_login(username, password, response)
|
|
return self.do_email_login(username, password, response)
|
|
@@ -2275,4 +2282,172 @@ class V2LogoutView(TemplateView):
|
|
return response.json(tko.code)
|
|
return response.json(tko.code)
|
|
|
|
|
|
|
|
|
|
|
|
+# 图片验证码生成
|
|
|
|
+class generatePictureCodeView(TemplateView):
|
|
|
|
+ @method_decorator(csrf_exempt)
|
|
|
|
+ def dispatch(self, *args, **kwargs):
|
|
|
|
+ return super(generatePictureCodeView, self).dispatch(*args, **kwargs)
|
|
|
|
+
|
|
|
|
+ def post(self, request, *args, **kwargs):
|
|
|
|
+ request.encoding = 'utf-8'
|
|
|
|
+ request_dict = request.POST
|
|
|
|
+ return self.validates(request_dict)
|
|
|
|
+
|
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
|
+ request.encoding = 'utf-8'
|
|
|
|
+ request_dict = request.GET
|
|
|
|
+ return self.validates(request_dict)
|
|
|
|
+
|
|
|
|
+ # 生成验证码和验证码图片
|
|
|
|
+ def get_valid_code_img(self, request_dict):
|
|
|
|
+ # ---------生成背景图片-----------
|
|
|
|
+ # img = Image.new('RGB', (260, 34), color=get_random_color())
|
|
|
|
+ img = Image.new('RGB', (260, 34),
|
|
|
|
+ color=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
|
|
|
|
+ # -------/生成背景图片-----------
|
|
|
|
+
|
|
|
|
+ # --------生成随机验证码,设置字体格式,大小等属性------------
|
|
|
|
+ draw = ImageDraw.Draw(img)
|
|
|
|
+ kumo_font = ImageFont.truetype("consola.ttf", 40, encoding="unic") # 设置字体
|
|
|
|
+ valid_code_str = ''
|
|
|
|
+ for i in range(5):
|
|
|
|
+ random_num = str(random.randint(0, 9))
|
|
|
|
+ random_low_alpha = chr(random.randint(97, 122))
|
|
|
|
+ random_high_alpha = chr(random.randint(65, 90))
|
|
|
|
+ random_char = random.choice([random_num, random_low_alpha, random_high_alpha])
|
|
|
|
+ draw.text((i * 50 + 20, 5), random_char, 'white', kumo_font)
|
|
|
|
+
|
|
|
|
+ # 保存验证码字符串
|
|
|
|
+ valid_code_str += random_char
|
|
|
|
+ print(valid_code_str)
|
|
|
|
+ # --------/生成随机验证码,设置字体格式,大小等属性------------
|
|
|
|
+
|
|
|
|
+ # -------增加干扰线,点----------
|
|
|
|
+ # 噪点噪线
|
|
|
|
+ width = 260
|
|
|
|
+ height = 34
|
|
|
|
+ for i in range(5):
|
|
|
|
+ x1 = random.randint(0, width)
|
|
|
|
+ x2 = random.randint(0, width)
|
|
|
|
+ y1 = random.randint(0, height)
|
|
|
|
+ y2 = random.randint(0, height)
|
|
|
|
+ draw.line((x1, y1, x2, y2), fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
|
|
|
|
+ for i in range(5):
|
|
|
|
+ draw.point([random.randint(0, width), random.randint(0, height)],
|
|
|
|
+ fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
|
|
|
|
+ x = random.randint(0, width)
|
|
|
|
+ y = random.randint(0, height)
|
|
|
|
+ draw.arc((x, y, x + 4, y + 4), 0, 90,
|
|
|
|
+ fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
|
|
|
|
+ # -------/增加干扰线,点-----------
|
|
|
|
+ # ------存入内存---------
|
|
|
|
+ f = BytesIO() # 用完之后,BytesIO会自动清掉
|
|
|
|
+ img.save(f, 'png')
|
|
|
|
+ data = f.getvalue()
|
|
|
|
+ return data, valid_code_str
|
|
|
|
+
|
|
|
|
+ # 生成验证码方法
|
|
|
|
+ def validates(self, request_dict):
|
|
|
|
+ # 页面传过来的uuid
|
|
|
|
+ # id = request.GET.get('id','')
|
|
|
|
+ id = request_dict.get('id', '')
|
|
|
|
+ # 存入redis的key
|
|
|
|
+ image_code_id = "image_code_%s" % id
|
|
|
|
+ """
|
|
|
|
+ 基于PIL模块动态生成响应状态码图片
|
|
|
|
+ :param request:
|
|
|
|
+ :return:
|
|
|
|
+ """
|
|
|
|
+ # 图片data,验证码
|
|
|
|
+ data, valid_code_str = self.get_valid_code_img(request_dict)
|
|
|
|
+ if id:
|
|
|
|
+ redisObj = RedisObject(db=6)
|
|
|
|
+ # 单条维护
|
|
|
|
+ redisObj.set_data(key=image_code_id, val=valid_code_str, expire=120)
|
|
|
|
+ image_code_val = redisObj.get_data(key=image_code_id)
|
|
|
|
+ print("________获取验证码的值" + image_code_val)
|
|
|
|
+ return HttpResponse(data)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+# 图片验证码校验注册
|
|
|
|
+class Image_Code_RegisterView(TemplateView):
|
|
|
|
+ @method_decorator(csrf_exempt)
|
|
|
|
+ def dispatch(self, *args, **kwargs):
|
|
|
|
+ return super(Image_Code_RegisterView, self).dispatch(*args, **kwargs)
|
|
|
|
+
|
|
|
|
+ def post(self, request, *args, **kwargs):
|
|
|
|
+ print("post进来了吗")
|
|
|
|
+ request.encoding = 'utf-8'
|
|
|
|
+ request_dict = request.POST
|
|
|
|
+ return self.validates(request_dict)
|
|
|
|
+
|
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
|
+ request.encoding = 'utf-8'
|
|
|
|
+ request_dict = request.GET
|
|
|
|
+ return self.validates(request_dict)
|
|
|
|
+
|
|
|
|
+ #检测验证码,并注册
|
|
|
|
+ def validates(self,request_dict):
|
|
|
|
+ print("__________request_dict:%s" % request_dict)
|
|
|
|
+ phone = request_dict.get('phone',None)
|
|
|
|
+ username = request_dict.get('userName',None)
|
|
|
|
+ userEmail = request_dict.get('userEmail',None)
|
|
|
|
+ password = request_dict.get('userPwd',None)
|
|
|
|
+ language = request_dict.get('language',None)
|
|
|
|
+ #前端传进来的uuid
|
|
|
|
+ imageCodeId = request_dict.get('imageCodeId',None)
|
|
|
|
+ # 页面输入的验证码
|
|
|
|
+ valid_code = request_dict.get('id_v_code',None)
|
|
|
|
+ response = ResponseObject(language)
|
|
|
|
+ if not username:
|
|
|
|
+ return response.json(107)
|
|
|
|
+ if not userEmail:
|
|
|
|
+ return response.json(105)
|
|
|
|
+ if not password:
|
|
|
|
+ return response.json(109)
|
|
|
|
+ if not phone:
|
|
|
|
+ return response.json(100)
|
|
|
|
+ userEmail = userEmail.strip()
|
|
|
|
+ username = username.strip()
|
|
|
|
+ password = password.strip()
|
|
|
|
+ # 用户已存在
|
|
|
|
+ if username:
|
|
|
|
+ nameValid = Device_User.objects.filter(username=username)
|
|
|
|
+ if nameValid:
|
|
|
|
+ return response.json(179)
|
|
|
|
+ if userEmail:
|
|
|
|
+ emailValid = Device_User.objects.filter(userEmail=userEmail)
|
|
|
|
+ if emailValid:
|
|
|
|
+ return response.json(103)
|
|
|
|
+ if phone:
|
|
|
|
+ phoneValid = Device_User.objects.filter(phone=phone)
|
|
|
|
+ if phoneValid:
|
|
|
|
+ return response.json(101)
|
|
|
|
+ #key
|
|
|
|
+ image_code_id = "image_code_%s" %imageCodeId
|
|
|
|
+ redisObj = RedisObject(db=6)
|
|
|
|
+ #redis里面的验证码
|
|
|
|
+ redis_image_code = redisObj.get_data(key=image_code_id)
|
|
|
|
+ if not valid_code==redis_image_code:
|
|
|
|
+ return response.json(121)
|
|
|
|
+ # return response.json(0)
|
|
|
|
+ # #存用户名和密码
|
|
|
|
+ create_data = {
|
|
|
|
+ "phone":phone,
|
|
|
|
+ "username": username,
|
|
|
|
+ "NickName": username,
|
|
|
|
+ "userEmail": userEmail,
|
|
|
|
+ "password": make_password(password),
|
|
|
|
+ "userID": CommonService.getUserID(μs=False, setOTAID=True),
|
|
|
|
+ "is_active": True,
|
|
|
|
+ "user_isValid": True,
|
|
|
|
+ }
|
|
|
|
+ users = Device_User.objects.create(**create_data)
|
|
|
|
+ return response.json(0)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|