add product
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
<template>
|
||||
<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, index) in categoryList"
|
||||
:key="item.id"
|
||||
class="cell"
|
||||
:title="item.name || '未命名分类'"
|
||||
v-entrance-transition="index * 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 { useNavBarStore } from "@/store/navBarStore";
|
||||
import type { ProductCategory, ProductCategoryStatus } from "@/types/Product";
|
||||
import { getErrorMessage } from "@/utils/error";
|
||||
import { Message } from "@/utils/message";
|
||||
import { permissionCodes } from "@/utils/permission";
|
||||
import { useUserStore } from "timi-web-api";
|
||||
|
||||
defineOptions({ name: "ProductCategoryRank" });
|
||||
|
||||
const navBarStore = useNavBarStore();
|
||||
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 () => {
|
||||
navBarStore.clearRight();
|
||||
if (userStore.hasPermissions(permissionCodes.productCategory.update)) {
|
||||
navBarStore.setRightText("保存", "var(--td-brand-color)", saveSort);
|
||||
}
|
||||
try {
|
||||
await loadData();
|
||||
} catch (error) {
|
||||
Message.error(getErrorMessage(error, "产品分类加载失败"));
|
||||
}
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
navBarStore.clearRight();
|
||||
});
|
||||
</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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user