Files
downstream_domain_fix/controller/AdminIndexController.php
T
2026-07-01 18:12:37 +08:00

91 lines
3.4 KiB
PHP

<?php
namespace addons\downstream_domain_fix\controller;
class AdminIndexController extends \app\admin\controller\PluginAdminBaseController
{
public function index()
{
$result = null;
$uid = input('post.uid', 0, 'intval');
$domain = input('post.domain', '', 'trim');
if ($this->request->isPost()) {
$result = $this->fixDomain($uid, $domain);
}
$this->assign('Title', '下游推送域名修复');
$this->assign('uid', $uid ?: '');
$this->assign('domain', $domain);
$this->assign('result', $result);
return $this->fetch('/index');
}
private function fixDomain($uid, $domain)
{
$domain = rtrim($domain, '/');
if ($uid <= 0) {
return ['status' => 400, 'msg' => '客户ID不能为空', 'total' => 0, 'updated' => 0, 'skipped' => 0, 'list' => []];
}
if (!$this->isValidDomain($domain)) {
return ['status' => 400, 'msg' => '推送域名必须以 http:// 或 https:// 开头', 'total' => 0, 'updated' => 0, 'skipped' => 0, 'list' => []];
}
$hosts = \think\Db::name('host')
->field('id,uid,domain,dedicatedip,domainstatus,stream_info')
->where('uid', $uid)
->where('stream_info', '<>', '')
->select()
->toArray();
$result = ['status' => 200, 'msg' => '处理完成', 'total' => count($hosts), 'updated' => 0, 'skipped' => 0, 'list' => []];
foreach ($hosts as $host) {
$streamInfo = json_decode($host['stream_info'], true) ?: [];
if (empty($streamInfo['downstream_url']) || empty($streamInfo['downstream_token']) || empty($streamInfo['downstream_id'])) {
$result['skipped']++;
$result['list'][] = $this->buildRow($host, '', $domain, '跳过:没有完整下游推送信息');
continue;
}
$oldDomain = rtrim($streamInfo['downstream_url'], '/');
if ($oldDomain === $domain) {
$result['skipped']++;
$result['list'][] = $this->buildRow($host, $oldDomain, $domain, '跳过:域名未变化');
continue;
}
$streamInfo['downstream_url'] = $domain;
\think\Db::name('host')->where('id', $host['id'])->update([
'stream_info' => json_encode($streamInfo, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
]);
$result['updated']++;
$result['list'][] = $this->buildRow($host, $oldDomain, $domain, '已更新');
}
if (function_exists('active_log')) {
active_log(sprintf('插件批量修复下游推送域名,客户ID:%d,目标域名:%s,更新:%d,跳过:%d', $uid, $domain, $result['updated'], $result['skipped']));
}
return $result;
}
private function isValidDomain($domain)
{
return strpos($domain, 'https://') === 0 || strpos($domain, 'http://') === 0;
}
private function buildRow($host, $oldDomain, $newDomain, $status)
{
return [
'id' => $host['id'],
'domain' => $host['domain'],
'dedicatedip' => $host['dedicatedip'],
'domainstatus' => $host['domainstatus'],
'old_domain' => $oldDomain,
'new_domain' => $newDomain,
'status' => $status,
];
}
}