from django.shortcuts import render,HttpResponse
from qywechat.weworkapi.callback.WXBizMsgCrypt3 import WXBizMsgCrypt
import xml.etree.cElementTree as ET
import sys
from xml.dom.minidom import parse
# Create your views here.
"""
接收企业微信的消息,验证通过
"""
# def receive(request):
#     if request.method == "GET":
#         #校验消息
#         sVerifyMsgSig = request.GET.get("msg_signature")
#         sVerifyTimeStamp = request.GET.get("timestamp")
#         sVerifyNonce = request.GET.get("nonce")
#         #request.get("sReplyEchoStr")
#         sVerifyEchoStr = request.GET.get("echostr")
#         print("-----------------------------")
#         print(sVerifyMsgSig)
#         print(sVerifyTimeStamp)
#         print(sVerifyNonce)
#         print(sVerifyEchoStr)
#         print("-----------------------------")
#
#         # 假设企业在企业微信后台上设置的参数如下
#         sToken = "<KEY>"
#         sEncodingAESKey = "<KEY>"
#         sCorpID = "ww0195826a8c3bbd01"
#
#         # 创建解密对象
#         wxcpt = WXBizMsgCrypt(sToken, sEncodingAESKey, sCorpID)
#         ret, sEchoStr = wxcpt.VerifyURL(sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, sVerifyEchoStr)
#         if (ret != 0):
#             print("ERR: VerifyURL ret: " + str(ret))
#         print(sEchoStr)
#         return HttpResponse(sEchoStr)
#     #return HttpResponse("ok2")

"""
接收消息
"""
def receive(request):
    #校验消息
    sReqMsgSig = request.GET.get("msg_signature")
    sReqTimeStamp = request.GET.get("timestamp")
    sReqNonce = request.GET.get("nonce")
    sReqData = str(request.body,encoding="utf-8")
    #解析加密部分
    # with open("t.xml",'w') as f:
    #     f.write(sReqData)
    # dom = parse("t.xml")
    # data = dom.documentElement
    # #获取加密节点
    # st = data.getElementsByTagName("Encrypt")[0]
    #获取内容
    #value = st.childNodes[0].nodeValue
    # print("-----------------------------")
    # print(sVerifyMsgSig)
    # print(sVerifyTimeStamp)
    # print(sVerifyNonce)
    # print(st)
    # print("-----------------------------")

    # 假设企业在企业微信后台上设置的参数如下
    sToken = "<KEY>"
    sEncodingAESKey = "<KEY>"
    sCorpID = "ww0195826a8c3bbd01"

    # 创建解密对象
    wxcpt = WXBizMsgCrypt(sToken, sEncodingAESKey, sCorpID)
    ret, sMsg = wxcpt.DecryptMsg(sReqData, sReqMsgSig, sReqTimeStamp, sReqNonce)
    if (ret != 0):
        print("ERR: DecryptMsg ret: " + str(ret))
        sys.exit(1)
    # 解密成功，sMsg即为xml格式的明文
    # TODO: 对明文的处理
    # For example:
    xml_tree = ET.fromstring(sMsg)
    content = xml_tree.find("Content").text
    print("接收到的消息:",content)

    #发送消息
    #sRespData = "<xml><ToUserName>ww1436e0e65a779aee</ToUserName><FromUserName>ChenJiaShun</FromUserName><CreateTime>1476422779</CreateTime><MsgType>text</MsgType><Content>你好</Content><MsgId>1456453720</MsgId><AgentID>1000002</AgentID></xml>"
    sRespData = "<xml><MsgType>text</MsgType><Content>滚，你个LSP</Content></xml>"
    """
    <xml>
    <MsgType>text</MsgType>
    <Content>你好</Content> 文本消息内容,最长不超过2048个字节，超过将截断
    ------
    <CreateTime>1476422779</CreateTime> 消息创建时间（整型）
    <ToUserName>ww1436e0e65a779aee</ToUserName> 成员ID. 企业id时，响应所有人
    <FromUserName>ChenJiaShun</FromUserName> 企业微信CorpID
    <MsgId>1456453720</MsgId>
    <AgentID>1000002</AgentID>
</xml>
    """
    ret, sEncryptMsg = wxcpt.EncryptMsg(sRespData, sReqNonce, sReqTimeStamp)
    if (ret != 0):
        print("ERR: DecryptMsg ret: " + str(ret))
        sys.exit(1)
    # ret == 0 加密成功，企业需要将sEncryptMsg返回给企业号
    # TODO:
    # HttpUitls.SetResponse(sEncryptMsg)
    return HttpResponse(sEncryptMsg)
