refactor: 部门/用户/菜单上级选择器迁移 wot-ui v2 并修复回显
- 新增 utils/tree findOptionChain 抽取树节点链查找,替换各页重复递归 - 部门/用户选择器迁移 v2 静态模式,废除 lazy-load/display-format 等 v1 用法 - 修复编辑回显缺文字:值比较统一转字符串,部门已删/禁用时回退列表行名或显示顶级 - 修复部门/菜单新增子项时误发 /undefined/form 的无效请求
This commit is contained in:
1 parent
2172bb597b
commit
ac772d7262
4 files changed
+93
-168
No files matched your search
@@ -58,8 +58,7 @@
|
||||
v-model="parentSelected"
|
||||
v-model:visible="showParentPicker"
|
||||
:options="parentOptions"
|
||||
:lazy-load="handleParentLazyLoad"
|
||||
:display-format="displayParentFormat"
|
||||
text-key="label"
|
||||
@confirm="handleParentConfirm"
|
||||
/>
|
||||
<wd-form-item prop="name" title="部门名称" required>
|
||||
@@ -103,6 +102,7 @@
|
||||
<script lang="ts" setup>
|
||||
import { onLoad } from "@dcloudio/uni-app";
|
||||
import { toFormSchema } from "@/utils/form";
|
||||
import { findOptionChain } from "@/utils/tree";
|
||||
import { useToast, useDialog } from "@wot-ui/ui";
|
||||
import DeptAPI, { type DeptQuery, DeptItem, DeptForm } from "@/api/dept";
|
||||
import { hasPermission } from "@/utils/permission";
|
||||
@@ -153,53 +153,15 @@ function transformDeptToTree(dept: DeptItem): any {
|
||||
|
||||
// 上级部门选择器
|
||||
const showParentPicker = ref(false);
|
||||
const parentSelected = ref<(string | number)[]>([]);
|
||||
const parentSelected = ref<string | number>("");
|
||||
const parentOptions = ref<OptionType[]>([]);
|
||||
const parentLabel = ref("");
|
||||
|
||||
// 格式化上级部门展示
|
||||
const displayParentFormat = (selectedItems: OptionType[]) => {
|
||||
if (!selectedItems || selectedItems.length === 0) return "";
|
||||
return selectedItems
|
||||
.map((item) => item?.label)
|
||||
.filter((label) => !!label)
|
||||
.join("/");
|
||||
};
|
||||
|
||||
// 查找部门在树中的完整路径
|
||||
function findDeptPath(data: OptionType[], targetId: string, path: string[] = []): string[] | null {
|
||||
for (const item of data) {
|
||||
const currentPath = [...path, String(item.value)];
|
||||
if (String(item.value) === targetId) {
|
||||
return currentPath;
|
||||
}
|
||||
if (item.children && item.children.length > 0) {
|
||||
const found = findDeptPath(item.children, targetId, currentPath);
|
||||
if (found) return found;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// v2 Cascader 懒加载
|
||||
const handleParentLazyLoad = ({ selectedItem, resolve, finish }: any) => {
|
||||
if (String(selectedItem?.value) === "0") {
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
const children = selectedItem.children;
|
||||
if (children && children.length > 0) {
|
||||
resolve(children);
|
||||
} else {
|
||||
finish();
|
||||
}
|
||||
};
|
||||
|
||||
// 上级部门确认选择
|
||||
const handleParentConfirm = ({ value, selectedItems }: any) => {
|
||||
const handleParentConfirm = ({ value, selectedOptions }: any) => {
|
||||
parentSelected.value = value;
|
||||
formData.parentId = Number(value[value.length - 1]) || 0;
|
||||
parentLabel.value = selectedItems.map((item: any) => item.label).join("/");
|
||||
formData.parentId = Number(value) || 0;
|
||||
parentLabel.value = selectedOptions.map((item: any) => item.label).join("/");
|
||||
};
|
||||
|
||||
const rules = toFormSchema({
|
||||
@@ -280,34 +242,39 @@ function handleNodeAction(node: any) {
|
||||
async function openDeptDialog(dept?: DeptItem) {
|
||||
formRef.value?.reset();
|
||||
Object.assign(formData, initialFormData);
|
||||
parentSelected.value = [];
|
||||
parentSelected.value = "";
|
||||
parentLabel.value = "";
|
||||
dialog.visible = true;
|
||||
|
||||
const data = await DeptAPI.getOptions();
|
||||
parentOptions.value = [{ value: "0", label: "顶级部门" }, ...data];
|
||||
|
||||
if (dept) {
|
||||
if (dept?.id) {
|
||||
formData.id = dept.id;
|
||||
const form = await DeptAPI.getFormData(dept.id!);
|
||||
const form = await DeptAPI.getFormData(dept.id);
|
||||
Object.assign(formData, form, { id: dept.id });
|
||||
|
||||
if (form.parentId && form.parentId !== 0) {
|
||||
const path = findDeptPath(data, String(form.parentId));
|
||||
if (path) {
|
||||
parentSelected.value = path;
|
||||
} else {
|
||||
parentSelected.value = [String(form.parentId)];
|
||||
}
|
||||
// 编辑时回显上级部门
|
||||
if (form.parentId === 0) {
|
||||
parentSelected.value = "0";
|
||||
parentLabel.value = "顶级部门";
|
||||
} else if (form.parentId) {
|
||||
parentSelected.value = form.parentId;
|
||||
const chain = findOptionChain(data, form.parentId);
|
||||
parentLabel.value = chain ? chain.map((option) => option.label).join("/") : "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 新增子部门
|
||||
// 新增子部门:按新增流程打开弹窗,并预设上级部门
|
||||
function handleAddChild(dept: DeptItem) {
|
||||
openDeptDialog({ id: undefined, parentId: dept.id } as DeptItem);
|
||||
openDeptDialog();
|
||||
formData.parentId = dept.id!;
|
||||
parentSelected.value = [String(dept.id)];
|
||||
parentSelected.value = dept.id!;
|
||||
parentLabel.value =
|
||||
findOptionChain(treeData.value, dept.id!)?.map((option) => option.label).join("/") ||
|
||||
dept.name ||
|
||||
"";
|
||||
}
|
||||
|
||||
// 提交表单
|
||||
|
||||
@@ -67,8 +67,7 @@
|
||||
v-model="parentSelected"
|
||||
v-model:visible="showParentPicker"
|
||||
:options="parentOptions"
|
||||
:lazy-load="handleParentLazyLoad"
|
||||
:display-format="displayParentFormat"
|
||||
text-key="label"
|
||||
@confirm="handleParentConfirm"
|
||||
/>
|
||||
<wd-form-item prop="name" title="菜单名称" required>
|
||||
@@ -129,6 +128,7 @@
|
||||
<script lang="ts" setup>
|
||||
import { onLoad } from "@dcloudio/uni-app";
|
||||
import { toFormSchema } from "@/utils/form";
|
||||
import { findOptionChain } from "@/utils/tree";
|
||||
import { useToast, useDialog } from "@wot-ui/ui";
|
||||
import MenuAPI, { type MenuQuery, MenuItem, MenuForm } from "@/api/menu";
|
||||
import { hasPermission } from "@/utils/permission";
|
||||
@@ -210,54 +210,15 @@ function getMenuTypeText(type?: string | number) {
|
||||
|
||||
// 上级菜单选择器
|
||||
const showParentPicker = ref(false);
|
||||
const parentSelected = ref<(string | number)[]>([]);
|
||||
const parentSelected = ref<string | number>("");
|
||||
const parentOptions = ref<OptionType[]>([]);
|
||||
const parentLabel = ref("");
|
||||
|
||||
const displayParentFormat = (selectedItems: Record<string, any>[]) => {
|
||||
if (!selectedItems || selectedItems.length === 0) return "";
|
||||
return selectedItems
|
||||
.map((item) => item?.label)
|
||||
.filter((label) => !!label)
|
||||
.join("/");
|
||||
};
|
||||
|
||||
function findMenuPath(
|
||||
data: Record<string, any>[],
|
||||
targetId: string,
|
||||
path: (string | number)[] = []
|
||||
): (string | number)[] | null {
|
||||
for (const item of data) {
|
||||
const currentPath = [...path, item.value];
|
||||
if (String(item.value) === targetId) {
|
||||
return currentPath;
|
||||
}
|
||||
if (item.children && item.children.length > 0) {
|
||||
const found = findMenuPath(item.children, targetId, currentPath);
|
||||
if (found) return found;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// v2 Cascader 懒加载
|
||||
const handleParentLazyLoad = ({ selectedItem, resolve, finish }: any) => {
|
||||
if (String(selectedItem?.value) === "0") {
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
const children = selectedItem.children;
|
||||
if (children && children.length > 0) {
|
||||
resolve(children);
|
||||
} else {
|
||||
finish();
|
||||
}
|
||||
};
|
||||
|
||||
const handleParentConfirm = ({ value, selectedItems }: any) => {
|
||||
// 上级菜单确认选择
|
||||
const handleParentConfirm = ({ value, selectedOptions }: any) => {
|
||||
parentSelected.value = value;
|
||||
formData.parentId = String(value[value.length - 1]) || "0";
|
||||
parentLabel.value = selectedItems.map((item: any) => item.label).join("/");
|
||||
formData.parentId = String(value) || "0";
|
||||
parentLabel.value = selectedOptions.map((item: any) => item.label).join("/");
|
||||
};
|
||||
|
||||
const rules = toFormSchema({
|
||||
@@ -333,34 +294,39 @@ function handleNodeAction(node: any) {
|
||||
async function openMenuDialog(menu?: MenuItem) {
|
||||
formRef.value?.reset();
|
||||
Object.assign(formData, initialFormData);
|
||||
parentSelected.value = [];
|
||||
parentSelected.value = "";
|
||||
parentLabel.value = "";
|
||||
dialog.visible = true;
|
||||
|
||||
const data = await MenuAPI.getOptions(true);
|
||||
parentOptions.value = [{ value: "0", label: "顶级菜单" }, ...data];
|
||||
|
||||
if (menu) {
|
||||
if (menu?.id) {
|
||||
formData.id = menu.id;
|
||||
const form = await MenuAPI.getFormData(menu.id!);
|
||||
const form = await MenuAPI.getFormData(menu.id);
|
||||
Object.assign(formData, form, { id: menu.id });
|
||||
|
||||
if (form.parentId && String(form.parentId) !== "0") {
|
||||
const path = findMenuPath(data, String(form.parentId));
|
||||
if (path) {
|
||||
parentSelected.value = path;
|
||||
} else {
|
||||
parentSelected.value = [String(form.parentId)];
|
||||
}
|
||||
// 编辑时回显上级菜单
|
||||
if (String(form.parentId) === "0") {
|
||||
parentSelected.value = "0";
|
||||
parentLabel.value = "顶级菜单";
|
||||
} else if (form.parentId) {
|
||||
parentSelected.value = form.parentId;
|
||||
const chain = findOptionChain(data, form.parentId);
|
||||
parentLabel.value = chain ? chain.map((option) => option.label).join("/") : "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 新增子菜单
|
||||
// 新增子菜单:按新增流程打开弹窗,并预设上级菜单
|
||||
function handleAddChild(menu: MenuItem) {
|
||||
openMenuDialog({ id: undefined, parentId: menu.id } as MenuItem);
|
||||
openMenuDialog();
|
||||
formData.parentId = menu.id!;
|
||||
parentSelected.value = [menu.id!];
|
||||
parentSelected.value = menu.id!;
|
||||
parentLabel.value =
|
||||
findOptionChain(treeData.value, menu.id!)?.map((option) => option.label).join("/") ||
|
||||
menu.name ||
|
||||
"";
|
||||
}
|
||||
|
||||
// 提交表单
|
||||
|
||||
@@ -48,7 +48,7 @@
|
||||
v-for="item in pageData"
|
||||
:key="item.id"
|
||||
custom-class="item-card"
|
||||
@click="openUserDialog(item.id)"
|
||||
@click="openUserDialog(item)"
|
||||
>
|
||||
<!-- 主信息行 -->
|
||||
<view class="flex-start">
|
||||
@@ -141,8 +141,7 @@
|
||||
v-model="deptSelected"
|
||||
v-model:visible="showDeptPicker"
|
||||
:options="deptOptions"
|
||||
:lazy-load="handleDeptLazyLoad"
|
||||
:display-format="displayDeptFormat"
|
||||
text-key="label"
|
||||
@confirm="handleDeptConfirm"
|
||||
/>
|
||||
|
||||
@@ -219,6 +218,7 @@
|
||||
import { onLoad, onReachBottom } from "@dcloudio/uni-app";
|
||||
import { LoadMoreState } from "@wot-ui/ui/components/wd-loadmore/types";
|
||||
import { toFormSchema } from "@/utils/form";
|
||||
import { findOptionChain } from "@/utils/tree";
|
||||
import { useQueue, useToast, useDialog } from "@wot-ui/ui";
|
||||
import UserAPI, { type UserPageQuery, UserItem, UserForm } from "@/api/user";
|
||||
import RoleAPI from "@/api/role";
|
||||
@@ -266,48 +266,17 @@ const deptOptions = ref<OptionType[]>([]);
|
||||
|
||||
// 部门选择器状态
|
||||
const showDeptPicker = ref(false);
|
||||
const deptSelected = ref<(string | number)[]>([]);
|
||||
const deptSelected = ref<string | number>("");
|
||||
const deptLabel = ref("");
|
||||
|
||||
// 角色选择器状态
|
||||
const showRolePicker = ref(false);
|
||||
const roleLabel = ref("");
|
||||
|
||||
// 格式化部门展示
|
||||
const displayDeptFormat = (selectedItems: Record<string, any>[]) => {
|
||||
return selectedItems.map((item) => item.label).join("/");
|
||||
};
|
||||
|
||||
// 查找部门在树中的完整路径
|
||||
function findDeptPath(data: any[], targetId: string, path: string[] = []): string[] | null {
|
||||
for (const item of data) {
|
||||
const currentPath = [...path, item.value];
|
||||
if (item.value === targetId) {
|
||||
return currentPath;
|
||||
}
|
||||
if (item.children && item.children.length > 0) {
|
||||
const found = findDeptPath(item.children, targetId, currentPath);
|
||||
if (found) return found;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// v2 Cascader 懒加载
|
||||
const handleDeptLazyLoad = ({ selectedItem, resolve, finish }: any) => {
|
||||
const children = selectedItem.children;
|
||||
if (children && children.length > 0) {
|
||||
resolve(children);
|
||||
} else {
|
||||
finish();
|
||||
}
|
||||
};
|
||||
|
||||
// 部门确认选择
|
||||
const handleDeptConfirm = ({ value, selectedItems }: any) => {
|
||||
deptSelected.value = value;
|
||||
formData.deptId = value[value.length - 1];
|
||||
deptLabel.value = selectedItems.map((item: any) => item.label).join("/");
|
||||
const handleDeptConfirm = ({ value, selectedOptions }: any) => {
|
||||
formData.deptId = value;
|
||||
deptLabel.value = selectedOptions.map((item: any) => item.label).join("/");
|
||||
};
|
||||
|
||||
// 角色确认选择
|
||||
@@ -381,10 +350,10 @@ function fetchUserList() {
|
||||
}
|
||||
|
||||
// 打开弹窗(新增/编辑)
|
||||
async function openUserDialog(id?: number) {
|
||||
async function openUserDialog(user?: UserItem) {
|
||||
formRef.value?.reset();
|
||||
Object.assign(formData, initialFormData);
|
||||
deptSelected.value = [];
|
||||
deptSelected.value = "";
|
||||
deptLabel.value = "";
|
||||
roleLabel.value = "";
|
||||
dialog.visible = true;
|
||||
@@ -392,18 +361,15 @@ async function openUserDialog(id?: number) {
|
||||
const deptData = await DeptAPI.getOptions();
|
||||
deptOptions.value = deptData;
|
||||
|
||||
if (id) {
|
||||
formData.id = id;
|
||||
const data = await UserAPI.getFormData(id);
|
||||
Object.assign(formData, data, { id });
|
||||
// 编辑时回显部门
|
||||
if (user) {
|
||||
formData.id = user.id;
|
||||
const data = await UserAPI.getFormData(user.id);
|
||||
Object.assign(formData, data, { id: user.id });
|
||||
// 编辑时回显部门;部门已删除或禁用时不在选项树中,回退到列表行的部门名
|
||||
if (data.deptId) {
|
||||
const path = findDeptPath(deptData, String(data.deptId));
|
||||
if (path) {
|
||||
deptSelected.value = path;
|
||||
} else {
|
||||
deptSelected.value = [String(data.deptId)];
|
||||
}
|
||||
deptSelected.value = data.deptId;
|
||||
const chain = findOptionChain(deptData, data.deptId);
|
||||
deptLabel.value = chain ? chain.map((option) => option.label).join("/") : user.deptName || "";
|
||||
}
|
||||
// 回显角色
|
||||
if (data.roleIds && data.roleIds.length > 0) {
|
||||
@@ -466,7 +432,7 @@ function showUserActions(item: UserItem) {
|
||||
// 编辑
|
||||
if (hasPermission("sys:user:update")) {
|
||||
actions.push({ name: "编辑" });
|
||||
actionMap["编辑"] = () => openUserDialog(item.id);
|
||||
actionMap["编辑"] = () => openUserDialog(item);
|
||||
}
|
||||
|
||||
// 删除
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* 沿选项树深度优先查找目标节点的完整节点链(含自身)
|
||||
*
|
||||
* 值比较统一转为字符串,兼容选项值为 number、目标值为 string(或相反)的场景
|
||||
*
|
||||
* @param options 选项树,如部门选项
|
||||
* @param targetValue 目标节点值
|
||||
* @returns 从根到目标的节点链,未命中返回 null
|
||||
*/
|
||||
export function findOptionChain(
|
||||
options: OptionType[],
|
||||
targetValue: string | number
|
||||
): OptionType[] | null {
|
||||
for (const option of options) {
|
||||
if (String(option.value) === String(targetValue)) {
|
||||
return [option];
|
||||
}
|
||||
if (option.children?.length) {
|
||||
const chain = findOptionChain(option.children, targetValue);
|
||||
if (chain) {
|
||||
return [option, ...chain];
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
Reference in new issue
Block a user