72 lines
2.6 KiB
PHP
72 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\common\service\qywx\QywxPromotionRedirectService;
|
|
|
|
/** 企业微信获客助手公开端点:JS 与随机跳转。 */
|
|
class QywxPromotionPublicController extends BaseApiController
|
|
{
|
|
/** 公开安装代码与随机跳转不依赖前台用户登录。 */
|
|
public array $notNeedLogin = ['script', 'redirect'];
|
|
|
|
public function script(string $key)
|
|
{
|
|
if (!QywxPromotionRedirectService::poolExists($key)) {
|
|
return response('/* promotion pool not found */', 404, ['Content-Type' => '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 = <<<JS
|
|
(function(w,d){
|
|
'use strict';
|
|
var key={$jsonKey}, go={$jsonGo};
|
|
function openPromotion(){
|
|
var source=w.location.href;
|
|
w.location.assign(go+'?from='+encodeURIComponent(source));
|
|
}
|
|
d.addEventListener('click',function(event){
|
|
var node=event.target&&event.target.closest?event.target.closest('[data-wecom-promotion="'+key+'"],.wecom-promotion-link[data-pool="'+key+'"]'):null;
|
|
if(!node){return;}
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
openPromotion();
|
|
},true);
|
|
w.WecomPromotion=w.WecomPromotion||{};
|
|
w.WecomPromotion[key]={open:openPromotion};
|
|
})(window,document);
|
|
JS;
|
|
|
|
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',
|
|
]);
|
|
}
|
|
|
|
}
|