'application/javascript; charset=utf-8']); } $goUrl = rtrim($this->request->domain(), '/') . '/api/qywx-promotion/go/' . $key; $jsonKey = json_encode($key, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR); $jsonGo = json_encode($goUrl, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR); $javascript = << 'application/javascript; charset=utf-8', 'Cache-Control' => 'public, max-age=60', 'X-Content-Type-Options' => 'nosniff', ]); } public function redirect(string $key) { $picked = QywxPromotionRedirectService::pick($key, [ 'source_url' => (string) $this->request->get('from', ''), 'referer' => (string) $this->request->header('referer', ''), 'user_agent' => (string) $this->request->header('user-agent', ''), 'ip' => (string) $this->request->ip(), ]); if (!$picked) { return response('当前暂无可用的企业微信推广链接,请稍后再试。', 503, [ 'Content-Type' => 'text/plain; charset=utf-8', 'Cache-Control' => 'no-store', ]); } return redirect($picked['url'], 302)->header([ 'Cache-Control' => 'no-store', 'Referrer-Policy' => 'no-referrer', ]); } public function providerCallback() { try { $psr = QywxPromotionOpenWorkService::serveProviderCallback(); $body = $psr->getBody(); $body->rewind(); $headers = []; if ($psr->getHeaderLine('Content-Type') !== '') { $headers['Content-Type'] = $psr->getHeaderLine('Content-Type'); } return response($body->getContents(), $psr->getStatusCode(), $headers); } catch (\Throwable $e) { Log::error('企微推广服务商回调失败:' . $e->getMessage(), ['exception' => $e]); return response('error', 500, ['Content-Type' => 'text/plain; charset=utf-8']); } } public function authCallback() { $fallback = rtrim($this->request->domain(), '/') . '/admin/first_visit/wecom_promotion'; $returnUrl = QywxPromotionOpenWorkService::configuredAdminReturnUrl($fallback); try { $result = QywxPromotionOpenWorkService::consumeAuthorizationCallback( trim((string) $this->request->get('auth_code', '')), trim((string) $this->request->get('state', '')) ); $query = ['wecom_auth' => 'success', 'account_id' => (int) $result['id']]; } catch (\Throwable $e) { Log::error('企微推广授权回跳失败:' . $e->getMessage(), ['exception' => $e]); $query = ['wecom_auth' => 'failed', 'message' => mb_substr($e->getMessage(), 0, 160)]; } return redirect($returnUrl . (str_contains($returnUrl, '?') ? '&' : '?') . http_build_query($query), 302); } }