Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30a68d8
commit 7c62bee
Showing
24 changed files
with
395 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
*.js linguist-language=Python | ||
*.css linguist-language=Python | ||
*.html linguist-language=Python | ||
*.html linguist-language=Python | ||
*.vue linguist-language=Python |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import logging | ||
|
||
import requests | ||
from rest_framework.parsers import JSONParser | ||
from rest_framework.views import APIView | ||
|
||
from api_test.common.api_response import JsonResponse | ||
from api_test.models import VisitorsRecord | ||
|
||
logger = logging.getLogger(__name__) # 这里使用 __name__ 动态搜索定义的 logger 配置,这里有一个层次关系的知识点。 | ||
|
||
|
||
class Record(APIView): | ||
# authentication_classes = (TokenAuthentication,) | ||
permission_classes = () | ||
|
||
def parameter_check(self, data): | ||
""" | ||
校验参数 | ||
:param data: | ||
:return: | ||
""" | ||
try: | ||
# 必传参数 success | ||
if data["success"] not in [0, 1]: | ||
return JsonResponse(code="999996", msg="参数有误!") | ||
except KeyError: | ||
return JsonResponse(code="999996", msg="参数有误!") | ||
|
||
def ip_get_city(self, ip): | ||
""" | ||
通过ip获取城市信息 | ||
:param ip: | ||
:return: | ||
""" | ||
params = { | ||
"output": "json", | ||
# "location": "104.06151"+","+"30.54852", | ||
"key": "2200d7985fd43582411687abaa5b01eb", | ||
"ip": ip | ||
} | ||
headers = {"Content-Type": "application/json;charset=utf-8"} | ||
response = requests.get(url="http://restapi.amap.com/v3/ip", | ||
params=params, headers=headers, allow_redirects=False, | ||
timeout=8) | ||
if response.status_code == 301: | ||
response = requests.get(url=response.headers["location"]) | ||
return response.json() | ||
|
||
def post(self, request): | ||
""" | ||
记录访客 | ||
:param request: | ||
:return: | ||
""" | ||
if request.environ["REMOTE_ADDR"] == "127.0.0.1": | ||
return JsonResponse(code="999999", msg="成功!") | ||
data = JSONParser().parse(request) | ||
result = self.parameter_check(data) | ||
if result: | ||
return result | ||
if data["success"] == 0: | ||
ip = self.ip_get_city(request.environ["REMOTE_ADDR"]) | ||
VisitorsRecord(formattedAddress=request.environ["REMOTE_ADDR"], province=ip["province"], | ||
city=ip["city"], | ||
success="失败", reason="获取用户经纬度失败!").save() | ||
else: | ||
try: | ||
longitude = data["longitude"] | ||
latitude = data["latitude"] | ||
except KeyError: | ||
return JsonResponse(code="999996", msg="参数有误") | ||
params = { | ||
"output": "json", | ||
# "location": "104.06151"+","+"30.54852", | ||
"key": "2200d7985fd43582411687abaa5b01eb", | ||
"location": str(longitude)+","+str(latitude) | ||
} | ||
headers = {"Content-Type": "application/json;charset=utf-8"} | ||
response = requests.get(url="http://restapi.amap.com/v3/geocode/regeo", | ||
params=params, headers=headers, allow_redirects=False, | ||
timeout=8) | ||
if response.status_code == 301: | ||
response = requests.get(url=response.headers["location"]) | ||
try: | ||
visitor_addr = response.json() | ||
if visitor_addr["status"] == "1": | ||
VisitorsRecord( | ||
formattedAddress=visitor_addr["regeocode"]["formatted_address"], | ||
country=visitor_addr["regeocode"]["addressComponent"]["country"], | ||
province=visitor_addr["regeocode"]["addressComponent"]["province"], | ||
city=visitor_addr["regeocode"]["addressComponent"]["city"], | ||
district=visitor_addr["regeocode"]["addressComponent"]["district"], | ||
township=visitor_addr["regeocode"]["addressComponent"]["township"], | ||
street=visitor_addr["regeocode"]["addressComponent"]["streetNumber"]["street"], | ||
number=visitor_addr["regeocode"]["addressComponent"]["streetNumber"]["number"], | ||
success="成功", | ||
reason=visitor_addr["info"] | ||
).save() | ||
else: | ||
ip = self.ip_get_city(request.environ["REMOTE_ADDR"]) | ||
VisitorsRecord(formattedAddress=request.environ["REMOTE_ADDR"], province=ip["province"], | ||
city=ip["city"], | ||
success="失败", reason=ip["info"]).save() | ||
except Exception as e: | ||
ip = self.ip_get_city(request.environ["REMOTE_ADDR"]) | ||
VisitorsRecord(formattedAddress=request.environ["REMOTE_ADDR"], province=ip["province"], | ||
city=ip["city"], | ||
success="失败", reason=e).save() | ||
|
||
return JsonResponse(code="999999", msg="成功!") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<!DOCTYPE html><html><head><meta charset=utf-8><title>自动化测试平台</title><link href=/static/css/app.3a1563fcc9524db67e54f91a07474cdc.css rel=stylesheet></head><body><div id=app></div><script type=text/javascript src=/static/js/manifest.e9fecef20dfb3f85963f.js></script><script type=text/javascript src=/static/js/vendor.00ebef1d9e9c19674cd1.js></script><script type=text/javascript src=/static/js/app.e113befbeb3f47b05d96.js></script></body></html> | ||
<!DOCTYPE html><html><head><meta charset=utf-8><title>自动化测试平台</title><script type=text/javascript src="http://webapi.amap.com/maps?v=1.4.6&key=2200d7985fd43582411687abaa5b01eb"></script><link href=/static/css/app.3a1563fcc9524db67e54f91a07474cdc.css rel=stylesheet></head><body><div id=app></div><script type=text/javascript src=/static/js/manifest.81b59ff2778a95208898.js></script><script type=text/javascript src=/static/js/vendor.00ebef1d9e9c19674cd1.js></script><script type=text/javascript src=/static/js/app.d5f5bc29aa454af6c0d4.js></script></body></html> |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.