Files

64 lines
3.7 KiB
Python
Raw Permalink Normal View History

2026-08-02 14:07:04 +08:00
import pydash
from rest_framework import status
from rest_framework.decorators import action
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.viewsets import ViewSet
from sixGuoDjango import BaseResponse, ResData
from wechat_数据库.用户数据库.小程序登陆用户 import weiuser
from wechat_项目工具包.腾讯地图.腾讯地图工具 import wei地图工具
class 小程序用户的位置接口ViewSets(ViewSet):
@action(detail=False, methods=["post"], url_path="locating")
def 解析用户的定位(self, request: Request):
"""前端用户:发起定位请求的时候, """
self_mini_openid = request.data.get("self_mini_openid", None)
self_mini_unionid = request.data.get("self_mini_unionid", None)
result = request.data.get("result", None)
print("\n🍎 🍏 🍊 获取用户定位的请求进来了🍊 🍎 🍏 \n", self_mini_openid, self_mini_unionid)
six六果对象, sixBool = weiuser.objects.get_or_create(unionid=self_mini_unionid) # 拿到当前的用户请求的对象
# 判断 当前用户,是否已经拿有位置的信息数据了
if six六果对象.user_区县:
# 当这里为true的时候就可以直接返回了
2026-08-06 20:39:45 +08:00
print("判断当前的用户区县不为None")
2026-08-02 14:07:04 +08:00
return Response()
else:
纬度 = pydash.get(obj=result, path="latitude")
经度 = pydash.get(obj=result, path="longitude")
print("\n🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 🍊 🍎 用户的定位的数据", result)
地图解析结果 = wei地图工具.经纬度转换为地址(location=f"{纬度},{经度}")
省 = pydash.get(obj=地图解析结果, path="result.address_component.province")
市 = pydash.get(obj=地图解析结果, path="result.address_component.city")
区 = pydash.get(obj=地图解析结果, path="result.address_component.district")
街道 = pydash.get(obj=地图解析结果, path="result.address_component.street_number")
# 开始更新数据库
six六果对象.user_区县 = 区
six六果对象.user_省份 = 省
six六果对象.user_城市 = 市
six六果对象.user_街道信息 = 街道
2026-08-06 20:39:45 +08:00
six六果对象.save()
2026-08-02 14:07:04 +08:00
httpResData = BaseResponse(resData=ResData(listData=[""], dictData={"demo": "xiaozizi"}), ).model_dump()
return Response(data=httpResData, status=status.HTTP_200_OK)
@action(detail=False, methods=["post", ], url_path="ip")
def 解析用户IP地址接口(self, request: Request):
""" """
self_mini_unionid = request.data.get("self_mini_unionid", None)
2026-08-10 19:33:59 +08:00
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR', "")
2026-08-02 14:07:04 +08:00
ip = pydash.head(x_forwarded_for.split(','))
mini_unionid_user = weiuser.objects.get(unionid=self_mini_unionid, )
2026-08-06 20:39:45 +08:00
if mini_unionid_user.user_省份 is None or mini_unionid_user.user_城市 is None:
2026-08-02 14:07:04 +08:00
weiData = wei地图工具.ip地址转为地址(x_forwarded_for=ip, )
province_省份 = pydash.get(obj=weiData, path="result.ad_info.province")
city_城市 = pydash.get(obj=weiData, path="result.ad_info.city")
district_区县 = pydash.get(obj=weiData, path="result.ad_info.district") or None
mini_unionid_user.user_省份 = province_省份
mini_unionid_user.user_城市 = city_城市
mini_unionid_user.user_区县 = district_区县
mini_unionid_user.user_IP地址 = ip
mini_unionid_user.save()
httpResData = BaseResponse(resData=ResData(listData=[""], dictData={}), ).model_dump()
return Response(data=httpResData, status=status.HTTP_200_OK)