获取fdesk对应的用户id

This commit is contained in:
2026-03-03 14:50:41 +08:00
parent 3f0f5334e8
commit ae9b82e683
4 changed files with 158 additions and 5 deletions
+33
View File
@@ -3,6 +3,7 @@
namespace app\controller;
use app\model\User;
use app\model\UserSystemMapping;
use Webman\Http\Request;
use Webman\Http\Response;
@@ -130,4 +131,36 @@ class UserController extends BaseController
'page_size' => $pageSize,
]);
}
/**
* 获取用户在指定系统的映射ID
*/
public function systemMapping(Request $request): Response
{
$userId = $this->getUserId($request);
$systemCode = $request->get('system', 'fdesk');
// 获取用户信息以获取公司ID
$user = User::find($userId);
if (!$user) {
return $this->error('用户不存在');
}
// 查询系统映射
$mapping = UserSystemMapping::active()
->bySystem($systemCode)
->where('user_id', $userId)
->where('company_id', $user->company_id)
->first();
if (!$mapping) {
return $this->error('未找到系统映射信息,请联系管理员配置');
}
return $this->success([
'system_code' => $mapping->system_code,
'system_user_id' => $mapping->system_user_id,
'system_user_name' => $mapping->system_user_name,
]);
}
}
+89
View File
@@ -0,0 +1,89 @@
<?php
namespace app\model;
use support\Model;
class UserSystemMapping extends Model
{
protected $table = 'user_system_mapping';
/**
* 可填充字段
*/
protected $fillable = [
'user_id',
'company_id',
'system_code',
'system_user_id',
'system_user_name',
'status',
];
protected $casts = [
'user_id' => 'integer',
'company_id' => 'integer',
'status' => 'integer',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
/**
* 关联用户
*/
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
/**
* 关联公司
*/
public function company()
{
return $this->belongsTo(Company::class, 'company_id');
}
/**
* 获取有效的映射
*/
public function scopeActive($query)
{
return $query->where('status', 1);
}
/**
* 根据系统代码查找
*/
public function scopeBySystem($query, string $systemCode)
{
return $query->where('system_code', $systemCode);
}
/**
* 根据公司ID查找
*/
public function scopeByCompany($query, int $companyId)
{
return $query->where('company_id', $companyId);
}
/**
* 根据外部系统用户ID查找
*/
public function scopeBySystemUserId($query, string $systemUserId, string $systemCode = 'fdesk')
{
return $query->where('system_code', $systemCode)
->where('system_user_id', $systemUserId);
}
/**
* 获取用户在指定系统的映射
*/
public function scopeUserMapping($query, int $userId, int $companyId, string $systemCode = 'fdesk')
{
return $query->where('user_id', $userId)
->where('company_id', $companyId)
->where('system_code', $systemCode);
}
}
+1
View File
@@ -36,6 +36,7 @@ Route::group('/api/v1/user', function () {
Route::get('/menu', [app\controller\UserController::class, 'menu']);
Route::get('/companies', [app\controller\UserController::class, 'companies']);
Route::get('/list', [app\controller\UserController::class, 'list']);
Route::get('/system-mapping', [app\controller\UserController::class, 'systemMapping']);
Route::post('/switch-company', [app\controller\UserController::class, 'switchCompany']);
Route::post('/change-password', [app\controller\UserController::class, 'changePassword']);
})->middleware($authMiddleware);