1
0

Work.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\index\controller;
  3. use DOMDocument;
  4. use wx\offiaccount\msg\ResponseMsg;
  5. use wx\work\callback\WXBizMsgCrypt;
  6. use think\helper\Str;
  7. class work
  8. {
  9. /**
  10. * 应用
  11. * 接收消息
  12. */
  13. public function index()
  14. {
  15. $token = '9IgAfTZY34mA';
  16. $encodingAesKey = '92ccGXDDZE3eE1ws1XrAvyP3VLrB9PCPHbrwB8jz5QM';
  17. $corpId = 'wwf5d2dea0a059ab7a';
  18. $wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId);
  19. $request = request();
  20. if ($request->isGet()) {
  21. // return $this->verifyURL($wxcpt);
  22. echo $this->verifyURL($wxcpt);
  23. exit;
  24. }
  25. if ($request->isPost()) {
  26. // return $this->decryptMsg($wxcpt);
  27. echo $this->decryptMsg($wxcpt);
  28. exit;
  29. }
  30. }
  31. /**
  32. * 企业微信检测url
  33. */
  34. private function verifyURL(WXBizMsgCrypt $wxcpt)
  35. {
  36. $sVerifyMsgSig = $_GET['msg_signature'];
  37. $sVerifyTimeStamp = $_GET['timestamp'];
  38. $sVerifyNonce = $_GET['nonce'];
  39. $sVerifyEchoStr = $_GET["echostr"];
  40. // token验证
  41. $errCode = $wxcpt->VerifyURL($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sVerifyEchoStr, $sEchoStr);
  42. if ($errCode == 0) {
  43. return $sEchoStr;
  44. } else {
  45. trace("ERR: " . $errCode, 'error');
  46. }
  47. }
  48. private function decryptMsg(WXBizMsgCrypt $wxcpt)
  49. {
  50. $sMsgSignature = $_GET['msg_signature'];
  51. $sTimeStamp = $_GET['timestamp'];
  52. $sNonce = $_GET['nonce'];
  53. $sPostData = file_get_contents('php://input');
  54. trace('postxml:' . $sPostData);
  55. $errCode = $wxcpt->DecryptMsg($sMsgSignature, $sTimeStamp, $sNonce, $sPostData, $sMsg);
  56. if ($errCode == 0) {
  57. $xml = new DOMDocument();
  58. $xml->loadXML($sMsg);
  59. $msgType = $xml->getElementsByTagName('MsgType')->item(0)->nodeValue;
  60. if (method_exists($this, $msgType)) {
  61. return $this->$msgType($wxcpt, $xml);
  62. }
  63. } else {
  64. trace("ERR: " . $errCode, 'error');
  65. }
  66. }
  67. /**
  68. * 文本消息
  69. */
  70. private function text(WXBizMsgCrypt $wxcpt, $xml)
  71. {
  72. $postMsg = $xml->getElementsByTagName('Content')->item(0)->nodeValue;
  73. // 根据话术获取固定内容(分词查找内容)
  74. $fromUserName = $xml->getElementsByTagName('FromUserName')->item(0)->nodeValue;
  75. $toUserName = $xml->getElementsByTagName('ToUserName')->item(0)->nodeValue;
  76. $textData = [
  77. 'ToUserName' => $fromUserName,
  78. 'FromUserName' => $toUserName,
  79. 'MsgType' => 'text',
  80. 'Content' => '小爱还不知道你说的是什么意思,正在学习中。。。'
  81. ];
  82. $msg = new ResponseMsg();
  83. $sRespData = $msg->responseText($textData);
  84. $errCode = $wxcpt->EncryptMsg($sRespData, $_GET['timestamp'], $_GET['nonce'], $sEncryptMsg);
  85. if ($errCode == 0) {
  86. return $sEncryptMsg;
  87. } else {
  88. trace("ERR: " . $errCode, 'error');
  89. }
  90. }
  91. /**
  92. * 事件消息
  93. */
  94. private function event(WXBizMsgCrypt $wxcpt, $xml)
  95. {
  96. $event = $xml->getElementsByTagName('Event')->item(0)->nodeValue;
  97. $event = Str::camel($event).'Event';
  98. if (method_exists($this, $event)) {
  99. return $this->$event($wxcpt, $xml);
  100. }
  101. }
  102. /**
  103. * 用户进入应用事件(该事件在员工进入到该应用中时会一直有)
  104. */
  105. private function enterAgentEvent(WXBizMsgCrypt $wxcpt, $xml)
  106. {
  107. // $fromUserName = $xml->getElementsByTagName('FromUserName')->item(0)->nodeValue;
  108. // // 更新用户是否关注公众号
  109. // $toUserName = $xml->getElementsByTagName('ToUserName')->item(0)->nodeValue;
  110. // $textData = [
  111. // 'ToUserName' => $fromUserName,
  112. // 'FromUserName' => $toUserName,
  113. // 'MsgType' => 'text',
  114. // 'Content' => '欢迎进入志远装饰营销数字化系统'
  115. // ];
  116. // $msg = new ResponseMsg();
  117. // $sRespData = $msg->responseText($textData);
  118. // $errCode = $wxcpt->EncryptMsg($sRespData, $_GET['timestamp'], $_GET['nonce'], $sEncryptMsg);
  119. // if ($errCode == 0) {
  120. // return $sEncryptMsg;
  121. // } else {
  122. // trace("ERR: " . $errCode, 'error');
  123. // }
  124. }
  125. }