from django.shortcuts import render,redirect,reverse from jyapp import models from django.views import View from django.http import HttpResponse,JsonResponse
import base64
import random import urllib import http.client
from django.apps import AppConfig from aip import AipFace
from django.db import transaction
class AppConfig(AppConfig): name = ‘jyapp’ APP_ID = ‘16538158’ API_KEY = ‘HZI4cNgG8BGwry0v2GgQPxjm’ SECRECT_KEY = ‘pcPY2QIuNnkS9gFDmjvH0SI7whlb2ml9’ client = AipFace(APP_ID,API_KEY,SECRECT_KEY) client.setConnectionTimeoutInMillis(10005) client.setSocketTimeoutInMillis(10005)
class Index(View): def get(self,request): return render(request,‘contact.html’)
class Regist(View): def get(self,request): return render(request,‘moban_index.html’) def post(self,request): # 获取前端数据 imagecontent = request.POST.get(‘imagecontent’) username = request.POST.get(‘username’) mobile = request.POST.get(‘mobile’) password = request.POST.get(‘password’) if not all([imagecontent,username,mobile,password]): return JsonResponse({‘code’:100,‘result’:‘注册信息不能为空’}) # if mobile_code != mobile_code_right: # return JsonResponse({‘code’:111,‘result’:‘请输入正确的验证码’}) else: # 验证该用户是否存在 user = models.User.objects.filter(mobile=mobile) if user: return JsonResponse({‘code’:123,‘result’:‘该用户已存在,请直接登录’}) else: try: # 引入事务 with transaction.atomic(): # 分割字符串 base_data = imagecontent.split(’,’)[1] # base64解码 base64_decode = base64.b64decode(base_data) # 图片写入本地 with open(‘static/image/’+mobile+’.jpeg’, ‘wb’) as f: f.write(base64_decode) # 添加到mysql数据库 models.User.objects.create( imagecontent = ‘static/image/’+mobile+’.jpeg’, username = username, mobile = mobile, password = password, ) imageType = ‘base64’ groupId = ‘usergroup’ userId = mobile # 加入可选参数 options = {} options[‘user_info’] = username options[‘quality_control’] = ‘NORMAL’ options[‘liveness_control’] = ‘LOW’ result = AppConfig.client.addUser(base_data,imageType,groupId,userId,options) print(result) error_code = result[‘error_code’] if isinstance(error_code,int) and error_code == 0: request.session[‘mobile’] = mobile return JsonResponse({‘code’:200,‘result’:‘注册成功’}) # return JsonResponse({‘result’:‘注册成功’}) else: error = ErrorCode().getErrorInfo(error_code) return JsonResponse({‘result’:’{}’.format(error)}) except: return JsonResponse({‘code’:333,‘result’:‘注册失败’})
class Login(View): def get(self,request): return render(request,‘moban_index.html’) def post(self,request): mobile = request.POST.get(‘mobile’) password = request.POST.get(‘password’) print(mobile,password) if not all([mobile,password]): return JsonResponse({‘code’:100,‘mes’:‘登录信息不完整’}) else: user = models.User.objects.filter(mobile=mobile,password=password) if user: request.session[‘mobile’] = mobile return JsonResponse({‘code’:200,‘mes’:‘登录成功’}) else: return JsonResponse({‘code’:333,‘mes’:‘登录账号或密码错误’})
class Login_face(View): def get(self,request): return render(request,‘moban_index.html’) def post(self,request): imagecontent = request.POST.get(‘imagecontent’) mobile = request.POST.get(‘mobile’) if not all([imagecontent,mobile]): return JsonResponse({‘code’:100,‘result’:‘登录信息不能为空’}) else: user = models.User.objects.filter(mobile=mobile) if not user: return JsonResponse({‘code’:113,‘result’:‘用户不存在’}) else: base_data = imagecontent.split(’,’)[1] imageType = ‘base64’ groupIdList = ‘usergroup’ # 加入可选参数 options = {} options[‘max_user_num’] = 1 options[‘quality_control’] = ‘NORMAL’ options[‘liveness_control’] = ‘LOW’ # options[‘user_id’] = mobile result = AppConfig.client.search(base_data,imageType,groupIdList,options) print(result) error_code = result[‘error_code’] # try: user_id = result[‘result’][‘user_list’][0][‘user_id’] score = result[‘result’][‘user_list’][0][‘score’] if isinstance(error_code,int) and error_code == 0 and user_id == mobile and score >= 90: request.session[‘mobile’] = mobile return JsonResponse({‘code’:200,‘result’:‘快速登录成功’}) else: return JsonResponse({‘code’:111,‘result’:‘快速登录失败’}) # except: # return JsonResponse({‘code’:158,‘result’:‘未检测到人脸’})