12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace App\Http\Controllers\Admin;
- use App\Log;
- use App\Service\TemplateCallbackService;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- include '../app/Libs/qyWeChatSDK/WXBizMsgCrypt.php';
- class TemplateController extends Controller
- {
- private $aesKey;
- private $authToken;
- /*
- * 初始化
- * */
- public function __construct()
- {
- parent::__construct();
- $component = config('qyWechat.suite_info');
- if(empty($component)) {
- return self::returnValue([], 1001);
- }
- $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->corpid);
- $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
- ], 'MsgReceiveDecrypt-Get');
- }
- exit;
- } else { // 数据回调通知
- $str = file_get_contents('php://input');
- $encryptMsg = ""; // 解析之后的明文
- try {
- $crypt = new \WXBizMsgCrypt($this->authToken, $this->aesKey, $this->appTemplateId);
- $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, []);
- if($errCode == 0) {
- TemplateCallbackService::responseCommand($encryptMsg);
- echo 'success';
- exit;
- } else {
- Log::logError('verifyTicket获取失败', ['errCode' => $errCode], 'TemplateVerifyTicketGetFail');
- }
- } catch (\Exception $e) {
- Log::logError('事件信息捕获异常', [
- 'msg' => $e->getMessage(),
- 'line' => $e->getLine(),
- ],'TemplateEventGet');
- }
- echo "success";
- }
- }
- }
|