Files
x-server-mobile/backend/app/model/User.php
T
root a88944b5e1 refactor: 删除 users 表中未使用的字段
- 删除 report_to(上级ID)字段
- 删除 position(职位)字段
- 更新 User 模型移除相关关联和类型转换
- 更新 BusinessTripService 移除 position 返回

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-26 16:56:51 +08:00

152 lines
3.5 KiB
PHP

<?php
namespace app\model;
use support\Model;
class User extends Model
{
protected $table = 'users';
/**
* 可填充字段
*/
protected $fillable = [
'company_id',
'sn',
'name',
'avatar',
'sex',
'email',
'phone',
'password',
'status',
'user_type',
'union_id', // 飞书 UnionID - 用于跨应用识别用户
'feishu_app_id', // 最后登录的飞书应用ID - 用于快速获取默认应用
'sign_img',
];
protected $casts = [
'company_id' => '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 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);
}
/**
* 根据飞书 UnionID 查找
* union_id 用于跨应用识别用户
*/
public function scopeByFeishuUnionId($query, ?string $feishuUnionId)
{
if ($feishuUnionId === null) {
return $query;
}
return $query->where('union_id', $feishuUnionId);
}
/**
* 搜索用户
*/
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}%");
});
}
}