2026-02-24 17:57:25 +08:00
|
|
|
<?php
|
|
|
|
|
|
2026-02-25 18:09:04 +08:00
|
|
|
namespace app\model;
|
2026-02-24 17:57:25 +08:00
|
|
|
|
2026-02-25 15:22:12 +08:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2026-02-24 17:57:25 +08:00
|
|
|
|
|
|
|
|
class User extends Model
|
|
|
|
|
{
|
2026-02-24 18:12:18 +08:00
|
|
|
protected $table = 'users';
|
2026-02-24 17:57:25 +08:00
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'company_id',
|
2026-02-24 18:12:18 +08:00
|
|
|
'sn',
|
|
|
|
|
'report_to',
|
2026-02-24 17:57:25 +08:00
|
|
|
'name',
|
2026-02-24 18:12:18 +08:00
|
|
|
'avatar',
|
|
|
|
|
'sex',
|
2026-02-24 17:57:25 +08:00
|
|
|
'email',
|
|
|
|
|
'phone',
|
2026-02-24 18:12:18 +08:00
|
|
|
'password',
|
2026-02-24 17:57:25 +08:00
|
|
|
'status',
|
2026-02-24 18:12:18 +08:00
|
|
|
'user_type',
|
|
|
|
|
'union_id',
|
|
|
|
|
'feishu_user_id',
|
|
|
|
|
'feishu_open_id',
|
|
|
|
|
'tenant_key',
|
|
|
|
|
'position',
|
|
|
|
|
'sign_img',
|
2026-02-24 17:57:25 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'company_id' => 'integer',
|
2026-02-24 18:12:18 +08:00
|
|
|
'report_to' => 'integer',
|
|
|
|
|
'sex' => 'integer',
|
2026-02-24 17:57:25 +08:00
|
|
|
'status' => 'integer',
|
2026-02-24 18:12:18 +08:00
|
|
|
'user_type' => 'integer',
|
2026-02-24 17:57:25 +08:00
|
|
|
'created_at' => 'datetime',
|
|
|
|
|
'updated_at' => 'datetime',
|
|
|
|
|
];
|
|
|
|
|
|
2026-02-24 18:12:18 +08:00
|
|
|
protected $hidden = [
|
|
|
|
|
'password',
|
|
|
|
|
];
|
|
|
|
|
|
2026-02-24 17:57:25 +08:00
|
|
|
/**
|
|
|
|
|
* 关联公司
|
|
|
|
|
*/
|
|
|
|
|
public function company()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Company::class, 'company_id');
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-24 18:12:18 +08:00
|
|
|
/**
|
|
|
|
|
* 上级用户
|
|
|
|
|
*/
|
|
|
|
|
public function parent()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class, 'report_to');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 下级用户
|
|
|
|
|
*/
|
|
|
|
|
public function children()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(User::class, 'report_to');
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-24 17:57:25 +08:00
|
|
|
/**
|
|
|
|
|
* 获取启用的用户
|
|
|
|
|
*/
|
|
|
|
|
public function scopeActive($query)
|
|
|
|
|
{
|
|
|
|
|
return $query->where('status', 1);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-24 18:12:18 +08:00
|
|
|
/**
|
|
|
|
|
* 根据公司ID查找
|
|
|
|
|
*/
|
|
|
|
|
public function scopeByCompany($query, int $companyId)
|
|
|
|
|
{
|
|
|
|
|
return $query->where('company_id', $companyId);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-24 17:57:25 +08:00
|
|
|
/**
|
|
|
|
|
* 根据飞书用户ID查找
|
|
|
|
|
*/
|
2026-02-24 18:12:18 +08:00
|
|
|
public function scopeByFeishuUserId($query, ?string $feishuUserId)
|
2026-02-24 17:57:25 +08:00
|
|
|
{
|
2026-02-24 18:12:18 +08:00
|
|
|
if ($feishuUserId === null) {
|
|
|
|
|
return $query;
|
|
|
|
|
}
|
2026-02-24 17:57:25 +08:00
|
|
|
return $query->where('feishu_user_id', $feishuUserId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据飞书 UnionID 查找
|
|
|
|
|
*/
|
2026-02-24 18:12:18 +08:00
|
|
|
public function scopeByFeishuUnionId($query, ?string $feishuUnionId)
|
|
|
|
|
{
|
|
|
|
|
if ($feishuUnionId === null) {
|
|
|
|
|
return $query;
|
|
|
|
|
}
|
|
|
|
|
return $query->where('union_id', $feishuUnionId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据OpenID查找
|
|
|
|
|
*/
|
|
|
|
|
public function scopeByFeishuOpenId($query, ?string $openId)
|
|
|
|
|
{
|
|
|
|
|
if ($openId === null) {
|
|
|
|
|
return $query;
|
|
|
|
|
}
|
|
|
|
|
return $query->where('feishu_open_id', $openId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 搜索用户
|
|
|
|
|
*/
|
|
|
|
|
public function scopeSearch($query, ?string $keyword)
|
2026-02-24 17:57:25 +08:00
|
|
|
{
|
2026-02-24 18:12:18 +08:00
|
|
|
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}%");
|
|
|
|
|
});
|
2026-02-24 17:57:25 +08:00
|
|
|
}
|
|
|
|
|
}
|