fix(admin): admin管理页面优化,增加排序过滤等
This commit is contained in:
1 parent
0939aeb0d8
commit
fc843905d1
31 files changed
+1090
-671
No files matched your search
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* shared/components/SortableTableHeader.vue
|
||||
* 用途:可点击的表格排序表头(文案 + 升/降/未排序图标)。
|
||||
* 依赖:lucide 图标、common.sort i18n、table-sort 类型。
|
||||
* 逻辑:管理端表格 th 内使用;点击 emit sort,由 useTableSort.toggleSort 处理。
|
||||
*/
|
||||
<script setup lang="ts" generic="K extends string">
|
||||
import { computed } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { ArrowDown, ArrowUp, ArrowUpDown } from 'lucide-vue-next';
|
||||
import type { SortDir } from '@/shared/utils/table-sort';
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
/** 本列表头对应的列键 */
|
||||
columnKey: K;
|
||||
/** 表头展示文案 */
|
||||
label: string;
|
||||
/** 当前激活的排序列 */
|
||||
activeKey?: K;
|
||||
/** 当前排序方向 */
|
||||
direction?: SortDir;
|
||||
/** 对齐方式 */
|
||||
align?: 'left' | 'right';
|
||||
}>(),
|
||||
{
|
||||
direction: 'asc',
|
||||
align: 'left',
|
||||
},
|
||||
);
|
||||
|
||||
const emit = defineEmits<{
|
||||
sort: [key: K];
|
||||
}>();
|
||||
|
||||
const { t } = useI18n();
|
||||
|
||||
const isActive = computed(() => props.activeKey === props.columnKey);
|
||||
|
||||
/**
|
||||
* 无障碍 title:标明未排序 / 升序 / 降序。
|
||||
*/
|
||||
const title = computed(() => {
|
||||
if (!isActive.value) return t('common.sort.unsorted');
|
||||
return props.direction === 'asc' ? t('common.sort.asc') : t('common.sort.desc');
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<button
|
||||
type="button"
|
||||
class="inline-flex items-center gap-1.5 hover:text-primary transition-colors"
|
||||
:class="align === 'right' ? 'ml-auto' : ''"
|
||||
:title="title"
|
||||
@click="emit('sort', columnKey)"
|
||||
>
|
||||
<span>{{ label }}</span>
|
||||
<ArrowUp v-if="isActive && direction === 'asc'" class="w-3.5 h-3.5 text-brand-600" />
|
||||
<ArrowDown v-else-if="isActive && direction === 'desc'" class="w-3.5 h-3.5 text-brand-600" />
|
||||
<ArrowUpDown v-else class="w-3.5 h-3.5 opacity-40" />
|
||||
</button>
|
||||
</template>
|
||||
@@ -1,10 +1,8 @@
|
||||
/**
|
||||
* shared/composables/useReportLayoutStorage.ts
|
||||
* 报表页面布局偏好 localStorage 持久化。
|
||||
*/
|
||||
/**
|
||||
* useReportLayoutStorage.ts
|
||||
* 用途:为报表页面提供统一的布局偏好持久化能力(按用户 + 表名隔离)
|
||||
* 用途:报表页面布局偏好 localStorage 持久化(按用户 + 表名隔离)。
|
||||
* 依赖:localStorage / sessionStorage 中的 hxops_user。
|
||||
* 逻辑:ReportsPage / useReportsAdmin 读写列宽、显隐与排序偏好;v3 起聊天日志默认列宽收窄。
|
||||
*/
|
||||
|
||||
const getCurrentAuthUser = () => {
|
||||
@@ -24,10 +22,20 @@ const getCurrentIdentity = () => {
|
||||
return user?.id ? `id_${user.id}` : `u_${user?.username || 'guest'}`;
|
||||
};
|
||||
|
||||
/**
|
||||
* 生成报表布局偏好的 storage key。
|
||||
* @param table - 报表表名(如 chat_logs)
|
||||
*/
|
||||
const getReportLayoutStorageKey = (table: string) => {
|
||||
return `hxops_report_layout_v2_${getCurrentIdentity()}_${table}`;
|
||||
// v3:聊天日志默认列宽收窄(应用名/用户/姓名/Token),旧 v2 偏好不再沿用
|
||||
return `hxops_report_layout_v3_${getCurrentIdentity()}_${table}`;
|
||||
};
|
||||
|
||||
/**
|
||||
* 读取指定报表表的布局偏好。
|
||||
* @param table - 报表表名
|
||||
* @returns 解析后的偏好,失败或不存在时返回 null
|
||||
*/
|
||||
export const loadReportLayoutPref = <T = any>(table: string): T | null => {
|
||||
try {
|
||||
const raw = localStorage.getItem(getReportLayoutStorageKey(table));
|
||||
@@ -38,6 +46,11 @@ export const loadReportLayoutPref = <T = any>(table: string): T | null => {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 写入指定报表表的布局偏好。
|
||||
* @param table - 报表表名
|
||||
* @param payload - 可序列化的布局对象
|
||||
*/
|
||||
export const saveReportLayoutPref = (table: string, payload: unknown) => {
|
||||
try {
|
||||
localStorage.setItem(getReportLayoutStorageKey(table), JSON.stringify(payload));
|
||||
@@ -45,4 +58,3 @@ export const saveReportLayoutPref = (table: string, payload: unknown) => {
|
||||
// Ignore storage write failure to avoid blocking core report flow.
|
||||
}
|
||||
};
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* shared/composables/useTableSort.ts
|
||||
* 用途:表格列排序状态(当前列、方向)与切换/排序辅助。
|
||||
* 依赖:vue ref、table-sort 纯函数。
|
||||
* 逻辑:业务 composable(如 useDepartmentsAdmin)注入本 hook,页面用 SortableTableHeader 展示。
|
||||
*/
|
||||
import { ref } from 'vue';
|
||||
import {
|
||||
nextSortState,
|
||||
sortByKey,
|
||||
type SortComparable,
|
||||
type SortDir,
|
||||
} from '@/shared/utils/table-sort';
|
||||
|
||||
export interface UseTableSortOptions<K extends string> {
|
||||
/** 初始排序列 */
|
||||
initialKey?: K;
|
||||
/** 初始方向,默认 asc */
|
||||
initialDir?: SortDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理表格排序状态,并提供按当前状态排序列表的方法。
|
||||
* @param options.initialKey - 默认排序列
|
||||
* @param options.initialDir - 默认方向
|
||||
*/
|
||||
export function useTableSort<K extends string>(options: UseTableSortOptions<K> = {}) {
|
||||
const sortKey = ref(options.initialKey as K | undefined);
|
||||
const sortDir = ref<SortDir>(options.initialDir ?? 'asc');
|
||||
|
||||
/**
|
||||
* 切换表头排序:同列升/降切换,换列从升序开始。
|
||||
* @param key - 点击的列键
|
||||
*/
|
||||
const toggleSort = (key: K): void => {
|
||||
const next = nextSortState(sortKey.value, sortDir.value, key);
|
||||
sortKey.value = next.sortKey;
|
||||
sortDir.value = next.sortDir;
|
||||
};
|
||||
|
||||
/**
|
||||
* 按当前 sortKey / sortDir 对列表排序。
|
||||
* @param items - 源列表
|
||||
* @param getValue - 从行取比较值
|
||||
* @param tieBreaker - 可选稳定次序
|
||||
* @returns 新排序数组
|
||||
*/
|
||||
const sortItems = <T>(
|
||||
items: readonly T[],
|
||||
getValue: (item: T, key: K) => SortComparable,
|
||||
tieBreaker?: (a: T, b: T) => number,
|
||||
): T[] =>
|
||||
sortByKey(items, {
|
||||
sortKey: sortKey.value,
|
||||
sortDir: sortDir.value,
|
||||
getValue,
|
||||
tieBreaker,
|
||||
});
|
||||
|
||||
return {
|
||||
sortKey,
|
||||
sortDir,
|
||||
toggleSort,
|
||||
sortItems,
|
||||
};
|
||||
}
|
||||
@@ -8,6 +8,8 @@ export * from './composables/useIsMobile';
|
||||
export * from './composables/useGlobalMessageDialog';
|
||||
export * from './composables/useReportLayoutStorage';
|
||||
export * from './composables/useAudioTranscription';
|
||||
export * from './composables/useTableSort';
|
||||
export * from './utils/table-sort';
|
||||
export * from './api/client';
|
||||
export * from './constants/routes';
|
||||
export type * from './types/schedule.types';
|
||||
@@ -395,7 +395,7 @@
|
||||
}
|
||||
|
||||
.admin-report-filter-input {
|
||||
@apply w-full px-2 py-1 text-xs border rounded outline-none font-normal normal-case;
|
||||
@apply w-full max-w-full min-w-0 box-border px-1.5 py-1 text-xs border rounded outline-none font-normal normal-case;
|
||||
border-color: var(--border-default);
|
||||
background-color: var(--bg-surface);
|
||||
color: var(--text-primary);
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* shared/utils/table-sort.test.ts
|
||||
* 用途:保护表格列排序纯函数的比较与切换语义。
|
||||
* 依赖:Vitest、table-sort。
|
||||
* 逻辑:覆盖空值、数字自然序、同列切换方向、换列重置升序。
|
||||
*/
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
compareSortValues,
|
||||
nextSortState,
|
||||
normalizeSortValue,
|
||||
sortByKey,
|
||||
} from './table-sort';
|
||||
|
||||
describe('table-sort utils', () => {
|
||||
it('normalizes blank strings to null', () => {
|
||||
expect(normalizeSortValue(' ')).toBeNull();
|
||||
expect(normalizeSortValue('IT')).toBe('IT');
|
||||
expect(normalizeSortValue(12)).toBe(12);
|
||||
});
|
||||
|
||||
it('compares numeric-looking codes naturally', () => {
|
||||
expect(compareSortValues('2', '10')).toBeLessThan(0);
|
||||
expect(compareSortValues(null, 'a')).toBe(1);
|
||||
});
|
||||
|
||||
it('toggles direction on same key and resets on new key', () => {
|
||||
expect(nextSortState('name', 'asc', 'name')).toEqual({
|
||||
sortKey: 'name',
|
||||
sortDir: 'desc',
|
||||
});
|
||||
expect(nextSortState('name', 'desc', 'code')).toEqual({
|
||||
sortKey: 'code',
|
||||
sortDir: 'asc',
|
||||
});
|
||||
});
|
||||
|
||||
it('sorts rows by key with id tie-breaker', () => {
|
||||
const rows = [
|
||||
{ id: 2, name: 'B' },
|
||||
{ id: 1, name: 'A' },
|
||||
{ id: 3, name: 'A' },
|
||||
];
|
||||
const sorted = sortByKey(rows, {
|
||||
sortKey: 'name',
|
||||
sortDir: 'asc',
|
||||
getValue: (row, key) => row[key],
|
||||
tieBreaker: (a, b) => a.id - b.id,
|
||||
});
|
||||
expect(sorted.map((r) => r.id)).toEqual([1, 3, 2]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
* shared/utils/table-sort.ts
|
||||
* 用途:管理端表格列排序的纯函数(比较、切换方向、按列排序)。
|
||||
* 依赖:无。
|
||||
* 逻辑:useTableSort / SortableTableHeader 复用;业务页传入取值函数即可。
|
||||
*/
|
||||
|
||||
export type SortDir = 'asc' | 'desc';
|
||||
|
||||
/** 可参与比较的原始值(空值统一排后) */
|
||||
export type SortComparable = string | number | null | undefined;
|
||||
|
||||
/**
|
||||
* 规范化比较值:字符串 trim,空串/null/undefined 视为空。
|
||||
* @param value - 原始字段值
|
||||
* @returns 规范化后的 string | number,空则为 null
|
||||
*/
|
||||
export function normalizeSortValue(value: SortComparable): string | number | null {
|
||||
if (value == null) return null;
|
||||
if (typeof value === 'number') {
|
||||
return Number.isFinite(value) ? value : null;
|
||||
}
|
||||
const trimmed = String(value).trim();
|
||||
return trimmed ? trimmed : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较两个规范化后的排序值。
|
||||
* 核心逻辑:空值排后;数字直接相减;字符串用 localeCompare(numeric)。
|
||||
* @param a - 左值
|
||||
* @param b - 右值
|
||||
* @returns 负/零/正,与 Array.sort 约定一致
|
||||
*/
|
||||
export function compareSortValues(
|
||||
a: string | number | null,
|
||||
b: string | number | null,
|
||||
): number {
|
||||
if (a == null && b == null) return 0;
|
||||
if (a == null) return 1;
|
||||
if (b == null) return -1;
|
||||
if (typeof a === 'number' && typeof b === 'number') return a - b;
|
||||
return String(a).localeCompare(String(b), undefined, {
|
||||
numeric: true,
|
||||
sensitivity: 'base',
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换排序状态:同列在升/降序间切换,换列则从升序开始。
|
||||
* @param currentKey - 当前排序列
|
||||
* @param currentDir - 当前方向
|
||||
* @param nextKey - 点击的列
|
||||
* @returns 新的 sortKey / sortDir
|
||||
*/
|
||||
export function nextSortState<K extends string>(
|
||||
currentKey: K | undefined,
|
||||
currentDir: SortDir,
|
||||
nextKey: K,
|
||||
): { sortKey: K; sortDir: SortDir } {
|
||||
if (currentKey === nextKey) {
|
||||
return {
|
||||
sortKey: nextKey,
|
||||
sortDir: currentDir === 'asc' ? 'desc' : 'asc',
|
||||
};
|
||||
}
|
||||
return { sortKey: nextKey, sortDir: 'asc' };
|
||||
}
|
||||
|
||||
export interface SortByKeyOptions<T, K extends string> {
|
||||
/** 当前排序列;未指定时返回浅拷贝原列表 */
|
||||
sortKey?: K;
|
||||
sortDir?: SortDir;
|
||||
/** 从行中取出比较值 */
|
||||
getValue: (item: T, key: K) => SortComparable;
|
||||
/** 主值相等时的稳定次序(如按 id) */
|
||||
tieBreaker?: (a: T, b: T) => number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按指定列对列表排序(不修改原数组)。
|
||||
* @param items - 源列表
|
||||
* @param options - 排序列、方向、取值与 tie-break
|
||||
* @returns 新排序数组
|
||||
*/
|
||||
export function sortByKey<T, K extends string>(
|
||||
items: readonly T[],
|
||||
options: SortByKeyOptions<T, K>,
|
||||
): T[] {
|
||||
const { sortKey, sortDir = 'asc', getValue, tieBreaker } = options;
|
||||
const list = [...items];
|
||||
if (!sortKey) return list;
|
||||
|
||||
const dir = sortDir === 'asc' ? 1 : -1;
|
||||
list.sort((a, b) => {
|
||||
const av = normalizeSortValue(getValue(a, sortKey));
|
||||
const bv = normalizeSortValue(getValue(b, sortKey));
|
||||
const cmp = compareSortValues(av, bv);
|
||||
if (cmp !== 0) return cmp * dir;
|
||||
return tieBreaker ? tieBreaker(a, b) : 0;
|
||||
});
|
||||
return list;
|
||||
}
|
||||
Reference in new issue
Block a user