Files
gao-customer-h5/src/pages/product/ProductCategoryRank.vue
T
2026-10-17 12:01:28 +08:00

305 lines
7.1 KiB
Vue

<template>
<t-navbar
fixed
:z-index="20"
title="产品分类排序"
left-arrow
@left-click="$router.back()"
>
<template #right>
<div
v-if="pageNavBarRight.icon || pageNavBarRight.text"
class="nav-right-slot"
>
<t-badge
v-if="pageNavBarRight.icon && pageNavBarRight.badge !== undefined"
class="badge"
:count="pageNavBarRight.badge"
:max-count="99"
:show-zero="false"
>
<button class="button" type="button" :disabled="pageNavBarRight.disabled" @click="handlePageNavBarRightAction">
<t-icon :name="pageNavBarRight.icon" size="1.25rem" />
</button>
</t-badge>
<button
v-else-if="pageNavBarRight.icon"
class="button"
type="button"
:disabled="pageNavBarRight.disabled"
@click="handlePageNavBarRightAction"
>
<t-icon :name="pageNavBarRight.icon" size="1.25rem" />
</button>
<button
v-else
class="nav-text"
type="button"
:disabled="pageNavBarRight.disabled"
:style="{ color: pageNavBarRight.disabled ? 'var(--td-text-color-disabled)' : pageNavBarRight.textColor || 'var(--td-text-color-primary, #333)' }"
:class="{ clickable: !!pageNavBarRight.action, disabled: pageNavBarRight.disabled }"
@click="handlePageNavBarRightAction"
v-text="pageNavBarRight.text"
/>
</div>
</template>
</t-navbar>
<div v-can="permissionCodes.productCategory.update" class="product-category-rank">
<div class="tip" v-text="'按住右侧拖动手柄调整分类顺序,保存后生效'" />
<div class="items">
<t-cell-group v-if="categoryList.length" theme="card">
<vue-draggable-next
v-model="categoryList"
class="draggable"
item-key="id"
handle=".handle"
ghost-class="ghost"
chosen-class="chosen"
drag-class="dragging"
:animation="180"
:disabled="isLoading || isSaving"
@change="handleSortChange"
>
<t-cell
v-for="item in categoryList"
:key="item.id"
class="cell"
:title="item.name || '未命名分类'"
v-entrance-transition="{ step: 30 }"
>
<template #note>
<t-tag
:theme="getStatusTheme(item.status)"
variant="outline"
:content="getStatusText(item.status)"
/>
</template>
<template #rightIcon>
<t-icon class="handle" role="button" name="move" aria-label="拖动排序" />
</template>
</t-cell>
</vue-draggable-next>
</t-cell-group>
<div v-else class="empty-block" v-text="isLoading ? '正在加载产品分类' : '暂无产品分类'" />
</div>
</div>
</template>
<script setup lang="ts">
import type { TagProps } from "tdesign-mobile-vue";
import { VueDraggableNext } from "vue-draggable-next";
import ProductAPI from "@/api/ProductAPI";
import { getErrorMessage } from "@/utils/error";
import { Message } from "@/utils/message";
import { permissionCodes } from "@/utils/permission";
import { useUserStore } from "timi-web-api";
const pageNavBarRight = reactive({
icon: "",
badge: undefined as number | undefined,
text: "",
textColor: "",
disabled: false,
action: undefined as (() => void | Promise<void>) | undefined
});
function setPageNavBarRightText(value?: string, color?: string, action?: () => void | Promise<void>): void {
pageNavBarRight.text = value || "";
pageNavBarRight.textColor = color || "";
pageNavBarRight.disabled = false;
pageNavBarRight.action = action;
if (value) {
pageNavBarRight.badge = undefined;
pageNavBarRight.icon = "";
}
}
function clearPageNavBarRight(): void {
Object.assign(pageNavBarRight, { icon: "", badge: undefined, text: "", textColor: "", disabled: false, action: undefined });
}
async function handlePageNavBarRightAction(): Promise<void> {
if (pageNavBarRight.disabled || !pageNavBarRight.action) {
return;
}
await pageNavBarRight.action();
}
import type { ProductCategory, ProductCategoryStatus } from "@/types/Product";
defineOptions({ name: "ProductCategoryRank" });
const userStore = useUserStore();
const router = useRouter();
const categoryList = ref<ProductCategory[]>([]);
const isLoading = ref(false);
const isSaving = ref(false);
const sortDirty = ref(false);
function getStatusText(status?: ProductCategoryStatus): string {
return status === "ACTIVE" ? "启用" : "停用";
}
function getStatusTheme(status?: ProductCategoryStatus): TagProps["theme"] {
return status === "ACTIVE" ? "primary" : "default";
}
function handleSortChange(): void {
sortDirty.value = true;
}
async function saveSort(): Promise<void> {
if (!sortDirty.value || isSaving.value || !userStore.hasPermissions(permissionCodes.productCategory.update)) {
return;
}
isSaving.value = true;
try {
const idList = categoryList.value
.map(item => item.id)
.filter((id): id is string => Boolean(id));
await ProductAPI.categorySort(idList);
categoryList.value.forEach((item, index) => {
item.sort = index;
});
sortDirty.value = false;
Message.success("产品分类排序已保存");
await router.back();
} catch (error) {
Message.error(getErrorMessage(error, "产品分类排序保存失败"));
} finally {
isSaving.value = false;
}
}
async function loadData(): Promise<void> {
isLoading.value = true;
try {
const result = await ProductAPI.categoryList({ index: 0, size: 1000 });
categoryList.value = result.list || [];
sortDirty.value = false;
} finally {
isLoading.value = false;
}
}
onMounted(async () => {
clearPageNavBarRight();
if (userStore.hasPermissions(permissionCodes.productCategory.update)) {
setPageNavBarRightText("保存", "var(--td-brand-color)", saveSort);
}
try {
await loadData();
} catch (error) {
Message.error(getErrorMessage(error, "产品分类加载失败"));
}
});
onUnmounted(() => {
clearPageNavBarRight();
});
</script>
<style scoped lang="less">
.product-category-rank {
padding: calc(var(--app-nav-height) + 1rem) 0 6rem;
.tip {
color: #52607a;
padding: 0 1rem .75rem;
font-size: .8125rem;
}
.items {
.draggable {
.cell {
position: relative;
.handle {
color: #52607a;
cursor: grab;
touch-action: none;
}
}
.ghost {
opacity: .4;
}
.chosen,
.dragging {
z-index: 1;
box-shadow: 0 .5rem 1rem rgba(15, 23, 42, .16);
}
}
}
.empty-block {
color: #52607a;
padding: 2rem 1rem;
text-align: center;
}
}
:global(html.theme-dark) {
.product-category-rank {
.tip,
.empty-block,
.handle {
color: #94a3b8;
}
.draggable {
.cell {
background: rgba(15, 23, 42, .88);
box-shadow: 0 .5rem 1rem rgba(2, 6, 23, .28);
}
.ghost {
background: rgba(15, 23, 42, .6);
}
}
}
}
.nav-right-slot {
width: 6rem;
height: 100%;
display: flex;
align-items: center;
justify-content: flex-end;
.badge {
:deep(.t-badge__content) {
display: flex;
align-items: center;
}
}
.button,
.nav-text {
color: var(--td-text-color-primary, #333);
border: none;
padding: 0;
display: inline-flex;
background: transparent;
align-items: center;
justify-content: center;
}
.button {
&:disabled {
color: var(--td-text-color-disabled);
cursor: not-allowed;
}
}
.nav-text {
font: inherit;
cursor: default;
&.clickable { cursor: pointer; }
&.disabled { cursor: not-allowed; }
}
}
</style>