2026-08-02 14:07:04 +08:00
|
|
|
import orjson
|
|
|
|
|
import pydash
|
|
|
|
|
from asgiref.sync import async_to_sync
|
|
|
|
|
from channels.layers import BaseChannelLayer, get_channel_layer
|
|
|
|
|
from django.forms import model_to_dict
|
|
|
|
|
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, RedisClientManager, ResData
|
|
|
|
|
from wechat_数据库.用户数据库.小程序用户地址 import weiuser微信用户地址
|
|
|
|
|
from wechat_数据库.用户数据库.小程序登陆用户 import weiuser
|
|
|
|
|
|
|
|
|
|
redis_socked = RedisClientManager().get_client(db_name="redis_5")
|
|
|
|
|
|
|
|
|
|
example通道层实例: BaseChannelLayer = get_channel_layer(alias="default")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class 用户配送地址ViewSets(ViewSet):
|
2026-08-10 19:33:59 +08:00
|
|
|
def send前端发送最新用户数据(self, self_mini_unionid):
|
2026-08-02 14:07:04 +08:00
|
|
|
channel_id = redis_socked.get(self_mini_unionid)
|
|
|
|
|
user_obj = weiuser.objects.get(unionid=self_mini_unionid)
|
|
|
|
|
user_data = model_to_dict(instance=user_obj, fields=None, exclude=None)
|
|
|
|
|
async_to_sync(awaitable=example通道层实例.send)(channel=channel_id,
|
|
|
|
|
message={"type": "update_更新用户头像",
|
|
|
|
|
"text": orjson.dumps(user_data,
|
|
|
|
|
option=orjson.OPT_SORT_KEYS).decode(
|
|
|
|
|
encoding="utf-8")
|
|
|
|
|
})
|
|
|
|
|
|
2026-08-10 19:33:59 +08:00
|
|
|
def update更新用户手机号码方法(self, address, self_user_unionid):
|
2026-08-02 14:07:04 +08:00
|
|
|
pass
|
2026-08-10 19:33:59 +08:00
|
|
|
print(address)
|
2026-08-02 14:07:04 +08:00
|
|
|
用户手机号码 = pydash.get(obj=address, path="telNumber")
|
|
|
|
|
用户昵称 = pydash.get(obj=address, path="userName")
|
|
|
|
|
用户所在省 = pydash.get(obj=address, path="provinceName")
|
|
|
|
|
用户所在市 = pydash.get(obj=address, path="cityName")
|
|
|
|
|
用户所在区县 = pydash.get(obj=address, path="countyName")
|
|
|
|
|
用户所在街道 = pydash.get(obj=address, path="detailInfo")
|
2026-08-10 19:33:59 +08:00
|
|
|
weiuser.objects.filter(unionid=self_user_unionid).update(uesr_手机号码=用户手机号码, user_昵称=用户昵称, )
|
2026-08-06 20:39:45 +08:00
|
|
|
six_user六果对象 = weiuser.objects.get(unionid=self_user_unionid)
|
|
|
|
|
if not six_user六果对象.user_区县 or not six_user六果对象.user_城市 or not six_user六果对象.user_省份:
|
|
|
|
|
six_user六果对象.user_省份 = 用户所在省
|
|
|
|
|
six_user六果对象.user_城市 = 用户所在市
|
|
|
|
|
six_user六果对象.user_区县 = 用户所在区县,
|
|
|
|
|
six_user六果对象.user_街道信息 = 用户所在街道
|
|
|
|
|
six_user六果对象.save()
|
2026-08-02 14:07:04 +08:00
|
|
|
|
|
|
|
|
@action(detail=False, methods=["post"], url_path="first_add")
|
|
|
|
|
def 首次添加用户收件地址(self, request: Request):
|
|
|
|
|
""""""
|
|
|
|
|
addressData = request.data.get("addressData", None)
|
|
|
|
|
self_mini_openid = request.data.get("self_mini_openid", None)
|
|
|
|
|
self_mini_unionid = request.data.get("self_mini_unionid", None)
|
2026-08-10 19:33:59 +08:00
|
|
|
# 调用: 更新手机号码的方法,
|
|
|
|
|
self.update更新用户手机号码方法(address=addressData, self_user_unionid=self_mini_unionid)
|
|
|
|
|
# 调用更新有户头像的方法 此方法: 就是通过channles将最新的数据返回给前端(以便前端用户即时更新)
|
|
|
|
|
self.send前端发送最新用户数据(self_mini_unionid=self_mini_unionid)
|
|
|
|
|
weiuser微信用户地址.objects.update_or_create(
|
2026-08-02 14:07:04 +08:00
|
|
|
address=addressData, mini_unionid=self_mini_unionid, mini_openid=self_mini_openid,
|
|
|
|
|
isDefaultAddress=weiuser微信用户地址.choicesAddress.是默认地址.value,
|
|
|
|
|
)
|
|
|
|
|
查询当前用户的全部地址 = weiuser微信用户地址.objects.filter(mini_unionid=self_mini_unionid,
|
2026-08-10 19:33:59 +08:00
|
|
|
mini_openid=self_mini_openid, ).values("id_主键_配送地址", "id_str", "address", "isDefaultAddress")
|
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="query_all")
|
|
|
|
|
def 查询当前用户的全部地址(self, request: Request):
|
|
|
|
|
"""查询当前用户所有的配置过的配送地址"""
|
|
|
|
|
self_mini_openid = request.data.get("self_mini_openid", None)
|
|
|
|
|
self_mini_unionid = request.data.get("self_mini_unionid", None)
|
|
|
|
|
|
|
|
|
|
查询当前用户的全部地址 = weiuser微信用户地址.objects.filter(mini_openid=self_mini_openid,
|
|
|
|
|
mini_unionid=self_mini_unionid).values(
|
|
|
|
|
"id_主键_配送地址", "id_str", "address", "isDefaultAddress", "mini_unionid", "mini_openid")
|
|
|
|
|
httpResData = BaseResponse(
|
|
|
|
|
resData=ResData(listData=查询当前用户的全部地址, dictData={"demo": "在这里返回的吗"}), ).model_dump()
|
|
|
|
|
return Response(data=httpResData, status=status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
@action(detail=False, methods=["post", ], url_path="add")
|
|
|
|
|
def 添加用户地址(self, request: Request):
|
|
|
|
|
""""""
|
|
|
|
|
self_mini_openid = request.data.get("self_mini_openid", None)
|
|
|
|
|
self_mini_unionid = request.data.get("self_mini_unionid", None)
|
|
|
|
|
addressData = request.data.get("addressData", None)
|
2026-08-10 19:33:59 +08:00
|
|
|
self.update更新用户手机号码方法(address=addressData, self_user_unionid=self_mini_unionid)
|
|
|
|
|
self.send前端发送最新用户数据(self_mini_unionid=self_mini_unionid)
|
2026-08-02 14:07:04 +08:00
|
|
|
|
|
|
|
|
是否新存在 = weiuser微信用户地址.objects.filter(address=addressData, mini_unionid=self_mini_unionid,
|
|
|
|
|
mini_openid=self_mini_openid).exists()
|
|
|
|
|
if 是否新存在:
|
|
|
|
|
# 这里说明的是用户首次添加新地址成功了,应该更新数据为 默认地址,
|
|
|
|
|
weiuser微信用户地址.objects.filter(mini_openid=self_mini_openid, mini_unionid=self_mini_unionid).update(
|
|
|
|
|
isDefaultAddress=weiuser微信用户地址.choicesAddress.不是默认地址.value)
|
|
|
|
|
print("判断,同时更新的当前所有的地址都为不是默认的值,")
|
|
|
|
|
weiuser微信用户地址.objects.filter(address=addressData, mini_openid=self_mini_openid,
|
|
|
|
|
mini_unionid=self_mini_unionid).update(
|
|
|
|
|
isDefaultAddress=weiuser微信用户地址.choicesAddress.是默认地址.value,
|
|
|
|
|
)
|
|
|
|
|
print("将当前的地址更新为默认值")
|
|
|
|
|
查询当前用户的全部地址 = weiuser微信用户地址.objects.filter(mini_openid=self_mini_openid,
|
|
|
|
|
mini_unionid=self_mini_unionid).values(
|
|
|
|
|
"id_主键_配送地址", "id_str", "address", "isDefaultAddress",
|
|
|
|
|
)
|
|
|
|
|
httpResData = BaseResponse(
|
|
|
|
|
resData=ResData(listData=查询当前用户的全部地址, dictData={"demo": "地址相同"}), ).model_dump()
|
|
|
|
|
return Response(data=httpResData, status=status.HTTP_200_OK)
|
|
|
|
|
else:
|
|
|
|
|
# 同时还需要将以前的地址全部更新为非默认地址,
|
2026-08-10 19:33:59 +08:00
|
|
|
weiuser微信用户地址.objects.update_or_create(
|
2026-08-02 14:07:04 +08:00
|
|
|
address=addressData, mini_openid=self_mini_openid,
|
|
|
|
|
mini_unionid=self_mini_unionid) # 在这里更新了所有的地址都不是 默认的地址
|
|
|
|
|
(weiuser微信用户地址.objects.filter(mini_openid=self_mini_openid, mini_unionid=self_mini_unionid)
|
|
|
|
|
.update(isDefaultAddress=weiuser微信用户地址.choicesAddress.不是默认地址.value))
|
|
|
|
|
# 创建新对象.isDefaultAddress = weiuser微信用户地址.choicesAddress.是默认地址.value
|
|
|
|
|
# 创建新对象.save()
|
|
|
|
|
weiuser微信用户地址.objects.filter(address=addressData, mini_openid=self_mini_openid,
|
|
|
|
|
mini_unionid=self_mini_unionid).update(
|
|
|
|
|
isDefaultAddress=weiuser微信用户地址.choicesAddress.是默认地址.value,
|
|
|
|
|
)
|
|
|
|
|
查询当前用户的全部地址 = weiuser微信用户地址.objects.filter(mini_openid=self_mini_openid,
|
|
|
|
|
mini_unionid=self_mini_unionid).values(
|
|
|
|
|
"id_主键_配送地址", "id_str", "address", "isDefaultAddress",
|
|
|
|
|
)
|
|
|
|
|
httpResData = BaseResponse(
|
|
|
|
|
resData=ResData(listData=查询当前用户的全部地址, dictData={"demo": "添加成功"}), ).model_dump()
|
|
|
|
|
return Response(data=httpResData, status=status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
@action(detail=False, methods=["post"], url_path="delete")
|
|
|
|
|
def 单独删除地址(self, request: Request):
|
|
|
|
|
"""删除单个地址"""
|
|
|
|
|
idKey = request.data.get("idKey", None)
|
|
|
|
|
self_mini_openid = request.data.get("self_mini_openid", None)
|
|
|
|
|
self_mini_unionid = request.data.get("self_mini_unionid", None)
|
2026-08-10 19:33:59 +08:00
|
|
|
weiuser微信用户地址.objects.filter(id_主键_配送地址=idKey).delete()
|
2026-08-02 14:07:04 +08:00
|
|
|
查询当前用户的全部地址 = weiuser微信用户地址.objects.filter(mini_openid=self_mini_openid,
|
|
|
|
|
mini_unionid=self_mini_unionid).values(
|
|
|
|
|
"id_主键_配送地址", "id_str", "address", "isDefaultAddress")
|
|
|
|
|
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="all_delete")
|
|
|
|
|
def 删除当前用户所有的配送地址(self, request: Request):
|
|
|
|
|
"""删除全部的地址"""
|
|
|
|
|
self_mini_openid = request.data.get("self_mini_openid", None)
|
|
|
|
|
self_mini_unionid = request.data.get("self_mini_unionid", None)
|
|
|
|
|
weiuser微信用户地址.objects.filter(mini_unionid=self_mini_unionid, mini_openid=self_mini_openid).delete()
|
|
|
|
|
查询当前用户的全部地址 = weiuser微信用户地址.objects.filter(mini_unionid=self_mini_unionid,
|
|
|
|
|
mini_openid=self_mini_openid).values(
|
|
|
|
|
"id_主键_配送地址", "id_str", "address", "isDefaultAddress")
|
|
|
|
|
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="change_default")
|
|
|
|
|
def 设为默认地址(self, request: Request):
|
|
|
|
|
""""""
|
|
|
|
|
idKey = request.data.get("idKey", None)
|
|
|
|
|
self_mini_openid = request.data.get("self_mini_openid", None)
|
|
|
|
|
self_mini_unionid = request.data.get("self_mini_unionid", None)
|
2026-08-10 19:33:59 +08:00
|
|
|
print("这里没有拿值的是吗", self_mini_openid, self_mini_unionid, idKey)
|
2026-08-02 14:07:04 +08:00
|
|
|
addressData = weiuser微信用户地址.objects.get(id_主键_配送地址=idKey).address
|
2026-08-10 19:33:59 +08:00
|
|
|
self.update更新用户手机号码方法(address=addressData, self_user_unionid=self_mini_unionid)
|
|
|
|
|
self.send前端发送最新用户数据(self_mini_unionid=self_mini_unionid)
|
2026-08-02 14:07:04 +08:00
|
|
|
|
|
|
|
|
# 将所有的先更新为非默认地址
|
|
|
|
|
weiuser微信用户地址.objects.filter(mini_unionid=self_mini_unionid, mini_openid=self_mini_openid).update(
|
|
|
|
|
isDefaultAddress=weiuser微信用户地址.choicesAddress.不是默认地址.value)
|
2026-08-10 19:33:59 +08:00
|
|
|
weiuser微信用户地址.objects.filter(id_主键_配送地址=idKey, mini_unionid=self_mini_unionid, mini_openid=self_mini_openid).update(
|
2026-08-02 14:07:04 +08:00
|
|
|
isDefaultAddress=weiuser微信用户地址.choicesAddress.是默认地址.value)
|
|
|
|
|
查询当前用户的全部地址 = weiuser微信用户地址.objects.filter(mini_unionid=self_mini_unionid,
|
2026-08-10 19:33:59 +08:00
|
|
|
mini_openid=self_mini_openid).values("id_主键_配送地址", "id_str", "address", "isDefaultAddress")
|
|
|
|
|
httpResData = BaseResponse(resData=ResData(listData=查询当前用户的全部地址, dictData={"demo": "xiaozizi"}), ).model_dump()
|
2026-08-02 14:07:04 +08:00
|
|
|
return Response(data=httpResData, status=status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
@action(detail=False, methods=["post"], url_path="update")
|
|
|
|
|
def 更新当前配送地址(self, request: Request):
|
|
|
|
|
""""""
|
|
|
|
|
addressData = request.data.get("addressData", None)
|
|
|
|
|
idkey = request.data.get("idkey", None)
|
|
|
|
|
self_mini_openid = request.data.get("self_mini_openid", None)
|
|
|
|
|
self_mini_unionid = request.data.get("self_mini_unionid", None)
|
|
|
|
|
self.update更新用户手机号码方法(address=addressData, self_mini_openid=self_mini_openid,
|
|
|
|
|
self_user_unionid=self_mini_unionid)
|
|
|
|
|
对象是否存在 = weiuser微信用户地址.objects.filter(address=addressData,
|
|
|
|
|
mini_unionid=self_mini_unionid,
|
|
|
|
|
mini_openid=self_mini_openid).exists()
|
2026-08-10 19:33:59 +08:00
|
|
|
self.send前端发送最新用户数据(self_mini_unionid=self_mini_unionid)
|
2026-08-02 14:07:04 +08:00
|
|
|
if 对象是否存在:
|
|
|
|
|
# 说明用户当前是存在的啦(说明,addressData)是有相同的值存在的
|
|
|
|
|
查询当前用户的全部地址 = weiuser微信用户地址.objects.filter(mini_unionid=self_mini_unionid,
|
2026-08-10 19:33:59 +08:00
|
|
|
mini_openid=self_mini_openid).values("id_主键_配送地址", "id_str", "address", "isDefaultAddress")
|
|
|
|
|
httpResData = BaseResponse(resData=ResData(listData=查询当前用户的全部地址, dictData={"demo": "没有更新"}), ).model_dump()
|
2026-08-02 14:07:04 +08:00
|
|
|
return Response(data=httpResData, status=status.HTTP_200_OK)
|
|
|
|
|
else:
|
|
|
|
|
weiuser微信用户地址.objects.filter(id_主键_配送地址=idkey).update(address=addressData)
|
|
|
|
|
查询当前用户的全部地址 = weiuser微信用户地址.objects.filter(mini_unionid=self_mini_unionid,
|
2026-08-10 19:33:59 +08:00
|
|
|
mini_openid=self_mini_openid).values("id_主键_配送地址", "id_str", "address", "isDefaultAddress")
|
|
|
|
|
httpResData = BaseResponse(resData=ResData(listData=查询当前用户的全部地址, dictData={"demo": "xiaozizi"}), ).model_dump()
|
2026-08-02 14:07:04 +08:00
|
|
|
return Response(data=httpResData, status=status.HTTP_200_OK)
|