feat: add multi-company Feishu integration and data migration
- Add companies table enhancements (abbr, tenant_key, feishu_enabled) - Add users table enhancements (feishu fields, sn, report_to, etc.) - Create company_feishu_apps table for multi-app per company - Create user_sn_pool table for user number management - Update FeishuService to support multiple Feishu apps per company - Add data migration script (002_merge_old_data.sql) - Add import script (scripts/import_old_data.php) - Update User and Company models with new scopes and relationships - Add CompanyFeishuApp model - Add Git workflow documentation (GIT_WORKFLOW.md) This supports: - Multi-company architecture with Feishu integration - Multiple Feishu apps per company (X-Server, Ticket, CRM) - User number assignment system - Data migration from old project Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,12 +4,14 @@ namespace App\Service;
|
||||
|
||||
use App\Model\Company;
|
||||
use App\Model\User;
|
||||
use App\Util\Db;
|
||||
use support\Cache;
|
||||
|
||||
class FeishuService
|
||||
{
|
||||
private ?string $accessToken = null;
|
||||
private ?int $companyId = null;
|
||||
private ?int $appId = null;
|
||||
|
||||
/**
|
||||
* 设置当前操作的公司
|
||||
@@ -21,12 +23,72 @@ class FeishuService
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置飞书应用ID(支持多应用)
|
||||
*/
|
||||
public function setApp(int $appId): self
|
||||
{
|
||||
$this->appId = $appId;
|
||||
$this->accessToken = null;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 tenant_key 获取公司配置
|
||||
*/
|
||||
public function getCompanyByTenantKey(string $tenantKey): ?Company
|
||||
{
|
||||
return Company::active()->byFeishuTenantKey($tenantKey)->first();
|
||||
return Company::active()->byTenantKey($tenantKey)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取公司的飞书应用配置
|
||||
*/
|
||||
private function getFeishuApp(int $appId): array
|
||||
{
|
||||
$sql = "SELECT * FROM company_feishu_apps WHERE id = ? AND status = 1";
|
||||
$stmt = Db::connection()->getPdo()->prepare($sql);
|
||||
$stmt->execute([$appId]);
|
||||
$app = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$app) {
|
||||
throw new \Exception('飞书应用配置不存在');
|
||||
}
|
||||
|
||||
return $app;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取公司的默认飞书应用
|
||||
*/
|
||||
private function getDefaultFeishuApp(int $companyId): array
|
||||
{
|
||||
$sql = "SELECT * FROM company_feishu_apps WHERE company_id = ? AND system_id = 1 AND status = 1 ORDER BY sort ASC LIMIT 1";
|
||||
$stmt = Db::connection()->getPdo()->prepare($sql);
|
||||
$stmt->execute([$companyId]);
|
||||
$app = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$app) {
|
||||
throw new \Exception('未配置飞书应用');
|
||||
}
|
||||
|
||||
return $app;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取飞书应用配置(优先使用指定应用,否则使用默认应用)
|
||||
*/
|
||||
private function getAppConfig(): array
|
||||
{
|
||||
if (!$this->companyId) {
|
||||
throw new \Exception('未设置公司ID');
|
||||
}
|
||||
|
||||
if ($this->appId) {
|
||||
return $this->getFeishuApp($this->appId);
|
||||
}
|
||||
|
||||
return $this->getDefaultFeishuApp($this->companyId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,14 +100,10 @@ class FeishuService
|
||||
return $this->accessToken;
|
||||
}
|
||||
|
||||
if (!$this->companyId) {
|
||||
throw new \Exception('未设置公司ID');
|
||||
}
|
||||
|
||||
$company = Company::findOrFail($this->companyId);
|
||||
$app = $this->getAppConfig();
|
||||
|
||||
// 从缓存获取
|
||||
$cacheKey = "feishu:access_token:{$company->id}";
|
||||
$cacheKey = "feishu:access_token:{$app['id']}";
|
||||
$token = Cache::get($cacheKey);
|
||||
|
||||
if ($token) {
|
||||
@@ -55,8 +113,8 @@ class FeishuService
|
||||
|
||||
// 请求新的 token
|
||||
$response = $this->httpPost('https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal', [
|
||||
'app_id' => $company->feishu_app_id,
|
||||
'app_secret' => $company->feishu_app_secret,
|
||||
'app_id' => $app['app_id'],
|
||||
'app_secret' => $app['app_secret'],
|
||||
]);
|
||||
|
||||
if ($response['code'] !== 0) {
|
||||
@@ -78,17 +136,14 @@ class FeishuService
|
||||
*/
|
||||
public function getUserInfo(string $authCode): array
|
||||
{
|
||||
if (!$this->companyId) {
|
||||
throw new \Exception('未设置公司ID');
|
||||
}
|
||||
|
||||
$app = $this->getAppConfig();
|
||||
$token = $this->getAccessToken();
|
||||
|
||||
// 获取用户 access_token
|
||||
$response = $this->httpPost('https://open.feishu.cn/open-apis/authen/v1/oidc/access_token', [
|
||||
'grant_type' => 'authorization_code',
|
||||
'client_id' => Company::findOrFail($this->companyId)->feishu_app_id,
|
||||
'client_secret' => Company::findOrFail($this->companyId)->feishu_app_secret,
|
||||
'client_id' => $app['app_id'],
|
||||
'client_secret' => $app['app_secret'],
|
||||
'code' => $authCode,
|
||||
], [
|
||||
'Authorization: Bearer ' . $token,
|
||||
|
||||
Reference in New Issue
Block a user