From 592be4e43e010619550e5e4b09b957cf976e2b7e Mon Sep 17 00:00:00 2001
From: cs10086 <1291454411@qq.com>
Date: Fri, 27 Mar 2026 08:54:47 +0800
Subject: [PATCH] test
---
.idea/workspace.xml | 18 ++++++++++++--
api/urls.py | 7 +++---
api/views/BaiduFanyiView.py | 48 +++++++++++++++++++++++++++++++++++++
api/views/GetIPDataView.py | 6 +++--
chunyu_project/settings.py | 3 ++-
5 files changed, 74 insertions(+), 8 deletions(-)
create mode 100644 api/views/BaiduFanyiView.py
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 69b021d..fb63dbe 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -5,7 +5,10 @@
+
+
+
@@ -117,6 +120,9 @@
+
+
+
C:\Users\12914\AppData\Roaming\Subversion
@@ -128,7 +134,7 @@
1774436691939
-
+
@@ -146,7 +152,15 @@
1774552113802
-
+
+
+ 1774565193807
+
+
+
+ 1774565193807
+
+
diff --git a/api/urls.py b/api/urls.py
index cd1f572..f45bf9e 100644
--- a/api/urls.py
+++ b/api/urls.py
@@ -1,7 +1,8 @@
from django.contrib import admin
from django.urls import path, include
-from .views.GetIPDataView import GetIPData
-
+from .views.GetIPDataView import GetIPDataView
+from .views.BaiduFanyiView import BaiduFanyiView
urlpatterns = [
- path("getIpData/", GetIPData.as_view()),
+ path("getIpData/", GetIPDataView.as_view()),
+ path("baiduFanyi/", BaiduFanyiView.as_view()),
]
\ No newline at end of file
diff --git a/api/views/BaiduFanyiView.py b/api/views/BaiduFanyiView.py
new file mode 100644
index 0000000..a56a71c
--- /dev/null
+++ b/api/views/BaiduFanyiView.py
@@ -0,0 +1,48 @@
+from rest_framework.decorators import permission_classes
+from rest_framework.permissions import AllowAny
+from rest_framework.views import APIView
+
+from rest_framework.response import Response
+from rest_framework import status
+
+import requests
+import json
+
+import requests
+import random
+import json
+from hashlib import md5
+
+
+def make_md5(s, encoding='utf-8'):
+ return md5(s.encode(encoding)).hexdigest()
+
+@permission_classes([AllowAny])
+class BaiduFanyiView(APIView):
+ def post(self, request):
+ print(request.data)
+
+ # Set your own appid/appkey.
+ appid = '20220718001275847'
+ appkey = 'CJHmvf7qGs3szQy32cMg'
+
+ from_lang = 'en'
+ to_lang = 'zh'
+
+ endpoint = 'http://api.fanyi.baidu.com'
+ path = '/api/trans/vip/translate'
+ url = endpoint + path
+
+ query = request.data['q']
+
+ salt = random.randint(32768, 65536)
+ sign = make_md5(appid + query + str(salt) + appkey)
+
+ headers = {'Content-Type': 'application/x-www-form-urlencoded'}
+ payload = {'appid': appid, 'q': query, 'from': from_lang, 'to': to_lang, 'salt': salt, 'sign': sign}
+
+ r = requests.post(url, params=payload, headers=headers)
+ result = r.json()
+
+
+ return Response(result, status=status.HTTP_200_OK)
\ No newline at end of file
diff --git a/api/views/GetIPDataView.py b/api/views/GetIPDataView.py
index 221bac1..ea1dbc5 100644
--- a/api/views/GetIPDataView.py
+++ b/api/views/GetIPDataView.py
@@ -6,7 +6,7 @@ from rest_framework.permissions import AllowAny
@permission_classes([AllowAny])
-class GetIPData(APIView):
+class GetIPDataView(APIView):
def get(self, request):
ip_info = {
'remote_addr': request.META.get('REMOTE_ADDR'),
@@ -19,4 +19,6 @@ class GetIPData(APIView):
'http_forwarded': request.META.get('HTTP_FORWARDED'),
}
- return Response({"ip_info": ip_info}, status=status.HTTP_200_OK)
\ No newline at end of file
+ return Response({"ip_info": ip_info}, status=status.HTTP_200_OK)
+
+
diff --git a/chunyu_project/settings.py b/chunyu_project/settings.py
index 0f235fa..35f570b 100644
--- a/chunyu_project/settings.py
+++ b/chunyu_project/settings.py
@@ -67,7 +67,8 @@ ALLOWED_HOSTS = ["*"]
CORS_ALLOWED_ORIGINS = [
"http://localhost:3000", # 例如:您的React/Vue开发服务器
"http://127.0.0.1:3000",
- "*"
+ "http://*",
+ "https://*"
]
# CORS设置