This commit is contained in:
2026-03-27 08:54:47 +08:00
parent 2920abbe17
commit 592be4e43e
5 changed files with 74 additions and 8 deletions
+16 -2
View File
@@ -5,7 +5,10 @@
</component>
<component name="ChangeListManager">
<list default="true" id="a7465ceb-8d86-4d89-899e-8242cccf605d" name="更改" comment="test">
<change afterPath="$PROJECT_DIR$/api/views/BaiduFanyiView.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/api/urls.py" beforeDir="false" afterPath="$PROJECT_DIR$/api/urls.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/api/views/GetIPDataView.py" beforeDir="false" afterPath="$PROJECT_DIR$/api/views/GetIPDataView.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/chunyu_project/settings.py" beforeDir="false" afterPath="$PROJECT_DIR$/chunyu_project/settings.py" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
@@ -117,6 +120,9 @@
</set>
</attachedChunks>
</component>
<component name="StructureViewState">
<option name="selectedTab" value="逻辑" />
</component>
<component name="SvnConfiguration">
<configuration>C:\Users\12914\AppData\Roaming\Subversion</configuration>
</component>
@@ -128,7 +134,7 @@
<option name="presentableId" value="Default" />
<updated>1774436691939</updated>
<workItem from="1774436692980" duration="11637000" />
<workItem from="1774474993123" duration="10150000" />
<workItem from="1774474993123" duration="15275000" />
</task>
<task id="LOCAL-00001" summary="test">
<option name="closed" value="true" />
@@ -146,7 +152,15 @@
<option name="project" value="LOCAL" />
<updated>1774552113802</updated>
</task>
<option name="localTasksCounter" value="3" />
<task id="LOCAL-00003" summary="test">
<option name="closed" value="true" />
<created>1774565193807</created>
<option name="number" value="00003" />
<option name="presentableId" value="LOCAL-00003" />
<option name="project" value="LOCAL" />
<updated>1774565193807</updated>
</task>
<option name="localTasksCounter" value="4" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
+4 -3
View File
@@ -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()),
]
+48
View File
@@ -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)
+4 -2
View File
@@ -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)
return Response({"ip_info": ip_info}, status=status.HTTP_200_OK)
+2 -1
View File
@@ -67,7 +67,8 @@ ALLOWED_HOSTS = ["*"]
CORS_ALLOWED_ORIGINS = [
"http://localhost:3000", # 例如:您的React/Vue开发服务器
"http://127.0.0.1:3000",
"*"
"http://*",
"https://*"
]
# CORS设置