Files

54 lines
2.2 KiB
Python
Raw Permalink Normal View History

2026-08-02 14:07:04 +08:00
import hashlib
import xmltodict
from django.core.handlers.wsgi import WSGIRequest
from django.http import HttpResponse
from rest_framework import status
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.viewsets import ViewSet
class sixwechat( ViewSet ):
token = "xiaozizi" # 配置时填写的token的值(随机字符串)
EncodingAESKey = "RWxYCl7lGDilyJ5BumHDCksnU07EpQLlo87876uUOCq" # 消息加解密密钥 (EncodingAESKey)(服务号)
def api_get( self, request: Request, ):
""""""
signature = request.query_params.get( key="signature", default="" )
timestamp = request.query_params.get( key="timestamp", default="" )
nonce = request.query_params.get( key="nonce", default="" )
echostr = request.query_params.get( "echostr", False )
# TODO:sorted([token, timestamp, nonce])这是就是对三个参数进行字典排序
hashstr = "".join( sorted( [ self.token, timestamp, nonce ] ) ) # 这是就是对三个参数进行字典排序
# hashstr = f"{}" # 这是就是对三个参数进行字典排序
print( hashstr )
sig = hashlib.new( name="sha1", data=hashstr.encode( "utf-8" ) ).hexdigest( )
if sig == signature:
# 当上面的值判断正确后 官方要求原样返回 echostr;
print( "服务号消息推送授权成功了" )
return HttpResponse( content=echostr, content_type="text/plan" )
else:
return Response( data="不对", )
def api_text( self, request: WSGIRequest | Request, ):
""" 这个接口是的配置路径为 服务号官网 ---> 功能设置 ---> 网页授权配置 """
print( "============" )
return HttpResponse( content="kMmcLX8ZO79apUxA" )
def api_post( self, request: WSGIRequest | Request, ):
""""""
xml_data = request.body # 微信会以 XML 格式推送消息
xml_str = xml_data.decode( "utf-8" )
xml_dict = xmltodict.parse( xml_input=xml_str.encode( "utf-8" ) )
return Response( data={ "message": "xiao方法被调用了" }, status=status.HTTP_200_OK )