add TagDetail

This commit is contained in:
Timi
2026-10-21 11:20:12 +08:00
parent 0a283bf8e9
commit dd09e500d6
3 changed files with 485 additions and 29 deletions
+465
View File
@@ -0,0 +1,465 @@
<template>
<div class="page-layout navbar tag-detail">
<t-navbar
fixed
:z-index="20"
title="标签详情"
left-arrow
@left-click="$router.back()"
/>
<main v-can="corePermissionCodes.tag.read" class="page-content scroll list">
<t-pull-down-refresh
class="pull-refresh"
v-model="isRefreshing"
:refresh-timeout="30000"
@refresh="refresh"
@scrolltolower="loadMore"
>
<section v-if="tag" class="tag-card">
<div class="field">
<span class="label">标签名称</span>
<t-input
v-model.trim="tagName"
class="name-input"
borderless
:readonly="!canUpdateTag"
placeholder="请输入标签名称"
/>
<t-button
v-if="canUpdateTag"
class="save"
theme="primary"
size="small"
:loading="isSaving"
:disabled="!isNameChanged || isSaving"
@click="saveTag"
>
保存
</t-button>
</div>
<div class="usage" v-text="tagUsageText" />
</section>
<center-tips v-else :content="isTagLoading ? '正在加载标签' : '标签不存在或无权查看'" />
<section v-if="canReadCustomer" class="customer-section">
<div class="head">
<h2 class="title" v-text="`使用客户(${tag?.useCount ?? 0})`" />
<t-button
v-if="canManageTagCustomers"
size="small"
theme="primary"
:disabled="!tagId || isApplyingTag"
@click="customerSelectorVisible = true"
>
添加客户
</t-button>
</div>
<t-input
v-model.trim="keyword"
class="search"
borderless
clearable
placeholder="搜索编码、姓名(拼音)、电话"
/>
<t-cell-group v-if="customerList.length" class="customers" theme="card">
<t-cell
v-for="item in customerList"
:key="item.id"
:title="getCustomerName(item)"
:note="item.code || ''"
hover
@click="openCustomer(item)"
>
<template #description>
<telephone-link :value="item.telephone" placeholder="未填写电话" />
</template>
<template v-if="canManageTagCustomers" #rightIcon>
<t-button theme="danger" size="extra-small" @click.stop="confirmRemoveCustomer(item)">
移除
</t-button>
</template>
</t-cell>
</t-cell-group>
<center-tips v-else :content="customerEmptyText" />
<center-tips
v-if="customerList.length"
:content="isLoadingCustomers ? '正在加载更多' : isNoMore ? '已经到底了' : '上拉加载更多'"
/>
</section>
<center-tips v-else content="暂无客户查看权限" />
</t-pull-down-refresh>
<customer-selector
v-if="canManageTagCustomers"
v-model="selectedCustomerId"
v-model:visible="customerSelectorVisible"
selection-mode="single"
:enable-scan="false"
@change="addCustomer"
/>
</main>
</div>
</template>
<script setup lang="ts">
import { DialogPlugin } from "tdesign-mobile-vue";
import { useUserStore } from "timi-web-api";
import CustomerAPI from "@/api/CustomerAPI";
import TagAPI from "@/api/TagAPI";
import CustomerSelector from "@/components/CustomerSelector.vue";
import type { Customer } from "@/types/Customer";
import type { Tag } from "@/types/Tag";
import { Message } from "@/utils/message";
import { corePermissionCodes, permissionCodes } from "@/utils/permission";
defineOptions({
name: "TagDetail"
});
const route = useRoute();
const router = useRouter();
const userStore = useUserStore();
// ---------- 标签信息 ----------
const tagId = computed(() => String(route.params.id || ""));
const tag = ref<Tag>();
const tagName = ref("");
const isTagLoading = ref(false);
const isSaving = ref(false);
let tagRequestToken = 0;
const canUpdateTag = computed(() => userStore.hasPermissions(corePermissionCodes.tag.update));
const canApplyTag = computed(() => userStore.hasPermissions(corePermissionCodes.tag.apply));
const canReadCustomer = computed(() => userStore.hasPermissions(permissionCodes.customer.read));
const canManageTagCustomers = computed(() => canApplyTag.value && canReadCustomer.value);
const isNameChanged = computed(() => !!tag.value && !!tagName.value.trim() && tagName.value.trim() !== getTagName(tag.value));
const tagUsageText = computed(() => `共 ${tag.value?.useCount ?? 0} 位客户使用`);
function getTagName(value: Tag): string {
return value.name || value.zhCN || "未命名标签";
}
async function loadTag(): Promise<void> {
const id = tagId.value;
if (!id) {
tag.value = undefined;
return;
}
const token = ++tagRequestToken;
isTagLoading.value = true;
try {
const result = await TagAPI.detail(id);
if (token !== tagRequestToken) {
return;
}
tag.value = result;
tagName.value = result ? getTagName(result) : "";
} catch {
if (token === tagRequestToken) {
tag.value = undefined;
Message.error("标签加载失败");
}
} finally {
if (token === tagRequestToken) {
isTagLoading.value = false;
}
}
}
async function saveTag(): Promise<void> {
const id = tagId.value;
const name = tagName.value.trim();
if (!id || !name || !isNameChanged.value || isSaving.value) {
return;
}
isSaving.value = true;
try {
await TagAPI.update({
id,
name,
zhCN: name
});
tag.value = tag.value ? { ...tag.value, name, zhCN: name } : undefined;
Message.success("标签已保存");
} catch {
Message.error("标签保存失败");
} finally {
isSaving.value = false;
}
}
// ---------- 客户列表 ----------
const pageSize = 20;
const keyword = ref("");
const customerList = ref<Customer[]>([]);
const customerTotal = ref(0);
const page = reactive({ index: 0, size: pageSize });
const isLoadingCustomers = ref(false);
const isNoMore = ref(false);
const isRefreshing = ref(false);
const hasCustomerLoadError = ref(false);
let customerRequestToken = 0;
let keywordTimer: number | undefined;
const customerEmptyText = computed(() => {
if (isLoadingCustomers.value) {
return "正在加载客户";
}
return hasCustomerLoadError.value ? "客户加载失败,请下拉刷新" : "暂无使用该标签的客户";
});
function getCustomerName(customer: Customer): string {
return customer.name || customer.code || "未命名客户";
}
async function fetchCustomers(reset = false): Promise<void> {
if (!canReadCustomer.value || !tagId.value) {
customerList.value = [];
customerTotal.value = 0;
return;
}
if (!reset && (isLoadingCustomers.value || isNoMore.value)) {
return;
}
if (reset) {
page.index = 0;
customerList.value = [];
customerTotal.value = 0;
isNoMore.value = false;
hasCustomerLoadError.value = false;
}
const token = ++customerRequestToken;
isLoadingCustomers.value = true;
try {
const result = await CustomerAPI.list({
index: page.index,
size: page.size,
keyword: keyword.value.trim() || undefined,
tagIdList: [tagId.value]
});
if (token !== customerRequestToken) {
return;
}
const list = result.list || [];
customerList.value = reset ? list : customerList.value.concat(list);
customerTotal.value = result.total || 0;
hasCustomerLoadError.value = false;
page.index += 1;
isNoMore.value = list.length < page.size || customerList.value.length >= customerTotal.value;
} catch {
if (token === customerRequestToken) {
hasCustomerLoadError.value = true;
Message.error("客户列表加载失败");
}
} finally {
if (token === customerRequestToken) {
isLoadingCustomers.value = false;
}
}
}
async function loadMore(): Promise<void> {
await fetchCustomers();
}
async function refresh(): Promise<void> {
isRefreshing.value = true;
try {
await Promise.all([loadTag(), fetchCustomers(true)]);
} finally {
isRefreshing.value = false;
}
}
async function reload(): Promise<void> {
await Promise.all([loadTag(), fetchCustomers(true)]);
}
async function openCustomer(customer: Customer): Promise<void> {
if (!customer.id) {
return;
}
await router.push({ name: "CustomerDetail", params: { id: customer.id } });
}
// ---------- 客户标签维护 ----------
const customerSelectorVisible = ref(false);
const selectedCustomerId = ref("");
const isApplyingTag = ref(false);
const isRemovingTag = ref(false);
async function addCustomer(customerId: string): Promise<void> {
const id = tagId.value;
if (!id || !customerId || !canManageTagCustomers.value || isApplyingTag.value) {
return;
}
isApplyingTag.value = true;
try {
const currentTagList = await TagAPI.listByBizType("GAO_CUSTOMER", customerId);
const currentTagIdList = currentTagList.map(item => item.id || "").filter(Boolean);
if (currentTagIdList.includes(id)) {
Message.warning("该客户已使用此标签");
return;
}
await TagAPI.apply({
bizType: "GAO_CUSTOMER",
bizId: customerId,
tagIdList: [...currentTagIdList, id]
});
selectedCustomerId.value = "";
Message.success("客户已添加");
await reload();
} catch {
Message.error("添加客户失败");
} finally {
selectedCustomerId.value = "";
isApplyingTag.value = false;
}
}
function confirmRemoveCustomer(customer: Customer): void {
if (!customer.id || isRemovingTag.value || !canManageTagCustomers.value) {
return;
}
const currentTagId = tagId.value;
const customerName = getCustomerName(customer);
const dialog = DialogPlugin.confirm({
title: "移除标签",
content: `确定将 ${customerName} 从标签中移除吗?`,
cancelBtn: "取消",
confirmBtn: "移除",
onCancel() {
dialog.destroy();
},
async onConfirm() {
dialog.destroy();
await removeCustomerTag(customer.id || "", currentTagId);
}
});
}
async function removeCustomerTag(customerId: string, id: string): Promise<void> {
if (!id || !customerId || isRemovingTag.value) {
return;
}
isRemovingTag.value = true;
try {
const currentTagList = await TagAPI.listByBizType("GAO_CUSTOMER", customerId);
const nextTagIdList = currentTagList.map(item => item.id || "").filter(item => item && item !== id);
await TagAPI.apply({
bizType: "GAO_CUSTOMER",
bizId: customerId,
tagIdList: nextTagIdList
});
Message.success("已从该客户移除标签");
await reload();
} catch {
Message.error("移除标签失败");
} finally {
isRemovingTag.value = false;
}
}
watch(tagId, async () => {
tag.value = undefined;
tagName.value = "";
keyword.value = "";
customerSelectorVisible.value = false;
selectedCustomerId.value = "";
await reload();
}, { immediate: true });
watch(keyword, () => {
window.clearTimeout(keywordTimer);
keywordTimer = window.setTimeout(async () => {
await fetchCustomers(true);
}, 300);
});
onUnmounted(() => {
window.clearTimeout(keywordTimer);
tagRequestToken++;
customerRequestToken++;
});
</script>
<style scoped lang="less">
.tag-detail {
padding: var(--app-nav-height) 0 1rem;
.tag-card,
.customer-section {
margin: 0 1rem 1rem;
background: var(--td-bg-color-container, #fff);
border-radius: .875rem;
}
.tag-card {
padding: 1rem;
.field {
gap: .5rem;
display: flex;
align-items: center;
.label {
flex: none;
color: var(--td-text-color-secondary);
}
.name-input {
flex: 1;
border: 1px solid var(--td-component-border);
min-width: 0;
border-radius: .75rem;
}
.save {
flex: none;
}
}
.usage {
color: var(--td-text-color-secondary);
font-size: .875rem;
margin-top: .75rem;
}
}
.customer-section {
padding: 1rem;
.head {
gap: .5rem;
display: flex;
align-items: center;
justify-content: space-between;
}
.title {
margin: 0;
font-size: 1rem;
font-weight: 600;
}
.search {
border: 1px solid var(--td-component-border);
margin-top: .75rem;
border-radius: .75rem;
}
.customers {
margin-top: .5rem;
}
}
}
</style>
+19 -29
View File
@@ -68,9 +68,9 @@
class="item bg-white"
v-for="item in tagList"
:key="item.id"
@click="openEditDialog(item)"
@click="openDetail(item)"
>
<span class="name" v-text="getTagName(item)" />
<span class="name" v-text="`${getTagName(item)}(${item.useCount ?? 0})`" />
<button
v-if="canDeleteTag"
type="button"
@@ -86,11 +86,11 @@
</t-pull-down-refresh>
</main>
<t-dialog
v-model:visible="editDialogVisible"
:title="isCreateMode ? '新增标签' : '编辑标签'"
v-model:visible="createDialogVisible"
title="新增标签"
cancel-btn="取消"
confirm-btn="保存"
@cancel="closeEditDialog"
@cancel="closeCreateDialog"
@confirm="saveTag"
>
<div class="edit-dialog">
@@ -151,6 +151,7 @@ defineOptions({
});
const userStore = useUserStore();
const router = useRouter();
const pageSize = 100;
const keyword = ref("");
@@ -159,7 +160,7 @@ const isLoading = ref(false);
const isSaving = ref(false);
const isRefreshing = ref(false);
const isNoMore = ref(false);
const editDialogVisible = ref(false);
const createDialogVisible = ref(false);
const total = ref(0);
const page = reactive<Page<Tag>>({
index: 0,
@@ -168,13 +169,10 @@ const page = reactive<Page<Tag>>({
let keywordTimer: number | undefined;
let requestToken = 0;
const formModel = reactive({
id: "",
name: ""
});
const canUpdateTag = computed(() => userStore.hasPermissions(corePermissionCodes.tag.update));
const canDeleteTag = computed(() => userStore.hasPermissions(corePermissionCodes.tag.delete));
const isCreateMode = computed(() => !formModel.id);
const emptyText = computed(() => {
if (isLoading.value) {
return "正在加载标签";
@@ -241,22 +239,19 @@ async function reload(): Promise<void> {
}
function openCreateDialog(): void {
formModel.id = "";
formModel.name = "";
editDialogVisible.value = true;
createDialogVisible.value = true;
}
function openEditDialog(tag: Tag): void {
if (!canUpdateTag.value) {
async function openDetail(tag: Tag): Promise<void> {
if (!tag.id) {
return;
}
formModel.id = tag.id || "";
formModel.name = getTagName(tag);
editDialogVisible.value = true;
await router.push({ name: "TagDetail", params: { id: tag.id } });
}
function closeEditDialog(): void {
editDialogVisible.value = false;
function closeCreateDialog(): void {
createDialogVisible.value = false;
}
function validateForm(): boolean {
@@ -274,16 +269,11 @@ async function saveTag(): Promise<void> {
isSaving.value = true;
try {
const payload: Tag = {
id: formModel.id || undefined,
name: formModel.name.trim(),
zhCN: formModel.name.trim()
};
if (isCreateMode.value) {
await TagAPI.create(payload);
} else {
await TagAPI.update(payload);
}
editDialogVisible.value = false;
await TagAPI.create(payload);
createDialogVisible.value = false;
await reload();
Message.success("标签已保存");
} catch {
@@ -362,14 +352,14 @@ onUnmounted(() => {
.item {
flex: 1 1 auto;
color: var(--td-brand-color);
width: fit-content;
border: 1px solid var(--td-brand-color);
cursor: pointer;
margin: 0;
display: flex;
min-width: 5rem;
max-width: 100%;
padding: .25rem .5rem;
min-width: 0;
font-size: 1rem;
min-height: 2rem;
border: 1px solid var(--td-brand-color);
align-items: center;
border-radius: .5rem;
justify-content: center;
+1
View File
@@ -9,6 +9,7 @@ export type Tag = {
nameLangId?: string;
name?: string;
zhCN?: string;
useCount?: number;
} & Model;
export type TagApply = {