front/src/views/user/customer/index.vue
zzh 50aed33e19 feat: 客户余额人工调整留痕,并修复 OMS 代登 SSO 链路
编辑客户余额改为只读;列表「调整余额」写账户流水。代登改为落到 /login,守卫优先消费 sso,避免官网首页吞掉 token。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-11 17:34:43 +08:00

1500 lines
50 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<div class="customer-page">
<div class="customer-page-card">
<!-- 表格工具栏:左侧筛选 + 搜索/新增,右侧分页 -->
<div class="table-toolbar">
<div class="toolbar-left">
<a-space wrap :size="12">
<a-input
v-model="filterState.keyword"
allow-clear
:placeholder="t('user.customer.searchPlaceholder')"
style="width: 200px"
@press-enter="handleSearch"
/>
<a-select
v-model="filterState.agentId"
:placeholder="t('user.customer.fields.agent')"
allow-clear
allow-search
style="width: 180px"
>
<a-option
v-for="option in agentOptions"
:key="`agent-filter-${option.value}`"
:value="option.value"
>
{{ option.label }}
</a-option>
</a-select>
<a-select
v-model="filterState.customerService"
:placeholder="t('user.customer.fields.customerService')"
allow-clear
allow-search
style="width: 160px"
>
<a-option
v-for="option in userOptions"
:key="`cs-${option.value}`"
:value="option.value"
>
{{ option.label }}
</a-option>
</a-select>
<a-button class="app-btn-search" @click="handleSearch">
{{ t('user.customer.actions.search') }}
</a-button>
<a-button class="btn-add app-btn-primary-soft" @click="handleAdd">{{ t('user.customer.actions.add') }}
</a-button>
</a-space>
</div>
<div class="toolbar-right">
<div class="pagination-controls">
<PaginationPro
:total-count="totalCount"
v-model:current-page="currentPage"
v-model:page-size="pageSize"
@page-change="handlePageChange"
@page-size-change="handlePageSizeChange"
/>
</div>
</div>
</div>
<!-- 列表区域:固定高度 + mousedown 捕获,避免状态开关与 Handsontable 抢事件 -->
<div ref="tableContainerRef" class="list-wrap" @mousedown.capture="handleTableMouseDown">
<HandsontablePro
ref="handsontableRef"
:columns="hotColumns"
:data="processedTableData"
:total="totalCount"
:current-page="currentPage"
:page-size="pageSize"
:show-action-column="true"
:action-renderer="actionRenderer"
:custom-cell-renderer="customCellRenderer"
:stretch-columns="true"
:height="handsontableHeight"
:search-params="headerSearchParams"
sort-mode="remote"
:sort-field="listSortField === 'balance' ? 'accountBalance' : listSortField"
:sort-order="listSortOrder"
@search="handleHeaderSearch"
@sort-change="handleSortChange"
@action-click="handleActionClick"
/>
</div>
</div>
<!-- 编辑弹窗 -->
<a-modal
v-model:visible="modalVisible"
:title="isEdit ? t('user.customer.modal.editTitle') : t('user.customer.modal.addTitle')"
:ok-loading="modalLoading"
:width="720"
:mask-closable="false"
unmount-on-close
@before-ok="handleModalOk"
@cancel="handleModalCancel"
>
<div class="edit-form-body">
<a-form
ref="formRef"
:model="formState"
:rules="formRules"
layout="vertical"
class="edit-form"
>
<div class="form-section-title">{{ t('user.customer.section.basic') }}</div>
<a-row :gutter="16">
<a-col :span="12">
<a-form-item v-if="isEdit" field="customerCode">
<template #label>
<span class="label-with-tip">
{{ t('user.customer.fields.customerCode') }}
<a-tooltip :content="t('user.customer.tip.customerCode')">
<icon-question-circle class="tip-icon" />
</a-tooltip>
</span>
</template>
<a-input v-model="formState.customerCode" disabled />
</a-form-item>
<a-form-item v-else>
<template #label>{{ t('user.customer.fields.customerCode') }}</template>
<span class="customer-code-auto-hint">{{ t('user.customer.hint.autoCustomerCode') }}</span>
</a-form-item>
</a-col>
<a-col :span="12">
<a-form-item field="customerName" required>
<template #label>{{ t('user.customer.fields.customerName') }}</template>
<a-input
v-model="formState.customerName"
:placeholder="t('user.customer.placeholder.customerName')"
allow-clear
/>
</a-form-item>
</a-col>
</a-row>
<a-row :gutter="16">
<a-col :span="isEdit ? 24 : 12">
<a-form-item field="status">
<template #label>
<span class="label-with-tip">
{{ t('user.customer.fields.status') }}
<a-tooltip :content="t('user.customer.tip.status')">
<icon-question-circle class="tip-icon" />
</a-tooltip>
</span>
</template>
<div class="switch-with-text">
<a-switch
:model-value="formState.status === CUSTOMER_STATUS.normal"
@update:model-value="
(v: boolean) => {
formState.status = v ? CUSTOMER_STATUS.normal : CUSTOMER_STATUS.disabled
}
"
/>
<span class="switch-text">
{{
formState.status === CUSTOMER_STATUS.normal
? t('user.customer.status.enabled')
: t('user.customer.status.disabled')
}}
</span>
</div>
</a-form-item>
</a-col>
<a-col v-if="!isEdit" :span="12">
<a-form-item field="password" required>
<template #label>
<span class="label-with-tip">
{{ t('user.customer.fields.password') }}
<a-tooltip :content="t('user.customer.tip.password')">
<icon-question-circle class="tip-icon" />
</a-tooltip>
</span>
</template>
<a-input-password
v-model="formState.password"
:placeholder="t('user.customer.placeholder.password')"
allow-clear
/>
</a-form-item>
</a-col>
</a-row>
<div class="form-section-title">{{ t('user.customer.section.contact') }}</div>
<a-row :gutter="16">
<a-col :span="12">
<a-form-item field="phone">
<template #label>{{ t('user.customer.fields.phone') }}</template>
<a-input
v-model="formState.phone"
:placeholder="t('user.customer.placeholder.phone')"
allow-clear
/>
</a-form-item>
</a-col>
<a-col :span="12">
<a-form-item field="email">
<template #label>{{ t('user.customer.fields.email') }}</template>
<a-input
v-model="formState.email"
:placeholder="t('user.customer.placeholder.email')"
allow-clear
/>
</a-form-item>
</a-col>
</a-row>
<a-row :gutter="16">
<a-col :span="12">
<a-form-item field="wechatNickname">
<template #label>{{ t('user.customer.fields.wechatNickname') }}</template>
<a-input
v-model="formState.wechatNickname"
:placeholder="t('user.customer.placeholder.wechatNickname')"
allow-clear
/>
</a-form-item>
</a-col>
<a-col :span="12">
<a-form-item field="wechatAvatarUrl">
<template #label>
<span class="label-with-tip">
{{ t('user.customer.fields.wechatAvatar') }}
<a-tooltip :content="t('user.customer.tip.wechatAvatar')">
<icon-question-circle class="tip-icon" />
</a-tooltip>
</span>
</template>
<a-input
v-model="formState.wechatAvatarUrl"
:placeholder="t('user.customer.placeholder.wechatAvatarUrl')"
allow-clear
/>
</a-form-item>
</a-col>
</a-row>
<div class="form-section-title">{{ t('user.customer.section.account') }}</div>
<a-row :gutter="16">
<a-col :span="8">
<a-form-item field="accountBalance">
<template #label>
<span class="label-with-tip">
{{ t('user.customer.fields.accountBalance') }}
<a-tooltip :content="t('user.customer.tip.accountBalance')">
<icon-question-circle class="tip-icon" />
</a-tooltip>
</span>
</template>
<a-input-number
v-model="formState.accountBalance"
:placeholder="t('user.customer.placeholder.amount')"
:precision="2"
:disabled="isEdit"
hide-button
style="width: 100%"
/>
</a-form-item>
</a-col>
<a-col :span="8">
<a-form-item field="totalQuota">
<template #label>
<span class="label-with-tip">
{{ t('user.customer.fields.totalQuota') }}
<a-tooltip :content="t('user.customer.tip.totalQuota')">
<icon-question-circle class="tip-icon" />
</a-tooltip>
</span>
</template>
<a-input-number
v-model="formState.totalQuota"
:placeholder="t('user.customer.placeholder.amount')"
:precision="2"
hide-button
style="width: 100%"
/>
</a-form-item>
</a-col>
<a-col :span="8">
<a-form-item field="usedQuota">
<template #label>
<span class="label-with-tip">
{{ t('user.customer.fields.usedQuota') }}
<a-tooltip :content="t('user.customer.tip.usedQuota')">
<icon-question-circle class="tip-icon" />
</a-tooltip>
</span>
</template>
<a-input-number
v-model="formState.usedQuota"
:placeholder="t('user.customer.placeholder.amount')"
:precision="2"
hide-button
style="width: 100%"
/>
</a-form-item>
</a-col>
</a-row>
<div class="form-section-title">{{ t('user.customer.section.level') }}</div>
<a-row :gutter="16">
<a-col :span="12">
<a-form-item field="memberLevelId">
<template #label>
<span class="label-with-tip">
{{ t('user.customer.fields.memberLevel') }}
<a-tooltip :content="t('user.customer.tip.memberLevel')">
<icon-question-circle class="tip-icon" />
</a-tooltip>
</span>
</template>
<a-select
v-model="formState.memberLevelId"
:placeholder="t('user.customer.placeholder.select')"
allow-clear
>
<a-option
v-for="option in memberLevelOptions"
:key="option.value"
:value="option.value"
>
{{ option.label }}
</a-option>
</a-select>
</a-form-item>
</a-col>
<a-col :span="12">
<a-form-item field="priceTierKey">
<template #label>
<span class="label-with-tip">
{{ t('user.customer.fields.priceTierKey') }}
<a-tooltip :content="t('user.customer.tip.priceTierKey')">
<icon-question-circle class="tip-icon" />
</a-tooltip>
</span>
</template>
<a-select
v-model="formState.priceTierKey"
:options="priceTierOptions"
allow-search
allow-create
allow-clear
:placeholder="t('user.customer.placeholder.priceTierKey')"
/>
</a-form-item>
</a-col>
</a-row>
<a-row :gutter="16">
<a-col :span="12">
<a-form-item field="agentId">
<template #label>{{ t('user.customer.fields.agent') }}</template>
<a-select
v-model="formState.agentId"
:placeholder="t('user.customer.placeholder.select')"
allow-clear
allow-search
:disabled="isEdit && formState.parentCustomerId != null"
>
<a-option
v-for="option in agentOptions"
:key="`agent-form-${option.value}`"
:value="option.value"
>
{{ option.label }}
</a-option>
</a-select>
</a-form-item>
</a-col>
<a-col :span="12">
<a-form-item field="customerService">
<template #label>{{ t('user.customer.fields.customerService') }}</template>
<a-select
v-model="formState.customerService"
:placeholder="t('user.customer.placeholder.select')"
allow-clear
allow-search
>
<a-option
v-for="option in userOptions"
:key="option.value"
:value="option.value"
>
{{ option.label }}
</a-option>
</a-select>
</a-form-item>
</a-col>
</a-row>
<a-form-item field="remark">
<template #label>{{ t('user.customer.fields.remark') }}</template>
<a-textarea
v-model="formState.remark"
:placeholder="t('user.customer.placeholder.remark')"
:auto-size="{ minRows: 2, maxRows: 4 }"
allow-clear
/>
</a-form-item>
</a-form>
</div>
</a-modal>
<a-modal
v-model:visible="resetPwdVisible"
:title="t('user.customer.actions.resetPassword')"
@ok="handleResetPwdSubmit"
@cancel="resetPwdVisible = false"
>
<a-input-password v-model="resetPwdForm.newPassword" :placeholder="t('user.customer.placeholder.input')" />
</a-modal>
<a-modal
v-model:visible="openapiVisible"
:title="t('user.customer.actions.openapi')"
:footer="false"
:width="560"
unmount-on-close
@cancel="openapiVisible = false"
>
<CustomerOpenApiCredentialPanel
v-if="openapiVisible && openapiCustomerId != null"
:customer-id="openapiCustomerId"
/>
</a-modal>
<a-modal
v-model:visible="adjustBalanceVisible"
:title="t('user.customer.actions.adjustBalance')"
:ok-loading="adjustBalanceLoading"
unmount-on-close
:on-before-ok="handleAdjustBalanceSubmit"
@cancel="adjustBalanceVisible = false"
>
<a-form :model="adjustBalanceForm" layout="vertical">
<a-form-item :label="t('user.customer.fields.customerCode')">
<a-input :model-value="adjustBalanceTarget?.customerCode || ''" disabled />
</a-form-item>
<a-form-item :label="t('user.customer.fields.accountBalance')">
<a-input :model-value="adjustBalanceCurrentDisplay" disabled />
</a-form-item>
<a-form-item
:label="t('user.customer.fields.adjustAmount')"
required
>
<a-input-number
v-model="adjustBalanceForm.amount"
:placeholder="t('user.customer.placeholder.adjustAmount')"
:precision="2"
hide-button
style="width: 100%"
/>
</a-form-item>
<a-form-item
:label="t('user.customer.fields.adjustRemark')"
required
>
<a-textarea
v-model="adjustBalanceForm.remark"
:placeholder="t('user.customer.placeholder.adjustRemark')"
:max-length="512"
show-word-limit
:auto-size="{ minRows: 3, maxRows: 6 }"
/>
</a-form-item>
<p class="adjust-balance-hint">{{ t('user.customer.tip.adjustBalance') }}</p>
</a-form>
</a-modal>
</div>
</template>
<script setup lang="ts">
import { renderWarehouseActionBtn, wrapWarehouseActions } from '@/utils/warehouseActionRenderer'
import { ref, reactive, computed, onMounted } from 'vue'
import { useI18n } from 'vue-i18n'
import { Message, Modal } from '@arco-design/web-vue'
import { IconPlus, IconQuestionCircle } from '@arco-design/web-vue/es/icon'
import HandsontablePro from '@/components/HandsontablePro/index.vue'
import PaginationPro from '@/components/PaginationPro/index.vue'
import { useTableContainerHeight } from '@/composables/useTableContainerHeight'
import type { CustomerDto } from '@/api/customer'
import { CustomerService } from '@/api/customer'
import { AgentAdminApi, type AgentDto } from '@/api/agent'
import { resolveAvatarUrl } from '@/utils/avatar'
import { RoleService } from '@/api/rbac/generated/services/RoleService'
import { UserService } from '@/api/rbac/generated/services/UserService'
import {
DEFAULT_CUSTOMER_PASSWORD,
isLetterAndDigitPassword,
} from '@/utils/customerPassword'
import {
CUSTOMER_STATUS,
isCustomerStatusNormal,
toCustomerStatus,
} from '@/utils/customerStatus'
import { getMemberLevelOptions } from '@/api/marketing/memberLevel'
import {
adaptCustomerListHeaderFilters,
withServerFilterColumns,
withSortableColumns,
} from '@/utils/headerFilterAdapter'
import CustomerOpenApiCredentialPanel from './components/CustomerOpenApiCredentialPanel.vue'
const { t } = useI18n()
type CustomerRow = CustomerDto & { password?: string }
const { tableContainerRef, tableHeight } = useTableContainerHeight()
/** 列表区固定高度后由 ResizeObserver 得到稳定像素;兜底避免传 0 */
const handsontableHeight = computed(() => Math.max(tableHeight.value || 0, 400))
const tableData = ref<CustomerRow[]>([])
const listLoading = ref(false)
const totalCount = ref(0)
const currentPage = ref(1)
const pageSize = ref(50)
const filterState = reactive({
keyword: '',
agentId: undefined as number | undefined,
customerService: '' as string | undefined,
})
const headerSearchParams = reactive<Record<string, string>>({})
const listSortField = ref<string | null>(null)
const listSortOrder = ref<'asc' | 'desc' | null>(null)
const CUSTOMER_SERVER_FILTER_KEYS = [
'customerCode',
'customerName',
'email',
'phone',
'wechatNickname',
'agent',
'customerService',
'status',
] as const
const CUSTOMER_SORTABLE_KEYS = [
'createTime',
'updateTime',
'customerCode',
'customerName',
'accountBalance',
] as const
const processedTableData = computed(() => tableData.value)
type SelectOption = { label: string; value: string | number }
type AgentSelectOption = { label: string; value: number; agentCode: string }
const userOptions = ref<SelectOption[]>([])
const agentOptions = ref<AgentSelectOption[]>([])
const agentList = ref<AgentDto[]>([])
const memberLevelOptions = ref<SelectOption[]>([])
const userList = ref<any[]>([])
const CUSTOMER_ROLE_CODE = 'Customer'
const AGENT_ROLE_CODE = 'Agent'
function getUserRoleCodes(u: any): string[] {
const roles = u?.roles ?? u?.Roles ?? []
if (!Array.isArray(roles)) return []
return roles
.map((r: any) => String(r?.code ?? r?.Code ?? '').trim())
.filter(Boolean)
}
/** 客户门户 / 渠道代理账号不可作为所属顾问 */
function isExcludedAdvisorUser(u: any, excludedUserIds?: Set<string>): boolean {
const id = String(u?.id ?? u?.Id ?? '')
if (id && excludedUserIds?.has(id)) return true
return getUserRoleCodes(u).some(
(code) =>
code.localeCompare(CUSTOMER_ROLE_CODE, undefined, { sensitivity: 'accent' }) === 0 ||
code.localeCompare(AGENT_ROLE_CODE, undefined, { sensitivity: 'accent' }) === 0 ||
code.startsWith('CustomerSub_'),
)
}
function mapUserToSelectOption(u: any): SelectOption | null {
const account = String(u?.account ?? u?.Account ?? '').trim()
if (!account) return null
const name = getUserDisplayName(u)
const label = name && name !== account ? `${name}(${account})` : account
return { value: account, label }
}
function getUserDisplayName(u: any): string {
return String(
u?.name ??
u?.nickName ??
u?.realName ??
u?.userName ??
u?.username ??
u?.account ??
u?.id ??
''
)
}
/** 表单 value 存员工 Account;兼容历史姓名 / id */
function resolveStaffAccountForForm(value: string | number | undefined): string {
if (value == null || value === '') return ''
const str = String(value).trim()
const byAccount = userOptions.value.find((o) => String(o.value) === str)
if (byAccount) return String(byAccount.value)
const byLabel = userOptions.value.find((o) => o.label === str || String(o.label).includes(`(${str})`))
if (byLabel) return String(byLabel.value)
const user = userList.value.find(
(u) =>
String(u?.account ?? u?.Account ?? '') === str ||
String(u?.id ?? u?.Id ?? '') === str ||
getUserDisplayName(u) === str,
)
if (user) {
const account = String(user?.account ?? user?.Account ?? '').trim()
if (account) return account
}
return str
}
function formatStaffDisplay(value: string | number | undefined): string {
if (value == null || value === '') return '--'
const str = String(value).trim()
const opt = userOptions.value.find((o) => String(o.value) === str)
if (opt?.label) return String(opt.label)
const user = userList.value.find(
(u) =>
String(u?.account ?? u?.Account ?? '') === str ||
getUserDisplayName(u) === str,
)
if (user) {
const account = String(user?.account ?? user?.Account ?? '').trim()
const name = getUserDisplayName(user)
if (account && name && name !== account) return `${name}(${account})`
return name || account || str
}
return str
}
function normalizeListPayload(raw: any): any[] {
if (Array.isArray(raw)) return raw
if (Array.isArray(raw?.items)) return raw.items
if (Array.isArray(raw?.data?.items)) return raw.data.items
if (Array.isArray(raw?.data)) return raw.data
if (raw && typeof raw === 'object') return [raw]
return []
}
async function loadMemberLevelOptions() {
try {
const items = await getMemberLevelOptions()
memberLevelOptions.value = (items || [])
.map((item: any) => {
const id = item.id ?? item.Id
const name = item.levelName ?? item.LevelName
if (id == null || !name) return null
return { value: Number(id), label: String(name) }
})
.filter((item): item is SelectOption => item != null)
} catch {
memberLevelOptions.value = []
}
}
/** 启用状态:后端 EnableStatus.Enabled = 2 */
const AGENT_STATUS_ENABLED = 2
async function loadAgentOptions() {
try {
const pageSize = 100
const agents: AgentDto[] = []
let page = 1
while (true) {
const { items } = await AgentAdminApi.list({
page,
pageSize,
status: AGENT_STATUS_ENABLED,
})
const batch = items || []
agents.push(...batch)
if (batch.length < pageSize) break
page += 1
}
agentList.value = agents
agentOptions.value = agents
.filter((a) => a?.id && a.agentCode)
.map((a) => {
const name = String(a.agentName || '').trim()
const code = String(a.agentCode || '').trim()
return {
value: Number(a.id),
agentCode: code,
label: name && name !== code ? `${name}(${code})` : code,
}
})
} catch (e: any) {
Message.error(e?.message || '加载代理下拉失败')
agentList.value = []
agentOptions.value = []
}
}
function resolveAgentCodeForForm(agentId: number | null | undefined): string {
if (agentId == null || agentId <= 0) return ''
const hit = agentOptions.value.find((o) => o.value === Number(agentId))
if (hit?.agentCode) return hit.agentCode
const fromList = agentList.value.find((a) => Number(a.id) === Number(agentId))
return String(fromList?.agentCode || '').trim()
}
function formatAgentDisplay(row: CustomerRow | any): string {
const name = String(row?.agentName ?? '').trim()
const code = String(row?.agentCode ?? '').trim()
if (name && code && name !== code) return `${name}(${code})`
return name || code || '--'
}
async function loadFormSelectOptions() {
try {
const pageSize = 100
const roles: any[] = []
const users: any[] = []
// 角色全量分页拉取(接口限制 PageSize 1-100)
{
let page = 1
while (true) {
const res = await RoleService.getRbacRoleGetList(undefined, page, pageSize)
const items = normalizeListPayload(res)
roles.push(...items)
if (items.length < pageSize) break
page += 1
}
}
// 用户全量分页拉取(接口限制 PageSize 1-100)
{
let page = 1
while (true) {
const res = await UserService.getRbacUserGetList(undefined, undefined, page, pageSize)
const items = normalizeListPayload(res)
users.push(...items)
if (items.length < pageSize) break
page += 1
}
}
const excludedUserIds = new Set<string>()
const excludeRoleCodes = [CUSTOMER_ROLE_CODE, AGENT_ROLE_CODE]
for (const roleCode of excludeRoleCodes) {
const role = roles.find(
(r: any) =>
String(r?.code ?? r?.Code ?? '').localeCompare(roleCode, undefined, {
sensitivity: 'accent',
}) === 0,
)
const roleId = role?.id ?? role?.Id
if (!roleId) continue
try {
const roleUsers = await UserService.getRbacUserGetUsersByRole(String(roleId))
for (const u of normalizeListPayload(roleUsers)) {
const id = String(u?.id ?? u?.Id ?? '')
if (id) excludedUserIds.add(id)
}
} catch {
// 按角色拉用户失败时,仍依赖用户列表上的 roles 字段过滤
}
}
userList.value = users
const staffUsers = users.filter((u) => !isExcludedAdvisorUser(u, excludedUserIds))
userOptions.value = staffUsers
.map(mapUserToSelectOption)
.filter((item): item is SelectOption => item != null)
} catch (e: any) {
Message.error(e?.message || '加载员工用户下拉失败')
userOptions.value = []
userList.value = []
}
}
function resolveCustomerId(record: CustomerRow | any): string | number | undefined {
const raw = record?.id ?? record?.Id
if (raw === undefined || raw === null || raw === '') return undefined
return raw
}
function mapCustomerRow(r: any): CustomerRow {
const agentIdRaw = r.agentId ?? r.AgentId
return {
...r,
id: r.id ?? r.Id,
accountBalance: r.accountBalance ?? r.balance ?? r.Balance,
agentId: agentIdRaw != null && agentIdRaw !== '' ? Number(agentIdRaw) : null,
agentCode: r.agentCode ?? r.AgentCode ?? '',
agentName: r.agentName ?? r.AgentName ?? '',
memberLevelId: r.memberLevelId ?? r.MemberLevelId,
memberLevelName: r.memberLevelName ?? r.MemberLevelName,
priceTierKey: r.priceTierKey ?? r.PriceTierKey ?? '',
}
}
async function loadList() {
listLoading.value = true
try {
const adapted = adaptCustomerListHeaderFilters(headerSearchParams)
const keyword = (adapted.keyword || filterState.keyword.trim() || '').trim()
const { items, total } = await CustomerService.getList({
page: currentPage.value,
pageSize: pageSize.value,
keyword: keyword || undefined,
agentId: adapted.agentId || filterState.agentId || undefined,
customerService: adapted.customerService || filterState.customerService || undefined,
status: adapted.status as any,
sortField: listSortField.value || undefined,
sortOrder: listSortOrder.value || undefined,
})
tableData.value = (items || []).map(mapCustomerRow)
totalCount.value = total
} catch (e: any) {
Message.error(e?.message || '加载失败')
tableData.value = []
totalCount.value = 0
} finally {
listLoading.value = false
}
}
function handleHeaderSearch() {
currentPage.value = 1
loadList()
}
function handleSortChange(payload: { field: string | null; order: 'asc' | 'desc' | null }) {
listSortField.value = payload.field === 'accountBalance' ? 'balance' : payload.field
listSortOrder.value = payload.order
currentPage.value = 1
loadList()
}
function handleSearch() {
currentPage.value = 1
loadList()
}
// HandsontablePro 列定义(按 t_customer 表结构)
const hotColumns = computed(() =>
withSortableColumns(
withServerFilterColumns(
[
{ key: 'customerCode', label: t('user.customer.fields.customerCode'), width: 90 },
{ key: 'customerName', label: t('user.customer.fields.customerName'), width: 100 },
{ key: 'wechatAvatarUrl', label: t('user.customer.fields.wechatAvatar'), width: 70 },
{ key: 'wechatNickname', label: t('user.customer.fields.wechatNickname'), width: 100 },
{ key: 'email', label: t('user.customer.fields.email'), width: 140 },
{ key: 'phone', label: t('user.customer.fields.phone'), width: 110 },
{ key: 'accountBalance', label: t('user.customer.fields.accountBalance'), width: 95 },
{ key: 'totalQuota', label: t('user.customer.fields.totalQuota'), width: 95 },
{ key: 'usedQuota', label: t('user.customer.fields.usedQuota'), width: 95 },
{ key: 'status', label: t('user.customer.fields.status'), width: 75 },
{ key: 'memberLevelName', label: t('user.customer.fields.memberLevel'), width: 100 },
{ key: 'priceTierKey', label: t('user.customer.fields.priceTierKey'), width: 100 },
{ key: 'remark', label: t('user.customer.fields.remark'), width: 100 },
{ key: 'agent', label: t('user.customer.fields.agent'), width: 120 },
{ key: 'customerService', label: t('user.customer.fields.customerService'), width: 90 },
{ key: 'createTime', label: t('user.customer.fields.createTime'), width: 150 },
{ key: 'createBy', label: t('user.customer.fields.createBy'), width: 90 },
{ key: 'updateTime', label: t('user.customer.fields.updateTime'), width: 150 },
{ key: 'updateBy', label: t('user.customer.fields.updateBy'), width: 90 },
{ key: 'lastOnlineTime', label: t('user.customer.fields.lastOnlineTime'), width: 150 },
],
CUSTOMER_SERVER_FILTER_KEYS,
(col) =>
col.key === 'status'
? {
filterType: 'enum' as const,
headerMode: 'icon' as const,
filterOptions: [
{ label: t('user.customer.status.enabled'), value: String(CUSTOMER_STATUS.normal) },
{ label: t('user.customer.status.disabled'), value: String(CUSTOMER_STATUS.disabled) },
],
}
: {},
),
CUSTOMER_SORTABLE_KEYS,
),
)
// 自定义单元格渲染(操作列在前,col: 0=操作列, 1=序号, 2..n=数据列)
function customCellRenderer(row: number, col: number, value: any, rowData: any): string | null {
const dataColIndex = col - 2
if (dataColIndex < 0 || dataColIndex >= hotColumns.value.length) return null
const colKey = hotColumns.value[dataColIndex].key
const avatarUrl = resolveAvatarUrl(rowData?.wechatAvatarUrl || rowData?.wechatAvatar)
if (colKey === 'wechatAvatarUrl') {
return `<img src="${avatarUrl}" style="width:32px;height:32px;border-radius:50%;object-fit:cover" />`
}
if (colKey === 'status') {
const s = rowData?.status
const enabled = isCustomerStatusNormal(s)
const text = enabled ? t('user.customer.status.enabled') : t('user.customer.status.disabled')
return `
<label class="customer-status-switch" data-row-index="${row}" style="display:inline-flex;align-items:center;cursor:pointer;margin:0;">
<input type="checkbox" ${enabled ? 'checked' : ''} style="display:none;">
<span class="switch-track" style="position:relative;width:36px;height:20px;background:${enabled ? '#00b42a' : '#e5e6eb'};border-radius:10px;transition:background .2s;">
<span class="switch-thumb" style="position:absolute;top:2px;left:${enabled ? '18px' : '2px'};width:16px;height:16px;background:#fff;border-radius:50%;box-shadow:0 2px 4px rgba(0,0,0,.2);transition:left .2s;"></span>
</span>
</label>
`
}
if (colKey === 'memberLevelName') {
const name = rowData?.memberLevelName
if (name) return String(name)
const levelId = rowData?.memberLevelId
if (levelId != null) {
const option = memberLevelOptions.value.find((o) => o.value === levelId)
return option?.label ?? String(levelId)
}
return '--'
}
if (['accountBalance', 'totalQuota', 'usedQuota'].includes(colKey)) {
const val = rowData?.[colKey] ?? rowData?.balance
if (val === undefined || val === null) return '--'
return Number(val).toLocaleString()
}
if (colKey === 'agent') {
return formatAgentDisplay(rowData)
}
if (colKey === 'customerService') {
return formatStaffDisplay(rowData?.customerService)
}
if (colKey === 'createTime') return rowData?.createTime ?? rowData?.createdAt ?? '--'
if (colKey === 'updateTime') return rowData?.updateTime ?? '--'
if (colKey === 'lastOnlineTime') return rowData?.lastOnlineTime ?? rowData?.lastLoginTime ?? '--'
if (colKey === 'createBy') return rowData?.createBy ?? rowData?.createdBy ?? '--'
if (colKey === 'updateBy') return rowData?.updateBy ?? '--'
if (colKey === 'lastOnlineIp') return rowData?.lastOnlineIp ?? rowData?.lastLoginIp ?? '--'
return null
}
// 操作列渲染(统一图标风格)
function actionRenderer(_row: any, rowIndex: number): string {
const record = tableData.value[rowIndex]
const isMain = record?.parentCustomerId == null
return wrapWarehouseActions(
[
renderWarehouseActionBtn('edit', t('user.customer.actions.edit')),
renderWarehouseActionBtn(
'adjustBalance',
t('user.customer.actions.adjustBalance'),
'adjustBalance',
),
...(isMain
? [renderWarehouseActionBtn('openapi', t('user.customer.actions.openapi'), 'auth')]
: []),
renderWarehouseActionBtn('resetPassword', t('user.customer.actions.resetPassword')),
renderWarehouseActionBtn('omsLogin', t('user.customer.actions.omsLogin'), 'login'),
renderWarehouseActionBtn('delete', t('user.customer.actions.delete')),
],
rowIndex,
)
}
/** 捕获阶段统一处理操作列/状态开关,避免 Handsontable 裁切或列错位导致点击无响应 */
function handleTableMouseDown(e: MouseEvent) {
const target = e.target as HTMLElement | null
if (!target) return
const actionEl = target.closest('[data-action]')
if (actionEl?.closest('.wh-action-group')) {
e.preventDefault()
e.stopPropagation()
const action = actionEl.getAttribute('data-action') || undefined
const rowIndex = parseInt(actionEl.getAttribute('data-row-index') || '0', 10)
const record = tableData.value[rowIndex]
if (!record) return
handleActionClick(record, rowIndex, action)
return
}
handleStatusSwitchMouseDown(e)
}
/** 在捕获阶段处理开关,避免 Handsontable 先拦截 mousedown 导致无法切换 / 点击异常 */
async function handleStatusSwitchMouseDown(e: MouseEvent) {
const switchEl = (e.target as HTMLElement | null)?.closest?.('.customer-status-switch')
if (!switchEl) return
e.preventDefault()
e.stopPropagation()
const rowIndex = parseInt(switchEl.getAttribute('data-row-index') || '0', 10)
const record = tableData.value[rowIndex]
const customerId = resolveCustomerId(record)
if (customerId == null) return
const next = toCustomerStatus(!isCustomerStatusNormal(record.status))
try {
await CustomerService.update(customerId, { ...record, status: next as any })
record.status = next as any
Message.success(t('user.customer.message.updateSuccess'))
} catch (err: any) {
Message.error(err?.message || '更新失败')
}
}
// 操作点击
function handleActionClick(row: any, rowIndex: number, action?: string) {
const record = tableData.value[rowIndex] || row
switch (action) {
case 'edit':
handleEdit(record)
break
case 'adjustBalance':
handleAdjustBalance(record)
break
case 'delete':
handleDelete(resolveCustomerId(record))
break
case 'resetPassword':
handleResetPassword(record)
break
case 'openapi':
handleOpenApi(record)
break
case 'omsLogin':
handleOmsLogin(record)
break
}
}
function handlePageChange(page: number) {
currentPage.value = page
loadList()
}
function handlePageSizeChange(size: number) {
pageSize.value = size
currentPage.value = 1
loadList()
}
// 表单
const modalVisible = ref(false)
const modalLoading = ref(false)
const isEdit = ref(false)
const formRef = ref()
const currentId = ref<string | number | undefined>(undefined)
const formState = reactive<Partial<CustomerRow>>({
customerCode: '',
customerName: '',
wechatAvatarUrl: '',
wechatNickname: '',
email: '',
phone: '',
password: DEFAULT_CUSTOMER_PASSWORD,
accountBalance: undefined,
totalQuota: undefined,
usedQuota: undefined,
status: CUSTOMER_STATUS.normal,
memberLevelId: undefined,
priceTierKey: '普通会员',
remark: '',
agentId: undefined as number | null | undefined,
customerService: '',
parentCustomerId: null as number | null | undefined,
})
const priceTierOptions = [
{ label: '普通会员', value: '普通会员' },
{ label: '普通客户', value: '普通客户' },
{ label: 'VIP1', value: 'VIP1' },
{ label: 'VIP2', value: 'VIP2' },
{ label: 'VIP3', value: 'VIP3' },
]
const formRules = computed(() => ({
customerName: [{ required: true, message: t('user.customer.rules.customerNameRequired') }],
email: [{ required: true, message: t('user.customer.rules.emailRequired') }],
phone: [{ required: true, message: t('user.customer.rules.phoneRequired') }],
password: isEdit.value
? []
: [
{ required: true, message: t('user.customer.rules.passwordRequired') },
{
validator: (value: string, callback: (error?: string) => void) => {
if (!isLetterAndDigitPassword(value)) {
callback(t('user.customer.rules.passwordLetterAndDigit'))
} else {
callback()
}
},
},
],
}))
function handleAdd() {
isEdit.value = false
currentId.value = undefined
Object.assign(formState, {
customerCode: '',
customerName: '',
wechatAvatarUrl: '',
wechatNickname: '',
email: '',
phone: '',
password: DEFAULT_CUSTOMER_PASSWORD,
accountBalance: undefined,
totalQuota: undefined,
usedQuota: undefined,
status: CUSTOMER_STATUS.normal,
memberLevelId: undefined,
priceTierKey: '普通会员',
remark: '',
agentId: undefined,
customerService: '',
parentCustomerId: null,
})
modalVisible.value = true
}
function handleEdit(record: CustomerRow) {
isEdit.value = true
currentId.value = resolveCustomerId(record)
const agentId =
record.agentId != null && Number(record.agentId) > 0 ? Number(record.agentId) : undefined
// 已停用代理仍展示当前绑定,避免下拉空白
if (agentId && !agentOptions.value.some((o) => o.value === agentId)) {
const code = String(record.agentCode || '').trim()
agentOptions.value = [
...agentOptions.value,
{
value: agentId,
agentCode: code,
label: formatAgentDisplay(record),
},
]
}
Object.assign(formState, {
customerCode: record.customerCode,
customerName: record.customerName,
wechatAvatarUrl: record.wechatAvatarUrl ?? record.wechatAvatar,
wechatNickname: record.wechatNickname,
email: record.email,
phone: record.phone,
accountBalance: record.accountBalance ?? record.balance,
totalQuota: record.totalQuota,
usedQuota: record.usedQuota,
status: record.status ?? CUSTOMER_STATUS.normal,
memberLevelId: record.memberLevelId ?? undefined,
priceTierKey: record.priceTierKey || '普通客户',
remark: record.remark,
agentId,
customerService: resolveStaffAccountForForm(record.customerService),
parentCustomerId: record.parentCustomerId ?? null,
})
modalVisible.value = true
}
function handleDelete(id: string | number | undefined) {
if (id === undefined || id === null || id === '') {
Message.warning('无法获取客户ID,请刷新列表后重试')
return
}
Modal.confirm({
content: t('user.customer.modal.deleteConfirm'),
onOk: async () => {
try {
await CustomerService.delete(id)
Message.success(t('user.customer.message.deleteSuccess'))
await loadList()
} catch (e: any) {
Message.error(e?.message || t('user.customer.message.deleteFailed'))
throw e
}
},
})
}
const resetPwdVisible = ref(false)
const resetPwdForm = reactive({ newPassword: '' })
const resetPwdTarget = ref<CustomerRow | null>(null)
const openapiVisible = ref(false)
const openapiCustomerId = ref<string | number | null>(null)
function handleOpenApi(record: CustomerRow) {
if (record.parentCustomerId != null) {
Message.warning(t('user.customer.openapi.mainOnly'))
return
}
const id = resolveCustomerId(record)
if (id == null) return
openapiCustomerId.value = id
openapiVisible.value = true
}
function handleResetPassword(record: CustomerRow) {
resetPwdTarget.value = record
resetPwdForm.newPassword = ''
resetPwdVisible.value = true
}
const adjustBalanceVisible = ref(false)
const adjustBalanceLoading = ref(false)
const adjustBalanceTarget = ref<CustomerRow | null>(null)
const adjustBalanceForm = reactive({
amount: undefined as number | undefined,
remark: '',
})
const adjustBalanceCurrentDisplay = computed(() => {
const raw = adjustBalanceTarget.value?.accountBalance ?? adjustBalanceTarget.value?.balance
const n = Number(raw)
if (!Number.isFinite(n)) return '--'
return n.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 })
})
function handleAdjustBalance(record: CustomerRow) {
adjustBalanceTarget.value = record
adjustBalanceForm.amount = undefined
adjustBalanceForm.remark = ''
adjustBalanceVisible.value = true
}
async function handleAdjustBalanceSubmit(): Promise<boolean> {
const customerId = resolveCustomerId(adjustBalanceTarget.value)
if (customerId == null) {
Message.warning(t('user.customer.message.adjustBalanceIdMissing'))
return false
}
const amount = Number(adjustBalanceForm.amount)
if (!Number.isFinite(amount) || amount === 0) {
Message.warning(t('user.customer.rules.adjustAmountRequired'))
return false
}
const remark = String(adjustBalanceForm.remark || '').trim()
if (remark.length < 2) {
Message.warning(t('user.customer.rules.adjustRemarkRequired'))
return false
}
adjustBalanceLoading.value = true
try {
await CustomerService.adjustBalance(customerId, amount, remark)
Message.success(t('user.customer.message.adjustBalanceSuccess'))
adjustBalanceVisible.value = false
await loadList()
return true
} catch (e: any) {
Message.error(e?.message || t('user.customer.message.adjustBalanceFailed'))
return false
} finally {
adjustBalanceLoading.value = false
}
}
async function handleResetPwdSubmit() {
const customerId = resolveCustomerId(resetPwdTarget.value)
if (customerId == null) {
resetPwdVisible.value = false
return
}
if (!resetPwdForm.newPassword?.trim()) {
Message.warning(t('user.customer.rules.passwordRequired'))
return
}
try {
await CustomerService.resetPassword(customerId, resetPwdForm.newPassword.trim())
Message.success(t('user.customer.message.resetPasswordSuccess'))
resetPwdVisible.value = false
} catch (e: any) {
Message.error(e?.message || t('user.customer.message.resetPasswordFailed'))
}
}
async function handleOmsLogin(record: CustomerRow) {
const customerId = resolveCustomerId(record)
if (customerId == null) {
Message.warning('客户ID无效')
return
}
try {
const result = await CustomerService.omsLogin(customerId)
if (!result?.accessToken) {
Message.error('获取客户登录凭证失败')
return
}
const url = CustomerService.buildCustomerSsoUrl(result)
window.open(url, '_blank', 'noopener,noreferrer')
} catch (e: any) {
Message.error(e?.message || 'OMS登录失败')
}
}
async function handleModalOk(): Promise<boolean> {
try {
await formRef.value?.validate()
} catch {
return false
}
modalLoading.value = true
try {
const customerServiceAccount = resolveStaffAccountForForm(formState.customerService)
const agentCode = resolveAgentCodeForForm(formState.agentId as number | null | undefined)
// 主客户才改绑;子客户禁用下拉,不传 agentCode 以免误解绑
const canBindAgent = !(isEdit.value && formState.parentCustomerId != null)
if (isEdit.value && currentId.value != null && currentId.value !== '') {
await CustomerService.update(currentId.value, {
...formState,
salesPerson: undefined,
salesman: undefined,
customerService: customerServiceAccount,
agentCode: canBindAgent ? agentCode : undefined,
})
Message.success(t('user.customer.message.updateSuccess'))
} else {
await CustomerService.create({
...formState,
customerCode: undefined,
password: formState.password,
salesPerson: undefined,
salesman: undefined,
customerService: customerServiceAccount,
agentCode: agentCode || undefined,
})
Message.success(t('user.customer.message.createSuccess'))
currentPage.value = 1
}
await loadList()
return true
} catch (e: any) {
Message.error(e?.message || t('user.customer.message.saveFailed'))
return false
} finally {
modalLoading.value = false
}
}
function handleModalCancel() {
modalVisible.value = false
}
const handsontableRef = ref<InstanceType<typeof HandsontablePro>>()
onMounted(() => {
loadList()
loadFormSelectOptions()
loadAgentOptions()
loadMemberLevelOptions()
})
</script>
<style scoped lang="less">
// 与「我的订单」等页一致:整页可纵向滚动,列表区固定高度由 Handsontable .wtHolder 承担滚动
.customer-page {
width: 100%;
box-sizing: border-box;
padding: 16px 16px 24px;
display: flex;
flex-direction: column;
background: var(--app-page-bg);
flex: 1;
min-height: 0;
overflow-x: hidden;
overflow-y: hidden;
}
.customer-page-card {
flex: 1;
min-height: 0;
display: flex;
flex-direction: column;
background: var(--app-card-bg);
border-radius: 8px;
padding: 0;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
box-sizing: border-box;
overflow: hidden;
}
.table-toolbar {
display: flex;
justify-content: space-between;
align-items: flex-start;
padding: 16px 16px 0;
margin-bottom: 16px;
flex-shrink: 0;
flex-wrap: wrap;
gap: 12px;
.toolbar-left {
display: flex;
align-items: center;
gap: 8px;
flex: 1;
min-width: 0;
}
.toolbar-right {
.pagination-controls {
display: flex;
align-items: center;
}
}
}
.list-wrap {
width: 100%;
min-width: 0;
flex: 1;
min-height: 0;
padding: 0 16px 16px;
box-sizing: border-box;
display: flex;
flex-direction: column;
overflow: hidden;
overscroll-behavior: contain;
touch-action: pan-x pan-y;
position: relative;
.customer-status-switch {
user-select: none;
.switch-track:hover {
opacity: 0.9;
}
}
}
/* 仅拉伸容器,不覆盖 Handsontable 根节点 height(避免与 .wtHolder 内部滚动冲突) */
.list-wrap :deep(.handsontable-pro-container) {
width: 100%;
flex: 1;
min-width: 0;
min-height: 0;
display: flex;
flex-direction: column;
}
.customer-code-auto-hint {
color: var(--color-text-3);
font-size: 13px;
line-height: 32px;
}
.edit-form-body {
padding: 4px 0;
}
.adjust-balance-hint {
margin: 0;
font-size: 12px;
line-height: 1.5;
color: var(--color-text-3);
}
.form-section-title {
margin: 0 0 12px;
padding: 0 0 8px;
font-size: 13px;
font-weight: 600;
color: var(--color-text-2);
border-bottom: 1px solid var(--color-border-2);
&:not(:first-child) {
margin-top: 8px;
}
}
.label-with-tip {
display: inline-flex;
align-items: center;
gap: 4px;
}
.tip-icon {
color: var(--color-text-3);
cursor: help;
font-size: 14px;
}
.switch-with-text {
display: inline-flex;
align-items: center;
gap: 8px;
min-height: 32px;
}
.switch-text {
color: var(--color-text-2);
font-size: 13px;
}
.edit-form {
:deep(.arco-form-item) {
margin-bottom: 16px;
}
}
</style>