From 855684472d43561972fb0f87f1e9693df4159755 Mon Sep 17 00:00:00 2001 From: shaohuzhang1 Date: Fri, 1 Mar 2024 14:25:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B5=8C=E5=85=A5=E8=AE=A4=E8=AF=81?= =?UTF-8?q?=E8=B7=A8=E5=9F=9F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/application/views/application_views.py | 18 +++++++++++++++--- apps/common/response/result.py | 6 +++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/apps/application/views/application_views.py b/apps/application/views/application_views.py index 7ae7e15aa..0b369f011 100644 --- a/apps/application/views/application_views.py +++ b/apps/application/views/application_views.py @@ -6,7 +6,7 @@ @date:2023/10/27 14:56 @desc: """ - +from django.http import HttpResponse from drf_yasg.utils import swagger_auto_schema from rest_framework.decorators import action from rest_framework.request import Request @@ -147,6 +147,12 @@ class Application(APIView): ApplicationSerializer.AccessTokenSerializer(data={'application_id': application_id}).one()) class Authentication(APIView): + @action(methods=['OPTIONS'], detail=False) + def options(self, request, *args, **kwargs): + return HttpResponse(headers={"Access-Control-Allow-Origin": "*", "Access-Control-Allow-Credentials": "true", + "Access-Control-Allow-Methods": "POST", + "Access-Control-Allow-Headers": "Origin,Content-Type,Cookie,Accept,Token"}, ) + @action(methods=['POST'], detail=False) @swagger_auto_schema(operation_summary="应用认证", operation_id="应用认证", @@ -154,8 +160,14 @@ class Application(APIView): tags=["应用/认证"], security=[]) def post(self, request: Request): - return result.success( - ApplicationSerializer.Authentication(data={'access_token': request.data.get("access_token")}).auth()) + response = result.success( + ApplicationSerializer.Authentication(data={'access_token': request.data.get("access_token")}).auth(), + headers={"Access-Control-Allow-Origin": "*", "Access-Control-Allow-Credentials": "true", + "Access-Control-Allow-Methods": "POST", + "Access-Control-Allow-Headers": "Origin,Content-Type,Cookie,Accept,Token"} + ) + + return response @action(methods=['POST'], detail=False) @swagger_auto_schema(operation_summary="创建应用", diff --git a/apps/common/response/result.py b/apps/common/response/result.py index 8e756aec7..d1cf6a3ad 100644 --- a/apps/common/response/result.py +++ b/apps/common/response/result.py @@ -21,7 +21,7 @@ class Result(JsonResponse): def __init__(self, code=200, message="成功", data=None, response_status=status.HTTP_200_OK, **kwargs): back_info_dict = {"code": code, "message": message, 'data': data} - super().__init__(data=back_info_dict, status=response_status) + super().__init__(data=back_info_dict, status=response_status, **kwargs) def get_page_request_params(other_request_params=None): @@ -147,13 +147,13 @@ def get_api_array_response(response_data_schema: openapi.Schema): )}) -def success(data): +def success(data, **kwargs): """ 获取一个成功的响应对象 :param data: 接口响应数据 :return: 请求响应对象 """ - return Result(data=data) + return Result(data=data, **kwargs) def error(message):