tp/app/admin/controller/Cms/Domain/DomainPoolController.php
2026-03-09 16:35:17 +08:00

228 lines
5.3 KiB
PHP

<?php
declare(strict_types=1);
namespace app\admin\controller\Cms\Domain;
use app\admin\BaseController;
use think\facade\Db;
use think\Request;
use app\model\System\SystemDomainPool;
/**
* 主域名池管理控制器
*/
class DomainPoolController extends BaseController
{
/**
* 获取域名池列表
*/
public function index(Request $request)
{
$page = $request->param('page', 1, 'int');
$pageSize = $request->param('pageSize', 10, 'int');
$mainDomain = $request->param('main_domain', '');
$status = $request->param('status', '');
$where = [['delete_time', '=', null]];
if ($mainDomain) {
$where[] = ['main_domain', 'like', "%$mainDomain%"];
}
if ($status !== '' && $status !== null) {
$where[] = ['status', '=', $status];
}
$list = SystemDomainPool::where($where)
->page($page, $pageSize)
->order('id', 'desc')
->select()
->toArray();
$total = SystemDomainPool::where($where)
->count();
return json([
'code' => 200,
'msg' => 'success',
'data' => [
'list' => $list,
'total' => $total
]
]);
}
/**
* 获取启用状态的主域名列表(供租户选择)
*/
public function getEnabledDomains()
{
$list = SystemDomainPool::where('status', 1)
->where('delete_time', null)
->select()
->toArray();
return json([
'code' => 200,
'msg' => 'success',
'data' => $list
]);
}
/**
* 创建主域名
*/
public function create(Request $request)
{
$mainDomain = $request->param('main_domain', '');
if (empty($mainDomain)) {
return json([
'code' => 400,
'msg' => '主域名不能为空'
]);
}
// 检查域名是否已存在
$exists = SystemDomainPool::where('main_domain', $mainDomain)
->where('delete_time', null)
->find();
if ($exists) {
return json([
'code' => 400,
'msg' => '该域名已存在'
]);
}
$now = date('Y-m-d H:i:s');
$id = SystemDomainPool::insertGetId([
'main_domain' => $mainDomain,
'status' => 1,
'create_time' => $now,
'update_time' => $now
]);
return json([
'code' => 200,
'msg' => '创建成功',
'data' => ['id' => $id]
]);
}
/**
* 编辑主域名
*/
public function update(Request $request)
{
$id = $request->param('id', 0, 'int');
$mainDomain = $request->param('main_domain', '');
$status = $request->param('status', null);
if ($id <= 0) {
return json([
'code' => 400,
'msg' => '参数错误'
]);
}
if (empty($mainDomain)) {
return json([
'code' => 400,
'msg' => '主域名不能为空'
]);
}
// 检查域名是否与其他记录重复
$exists = SystemDomainPool::where('main_domain', $mainDomain)
->where('id', '<>', $id)
->where('delete_time', null)
->find();
if ($exists) {
return json([
'code' => 400,
'msg' => '该域名已存在'
]);
}
$data = [
'main_domain' => $mainDomain,
'update_time' => date('Y-m-d H:i:s')
];
if ($status !== null) {
$data['status'] = $status;
}
SystemDomainPool::where('id', $id)->update($data);
return json([
'code' => 200,
'msg' => '更新成功'
]);
}
/**
* 删除主域名(软删除)
*/
public function delete($id)
{
if ($id <= 0) {
return json([
'code' => 400,
'msg' => '参数错误'
]);
}
SystemDomainPool::where('id', $id)
->update([
'delete_time' => date('Y-m-d H:i:s')
]);
return json([
'code' => 200,
'msg' => '删除成功'
]);
}
/**
* 切换主域名状态
*/
public function toggleStatus(Request $request)
{
$id = $request->param('id', 0, 'int');
if ($id <= 0) {
return json([
'code' => 400,
'msg' => '参数错误'
]);
}
$domain = SystemDomainPool::where('id', $id)
->find();
if (!$domain) {
return json([
'code' => 404,
'msg' => '域名不存在'
]);
}
$newStatus = $domain['status'] == 1 ? 0 : 1;
SystemDomainPool::where('id', $id)
->update([
'status' => $newStatus,
'update_time' => date('Y-m-d H:i:s')
]);
return json([
'code' => 200,
'msg' => '状态更新成功',
'data' => ['status' => $newStatus]
]);
}
}