Files
zyt/server/app/api/controller/QywxPromotionPublicController.php
T
2026-08-08 15:42:45 +08:00

61 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace app\api\controller;
use app\common\service\qywx\QywxPromotionRedirectService;
use app\common\service\qywx\QywxPromotionWidgetService;
/** 企业微信获客助手公开端点:JS 与随机跳转。 */
class QywxPromotionPublicController extends BaseApiController
{
/** 公开安装代码与随机跳转不依赖前台用户登录。 */
public array $notNeedLogin = ['script', 'redirect'];
public function script(string $key)
{
$pool = QywxPromotionRedirectService::publicPoolConfig($key);
if ($pool === null) {
return response('/* promotion pool not found */', 404, ['Content-Type' => 'application/javascript; charset=utf-8']);
}
// 由安装脚本自身的 src 解析 API 域名,避免把请求 Host 写入可公开缓存的 JavaScript。
$goUrl = '/api/qywx-promotion/go/' . $key;
$config = QywxPromotionWidgetService::decode($pool['widget_config_json'] ?? null);
$javascript = QywxPromotionWidgetService::renderScript(
$key,
$goUrl,
$config,
(int) ($pool['status'] ?? 0) === 1
);
return response($javascript, 200, [
'Content-Type' => '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',
]);
}
}