0489da5e40
- 新增 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>
205 lines
4.9 KiB
PHP
205 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace app\model;
|
|
|
|
use support\Model;
|
|
|
|
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',
|
|
'report_to',
|
|
'name',
|
|
'avatar',
|
|
'sex',
|
|
'email',
|
|
'phone',
|
|
'password',
|
|
'status',
|
|
'user_type',
|
|
'union_id',
|
|
// 废弃字段 - 请使用 UserFeishuApp 模型
|
|
'feishu_user_id', // @deprecated
|
|
'feishu_open_id', // @deprecated
|
|
'tenant_key', // @deprecated
|
|
// 保留字段:记录最后登录的应用,用于快速获取默认应用
|
|
'feishu_app_id',
|
|
'position',
|
|
'sign_img',
|
|
];
|
|
|
|
protected $casts = [
|
|
'company_id' => 'integer',
|
|
'report_to' => 'integer',
|
|
'sex' => 'integer',
|
|
'status' => 'integer',
|
|
'user_type' => 'integer',
|
|
'created_at' => 'datetime',
|
|
'updated_at' => 'datetime',
|
|
];
|
|
|
|
protected $hidden = [
|
|
'password',
|
|
];
|
|
|
|
/**
|
|
* 关联公司
|
|
*/
|
|
public function company()
|
|
{
|
|
return $this->belongsTo(Company::class, 'company_id');
|
|
}
|
|
|
|
/**
|
|
* 上级用户
|
|
*/
|
|
public function parent()
|
|
{
|
|
return $this->belongsTo(User::class, 'report_to');
|
|
}
|
|
|
|
/**
|
|
* 下级用户
|
|
*/
|
|
public function children()
|
|
{
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* 获取启用的用户
|
|
*/
|
|
public function scopeActive($query)
|
|
{
|
|
return $query->where('status', 1);
|
|
}
|
|
|
|
/**
|
|
* 根据公司ID查找
|
|
*/
|
|
public function scopeByCompany($query, int $companyId)
|
|
{
|
|
return $query->where('company_id', $companyId);
|
|
}
|
|
|
|
/**
|
|
* 根据飞书用户ID查找
|
|
* @deprecated 请通过 UserFeishuApp 模型查询
|
|
*/
|
|
public function scopeByFeishuUserId($query, ?string $feishuUserId)
|
|
{
|
|
if ($feishuUserId === null) {
|
|
return $query;
|
|
}
|
|
return $query->where('feishu_user_id', $feishuUserId);
|
|
}
|
|
|
|
/**
|
|
* 根据飞书 UnionID 查找
|
|
* union_id 用于跨应用识别用户,保留此方法
|
|
*/
|
|
public function scopeByFeishuUnionId($query, ?string $feishuUnionId)
|
|
{
|
|
if ($feishuUnionId === null) {
|
|
return $query;
|
|
}
|
|
return $query->where('union_id', $feishuUnionId);
|
|
}
|
|
|
|
/**
|
|
* 根据OpenID查找
|
|
* @deprecated 请通过 UserFeishuApp 模型查询
|
|
*/
|
|
public function scopeByFeishuOpenId($query, ?string $openId)
|
|
{
|
|
if ($openId === null) {
|
|
return $query;
|
|
}
|
|
return $query->where('feishu_open_id', $openId);
|
|
}
|
|
|
|
/**
|
|
* 搜索用户
|
|
*/
|
|
public function scopeSearch($query, ?string $keyword)
|
|
{
|
|
if (!$keyword) {
|
|
return $query;
|
|
}
|
|
|
|
return $query->where(function ($q) use ($keyword) {
|
|
$q->where('name', 'like', "%{$keyword}%")
|
|
->orWhere('email', 'like', "%{$keyword}%")
|
|
->orWhere('phone', 'like', "%{$keyword}%")
|
|
->orWhere('sn', 'like', "%{$keyword}%");
|
|
});
|
|
}
|
|
}
|