123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace App\Http\Controllers\Admin;
- use App\Log;
- use App\Service\SystemCallbackService;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- include '../app/Libs/qyWeChatSDK/WXBizMsgCrypt.php';
- class SystemCallbackController extends Controller
- {
- private $aesKey;
- private $authToken;
- private $serverCorpid;
- /**
- * 初始化
- * */
- public function __construct()
- {
- parent::__construct();
- $component = config('qyWechat.suite_info');
- if(empty($component)) {
- return self::returnValue([], 1001);
- }
- $this->serverCorpid = $component['server_corpid'];
- $this->aesKey = $component['aes_key'];
- $this->authToken = $component['auth_token'];
- }
- /**
- * 消息与事件接收
- * */
- public function receive(Request $request)
- {
- $msgSign = trim($request->input('msg_signature'));
- $timestamp = trim($request->input('timestamp'));
- $nonce = trim($request->input('nonce'));
- if($request->isMethod('get')) { // 初步校验回调链接合法性
- $echoStr = trim($request->input('echostr'));
- $encryptMsg = ""; // 解析之后的明文
- $crypt = new \WXBizMsgCrypt($this->authToken, $this->aesKey, $this->serverCorpid);
- $errCode = $crypt->VerifyURL($msgSign, $timestamp, $nonce, $echoStr, $encryptMsg);
- if($errCode == 0) {
- echo $encryptMsg;
- } else {
- # 解密失败,写入日志
- Log::logError($echoStr, [
- 'msgSign' => $msgSign,
- 'timestamp' => $timestamp,
- 'nonce' => $nonce,
- 'err_code' => $errCode
- ], 'SystemMsgReceiveDecrypt-Get');
- }
- exit;
- } else { // 数据回调通知
- $str = file_get_contents('php://input');
- $encryptMsg = ""; // 解析之后的明文
- try {
- $crypt = new \WXBizMsgCrypt($this->authToken, $this->aesKey, $this->serverCorpid);
- $encrypt = getInfoFromXMl($str, 'Encrypt');
- $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
- $from_xml = sprintf($format, $encrypt);
- $errCode = $crypt->decryptMsg($msgSign, $timestamp, $nonce, $from_xml, $encryptMsg);
- Log::logInfo($encryptMsg, [], 'sysInfo');
- if ($errCode == 0) {
- SystemCallbackService::responseDeal($encryptMsg);
- echo 'success';
- exit;
- } else {
- Log::logError('verifyTicket获取失败', ['errCode' => $errCode], 'SystemVerifyTicketGetFail');
- }
- } catch (\Exception $e) {
- Log::logError('事件信息捕获异常', [
- 'msg' => $e->getMessage(),
- 'line' => $e->getLine(),
- ], 'SystemEventGet');
- }
- echo "success";
- }
- }
- }
|