171 lines
4.5 KiB
PHP
171 lines
4.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\admin\controller\Tenant;
|
|
|
|
use app\admin\BaseController;
|
|
use think\exception\ValidateException;
|
|
use think\facade\Db;
|
|
use think\facade\Session;
|
|
use think\response\Json;
|
|
use think\db\exception\DbException;
|
|
use think\Request;
|
|
use app\model\Tenant\Tenant;
|
|
use app\model\AdminUser;
|
|
use app\model\Template\TemplateSiteConfig;
|
|
use app\model\Template\TemplateSiteConfig;
|
|
|
|
class TenantController extends BaseController
|
|
{
|
|
/**
|
|
* 获取租户列表
|
|
*/
|
|
public function getTenant(Request $request)
|
|
{
|
|
// 获取参数
|
|
$page = $request->param('page', 1, 'int');
|
|
$pageSize = $request->param('pageSize', 10, 'int');
|
|
$tenantName = $request->param('tenant_name', '');
|
|
$tenantCode = $request->param('tenant_code', '');
|
|
$contactPerson = $request->param('contact_person', '');
|
|
$contactPhone = $request->param('contact_phone', '');
|
|
|
|
$where = [['delete_time', '=', null]];
|
|
|
|
// 动态增加模糊搜索条件
|
|
if ($tenantName) $where[] = ['tenant_name', 'like', "%$tenantName%"];
|
|
if ($tenantCode) $where[] = ['tenant_code', 'like', "%$tenantCode%"];
|
|
if ($contactPerson) $where[] = ['contact_person', 'like', "%$contactPerson%"];
|
|
if ($contactPhone) $where[] = ['contact_phone', 'like', "%$contactPhone%"];
|
|
|
|
$list = Tenant::where($where)->page($page, $pageSize)->select()->toArray();
|
|
$total = Tenant::where($where)->count();
|
|
|
|
return json(['code' => 200, 'data' => ['list' => $list, 'total' => $total]]);
|
|
}
|
|
|
|
/**
|
|
* 获取租户详情
|
|
*/
|
|
public function getTenantDetail($id)
|
|
{
|
|
$detail = Tenant::where('id', $id)->where('delete_time', null)->find()->toArray();
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '获取成功',
|
|
'data' => $detail
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 创建租户
|
|
*/
|
|
public function createTenant()
|
|
{
|
|
$data = $this->request->post();
|
|
$tenant = Tenant::create($data);
|
|
if ($tenant) {
|
|
// 创建租户默认数据
|
|
$this->createTenantDefaultData($tenant->id);
|
|
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '创建成功',
|
|
'data' => $tenant
|
|
]);
|
|
} else {
|
|
return json([
|
|
'code' => 500,
|
|
'msg' => '创建失败',
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 创建租户默认数据
|
|
* @param int $tenantId 租户ID
|
|
*/
|
|
private function createTenantDefaultData(int $tenantId)
|
|
{
|
|
$now = date('Y-m-d H:i:s');
|
|
|
|
// 创建租户默认模板配置
|
|
TemplateSiteConfig::create([
|
|
'tid' => $tenantId,
|
|
'key' => 'current_theme',
|
|
'value' => 'default',
|
|
'create_time' => $now,
|
|
'update_time' => $now
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 编辑租户
|
|
*/
|
|
public function editTenant($id)
|
|
{
|
|
$data = $this->request->post();
|
|
$tenant = Tenant::where('id', $id)->update($data);
|
|
if ($tenant) {
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '编辑成功',
|
|
'data' => $tenant
|
|
]);
|
|
} else {
|
|
return json([
|
|
'code' => 500,
|
|
'msg' => '编辑失败',
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除租户
|
|
*/
|
|
public function deleteTenant($id)
|
|
{
|
|
$tenant = Tenant::where('id', $id)->update(['delete_time' => date('Y-m-d H:i:s')]);
|
|
if ($tenant) {
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '删除成功',
|
|
]);
|
|
} else {
|
|
return json([
|
|
'code' => 500,
|
|
'msg' => '删除失败',
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 查询租户编码重复(不筛选删除数据)
|
|
*/
|
|
public function findTenantCode(Request $request)
|
|
{
|
|
$tenantCode = $request->param('tenant_code', '');
|
|
|
|
if (!$tenantCode) {
|
|
return json(['code' => 400, 'msg' => '缺少编码参数']);
|
|
}
|
|
|
|
// 直接去表里查,只要有一条一样的就返回重复
|
|
$hasOne = Tenant::where('tenant_code', $tenantCode)->find();
|
|
|
|
if ($hasOne) {
|
|
return json([
|
|
'code' => 201,
|
|
'msg' => '编码已存在',
|
|
'data' => $hasOne
|
|
]);
|
|
}
|
|
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '编码可用'
|
|
]);
|
|
}
|
|
}
|