新增后台
This commit is contained in:
@@ -4,6 +4,7 @@ namespace app\admin\logic;
|
||||
|
||||
use app\common\traits\crud\CrudLogicTrait;
|
||||
use app\common\model\User;
|
||||
use support\Db;
|
||||
|
||||
class UserLogic
|
||||
{
|
||||
@@ -14,4 +15,83 @@ class UserLogic
|
||||
*/
|
||||
protected string $modelClass = User::class;
|
||||
|
||||
|
||||
public function updateRoleAndSystem(array $data): bool
|
||||
{
|
||||
$roleIds = $data['role_ids'] ?? [];
|
||||
$systemIds = $data['system_ids'] ?? [];
|
||||
$userId = $data['user_id'] ?? 0;
|
||||
|
||||
if (empty($roleId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Db::beginTransaction();
|
||||
try {
|
||||
Db::table('user_roles')->where('user_id', $userId)->delete();
|
||||
if (!empty($roleIds)) {
|
||||
$insertData = [];
|
||||
foreach ($roleIds as $roleId) {
|
||||
$insertData[] = [
|
||||
'role_id' => $roleId,
|
||||
'user_id' => $userId
|
||||
];
|
||||
}
|
||||
Db::table('user_roles')->insert($insertData);
|
||||
}
|
||||
|
||||
Db::table('user_systems')->where('user_id', $userId)->delete();
|
||||
if (!empty($systemIds)) {
|
||||
$insertData = [];
|
||||
foreach ($systemIds as $systemId) {
|
||||
$insertData[] = [
|
||||
'system_id' => $systemId,
|
||||
'user_id' => $userId
|
||||
];
|
||||
}
|
||||
Db::table('user_systems')->insert($insertData);
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Throwable $exception) {
|
||||
Db::rollBack();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function updateUserResources(array $data): bool
|
||||
{
|
||||
$userId = $data['role_id'] ?? 0;
|
||||
$resources = $data['resources'] ?? [];
|
||||
|
||||
if (empty($userId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Db::beginTransaction();
|
||||
try {
|
||||
// 删除原有资源关联
|
||||
Db::table('user_resources')->where('user_id', $userId)->delete();
|
||||
|
||||
// 批量插入新角色资源关联
|
||||
if (!empty($resources)) {
|
||||
$insertData = [];
|
||||
foreach ($resources as $resourceId) {
|
||||
$insertData[] = [
|
||||
'user_id' => $userId,
|
||||
'resource_id' => $resourceId
|
||||
];
|
||||
}
|
||||
Db::table('user_resources')->insert($insertData);
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Throwable $exception) {
|
||||
Db::rollBack();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user