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 login_redirect(request): """ 微信登录重定向 :param request: 请求对象 :return: 获取用户信息并跳转回redirect_uri """ code = request.GET.get('code', '') state = request.GET.get('state', '') state = unquote_state(request, state) access_info = get_access_info( settings.WXWEB_LOGIN_PARAMS.get('appid', ''), settings.WXWEB_LOGIN_PARAMS.get('appsecret', ''), code) if 'errcode' in access_info: return redirect( AUTHORIZE_URI.format(settings.WXWEB_LOGIN_PARAMS.get('appid'), settings.WEB_LOGIN_REDIRECT_URI, 'snsapi_userinfo', state)) userinfo = get_userinfo(access_info.get('access_token', ''), access_info.get('openid', '')) if 'openid' not in userinfo: return redirect( AUTHORIZE_URI.format(settings.WXWEB_LOGIN_PARAMS.get('appid'), settings.WEB_LOGIN_REDIRECT_URI, 'snsapi_userinfo', state)) profile_values = { 'openid': userinfo.get('openid', ''), 'user_src': 1, 'sex': userinfo.get('sex', 0), 'nickname': userinfo.get('nickname', ''), 'avatar': userinfo.get('headimgurl', ''), 'country': userinfo.get('country', ''), 'province': userinfo.get('province', ''), 'city': userinfo.get('city', ''), } profile, created = Profile.objects.select_for_update().get_or_create( openid=userinfo.get('openid', ''), defaults=profile_values) if not profile.unionid: profile.unionid = userinfo.get('unionid', '') profile.save() if not created: for key, value in profile_values.items(): setattr(profile, key, value) profile.save() set_profile(profile.data) request.session['uid'] = profile.uid request.session['username'] = profile.name return redirect(furl(state).url)
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 save_model(self, request, obj, form, change): obj.save() set_profile(obj.data)