feat: 添加部门成员同步功能

- 在 UsersController 中新增同步部门成员的 API 接口
- 在 UserDepartment 模型中添加递归获取子部门 ID 的方法
- 在前端 TeamManagement 组件中添加同步部门成员的操作选项
This commit is contained in:
kuaifan 2025-09-22 05:07:45 +08:00
parent 02e56f87bc
commit b044d8d90e
4 changed files with 151 additions and 4 deletions

View File

@ -1792,7 +1792,94 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/checkin/get 29. 获取签到设置
* @api {get} api/users/department/sync 29. 同步部门成员(限管理员)
*
* @apiDescription 需要token身份将子部门成员同步到当前部门
* @apiVersion 1.0.0
* @apiGroup users
* @apiName department__sync
*
* @apiParam {Number} id 部门id
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function department__sync()
{
User::auth('admin');
//
$id = intval(Request::input('id'));
//
$userDepartment = UserDepartment::find($id);
if (empty($userDepartment)) {
return Base::retError('部门不存在或已被删除');
}
// 获取所有子部门(递归)
$subDepartmentIds = UserDepartment::getAllSubDepartmentIds($id);
if (empty($subDepartmentIds)) {
return Base::retSuccess('同步完成,子部门中没有成员需要同步', [
'synced_count' => 0,
'already_in_dept_count' => 0,
'sub_department_ids' => $subDepartmentIds
]);
}
// 获取子部门中的所有成员
$subDepartmentMembers = [];
foreach ($subDepartmentIds as $subId) {
$users = User::where("department", "like", "%,{$subId},%")->get();
foreach ($users as $user) {
if (!in_array($user->userid, $subDepartmentMembers)) {
$subDepartmentMembers[] = $user->userid;
}
}
}
if (empty($subDepartmentMembers)) {
return Base::retSuccess('同步完成,子部门中没有成员需要同步', [
'synced_count' => 0,
'already_in_dept_count' => 0,
'sub_department_ids' => $subDepartmentIds
]);
}
// 将子部门成员添加到当前部门
$syncedCount = 0;
$alreadyInDeptCount = 0;
AbstractModel::transaction(function () use ($id, $subDepartmentMembers, &$syncedCount, &$alreadyInDeptCount) {
foreach ($subDepartmentMembers as $userid) {
$user = User::find($userid);
if ($user) {
$userDepartments = $user->department;
if (!in_array($id, $userDepartments)) {
$userDepartments[] = $id;
$user->department = Base::arrayImplode($userDepartments);
$user->save();
$syncedCount++;
} else {
$alreadyInDeptCount++;
}
}
}
});
$message = "同步完成,共同步 {$syncedCount} 个成员";
if ($alreadyInDeptCount > 0) {
$message .= ",其中 {$alreadyInDeptCount} 个成员已在当前部门";
}
return Base::retSuccess($message, [
'synced_count' => $syncedCount,
'already_in_dept_count' => $alreadyInDeptCount,
'sub_department_ids' => $subDepartmentIds
]);
}
/**
* @api {get} api/users/checkin/get 30. 获取签到设置
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0

View File

@ -170,6 +170,26 @@ class UserDepartment extends AbstractModel
});
}
/**
* 递归获取所有子部门ID
* @param int $departmentId
* @return array
*/
public static function getAllSubDepartmentIds($departmentId)
{
$subIds = [];
$directSubs = self::whereParentId($departmentId)->pluck('id')->toArray();
foreach ($directSubs as $subId) {
$subIds[] = $subId;
// 递归获取子部门的子部门
$subSubIds = self::getAllSubDepartmentIds($subId);
$subIds = array_merge($subIds, $subSubIds);
}
return array_unique($subIds);
}
/**
* 获取部门基本信息缓存时间1小时
* @param int|array $ids

View File

@ -189,7 +189,7 @@ class AI
$result = Cache::remember($cacheKey, Carbon::now()->addDays(7), function () use ($text, $targetLanguage) {
$post = json_encode([
"model" => "gpt-4.1-nano",
"model" => "gpt-5-nano",
"messages" => [
[
"role" => "system",
@ -264,7 +264,7 @@ class AI
$result = Cache::remember($cacheKey, Carbon::now()->addHours(24), function () use ($text) {
$post = json_encode([
"model" => "gpt-4.1-nano",
"model" => "gpt-5-nano",
"messages" => [
[
"role" => "system",
@ -333,7 +333,7 @@ class AI
$result = Cache::remember($cacheKey, Carbon::now()->addHours(6), function () {
$post = json_encode([
"model" => "gpt-4.1-nano",
"model" => "gpt-5-nano",
"messages" => [
[
"role" => "system",

View File

@ -54,6 +54,9 @@
<EDropdownItem v-if="item.dialog_id" :command="`dialog_${item.dialog_id}`">
<div>{{$L('部门交流群')}}</div>
</EDropdownItem>
<EDropdownItem :command="`sync_${item.id}`">
<div>{{$L('同步部门成员')}}</div>
</EDropdownItem>
<EDropdownItem :command="`edit_${item.id}`">
<div>{{$L('编辑')}}</div>
</EDropdownItem>
@ -1336,6 +1339,43 @@ export default {
return;
}
if ($A.leftExists(val, 'sync_')) {
const departmentId = parseInt(val.substr(5));
//
const hasSubDepartments = this.departmentList.some(dept => dept.parent_id === departmentId);
if (!hasSubDepartments) {
$A.modalWarning({
title: this.$L('同步部门成员'),
content: this.$L('当前部门没有子部门,无需同步'),
});
return;
}
$A.modalConfirm({
title: this.$L('同步部门成员'),
content: `<div>${this.$L(`你确定要同步部门成员吗?`)}</div><div style="color:#f00;font-weight:600">${this.$L(`注:此操作会同步子部门成员到当前部门`)}</div>`,
language: false,
loading: true,
onOk: () => {
return new Promise((resolve, reject) => {
this.$store.dispatch("call", {
url: 'users/department/sync',
data: {
id: departmentId
},
}).then(({msg}) => {
this.getLists();
resolve(msg);
}).catch(({msg}) => {
reject(msg);
});
});
}
});
return;
}
if ($A.leftExists(val, 'del_')) {
const delItem = this.departmentList.find(({id}) => id === parseInt(val.substr(4)))
if (delItem) {