feat: 飞书登录支持多租户和用户工号管理

后端改动:
- FeishuService: 从飞书用户信息获取 tenant_key 和手机号
- FeishuService: 新增 user_sn 表工号分配机制
- FeishuService: 优先使用 enterprise_email,支持手机号
- AuthController: 飞书免登支持 company 参数

前端改动:
- FeishuAutoLogin: 支持多租户 company 参数
- login/index: 支持 redirect 参数和已登录检测
- QRCodeLogin: 支持多租户参数传递
- router: 新增路由守卫和参数处理
- api.ts: 优化飞书登录 API 调用

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
root
2026-02-27 17:57:49 +08:00
parent b1895be50d
commit b1ca2c58b7
8 changed files with 241 additions and 41 deletions
+65 -9
View File
@@ -64,6 +64,7 @@ class AuthController extends BaseController
$authCode = $request->post('auth_code');
$appId = $request->post('app_id');
$tenantKey = $request->post('tenant_key');
$companyParam = $request->post('company'); // 新增:company 参数
if (!$authCode) {
return $this->error('缺少授权码');
@@ -71,9 +72,38 @@ class AuthController extends BaseController
try {
$feishuService = new FeishuService();
$companyId = null;
$feishuApp = null;
// 方式1: 根据 app_id 查找飞书应用配置(推荐)
if ($appId) {
// 方式1: 根据 company 参数查找公司配置(新增,用于多公司支持)
if ($companyParam) {
error_log("🔍 使用 company 参数查找配置: {$companyParam}");
// company 参数是 company_id
$company = \app\model\Company::find($companyParam);
if (!$company) {
return $this->error('公司不存在');
}
$companyId = $company->id;
$tenantKey = $company->tenant_key; // 从公司表获取 tenant_key
// 获取该公司的默认飞书应用
$feishuApp = \app\model\CompanyFeishuApp::where('company_id', $companyId)
->where('system_id', 1)
->where('status', 1)
->first();
if (!$feishuApp) {
return $this->error('该公司未配置飞书应用');
}
error_log("✅ 通过 company 找到配置: company_id={$companyId}, tenant_key={$tenantKey}, feishu_app_id={$feishuApp->id}, app_id={$feishuApp->app_id}");
$feishuService->setCompany($companyId);
$appId = $feishuApp->id;
}
// 方式2: 根据 app_id 查找飞书应用配置
elseif ($appId) {
error_log("🔍 使用 app_id 查找配置: {$appId}");
// 根据 app_id 查找飞书应用
@@ -90,7 +120,7 @@ class AuthController extends BaseController
$feishuService->setCompany($companyId);
}
// 方式2: 根据 tenant_key 查找(兼容旧版)
// 方式3: 根据 tenant_key 查找(兼容旧版)
elseif ($tenantKey) {
error_log("🔍 使用 tenant_key 查找配置: {$tenantKey}");
@@ -117,7 +147,7 @@ class AuthController extends BaseController
// 设置应用ID
$appId = $feishuApp->id;
} else {
return $this->error('缺少 app_id 或 tenant_key 参数');
return $this->error('缺少 company、app_id 或 tenant_key 参数');
}
// 获取用户信息
@@ -174,16 +204,42 @@ class AuthController extends BaseController
{
$tenantKey = $request->post('tenant_key');
$appId = $request->post('app_id');
$companyParam = $request->post('company'); // 新增:company 参数
error_log("📱 qrcodeLogin - tenant_key: {$tenantKey}, app_id: {$appId}");
error_log("📱 qrcodeLogin - tenant_key: {$tenantKey}, app_id: {$appId}, company: {$companyParam}");
try {
$feishuService = new \app\service\FeishuService();
$companyId = null;
$feishuApp = null;
// 方式1: 根据 app_id 查找飞书应用配置(推荐)
if ($appId) {
// 方式1: 根据 company 参数查找公司配置(新增,用于多公司支持)
if ($companyParam) {
error_log("🔍 使用 company 参数查找配置: {$companyParam}");
// company 参数是 company_id
$company = \app\model\Company::find($companyParam);
if (!$company) {
return $this->error('公司不存在');
}
$companyId = $company->id;
$tenantKey = $company->tenant_key; // 从公司表获取 tenant_key
// 获取该公司的默认飞书应用
$feishuApp = \app\model\CompanyFeishuApp::where('company_id', $companyId)
->where('system_id', 1)
->where('status', 1)
->first();
if (!$feishuApp) {
return $this->error('该公司未配置飞书应用');
}
error_log("✅ 通过 company 找到配置: company_id={$companyId}, tenant_key={$tenantKey}, feishu_app_id={$feishuApp->id}, app_id={$feishuApp->app_id}");
}
// 方式2: 根据 app_id 查找飞书应用配置(推荐)
elseif ($appId) {
$feishuApp = \app\model\CompanyFeishuApp::where('app_id', $appId)
->where('status', 1)
->first();
@@ -195,7 +251,7 @@ class AuthController extends BaseController
$companyId = $feishuApp->company_id;
error_log("✅ 通过 app_id 找到配置: company_id={$companyId}");
}
// 方式2: 根据 tenant_key 查找(兼容旧版)
// 方式3: 根据 tenant_key 查找(兼容旧版)
elseif ($tenantKey) {
$company = $feishuService->getCompanyByTenantKey($tenantKey);
if (!$company) {
@@ -216,7 +272,7 @@ class AuthController extends BaseController
error_log("✅ 通过 tenant_key 找到配置: company_id={$companyId}");
} else {
return $this->error('缺少 app_id 或 tenant_key 参数');
return $this->error('缺少 company、app_id 或 tenant_key 参数');
}
$feishuService->setCompany($companyId);