from django.shortcuts import render,HttpResponse
from qywechat.weworkapi.callback.WXBizMsgCrypt3 import WXBizMsgCrypt
import xml.etree.cElementTree as ET
import sys
from xml.dom.minidom import parse
# Create your views here.
"""
接收企业微信的消息,验证通过
"""
# def receive(request):
#     if request.method == "GET":
#         #校验消息
#         sVerifyMsgSig = request.GET.get("msg_signature")
#         sVerifyTimeStamp = request.GET.get("timestamp")
#         sVerifyNonce = request.GET.get("nonce")
#         #request.get("sReplyEchoStr")
#         sVerifyEchoStr = request.GET.get("echostr")
#         print("-----------------------------")
#         print(sVerifyMsgSig)
#         print(sVerifyTimeStamp)
#         print(sVerifyNonce)
#         print(sVerifyEchoStr)
#         print("-----------------------------")
#
#         # 假设企业在企业微信后台上设置的参数如下
#         sToken = "<KEY>"
#         sEncodingAESKey = "<KEY>"
#         sCorpID = "ww0195826a8c3bbd01"
#
#         # 创建解密对象
#         wxcpt = WXBizMsgCrypt(sToken, sEncodingAESKey, sCorpID)
#         ret, sEchoStr = wxcpt.VerifyURL(sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, sVerifyEchoStr)
#         if (ret != 0):
#             print("ERR: VerifyURL ret: " + str(ret))
#         print(sEchoStr)
#         return HttpResponse(sEchoStr)
#     #return HttpResponse("ok2")

"""
接收消息
"""
def receive(request):
    #校验消息
    sReqMsgSig = request.GET.get("msg_signature")
    sReqTimeStamp = request.GET.get("timestamp")
    sReqNonce = request.GET.get("nonce")
    sReqData = str(request.body,encoding="utf-8")
    #解析加密部分
    # with open("t.xml",'w') as f:
    #     f.write(sReqData)
    # dom = parse("t.xml")
    # data = dom.documentElement
    # #获取加密节点
    # st = data.getElementsByTagName("Encrypt")[0]
    #获取内容
    #value = st.childNodes[0].nodeValue
    # print("-----------------------------")
    # print(sVerifyMsgSig)
    # print(sVerifyTimeStamp)
    # print(sVerifyNonce)
    # print(st)
    # print("-----------------------------")

    # 假设企业在企业微信后台上设置的参数如下
    sToken = "<KEY>"
    sEncodingAESKey = "<KEY>"
    sCorpID = "ww0195826a8c3bbd01"

    # 创建解密对象
    wxcpt = WXBizMsgCrypt(sToken, sEncodingAESKey, sCorpID)
    ret, sMsg = wxcpt.DecryptMsg(sReqData, sReqMsgSig, sReqTimeStamp, sReqNonce)
    if (ret != 0):
        print("ERR: DecryptMsg ret: " + str(ret))
        sys.exit(1)
    # 解密成功，sMsg即为xml格式的明文
    # TODO: 对明文的处理
    # For example:
    xml_tree = ET.fromstring(sMsg)
    content = xml_tree.find("Content").text
    print("接收到的消息:",content)

    #发送消息
    #sRespData = "<xml><ToUserName>ww1436e0e65a779aee</ToUserName><FromUserName>ChenJiaShun</FromUserName><CreateTime>1476422779</CreateTime><MsgType>text</MsgType><Content>你好</Content><MsgId>1456453720</MsgId><AgentID>1000002</AgentID></xml>"
    sRespData = "<xml><MsgType>text</MsgType><Content>滚，你个LSP</Content></xml>"
    """
    <xml>
    <MsgType>text</MsgType>
    <Content>你好</Content> 文本消息内容,最长不超过2048个字节，超过将截断
    ------
    <CreateTime>1476422779</CreateTime> 消息创建时间（整型）
    <ToUserName>ww1436e0e65a779aee</ToUserName> 成员ID. 企业id时，响应所有人
    <FromUserName>ChenJiaShun</FromUserName> 企业微信CorpID
    <MsgId>1456453720</MsgId>
    <AgentID>1000002</AgentID>
</xml>
    """
    ret, sEncryptMsg = wxcpt.EncryptMsg(sRespData, sReqNonce, sReqTimeStamp)
    if (ret != 0):
        print("ERR: DecryptMsg ret: " + str(ret))
        sys.exit(1)
    # ret == 0 加密成功，企业需要将sEncryptMsg返回给企业号
    # TODO:
    # HttpUitls.SetResponse(sEncryptMsg)
    return HttpResponse(sEncryptMsg)
