def normal_login(request): """ 普通视图登录 :param request: 请求对象 :return: 返回json数据:user_info:用户信息;has_login:用户是否已登录 """ email = request.POST.get('email', '') password = request.POST.get('password', '') sign = request.POST.get('sign', '') vcode = request.POST.get('vcode', '') result = get_vcode(sign) if not (result and (result.decode('utf-8') == vcode.lower())): return json_response(*UserError.VeriCodeError) try: user = User.objects.get(email=email) except User.DoesNotExist: return json_response(*UserError.UserNotFound) user = authenticate(request, username=user.username, password=password) if user is not None: login(request, user) profile, created = Profile.objects.select_for_update().get_or_create(email=user.email) if profile.user_src != Profile.COMPANY_USER: profile.name = user.username profile.user_src = Profile.NORMAL_USER profile.save() request.session['uid'] = profile.uid request.session['username'] = profile.name set_profile(profile.data) else: return json_response(*UserError.PasswordError) return json_response(200, 'OK', { 'user_info': profile.data, 'has_login': bool(profile) })
def signup(request): email = request.POST.get('email', '') # 邮箱 password = request.POST.get('password', '') # 密码 password_again = request.POST.get('password_again', '') # 确认密码 vcode = request.POST.get('vcode', '') # 注册验证码 sign = request.POST.get('sign') # 注册验证码检验位 if password != password_again: # 两次密码不一样,返回错误码300002 return json_response(*UserError.PasswordError) result = get_vcode(sign) # 校验vcode,逻辑和登录视图相同 if not (result and (result.decode('utf-8') == vcode.lower())): return json_response(*UserError.VeriCodeError) if User.objects.filter(email__exact=email).exists(): # 检查数据库是否存在该用户 return json_response(*UserError.UserHasExists) # 返回错误码300004 username = email.split('@')[0] # 生成一个默认的用户名 if User.objects.filter(username__exact=username).exists(): username = email # 默认用户名已存在,使用邮箱作为用户名 User.objects.create_user( # 创建用户,并设置为不可登录 is_active=False, is_staff=False, username=username, email=email, password=password, ) Profile.objects.create( # 创建用户信息 name=username, email=email) sign = str(uuid.uuid1()) # 生成邮箱校验码 set_signcode(sign, email) # 在redis设置30min时限的验证周期 return json_response( 200, 'OK', { # 返回JSON数据 'email': email, 'sign': sign })
def normal_login(request): """ 普通登录视图 :param request: 请求对象 :return: 返回json数据: user_info: 用户信息;has_login: 用户是否已登录 """ phone = request.POST.get('phone', '') # 获取phone password = request.POST.get('password', '') # 获取password sign = request.POST.get('sign', '') # 获取登录验证码的sign vcode = request.POST.get('vcode', '') # 获取用户输入的验证码 result = get_vcode(sign) # 从redis中校验sign和vcode if not (result and (result.decode('utf-8') == vcode.lower())): return json_response(*UserError.VeriCodeError) # 校验失败返回错误码300003 try: user = User.objects.get(username=phone) # 使用phone获取Django用户 except User.DoesNotExist: return json_response(*UserError.UserNotFound) # 获取失败返回错误码300001 # print(user.username) # print(password) user = authenticate(request, username=user.username, password=password) # 授权校验 if user is not None: # 校验成功,获得返回用户信息 login(request, user) # 登录用户,设置登录session profile, created = Profile.objects.select_for_update( ).get_or_create( # 获取或创建Profile数据 name=user.username, ) if profile.user_src != Profile.COMPANY_USER: profile.name = user.username profile.user_src = Profile.NORMAL_USER profile.save() request.session.get("usertype") #让session创建usertyp这个值 request.session['uid'] = profile.uid # 设置Profile uid的session request.session['username'] = profile.name # 设置用户名的session request.session['displayname'] = profile.displayname # 设置用户名的session request.session[ 'usertype'] = profile.user_src # 设置用户类型session,普通用户,管理员 # print("---------------profile.user_src") # print(profile.user_src) # print(request.session.get("usertype")) set_profile(profile.data) # 将用户信息保存到redis,查用户信息从redis中查询 else: return json_response(*UserError.PasswordError) # 校验失败,返回错误码300002 recode = 200 if 33 == profile.user_src: recode = 300 return json_response(recode, 'OK', { # 返回JSON格式数据 'user_info': profile.data, 'has_login': bool(profile), })
def signup(request): phone = request.POST.get('phone', '') # 电话 displayname = request.POST.get('displayname', '') # 显示姓名 password = request.POST.get('password', '') # 密码 password_again = request.POST.get('password_again', '') # 确认密码 vcode = request.POST.get('vcode', '') # 注册验证码 classname = request.POST.get('classname', '') # 班级名称 trainee_type = request.POST.get('trainee_type', '') # 班级名称 trainee_code = request.POST.get('trainee_code', '') # 班级名称 sign = request.POST.get('sign') # 注册验证码检验位 if password != password_again: # 两次密码不一样,返回错误码300002 return json_response(*UserError.PasswordError) result = get_vcode(sign) # 校验vcode,逻辑和登录视图相同 if not (result and (result.decode('utf-8') == vcode.lower())): return json_response(*UserError.VeriCodeError) if User.objects.filter(username=phone).exists(): # 检查数据库是否存在该用户 return json_response(*UserError.UserHasExists) # 返回错误码300004 username = phone # 生成一个默认的用户名 if User.objects.filter(username__exact=username).exists(): username = phone # 默认用户名已存在,使用邮箱作为用户名 User.objects.create_user( # 创建用户,并设置为不可登录 is_active=False, is_staff=False, username=username, password=password, ) Profile.objects.create( # 创建用户信息 name=username, phone=phone, displayname=displayname, classname=classname, trainee_type=trainee_type, trainee_code=trainee_code, ) sign = str(uuid.uuid1()) # 生成邮箱校验码 set_signcode(sign, phone) # 在redis设置30min时限的验证周期 return json_response( 200, 'OK', { # 返回JSON数据 'phone': phone, 'sign': sign })
def signup(request): """ 用户注册接口 :param request: 请求对象 :return: 返回email用户注册邮箱和校验码sign """ email = request.POST.get('email', '') password = request.POST.get('password', '') password_again = request.POST.get('password_again', '') vcode = request.POST.get('vcode', '') sign = request.POST.get('sign') if password != password_again: return json_response(*UserError.PasswordError) result = get_vcode(sign) if not (result and (result.decode('utf-8') == vcode.lower())): return json_response(*UserError.VeriCodeError) if User.objects.filter(email__exact=email).exists(): return json_response(*UserError.UserHasExists) username = email.split('@')[0] if User.objects.filter(username__exact=username).exists(): username = email User.objects.create_user( is_active=False, is_staff=False, username=username, email=email, password=password, ) Profile.objects.create(name=username, email=email) sign = str(uuid.uuid1()) set_signcode(sign, email) return json_response(200, 'OK', {'email': email, 'sign': sign})