From f9aa16ef315f9cb510b27a953e157f812c84acce Mon Sep 17 00:00:00 2001 From: zlwu <704541015@qq.com> Date: Thu, 19 Nov 2026 14:24:21 +0800 Subject: [PATCH] =?UTF-8?q?feat(oa):=20=E8=AF=84=E8=AE=BA@=E4=BA=BA?= =?UTF-8?q?=E5=91=98=E4=BC=98=E5=85=88=E5=B1=95=E7=A4=BA=E5=90=8C=E9=83=A8?= =?UTF-8?q?=E9=97=A8=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit OA 评论选人列表改为请求 users/options 时按当前登录用户部门优先排序,Web 与移动 OA 共用后端稳定排序,便于先选到同部门同事。 Co-authored-by: Cursor --- backend/app/api/admin.py | 17 ++++++++++-- backend/app/services/user_select_options.py | 19 +++++++++++++ backend/tests/test_user_select_options.py | 27 +++++++++++++++++++ .../src/oa/components/OaCommentComposer.vue | 3 ++- web-admin/src/api/admin.ts | 9 ++++--- .../components/oa-approval/OaCommentPanel.vue | 3 ++- 6 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 backend/app/services/user_select_options.py create mode 100644 backend/tests/test_user_select_options.py diff --git a/backend/app/api/admin.py b/backend/app/api/admin.py index 57e7ce3c..6aae3670 100644 --- a/backend/app/api/admin.py +++ b/backend/app/api/admin.py @@ -29,6 +29,7 @@ from app.crud import user as user_svc from app.crud.collection_protocol import _lookup_attachment_by_url from app.models.models import BizAttachment, SysDept, SysRole, SysUser, SysMenu, SysPost from app.services.permission_administration import validate_tenant_references +from app.services.user_select_options import prioritize_caller_dept_options from app.services.web_auth_roles import ( _map_role_name_to_web, list_user_ids_with_effective_role_name, @@ -77,12 +78,15 @@ async def list_roles_for_select( @router.get("/users/options") async def list_users_for_select( roleName: Optional[str] = Query(None, alias="roleName"), + prioritizeCallerDept: bool = Query(False, alias="prioritizeCallerDept"), _user: dict = Depends(require_admin_or_super_admin), db: Session = Depends(get_db), ): - """返回用户下拉选项(label=昵称,value=用户ID),供流程设计器审批人选择使用。 + """返回用户下拉选项(label=昵称,value=用户ID,deptId=部门ID)。 + 供流程设计器审批人选择、OA 评论 @ 人员等使用。 可选 roleName:按有效角色(直接绑定 ∪ 职位继承)精确过滤。 + 可选 prioritizeCallerDept:当前登录用户同部门人员排在前面(组内仍按创建时间倒序)。 """ tenant_no = _user.get("tenant_no", "") q = db.query(SysUser).filter(SysUser.del_flag == 0, SysUser.status == "1") @@ -97,7 +101,16 @@ async def list_users_for_select( return success_response([]) q = q.filter(SysUser.id.in_(matched_ids)) rows = q.order_by(SysUser.create_time.desc()).distinct().all() - out = [{"label": (r.nick_name or r.user_no or ""), "value": r.id} for r in rows] + out = [ + { + "label": (r.nick_name or r.user_no or ""), + "value": r.id, + "deptId": (r.dept_id or ""), + } + for r in rows + ] + if prioritizeCallerDept: + out = prioritize_caller_dept_options(out, _user.get("region_id") or "") return success_response(out) diff --git a/backend/app/services/user_select_options.py b/backend/app/services/user_select_options.py new file mode 100644 index 00000000..e2d90d16 --- /dev/null +++ b/backend/app/services/user_select_options.py @@ -0,0 +1,19 @@ +"""用户下拉选项:同部门优先排序等轻量辅助。""" +from __future__ import annotations + +from typing import Any, Dict, List, Sequence + + +def prioritize_caller_dept_options( + options: Sequence[Dict[str, Any]], + caller_dept_id: str, +) -> List[Dict[str, Any]]: + """稳定排序:当前登录用户同部门在前,其余保持原相对顺序。""" + items = list(options) + dept = (caller_dept_id or "").strip() + if not dept or not items: + return items + return sorted( + items, + key=lambda item: 0 if str(item.get("deptId") or "").strip() == dept else 1, + ) diff --git a/backend/tests/test_user_select_options.py b/backend/tests/test_user_select_options.py new file mode 100644 index 00000000..4678e763 --- /dev/null +++ b/backend/tests/test_user_select_options.py @@ -0,0 +1,27 @@ +"""用户下拉选项:同部门优先排序。""" +from app.services.user_select_options import prioritize_caller_dept_options + + +def test_prioritize_caller_dept_keeps_relative_order(): + options = [ + {"label": "外1", "value": "a", "deptId": "d2"}, + {"label": "同1", "value": "b", "deptId": "d1"}, + {"label": "外2", "value": "c", "deptId": "d3"}, + {"label": "同2", "value": "d", "deptId": "d1"}, + {"label": "无部门", "value": "e", "deptId": ""}, + ] + out = prioritize_caller_dept_options(options, "d1") + assert [x["value"] for x in out] == ["b", "d", "a", "c", "e"] + + +def test_prioritize_caller_dept_noop_without_caller_dept(): + options = [ + {"label": "a", "value": "a", "deptId": "d1"}, + {"label": "b", "value": "b", "deptId": "d2"}, + ] + assert prioritize_caller_dept_options(options, "") == options + assert prioritize_caller_dept_options(options, " ") == options + + +def test_prioritize_caller_dept_empty_list(): + assert prioritize_caller_dept_options([], "d1") == [] diff --git a/mobile-oa-h5/src/oa/components/OaCommentComposer.vue b/mobile-oa-h5/src/oa/components/OaCommentComposer.vue index aea0c5e2..5fa7ff9c 100644 --- a/mobile-oa-h5/src/oa/components/OaCommentComposer.vue +++ b/mobile-oa-h5/src/oa/components/OaCommentComposer.vue @@ -216,7 +216,8 @@ async function ensureUserCache() { if (userCachePromise) return userCachePromise userCachePromise = (async () => { try { - const res = await fetchUserOptions() + // 同部门优先:由后端按当前登录用户部门排序 + const res = await fetchUserOptions({ prioritizeCallerDept: true }) const list = Array.isArray(res) ? res : res?.list || [] const next = [] const seen = new Set() diff --git a/web-admin/src/api/admin.ts b/web-admin/src/api/admin.ts index 1bb688d6..aad1d1fa 100644 --- a/web-admin/src/api/admin.ts +++ b/web-admin/src/api/admin.ts @@ -22,9 +22,12 @@ export function fetchGetRoleList() { }) } -/** 流程设计器:用户下拉选项(label=昵称,value=用户ID) */ -export function fetchUserOptions(params?: { roleName?: string }) { - return request.get>({ +/** 用户下拉选项(label=昵称,value=用户ID;可选 deptId / prioritizeCallerDept) */ +export function fetchUserOptions(params?: { + roleName?: string + prioritizeCallerDept?: boolean +}) { + return request.get>({ url: '/web/admin/users/options', params }) diff --git a/web-admin/src/components/oa-approval/OaCommentPanel.vue b/web-admin/src/components/oa-approval/OaCommentPanel.vue index fb28a685..a61f2e9f 100644 --- a/web-admin/src/components/oa-approval/OaCommentPanel.vue +++ b/web-admin/src/components/oa-approval/OaCommentPanel.vue @@ -255,7 +255,8 @@ if (userCachePromise) return userCachePromise userCachePromise = (async () => { try { - const res = await fetchUserOptions() + // 同部门优先:由后端按当前登录用户部门排序 + const res = await fetchUserOptions({ prioritizeCallerDept: true }) const list = Array.isArray(res) ? res : [] const next: MentionUser[] = [] const seen = new Set()