# Create your views here. 接收企业微信的消息,验证通过 # def receive(request): #     if request.method == "GET": #         #校验消息 #         sVerifyMsgSig = request.GET.get("msg_signature") #         sVerifyTimeStamp = request.GET.get("timestamp") #         sVerifyNonce = request.GET.get("nonce") #         #request.get("sReplyEchoStr") #         sVerifyEchoStr = request.GET.get("echostr") #         print("-----------------------------") #         print(sVerifyMsgSig) #         print(sVerifyTimeStamp) #         print(sVerifyNonce) #         print(sVerifyEchoStr) #         print("-----------------------------") # #         # 假设企业在企业微信后台上设置的参数如下 #         sToken = "<KEY>" #         sEncodingAESKey = "<KEY>" #         sCorpID = "ww0195826a8c3bbd01" # #         # 创建解密对象 #         wxcpt = WXBizMsgCrypt(sToken, sEncodingAESKey, sCorpID) #         ret, sEchoStr = wxcpt.VerifyURL(sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, sVerifyEchoStr) #         if (ret != 0): #             print("ERR: VerifyURL ret: " + str(ret)) #         print(sEchoStr) #         return HttpResponse(sEchoStr) #     #return HttpResponse("ok2") 接收消息 #校验消息 #解析加密部分 # with open("t.xml",'w') as f: #     f.write(sReqData) # dom = parse("t.xml") # data = dom.documentElement # #获取加密节点 # st = data.getElementsByTagName("Encrypt")[0] #获取内容 #value = st.childNodes[0].nodeValue # print("-----------------------------") # print(sVerifyMsgSig) # print(sVerifyTimeStamp) # print(sVerifyNonce) # print(st) # print("-----------------------------") # 假设企业在企业微信后台上设置的参数如下 # 创建解密对象 # 解密成功，sMsg即为xml格式的明文 # TODO: 对明文的处理 # For example: #发送消息 #sRespData = "<xml><ToUserName>ww1436e0e65a779aee</ToUserName><FromUserName>ChenJiaShun</FromUserName><CreateTime>1476422779</CreateTime><MsgType>text</MsgType><Content>你好</Content><MsgId>1456453720</MsgId><AgentID>1000002</AgentID></xml>" <xml>     <MsgType>text</MsgType>     <Content>你好</Content> 文本消息内容,最长不超过2048个字节，超过将截断     ------     <CreateTime>1476422779</CreateTime> 消息创建时间（整型）     <ToUserName>ww1436e0e65a779aee</ToUserName> 成员ID. 企业id时，响应所有人     <FromUserName>ChenJiaShun</FromUserName> 企业微信CorpID     <MsgId>1456453720</MsgId>     <AgentID>1000002</AgentID> </xml> # ret == 0 加密成功，企业需要将sEncryptMsg返回给企业号 # TODO: # HttpUitls.SetResponse(sEncryptMsg)