企微短剧业务系统

SystemCallbackController.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Log;
  4. use App\Service\SystemCallbackService;
  5. use Illuminate\Http\Request;
  6. use App\Http\Controllers\Controller;
  7. include '../app/Libs/qyWeChatSDK/WXBizMsgCrypt.php';
  8. class SystemCallbackController extends Controller
  9. {
  10. private $aesKey;
  11. private $authToken;
  12. private $serverCorpid;
  13. /**
  14. * 初始化
  15. * */
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. $component = config('qyWechat.suite_info');
  20. if(empty($component)) {
  21. return self::returnValue([], 1001);
  22. }
  23. $this->serverCorpid = $component['server_corpid'];
  24. $this->aesKey = $component['aes_key'];
  25. $this->authToken = $component['auth_token'];
  26. }
  27. /**
  28. * 消息与事件接收
  29. * */
  30. public function receive(Request $request)
  31. {
  32. $msgSign = trim($request->input('msg_signature'));
  33. $timestamp = trim($request->input('timestamp'));
  34. $nonce = trim($request->input('nonce'));
  35. if($request->isMethod('get')) { // 初步校验回调链接合法性
  36. $echoStr = trim($request->input('echostr'));
  37. $encryptMsg = ""; // 解析之后的明文
  38. $crypt = new \WXBizMsgCrypt($this->authToken, $this->aesKey, $this->serverCorpid);
  39. $errCode = $crypt->VerifyURL($msgSign, $timestamp, $nonce, $echoStr, $encryptMsg);
  40. if($errCode == 0) {
  41. echo $encryptMsg;
  42. } else {
  43. # 解密失败,写入日志
  44. Log::logError($echoStr, [
  45. 'msgSign' => $msgSign,
  46. 'timestamp' => $timestamp,
  47. 'nonce' => $nonce,
  48. 'err_code' => $errCode
  49. ], 'SystemMsgReceiveDecrypt-Get');
  50. }
  51. exit;
  52. } else { // 数据回调通知
  53. $str = file_get_contents('php://input');
  54. $encryptMsg = ""; // 解析之后的明文
  55. try {
  56. $crypt = new \WXBizMsgCrypt($this->authToken, $this->aesKey, $this->serverCorpid);
  57. $encrypt = getInfoFromXMl($str, 'Encrypt');
  58. $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
  59. $from_xml = sprintf($format, $encrypt);
  60. $errCode = $crypt->decryptMsg($msgSign, $timestamp, $nonce, $from_xml, $encryptMsg);
  61. Log::logInfo($encryptMsg, [], 'sysInfo');
  62. if ($errCode == 0) {
  63. SystemCallbackService::responseDeal($encryptMsg);
  64. echo 'success';
  65. exit;
  66. } else {
  67. Log::logError('verifyTicket获取失败', ['errCode' => $errCode], 'SystemVerifyTicketGetFail');
  68. }
  69. } catch (\Exception $e) {
  70. Log::logError('事件信息捕获异常', [
  71. 'msg' => $e->getMessage(),
  72. 'line' => $e->getLine(),
  73. ], 'SystemEventGet');
  74. }
  75. echo "success";
  76. }
  77. }
  78. }