feat(domain): 添加域名快速切换功能

- 在BaseController中新增getManagedDomainOptions方法,用于获取用户管理的域名选项
- 在Cloudflare和Domain控制器中集成域名选项数据传递到视图
- 更新前端模板文件以支持多域名选择下拉列表
- 扩展JavaScript中的initDomainQuickSwitch函数,增加AJAX支持选项
- 引入DnsHelper和Db门面用于域名数据查询和处理
```
This commit is contained in:
luo-bo
2026-04-02 03:22:42 +08:00
parent 141b2ead43
commit a9b773868d
6 changed files with 67 additions and 7 deletions

View File

@@ -3,9 +3,11 @@ declare (strict_types = 1);
namespace app;
use app\lib\DnsHelper;
use think\App;
use think\exception\ValidateException;
use think\Validate;
use think\facade\Db;
use think\facade\View;
/**
@@ -96,6 +98,36 @@ abstract class BaseController
return $v->failException(true)->check($data);
}
protected function getManagedDomainOptions(?string $type = null): array
{
if (!checkPermission(1)) {
return [];
}
$query = Db::name('domain')->alias('A')
->join('account B', 'A.aid = B.id')
->field('A.id,A.name,B.type');
if (!empty($type)) {
$query->where('B.type', $type);
}
if (request()->user['level'] == 1) {
$query->where('A.is_hide', 0)->where('A.name', 'in', request()->user['permission']);
}
$rows = $query->order('A.name', 'asc')->select();
$list = [];
foreach ($rows as $row) {
$typeName = DnsHelper::$dns_config[$row['type']]['name'] ?? strtoupper((string)$row['type']);
$list[] = [
'id' => intval($row['id']),
'name' => $row['name'],
'type' => $row['type'],
'text' => $row['name'] . ' [' . $typeName . ']',
];
}
return $list;
}
protected function alert($code, $msg = '', $url = null, $wait = 3)
{

View File

@@ -14,8 +14,18 @@ class Cloudflare extends BaseController
{
try {
$context = $this->getCloudflareDomainContext(input('param.id/d'));
$quickDomainOptions = $this->getManagedDomainOptions('cloudflare');
if (empty($quickDomainOptions)) {
$quickDomainOptions = [[
'id' => intval($context['domain']['id']),
'name' => $context['domain']['name'],
'type' => 'cloudflare',
'text' => $context['domain']['name'] . ' [Cloudflare]',
]];
}
View::assign('domainId', $context['domain']['id']);
View::assign('domainName', $context['domain']['name']);
View::assign('quickDomainOptions', $quickDomainOptions);
return view();
} catch (Exception $e) {
return $this->alert('error', $e->getMessage());

View File

@@ -452,9 +452,19 @@ class Domain extends BaseController
$dnsconfig = DnsHelper::$dns_config[$dnstype];
$dnsconfig['type'] = $dnstype;
$quickDomainOptions = $this->getManagedDomainOptions();
if (empty($quickDomainOptions)) {
$quickDomainOptions = [[
'id' => intval($id),
'name' => $drow['name'],
'type' => $dnstype,
'text' => $drow['name'] . ' [' . ($dnsconfig['name'] ?? strtoupper($dnstype)) . ']',
]];
}
View::assign('domainId', $id);
View::assign('domainName', $drow['name']);
View::assign('quickDomainOptions', $quickDomainOptions);
View::assign('recordLine', $recordLineArr);
View::assign('minTTL', $minTTL ? $minTTL : 1);
View::assign('dnsconfig', $dnsconfig);

View File

@@ -9,7 +9,9 @@
<div class="pull-right" style="margin-top:-6px;max-width:100%;">
<div style="display:inline-block;width:300px;max-width:100%;vertical-align:middle;margin-right:6px;">
<select id="quickDomainSwitch" class="form-control">
<option value="{$domainId}" selected>{$domainName}</option>
{volist name="quickDomainOptions" id="item"}
<option value="{$item.id}"{if $item.id == $domainId} selected{/if}>{$item.text}</option>
{/volist}
</select>
</div>
<button type="button" class="btn btn-sm btn-primary" id="quickDomainSwitchBtn" style="vertical-align:middle;margin-right:6px;"><i class="fa fa-random fa-fw"></i> 切换域名</button>

View File

@@ -171,7 +171,9 @@ td{overflow: hidden;text-overflow: ellipsis;white-space: nowrap;max-width:360px;
<div class="pull-right" style="margin-top:-6px;max-width:100%;">
<div style="display:inline-block;width:300px;max-width:100%;vertical-align:middle;margin-right:6px;">
<select id="quickDomainSwitch" class="form-control">
<option value="{$domainId}" selected>{$domainName}</option>
{volist name="quickDomainOptions" id="item"}
<option value="{$item.id}"{if $item.id == $domainId} selected{/if}>{$item.text}</option>
{/volist}
</select>
</div>
<button type="button" class="btn btn-sm btn-primary" id="quickDomainSwitchBtn" style="vertical-align:middle;margin-right:6px;"><i class="fa fa-random fa-fw"></i> 切换域名</button>

View File

@@ -66,6 +66,7 @@ function initDomainQuickSwitch(options){
currentId: '',
currentText: '',
placeholder: '搜索域名后切换',
useAjax: false,
type: '',
limit: 10,
buildUrl: function(id){
@@ -79,11 +80,13 @@ function initDomainQuickSwitch(options){
if(settings.currentId !== '' && settings.currentText !== '' && $select.find('option[value="' + settings.currentId + '"]').length === 0){
$select.append(new Option(settings.currentText, settings.currentId, true, true));
}
$select.select2({
var select2Options = {
width: '100%',
language: 'zh-CN',
placeholder: settings.placeholder,
ajax: {
placeholder: settings.placeholder
};
if(settings.useAjax){
select2Options.ajax = {
url: '/domain/data',
type: 'post',
dataType: 'json',
@@ -117,8 +120,9 @@ function initDomainQuickSwitch(options){
};
},
cache: true
}
});
};
}
$select.select2(select2Options);
var navigate = function(){
var targetId = $.trim($select.val());
if(targetId === ''){