63 lines
3.6 KiB
Python
63 lines
3.6 KiB
Python
from typing import cast
|
|
|
|
from channels.layers import BaseChannelLayer, get_channel_layer
|
|
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 wechat_数据库.微信支付券.代金券数据库 import stock券数据表
|
|
from wechat_项目工具包.微信支付工具包.领优惠券工具包 import stock发送代金券功能
|
|
|
|
default_channel_layer = cast(BaseChannelLayer, get_channel_layer(alias="default"))
|
|
|
|
from functools import wraps
|
|
import threading
|
|
|
|
|
|
def sync_lock(lock):
|
|
def decorator(fn):
|
|
@wraps(fn)
|
|
def wrapper(*args, **kwargs):
|
|
with lock:
|
|
return fn(*args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
return decorator
|
|
|
|
|
|
class 小程序发优惠券到钱包接口ViewSets(ViewSet):
|
|
lock = threading.Lock() # 所有请求 共享这一个 lock对象
|
|
stock_id_代金券批次号 = "22941326"
|
|
|
|
@action(detail=False, methods=["get", "post", "put", "delete"], url_path="生成优惠券")
|
|
def 生成优惠券(self, request: Request):
|
|
"""这里的接口就是将,券,直接发送给前端用户,"""
|
|
self_mini_openid = request.data.get("self_mini_openid", None)
|
|
self_mini_unionid = request.data.get("self_mini_unionid", None)
|
|
stock查询数据领券成功时的处理: stock券数据表 | None = stock券数据表.objects.filter(mini_unionid=self_mini_unionid, mini_openid=self_mini_openid, stock_use_券状态="已领券").first()
|
|
if stock查询数据领券成功时的处理 is not None:
|
|
return Response(data={"stock券状态": "已领券", "stock领券数据": {}})
|
|
stock查询数据: stock券数据表 | None = stock券数据表.objects.filter(mini_unionid=self_mini_unionid, mini_openid=self_mini_openid, stock_use_券状态="待领券").first()
|
|
if stock查询数据 is None:
|
|
# 查询数据库,看看用户当前有没有券已发了但是还没有领取的状态
|
|
# stock发券数据 = stock发送代金券功能(stock_id_代金券批次号="22512292").send发券并返回前端需要的数据()
|
|
stock发券数据 = stock发送代金券功能(stock_id_代金券批次号=self.stock_id_代金券批次号, stock券的状态="待领券").send发券并返回前端需要的数据(self_mini_openid=self_mini_openid, self_mini_unionid=self_mini_unionid)
|
|
# 4. 生成签名
|
|
# return Response(data=stock发券数据)
|
|
return Response(data={"stock券状态": "待领券", "stock领券数据": stock发券数据})
|
|
else:
|
|
pass
|
|
return Response(data={"stock券状态": stock查询数据.stock_use_券状态, "stock领券数据": stock查询数据.stock_未领取券的数据})
|
|
|
|
@action(detail=False, methods=["get", "post", "put", "delete"], url_path="领券后更新当前券的状态")
|
|
def 领券后更新当前券的状态(self, request: Request):
|
|
pass
|
|
self_mini_openid = request.data.get("self_mini_openid", None)
|
|
self_mini_unionid = request.data.get("self_mini_unionid", None)
|
|
stock券状态 = request.data.get("stock券状态", None)
|
|
print("用户领券成功了")
|
|
stock发送代金券功能(stock_id_代金券批次号=self.stock_id_代金券批次号, stock券的状态=stock券状态, stock券已领券="已领券").领券后更新当前券的状态(self_mini_openid=self_mini_openid, self_mini_unionid=self_mini_unionid)
|
|
return Response(data={"stock券状态": stock券状态, "stock领券数据": {}})
|