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:
root
2026-02-26 16:27:33 +08:00
parent 04947ae52d
commit 0489da5e40
11 changed files with 942 additions and 45 deletions
+73 -3
View File
@@ -8,6 +8,14 @@ class User extends Model
{
protected $table = 'users';
/**
* 可填充字段
*
* 注意:以下字段已废弃,保留仅用于向后兼容
* @deprecated feishu_user_id - 请使用 UserFeishuApp 模型获取
* @deprecated feishu_open_id - 请使用 UserFeishuApp 模型获取
* @deprecated tenant_key - 请使用 UserFeishuApp 模型获取
*/
protected $fillable = [
'company_id',
'sn',
@@ -21,9 +29,12 @@ class User extends Model
'status',
'user_type',
'union_id',
'feishu_user_id',
'feishu_open_id',
'tenant_key',
// 废弃字段 - 请使用 UserFeishuApp 模型
'feishu_user_id', // @deprecated
'feishu_open_id', // @deprecated
'tenant_key', // @deprecated
// 保留字段:记录最后登录的应用,用于快速获取默认应用
'feishu_app_id',
'position',
'sign_img',
];
@@ -66,6 +77,62 @@ class User extends Model
return $this->hasMany(User::class, 'report_to');
}
/**
* 用户飞书应用绑定
*/
public function feishuApps()
{
return $this->hasMany(UserFeishuApp::class, 'user_id');
}
/**
* 最后登录的飞书应用
*/
public function lastFeishuApp()
{
return $this->belongsTo(CompanyFeishuApp::class, 'feishu_app_id');
}
/**
* 获取用户在当前公司的默认飞书应用
*/
public function getDefaultFeishuApp()
{
return $this->hasOne(UserFeishuApp::class, 'user_id')
->where('company_id', $this->company_id)
->where('is_default', 1);
}
/**
* 获取用户在指定飞书应用下的绑定信息
*/
public function feishuAppBinding(int $feishuAppId)
{
return $this->hasOne(UserFeishuApp::class, 'user_id')
->where('company_id', $this->company_id)
->where('feishu_app_id', $feishuAppId);
}
/**
* 获取用户在指定飞书应用下的 feishu_user_id
*/
public function getFeishuUserId(int $feishuAppId): ?string
{
$binding = $this->feishuAppBinding($feishuAppId)->first();
return $binding ? $binding->feishu_user_id : null;
}
/**
* 获取用户在最后登录应用下的 feishu_user_id
*/
public function getLastFeishuUserId(): ?string
{
if (!$this->feishu_app_id) {
return null;
}
return $this->getFeishuUserId($this->feishu_app_id);
}
/**
* 获取启用的用户
*/
@@ -84,6 +151,7 @@ class User extends Model
/**
* 根据飞书用户ID查找
* @deprecated 请通过 UserFeishuApp 模型查询
*/
public function scopeByFeishuUserId($query, ?string $feishuUserId)
{
@@ -95,6 +163,7 @@ class User extends Model
/**
* 根据飞书 UnionID 查找
* union_id 用于跨应用识别用户,保留此方法
*/
public function scopeByFeishuUnionId($query, ?string $feishuUnionId)
{
@@ -106,6 +175,7 @@ class User extends Model
/**
* 根据OpenID查找
* @deprecated 请通过 UserFeishuApp 模型查询
*/
public function scopeByFeishuOpenId($query, ?string $openId)
{