2026-02-24 17:57:25 +08:00
|
|
|
<?php
|
|
|
|
|
|
2026-02-25 18:09:04 +08:00
|
|
|
namespace app\controller;
|
2026-02-24 17:57:25 +08:00
|
|
|
|
2026-02-25 18:09:04 +08:00
|
|
|
use app\model\User;
|
2026-03-03 14:50:41 +08:00
|
|
|
use app\model\UserSystemMapping;
|
2026-02-24 17:57:25 +08:00
|
|
|
use Webman\Http\Request;
|
|
|
|
|
use Webman\Http\Response;
|
|
|
|
|
|
|
|
|
|
class UserController extends BaseController
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 获取用户信息
|
|
|
|
|
*/
|
|
|
|
|
public function info(Request $request): Response
|
|
|
|
|
{
|
|
|
|
|
$userId = $this->getUserId($request);
|
2026-02-25 15:22:12 +08:00
|
|
|
$user = User::find($userId);
|
2026-02-24 17:57:25 +08:00
|
|
|
|
2026-02-25 15:22:12 +08:00
|
|
|
if (!$user) {
|
|
|
|
|
return $this->error('用户不存在');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->success($user);
|
2026-02-24 17:57:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取用户菜单权限
|
|
|
|
|
*/
|
|
|
|
|
public function menu(Request $request): Response
|
|
|
|
|
{
|
|
|
|
|
// TODO: 根据用户权限返回菜单
|
|
|
|
|
return $this->success([
|
|
|
|
|
['id' => 1, 'name' => '首页', 'path' => '/dashboard'],
|
|
|
|
|
['id' => 2, 'name' => '出差审批', 'path' => '/business-trip'],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取用户可切换的公司列表
|
|
|
|
|
*/
|
|
|
|
|
public function companies(Request $request): Response
|
|
|
|
|
{
|
|
|
|
|
$userId = $this->getUserId($request);
|
|
|
|
|
|
|
|
|
|
// TODO: 从数据库获取用户关联的公司列表
|
|
|
|
|
return $this->success([
|
|
|
|
|
['id' => 1, 'name' => '公司A'],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 切换当前公司
|
|
|
|
|
*/
|
|
|
|
|
public function switchCompany(Request $request): Response
|
|
|
|
|
{
|
|
|
|
|
$companyId = $request->post('company_id');
|
|
|
|
|
|
|
|
|
|
// TODO: 更新用户当前公司
|
|
|
|
|
|
|
|
|
|
return $this->success();
|
|
|
|
|
}
|
2026-02-25 15:22:12 +08:00
|
|
|
|
2026-02-26 15:19:37 +08:00
|
|
|
/**
|
|
|
|
|
* 修改密码
|
|
|
|
|
*/
|
|
|
|
|
public function changePassword(Request $request): Response
|
|
|
|
|
{
|
|
|
|
|
$userId = $this->getUserId($request);
|
|
|
|
|
$user = User::find($userId);
|
|
|
|
|
|
|
|
|
|
if (!$user) {
|
|
|
|
|
return $this->error('用户不存在');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$oldPassword = $request->post('old_password');
|
|
|
|
|
$newPassword = $request->post('new_password');
|
|
|
|
|
$confirmPassword = $request->post('confirm_password');
|
|
|
|
|
|
|
|
|
|
// 验证参数
|
|
|
|
|
if (!$oldPassword || !$newPassword || !$confirmPassword) {
|
|
|
|
|
return $this->error('请填写完整信息');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strlen($newPassword) < 6) {
|
|
|
|
|
return $this->error('新密码长度不能少于6位');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($newPassword !== $confirmPassword) {
|
|
|
|
|
return $this->error('两次输入的密码不一致');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 验证旧密码
|
|
|
|
|
if ($user->password && !password_verify($oldPassword, $user->password)) {
|
|
|
|
|
return $this->error('原密码错误');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新密码
|
|
|
|
|
$user->password = password_hash($newPassword, PASSWORD_DEFAULT);
|
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
|
|
return $this->success(null, '密码修改成功');
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-25 15:22:12 +08:00
|
|
|
/**
|
|
|
|
|
* 获取用户列表(用于选择审批人等)
|
|
|
|
|
*/
|
|
|
|
|
public function list(Request $request): Response
|
|
|
|
|
{
|
|
|
|
|
$userId = $this->getUserId($request);
|
|
|
|
|
$keyword = $request->get('keyword', '');
|
|
|
|
|
$page = (int) $request->get('page', 1);
|
|
|
|
|
$pageSize = (int) $request->get('page_size', 50);
|
|
|
|
|
|
|
|
|
|
$query = User::active();
|
|
|
|
|
|
|
|
|
|
if ($keyword) {
|
|
|
|
|
$query->search($keyword);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$total = $query->count();
|
|
|
|
|
$list = $query->orderBy('id')
|
|
|
|
|
->offset(($page - 1) * $pageSize)
|
|
|
|
|
->limit($pageSize)
|
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
return $this->success([
|
|
|
|
|
'list' => $list,
|
|
|
|
|
'total' => $total,
|
|
|
|
|
'page' => $page,
|
|
|
|
|
'page_size' => $pageSize,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2026-03-03 14:50:41 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取用户在指定系统的映射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,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2026-02-24 17:57:25 +08:00
|
|
|
}
|