Merge branch 'dev' into hugo
This commit is contained in:
commit
29a7704d46
55 files changed
+2852
-546
No files matched your search
@@ -0,0 +1,156 @@
|
||||
<script setup lang="ts">
|
||||
/**
|
||||
* shared/components/PaginationBar.vue
|
||||
* 用途:通用偏移分页条(摘要、每页条数、页码按钮)。
|
||||
* 依赖:buildOffsetPageButtons、common.pagination i18n、components-ds 样式类。
|
||||
* 逻辑:父组件 v-model:page / v-model:page-size,服务端或客户端分页均可复用。
|
||||
*/
|
||||
import { computed } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { buildOffsetPageButtons } from '@/shared/composables/useOffsetPagination';
|
||||
|
||||
interface Props {
|
||||
total: number;
|
||||
page: number;
|
||||
pageSize: number;
|
||||
/** 服务端已算好的总页数;不传则按 total/pageSize 推算 */
|
||||
pages?: number;
|
||||
pageSizeOptions?: number[];
|
||||
/** 为 false 时强制隐藏;默认 total > 0 时显示 */
|
||||
visible?: boolean;
|
||||
}
|
||||
|
||||
const props = withDefaults(defineProps<Props>(), {
|
||||
pages: 0,
|
||||
pageSizeOptions: () => [10, 20, 50, 100],
|
||||
visible: undefined,
|
||||
});
|
||||
|
||||
const emit = defineEmits<{
|
||||
(e: 'update:page', value: number): void;
|
||||
(e: 'update:pageSize', value: number): void;
|
||||
}>();
|
||||
|
||||
const { t } = useI18n();
|
||||
|
||||
const totalPages = computed(() => {
|
||||
if (props.pages > 0) return props.pages;
|
||||
const size = Math.max(1, Number(props.pageSize) || 20);
|
||||
const total = Math.max(0, Number(props.total) || 0);
|
||||
return Math.max(1, Math.ceil(total / size));
|
||||
});
|
||||
|
||||
const safePage = computed(() =>
|
||||
Math.min(Math.max(1, Number(props.page) || 1), totalPages.value),
|
||||
);
|
||||
|
||||
const pageButtons = computed(() => buildOffsetPageButtons(safePage.value, totalPages.value));
|
||||
|
||||
const showBar = computed(() => {
|
||||
if (props.visible !== undefined) return props.visible;
|
||||
return props.total > 0;
|
||||
});
|
||||
|
||||
/**
|
||||
* 跳转到指定页并通知父组件。
|
||||
* @param target - 目标页码(1-based)
|
||||
*/
|
||||
const onGoToPage = (target: number): void => {
|
||||
const next = Math.min(Math.max(1, target), totalPages.value);
|
||||
emit('update:page', next);
|
||||
};
|
||||
|
||||
/**
|
||||
* 上一页并通知父组件。
|
||||
*/
|
||||
const onPrevPage = (): void => {
|
||||
onGoToPage(safePage.value - 1);
|
||||
};
|
||||
|
||||
/**
|
||||
* 下一页并通知父组件。
|
||||
*/
|
||||
const onNextPage = (): void => {
|
||||
onGoToPage(safePage.value + 1);
|
||||
};
|
||||
|
||||
/**
|
||||
* 每页条数变更时重置到第 1 页。
|
||||
* @param size - 新的 pageSize
|
||||
*/
|
||||
const onPageSizeChange = (size: number): void => {
|
||||
emit('update:pageSize', size);
|
||||
emit('update:page', 1);
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
v-if="showBar"
|
||||
class="flex flex-col gap-3 rounded-2xl border border-default bg-surface px-4 py-3 md:flex-row md:items-center md:justify-between"
|
||||
>
|
||||
<div class="text-sm text-secondary">
|
||||
{{ t('common.pagination.summary', { total, page: safePage, pages: totalPages }) }}
|
||||
</div>
|
||||
<div class="flex flex-wrap items-center gap-2">
|
||||
<label class="flex items-center gap-2 text-xs text-secondary">
|
||||
<span>{{ t('common.pagination.perPage') }}</span>
|
||||
<span class="relative inline-flex items-center">
|
||||
<select
|
||||
:value="pageSize"
|
||||
class="admin-table-filter-input appearance-none pl-2 pr-7 py-1 text-xs leading-5"
|
||||
@change="onPageSizeChange(Number(($event.target as HTMLSelectElement).value))"
|
||||
>
|
||||
<option v-for="size in pageSizeOptions" :key="`page-size-${size}`" :value="size">
|
||||
{{ size }}
|
||||
</option>
|
||||
</select>
|
||||
<svg
|
||||
class="pointer-events-none absolute right-2 h-3.5 w-3.5 text-muted"
|
||||
viewBox="0 0 20 20"
|
||||
fill="currentColor"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<path
|
||||
fill-rule="evenodd"
|
||||
d="M5.23 7.21a.75.75 0 0 1 1.06.02L10 11.168l3.71-3.938a.75.75 0 1 1 1.08 1.04l-4.25 4.5a.75.75 0 0 1-1.08 0l-4.25-4.5a.75.75 0 0 1 .02-1.06Z"
|
||||
clip-rule="evenodd"
|
||||
/>
|
||||
</svg>
|
||||
</span>
|
||||
<span>{{ t('common.pagination.itemsUnit') }}</span>
|
||||
</label>
|
||||
<button
|
||||
type="button"
|
||||
:disabled="safePage <= 1"
|
||||
class="admin-report-page-btn"
|
||||
@click="onPrevPage"
|
||||
>
|
||||
{{ t('common.pagination.prevPage') }}
|
||||
</button>
|
||||
<button
|
||||
v-for="btn in pageButtons"
|
||||
:key="`page-btn-${btn}`"
|
||||
type="button"
|
||||
:disabled="btn === -1"
|
||||
class="admin-report-page-btn"
|
||||
:class="btn === -1
|
||||
? 'border-transparent text-muted cursor-default opacity-100'
|
||||
: btn === safePage
|
||||
? 'admin-report-page-btn--active'
|
||||
: ''"
|
||||
@click="btn !== -1 && onGoToPage(btn)"
|
||||
>
|
||||
{{ btn === -1 ? '...' : btn }}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
:disabled="safePage >= totalPages"
|
||||
class="admin-report-page-btn"
|
||||
@click="onNextPage"
|
||||
>
|
||||
{{ t('common.pagination.nextPage') }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* shared/composables/useDialogBackdropClose.ts
|
||||
* 用途:弹窗遮罩点击关闭,避免输入框拖选文字时误触关闭。
|
||||
* 依赖:无。
|
||||
* 逻辑:仅当 pointerdown 与 pointerup 均发生在遮罩自身时才触发 onClose。
|
||||
*/
|
||||
|
||||
export interface DialogBackdropCloseHandlers {
|
||||
onBackdropPointerDown: (event: PointerEvent) => void;
|
||||
onBackdropPointerUp: (event: PointerEvent) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建遮罩关闭处理器,防止从输入框拖选到遮罩外时误关弹窗。
|
||||
* @param onClose - 遮罩有效点击时的关闭回调
|
||||
* @returns pointerdown / pointerup 处理器
|
||||
*/
|
||||
export function createDialogBackdropCloseHandler(onClose: () => void): DialogBackdropCloseHandlers {
|
||||
let pointerDownOnBackdrop = false;
|
||||
|
||||
/**
|
||||
* 记录是否在遮罩上按下指针。
|
||||
* @param event - pointerdown 事件
|
||||
*/
|
||||
const onBackdropPointerDown = (event: PointerEvent): void => {
|
||||
pointerDownOnBackdrop = event.target === event.currentTarget;
|
||||
};
|
||||
|
||||
/**
|
||||
* 仅在遮罩上完成按下+抬起时关闭弹窗。
|
||||
* @param event - pointerup 事件
|
||||
*/
|
||||
const onBackdropPointerUp = (event: PointerEvent): void => {
|
||||
if (pointerDownOnBackdrop && event.target === event.currentTarget) {
|
||||
onClose();
|
||||
}
|
||||
pointerDownOnBackdrop = false;
|
||||
};
|
||||
|
||||
return { onBackdropPointerDown, onBackdropPointerUp };
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
* shared/composables/useOffsetPagination.ts
|
||||
* 用途:管理端偏移分页的页码计算与导航辅助。
|
||||
* 依赖:vue computed/ref/unref。
|
||||
* 逻辑:PaginationBar、报表页等复用 pageButtons 与翻页方法。
|
||||
*/
|
||||
import { computed, unref, type ComputedRef, type Ref } from 'vue';
|
||||
|
||||
export interface UseOffsetPaginationOptions {
|
||||
page: Ref<number>;
|
||||
pageSize: Ref<number>;
|
||||
total: Ref<number> | ComputedRef<number>;
|
||||
/** 服务端已计算总页数时传入,优先于 total/pageSize 推算 */
|
||||
pages?: Ref<number> | ComputedRef<number>;
|
||||
}
|
||||
|
||||
const DEFAULT_PAGE_SIZE_OPTIONS = [10, 20, 50, 100] as const;
|
||||
|
||||
/**
|
||||
* 根据当前页与总页数生成带省略号的页码按钮序列(-1 表示 ...)。
|
||||
* 核心逻辑:页数较多时开头仅展示 1–3 页,末尾仅展示最后 3 页,中间展示当前页邻域。
|
||||
* @param current - 当前页(1-based)
|
||||
* @param totalPages - 总页数
|
||||
*/
|
||||
export function buildOffsetPageButtons(current: number, totalPages: number): number[] {
|
||||
const total = Math.max(1, totalPages);
|
||||
const page = Math.min(Math.max(1, current), total);
|
||||
if (total <= 5) return Array.from({ length: total }, (_, i) => i + 1);
|
||||
if (page <= 3) return [1, 2, 3, -1, total];
|
||||
if (page >= total - 2) return [1, -1, total - 2, total - 1, total];
|
||||
return [1, -1, page - 1, page, page + 1, -1, total];
|
||||
}
|
||||
|
||||
/**
|
||||
* 提供偏移分页的 totalPages、range 与翻页辅助。
|
||||
* @param options.page - 当前页码 ref
|
||||
* @param options.pageSize - 每页条数 ref
|
||||
* @param options.total - 总条数 ref/computed
|
||||
* @param options.pages - 可选,服务端返回的总页数
|
||||
*/
|
||||
export function useOffsetPagination(options: UseOffsetPaginationOptions) {
|
||||
const totalPages = computed(() => {
|
||||
const explicit = options.pages ? Math.max(1, Number(unref(options.pages)) || 1) : 0;
|
||||
if (explicit > 0) return explicit;
|
||||
const size = Math.max(1, Number(unref(options.pageSize)) || 20);
|
||||
const total = Math.max(0, Number(unref(options.total)) || 0);
|
||||
return Math.max(1, Math.ceil(total / size));
|
||||
});
|
||||
|
||||
const safePage = computed(() =>
|
||||
Math.min(Math.max(1, Number(unref(options.page)) || 1), totalPages.value),
|
||||
);
|
||||
|
||||
const rangeStart = computed(() => {
|
||||
const total = Math.max(0, Number(unref(options.total)) || 0);
|
||||
if (total === 0) return 0;
|
||||
const size = Math.max(1, Number(unref(options.pageSize)) || 20);
|
||||
return (safePage.value - 1) * size + 1;
|
||||
});
|
||||
|
||||
const rangeEnd = computed(() => {
|
||||
const total = Math.max(0, Number(unref(options.total)) || 0);
|
||||
if (total === 0) return 0;
|
||||
const size = Math.max(1, Number(unref(options.pageSize)) || 20);
|
||||
return Math.min(rangeStart.value + size - 1, total);
|
||||
});
|
||||
|
||||
const pageButtons = computed(() => buildOffsetPageButtons(safePage.value, totalPages.value));
|
||||
|
||||
/**
|
||||
* 跳转到指定页,自动 clamp 到合法范围。
|
||||
* @param page - 目标页码
|
||||
*/
|
||||
const goToPage = (page: number): void => {
|
||||
options.page.value = Math.min(Math.max(1, page), totalPages.value);
|
||||
};
|
||||
|
||||
/** 上一页 */
|
||||
const prevPage = (): void => {
|
||||
goToPage(safePage.value - 1);
|
||||
};
|
||||
|
||||
/** 下一页 */
|
||||
const nextPage = (): void => {
|
||||
goToPage(safePage.value + 1);
|
||||
};
|
||||
|
||||
return {
|
||||
totalPages,
|
||||
safePage,
|
||||
rangeStart,
|
||||
rangeEnd,
|
||||
pageButtons,
|
||||
goToPage,
|
||||
prevPage,
|
||||
nextPage,
|
||||
pageSizeOptions: DEFAULT_PAGE_SIZE_OPTIONS,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* shared/types/pagination.types.ts
|
||||
* 用途:前后端通用的偏移分页响应结构。
|
||||
* 依赖:无。
|
||||
* 逻辑:分页 API 与 PaginationBar 等组件共享本类型。
|
||||
*/
|
||||
|
||||
export interface PaginatedResult<T> {
|
||||
items: T[];
|
||||
total: number;
|
||||
page: number;
|
||||
page_size: number;
|
||||
pages: number;
|
||||
}
|
||||
Reference in new issue
Block a user