コード例 #1
0
ファイル: views.py プロジェクト: cc0411/skyoms
    def post(self, request):
        result = {
            'code': 0,
            'msg': '请求成功',
            'data': {
                'menu': '',
            }
        }
        try:
            user = request.user
            menu = get_user_menu(user)
            result['data']['menu'] = menu

        except Exception as e:
            result = {'code': 500, 'msg': str(e), 'data': {}}
        finally:
            return JsonResponse(result)
コード例 #2
0
ファイル: views.py プロジェクト: cc0411/skyoms
def user_login(request):
    if request.method == 'POST':
        result = {
            'code': 0,
            'msg': '登录成功',
            'data': {
                'username': '',
                'password': '',
                'name': '',
                'token': '',
                'uuid': '',
                'menu': '',
                'router': ''
            }
        }
        try:
            login_ip = request.META['REMOTE_ADDR']
            raw_data = json.loads(request.body.decode('utf-8'))
            username = raw_data.get('username')
            password = raw_data.get('password')
            captcha = raw_data.get('code').lower()
            image_uuid = raw_data.get('image_uuid')
            # 判断验证码
            captcha_redis = cache.get(image_uuid)
            if not captcha_redis:
                raise CaptchaError('验证码过期')
            else:
                captcha_redis = captcha_redis.lower()
            if captcha_redis != captcha:
                raise CaptchaError('验证码错误')
            # 验证账号密码
            user = authenticate(username=username, password=password)
            if user:
                # 随机token
                rand_token = get_cookie(username)
                # 更新django登录状态
                login(request, user)
                # 保存最新token到数据库
                token_obj = Token.objects.filter(user=user)

                if token_obj:
                    token_obj.update(**{
                        'key': rand_token,
                        'created': datetime.datetime.now()
                    })
                else:
                    Token.objects.create(user=user, key=rand_token)
                # 获取登录用户有权限访问的菜单和路由
                menu = get_user_menu(user)
                router = get_user_router(user)
                # 插入审计日志
                LoginRecord.objects.create(user=request.user,
                                           ip=login_ip,
                                           login_time=datetime.datetime.now())
                data = {
                    'username': username,
                    'name': username,
                    'token': rand_token,
                    'uuid': str(uuid.uuid4()),
                    'menu': menu,
                    'router': router
                }
                result['data'] = data
            else:
                result = {'code': 401, 'msg': '用户名或密码错误', 'data': {}}
        except CaptchaError as e:
            result = {'code': 402, 'msg': str(e), 'data': {}}
        except Exception as e:
            result = {'code': 500, 'msg': str(e), 'data': {}}
        finally:
            return JsonResponse(result)