refactor: 重构多飞书应用绑定架构和数据库清理
- 新增 user_feishu_apps 表支持用户绑定多个飞书应用 - users 表添加唯一约束(公司内邮箱/手机唯一) - 迁移现有飞书数据到 user_feishu_apps 表 - 清理冗余表(users_bak, user_feishu_binding) - 废弃 users 表中的 feishu_user_id/feishu_open_id/tenant_key 字段 - 更新 FeishuNotificationService 使用新的数据获取方式 - User 模型新增 getFeishuUserId() 等便捷方法 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -27,7 +27,7 @@ class FeishuNotificationService
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置飞书应用ID(支持多应用)
|
||||
* 设置飞书应用ID(必须指定)
|
||||
*/
|
||||
public function setApp(int $appId): self
|
||||
{
|
||||
@@ -36,7 +36,29 @@ class FeishuNotificationService
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取飞书应用配置
|
||||
* 根据用户ID自动设置其最后登录的应用
|
||||
*/
|
||||
public function setAppByUser(int $userId): self
|
||||
{
|
||||
$user = \app\model\User::find($userId);
|
||||
if (!$user) {
|
||||
throw new Exception('用户不存在');
|
||||
}
|
||||
|
||||
$this->setCompany($user->company_id);
|
||||
|
||||
// 使用用户最后登录的应用
|
||||
if ($user->feishu_app_id) {
|
||||
$this->appId = $user->feishu_app_id;
|
||||
} else {
|
||||
throw new Exception('用户未绑定飞书应用,请先通过飞书登录');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取飞书应用配置(必须先通过 setApp 或 setAppByUser 指定)
|
||||
*/
|
||||
private function getAppConfig(): array
|
||||
{
|
||||
@@ -44,22 +66,17 @@ class FeishuNotificationService
|
||||
throw new Exception('未设置公司ID');
|
||||
}
|
||||
|
||||
if ($this->appId) {
|
||||
$app = \app\model\CompanyFeishuApp::find($this->appId);
|
||||
if (!$app || $app->status !== 1) {
|
||||
throw new Exception('飞书应用配置不存在');
|
||||
}
|
||||
return $app->toArray();
|
||||
if (!$this->appId) {
|
||||
throw new Exception('未指定飞书应用,请通过 setApp() 或 setAppByUser() 指定');
|
||||
}
|
||||
|
||||
$app = \app\model\CompanyFeishuApp::where('company_id', $this->companyId)
|
||||
->where('system_id', 1)
|
||||
->where('id', $this->appId)
|
||||
->where('status', 1)
|
||||
->orderBy('sort')
|
||||
->first();
|
||||
|
||||
if (!$app) {
|
||||
throw new Exception('未配置飞书应用');
|
||||
throw new Exception('指定的飞书应用不存在或未启用');
|
||||
}
|
||||
|
||||
return $app->toArray();
|
||||
@@ -74,6 +91,23 @@ class FeishuNotificationService
|
||||
return FeishuUtil::getAppAccessToken($app['id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户在指定飞书应用下的 feishu_user_id
|
||||
*/
|
||||
private function getFeishuUserId(int $userId, int $companyId): ?string
|
||||
{
|
||||
if (!$this->appId) {
|
||||
throw new Exception('未指定飞书应用,请先调用 setApp() 或 setAppByUser()');
|
||||
}
|
||||
|
||||
$binding = \app\model\UserFeishuApp::where('user_id', $userId)
|
||||
->where('company_id', $companyId)
|
||||
->where('feishu_app_id', $this->appId)
|
||||
->first();
|
||||
|
||||
return $binding ? $binding->feishu_user_id : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送审批通知
|
||||
*
|
||||
@@ -98,13 +132,20 @@ class FeishuNotificationService
|
||||
|
||||
try {
|
||||
$user = User::find($userId);
|
||||
if (!$user || !$user->feishu_user_id) {
|
||||
error_log("❌ sendApprovalNotification: 用户不存在或未绑定飞书, user_id={$userId}");
|
||||
if (!$user) {
|
||||
error_log("❌ sendApprovalNotification: 用户不存在, user_id={$userId}");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 从 user_feishu_apps 获取指定应用下的 feishu_user_id
|
||||
$feishuUserId = $this->getFeishuUserId($userId, $user->company_id);
|
||||
if (!$feishuUserId) {
|
||||
error_log("❌ sendApprovalNotification: 用户未绑定飞书应用, user_id={$userId}");
|
||||
return false;
|
||||
}
|
||||
|
||||
$card = $this->buildApprovalCard($params);
|
||||
return $this->sendMessage($user->feishu_user_id, $card);
|
||||
return $this->sendMessage($feishuUserId, $card);
|
||||
} catch (Exception $e) {
|
||||
error_log('❌ sendApprovalNotification 失败: ' . $e->getMessage());
|
||||
return false;
|
||||
@@ -133,13 +174,20 @@ class FeishuNotificationService
|
||||
foreach ($userIds as $userId) {
|
||||
try {
|
||||
$user = User::find($userId);
|
||||
if (!$user || !$user->feishu_user_id) {
|
||||
if (!$user) {
|
||||
$results[$userId] = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
// 从 user_feishu_apps 获取指定应用下的 feishu_user_id
|
||||
$feishuUserId = $this->getFeishuUserId($userId, $user->company_id);
|
||||
if (!$feishuUserId) {
|
||||
$results[$userId] = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
$card = $this->buildSystemCard($params);
|
||||
$results[$userId] = $this->sendMessage($user->feishu_user_id, $card);
|
||||
$results[$userId] = $this->sendMessage($feishuUserId, $card);
|
||||
} catch (Exception $e) {
|
||||
error_log("❌ sendSystemNotification 失败: user_id={$userId}, error=" . $e->getMessage());
|
||||
$results[$userId] = false;
|
||||
@@ -170,13 +218,20 @@ class FeishuNotificationService
|
||||
|
||||
try {
|
||||
$user = User::find($userId);
|
||||
if (!$user || !$user->feishu_user_id) {
|
||||
error_log("❌ sendReminderNotification: 用户不存在或未绑定飞书, user_id={$userId}");
|
||||
if (!$user) {
|
||||
error_log("❌ sendReminderNotification: 用户不存在, user_id={$userId}");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 从 user_feishu_apps 获取指定应用下的 feishu_user_id
|
||||
$feishuUserId = $this->getFeishuUserId($userId, $user->company_id);
|
||||
if (!$feishuUserId) {
|
||||
error_log("❌ sendReminderNotification: 用户未绑定飞书应用, user_id={$userId}");
|
||||
return false;
|
||||
}
|
||||
|
||||
$card = $this->buildReminderCard($params);
|
||||
return $this->sendMessage($user->feishu_user_id, $card);
|
||||
return $this->sendMessage($feishuUserId, $card);
|
||||
} catch (Exception $e) {
|
||||
error_log('❌ sendReminderNotification 失败: ' . $e->getMessage());
|
||||
return false;
|
||||
|
||||
@@ -123,11 +123,17 @@ class FeishuService
|
||||
/**
|
||||
* 创建或更新用户
|
||||
* 注意:同一用户在不同企业的账号是独立的,通过 company_id + union_id 组合查找
|
||||
*
|
||||
* @param array $feishuData 飞书用户数据
|
||||
* @param int $companyId 公司ID
|
||||
* @param int $appId 飞书应用配置ID
|
||||
* @return User
|
||||
*/
|
||||
public function createOrUpdateUser(array $feishuData, int $companyId): User
|
||||
public function createOrUpdateUser(array $feishuData, int $companyId, int $appId): User
|
||||
{
|
||||
$unionId = $feishuData['union_id'] ?? null;
|
||||
$userId = $feishuData['user_id'] ?? null;
|
||||
$feishuUserId = $feishuData['user_id'] ?? null;
|
||||
$openId = $feishuData['open_id'] ?? null;
|
||||
$name = $feishuData['name'] ?? '未知';
|
||||
$avatar = $feishuData['avatar_url'] ?? null;
|
||||
$email = $feishuData['email'] ?? null;
|
||||
@@ -136,8 +142,17 @@ class FeishuService
|
||||
throw new Exception('缺少 union_id');
|
||||
}
|
||||
|
||||
// 获取飞书应用配置
|
||||
$app = CompanyFeishuApp::where('company_id', $companyId)
|
||||
->where('id', $appId)
|
||||
->where('status', 1)
|
||||
->first();
|
||||
|
||||
if (!$app) {
|
||||
throw new Exception('飞书应用配置不存在或未启用');
|
||||
}
|
||||
|
||||
// ⭐ 关键:通过 company_id + union_id 组合查找用户
|
||||
// 这样同一个 union_id 在不同公司的账号是独立的
|
||||
$user = User::where('company_id', $companyId)
|
||||
->where('union_id', $unionId)
|
||||
->first();
|
||||
@@ -146,16 +161,16 @@ class FeishuService
|
||||
error_log("✨ 创建新用户: company={$companyId}, union_id={$unionId}, name={$name}");
|
||||
|
||||
// 初始密码使用 sn(工号),如果没有 sn 则使用 union_id 的前6位
|
||||
$initialPassword = $feishuData['sn'] ?? substr($unionId, 0, 6);
|
||||
$initialPassword = $feishuData['employee_no'] ?? substr($unionId, 0, 6);
|
||||
|
||||
$user = User::create([
|
||||
'company_id' => $companyId,
|
||||
'feishu_user_id' => $userId,
|
||||
'union_id' => $unionId,
|
||||
'name' => $name,
|
||||
'email' => $email,
|
||||
'avatar' => $avatar,
|
||||
'password' => password_hash($initialPassword, PASSWORD_DEFAULT), // 初始密码加密
|
||||
'feishu_app_id' => $appId, // 记录登录来源应用
|
||||
'status' => 1, // 新用户默认在职
|
||||
]);
|
||||
error_log("🔑 初始密码设置为 sn: {$initialPassword}");
|
||||
@@ -168,22 +183,57 @@ class FeishuService
|
||||
throw new Exception('该员工已离职,无法登录系统');
|
||||
}
|
||||
|
||||
// 更新用户信息(在职员工)
|
||||
$updateData = [
|
||||
'feishu_user_id' => $userId,
|
||||
// 更新用户信息和登录来源应用
|
||||
$user->update([
|
||||
'name' => $name,
|
||||
'avatar' => $avatar,
|
||||
'email' => $email,
|
||||
];
|
||||
|
||||
$user->update($updateData);
|
||||
'feishu_app_id' => $appId,
|
||||
]);
|
||||
}
|
||||
|
||||
// ⭐ 保存或更新用户飞书应用绑定关系
|
||||
$this->saveUserFeishuBinding($user->id, $companyId, $appId, $feishuUserId, $openId, $app->tenant_key);
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存或更新用户飞书应用绑定关系
|
||||
*/
|
||||
private function saveUserFeishuBinding(int $userId, int $companyId, int $appId, ?string $feishuUserId, ?string $openId, ?string $tenantKey): void
|
||||
{
|
||||
$binding = \app\model\UserFeishuApp::where('user_id', $userId)
|
||||
->where('company_id', $companyId)
|
||||
->where('feishu_app_id', $appId)
|
||||
->first();
|
||||
|
||||
$data = [
|
||||
'feishu_user_id' => $feishuUserId,
|
||||
'feishu_open_id' => $openId,
|
||||
'tenant_key' => $tenantKey,
|
||||
'last_login_at' => date('Y-m-d H:i:s'),
|
||||
];
|
||||
|
||||
if ($binding) {
|
||||
// 更新现有绑定
|
||||
$binding->update($data);
|
||||
error_log("🔄 更新飞书绑定: user_id={$userId}, app_id={$appId}, feishu_user_id={$feishuUserId}");
|
||||
} else {
|
||||
// 创建新绑定
|
||||
\app\model\UserFeishuApp::create([
|
||||
'user_id' => $userId,
|
||||
'company_id' => $companyId,
|
||||
'feishu_app_id' => $appId,
|
||||
'is_default' => 0, // 后续可由用户或管理员设置
|
||||
] + $data);
|
||||
error_log("✨ 创建飞书绑定: user_id={$userId}, app_id={$appId}, feishu_user_id={$feishuUserId}");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送审批消息通知
|
||||
* @deprecated 请使用 FeishuNotificationService::sendApprovalNotification() 代替
|
||||
*/
|
||||
public function sendApprovalMessage(int $userId, string $title, string $url): bool
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user