wip
This commit is contained in:
@@ -12,15 +12,20 @@ class AccountService
|
||||
{
|
||||
$users = UserModel::with('company')->select();
|
||||
return $users->map(function ($user) {
|
||||
$companyName = $user->role === 'client' && (bool) $user->can_view_all_tickets
|
||||
? '全部客户'
|
||||
: ($user->company->company_name ?? '');
|
||||
|
||||
return [
|
||||
'id' => $user->id,
|
||||
'company' => [
|
||||
'id' => $user->company_id,
|
||||
'company_name' => $user->company->company_name
|
||||
'company_name' => $companyName
|
||||
],
|
||||
'username' => $user->username,
|
||||
'real_name' => $user->real_name,
|
||||
'role' => $user->role,
|
||||
'can_view_all_tickets' => (bool) $user->can_view_all_tickets,
|
||||
'status' => $user->status,
|
||||
];
|
||||
})->toArray();
|
||||
@@ -40,19 +45,34 @@ class AccountService
|
||||
throw new \Exception('用户名已存在');
|
||||
}
|
||||
|
||||
$role = $data['role'] ?? 'user';
|
||||
$companyId = (int) ($data['company_id'] ?? 0);
|
||||
$canViewAllTickets = $role === 'client' && !empty($data['can_view_all_tickets']);
|
||||
|
||||
// 系统中只允许存在一个可查看全部工单的客户汇总账号。
|
||||
if ($canViewAllTickets) {
|
||||
$allAccessClient = UserModel::where('role', 'client')
|
||||
->where('can_view_all_tickets', 1)
|
||||
->find();
|
||||
if ($allAccessClient) {
|
||||
throw new \Exception('已存在可查看全部工单的客户账号');
|
||||
}
|
||||
}
|
||||
|
||||
$insertData = [
|
||||
'company_id' => $data['company_id'] ?? 0,
|
||||
'company_id' => $companyId,
|
||||
'username' => $data['username'],
|
||||
'real_name' => $data['real_name'] ?? $data['username'],
|
||||
'password' => password_hash('123456', PASSWORD_DEFAULT),
|
||||
'role' => $data['role'] ?? 'user',
|
||||
'role' => $role,
|
||||
'can_view_all_tickets' => $canViewAllTickets ? 1 : 0,
|
||||
'status' => 1,
|
||||
];
|
||||
|
||||
$user = UserModel::create($insertData);
|
||||
|
||||
/** @var \think\Model $user */
|
||||
return $user->visible(['id', 'username', 'real_name', 'role', 'status'])->toArray();
|
||||
return $user->visible(['id', 'username', 'real_name', 'role', 'can_view_all_tickets', 'status'])->toArray();
|
||||
}
|
||||
|
||||
/* 更新用户状态 */
|
||||
|
||||
Reference in New Issue
Block a user