107 lines
4.8 KiB
Python
107 lines
4.8 KiB
Python
import datetime
|
|||
|
|
import secrets
|
||
|
|
|
||
|
|
import orjson
|
||
|
|
from django.core.handlers.wsgi import WSGIRequest
|
||
|
|
from requests import get, post
|
||
|
|
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.routers import DefaultRouter
|
||
|
|
from rest_framework.viewsets import ViewSet
|
||
|
|
|
||
|
|
from sixGuoDjango import BaseResponse, ResData
|
||
|
|
from wechat_token.token工具包 import sixToken
|
||
|
|
from wechat_数据库.小程序后端数据库.订阅消息 import miniSub订阅消息模板
|
||
|
|
from wechat_数据库.雪花算法.雪花算法工具 import generator
|
||
|
|
|
||
|
|
|
||
|
|
class miniMsg订阅消息(ViewSet):
|
||
|
|
"""miniMsg订阅消息工具"""
|
||
|
|
|
||
|
|
@action(detail=False, methods=["post"], url_path="查询服务卡片状态")
|
||
|
|
def 查询服务卡片状态(self, request: WSGIRequest | Request):
|
||
|
|
"""
|
||
|
|
查询服务卡片状态
|
||
|
|
:return:
|
||
|
|
:rtype:
|
||
|
|
"""
|
||
|
|
uri = f"https://api.weixin.qq.com/wxa/get_user_notify"
|
||
|
|
notify_code = secrets.token_hex(10)
|
||
|
|
post_data = {"openid": "o6oV610oYhpoJYaCJ9tDJKehsJTc", # string 是 用户身份标识符
|
||
|
|
"notify_type": "vknIxXIC1O0IMhHI0yiS-zPUt3UvPYO-ihDM_mcHRJw", #
|
||
|
|
"notify_code": notify_code,
|
||
|
|
}
|
||
|
|
mini_token = sixToken.get_mini_token()
|
||
|
|
result_data = post(url=uri, params={"access_token": mini_token}, json=post_data)
|
||
|
|
print(result_data.json())
|
||
|
|
return Response()
|
||
|
|
|
||
|
|
@action(detail=False, methods=["post"], url_path="gettemplate")
|
||
|
|
def 获取消息列表(self, request: WSGIRequest | Request):
|
||
|
|
""""""
|
||
|
|
"""
|
||
|
|
获取个人模板列表
|
||
|
|
:return:
|
||
|
|
:rtype:
|
||
|
|
"""
|
||
|
|
print("----------------------------------------")
|
||
|
|
uri = f"https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate"
|
||
|
|
mini_token = sixToken.get_mini_token()
|
||
|
|
print(mini_token)
|
||
|
|
post_data = {}
|
||
|
|
result_data = get(url=uri, params={"access_token": mini_token}, )
|
||
|
|
print(orjson.loads(result_data.content.decode(encoding="utf-8")))
|
||
|
|
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="sendmsg")
|
||
|
|
def 发送模板消息(self, request: WSGIRequest | Request):
|
||
|
|
"""
|
||
|
|
如果不知道模板格式,就需要调用上面的接口
|
||
|
|
{'priTmplId': 'xCfF4yeTlKaEfZ4sh3z9C6r-wG845W4Bst2JO9hWR6A',
|
||
|
|
'title': '卡券到期提醒',
|
||
|
|
'content':
|
||
|
|
'卡券名称:{{thing1.DATA}}\n
|
||
|
|
到期时间:{{time2.DATA}}\n
|
||
|
|
使用规则:{{thing3.DATA}}\n
|
||
|
|
状态:{{phrase4.DATA}}\n', '
|
||
|
|
example': '卡券名称:软件维护服务1次\n到期时间:2020年10月14日 23:59\n使用规则:将有专业服务人员上门服务并核销卡券\n状态:未使用\n', 'type': 2, 'keywordEnumValueList': []},
|
||
|
|
content内的内容就是模板的信息 格式为 thing1:{value:"内容,"},time2:{"value":"时间字符串"}
|
||
|
|
"""
|
||
|
|
pass
|
||
|
|
uri = f"https://api.weixin.qq.com/cgi-bin/message/subscribe/send"
|
||
|
|
datetimd_str = datetime.datetime.now().strftime(format="%Y-%m-%d %H:%M:%S")
|
||
|
|
random_str = generator.generate_str()
|
||
|
|
post_data = {"touser": "ohQkD7A-N93kljcFmkCyG6e_D9vg",
|
||
|
|
"template_id": "RvGf7wyALX4djhBgeIp6jqlfAKNzd_weXRFEd1P54fk",
|
||
|
|
"page": "tabPage/Home/Home",
|
||
|
|
"data": {
|
||
|
|
"thing1": {"value": "100优惠券"},
|
||
|
|
"thing2": {"value": "random_str"},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
mini_token = sixToken.get_mini_token()
|
||
|
|
result_data = post(url=uri, params={"access_token": mini_token}, json=post_data)
|
||
|
|
print(result_data.json(), "订阅消息发送成功了啦")
|
||
|
|
return Response(data={"msg": "订阅消息"})
|
||
|
|
|
||
|
|
@action(detail=False, methods=["post"], url_path="add_messages")
|
||
|
|
def 计数订阅成功的次数(self, request: Request):
|
||
|
|
""""""
|
||
|
|
self_mini_openid = request.data.get("self_mini_openid", None)
|
||
|
|
self_mini_unionid = request.data.get("self_mini_unionid", None)
|
||
|
|
|
||
|
|
sub对象, sub对象是否存在 = miniSub订阅消息模板.objects.update_or_create(sub订阅用户ID=self_mini_openid,
|
||
|
|
sub订阅模板ID="RvGf7wyALX4djhBgeIp6jqlfAKNzd_weXRFEd1P54fk",
|
||
|
|
)
|
||
|
|
sub对象.sub订阅次数 = sub对象.sub订阅次数 + 1
|
||
|
|
sub对象.save()
|
||
|
|
httpResData = BaseResponse(resData=ResData(listData=[""], dictData={"demo": "xiaozizi"}), ).model_dump()
|
||
|
|
return Response(data=httpResData, status=status.HTTP_200_OK)
|
||
|
|
|
||
|
|
|
||
|
|
miniMsg订阅消息Router = DefaultRouter()
|
||
|
|
miniMsg订阅消息Router.register(prefix="", viewset=miniMsg订阅消息, basename="")
|