liufumingren 3a4eebb71d feat(shop): 店铺所在区刷新与解绑自动禁用,禁用时作废待审核订单
- 新增 shopAreaSync:按账号拉一次商家列表(同账号多店铺共享,PageSize=200 翻页),
  把最新商家结构(FxUserId、分区声明 DbAreas)写回 shops.external_data;
  商家已不在合作列表时自动把店铺置为禁用,并写明原因
- 手动刷新入口:POST /api/shops/{id}/refresh-area(货主权限)、
  POST /api/external-print-accounts/{id}/refresh-shops-area(集团权限)
- 新增定时任务 refreshExternalShopAreas:每小时巡检,按账号 6 小时刷新一次
  (DGJ_AREA_REFRESH_HOURS 可调),自动禁用店铺后不再参与自动拉单与一键拉单
- 新增 shopOrderGuard:店铺禁用立即作废该店铺待审核订单
  (void_reason 前缀「店铺已禁用,暂停处理」),重新启用时只恢复因此暂停的订单;
  禁用店铺再拉单直接拒绝,避免又拉回待审核单
- externalPrintOrder 待审核单核对改用 LogicOrderId 批量回读(50 单/批,最多 300 单)
- orderStatus.restore 支持显式 principalId,供无登录态的系统任务调用
- 前端:店铺列表新增「所在区」列与行内/详情「刷新所在区」按钮,
  系统自动禁用的店铺状态带原因提示,启停与刷新后提示作废/恢复了多少订单
- 文档:新增 §4.2.2「所在区」刷新、§4.2.3 店铺禁用/启用与待审核订单
  (原 LastWaybillCode 小节顺延为 §4.2.4,WMS 对接文档引用同步)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-07 00:19:02 +08:00

630 lines
21 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.

<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, reactive, ref } from 'vue'
import { ElMessage, ElMessageBox, type FormInstance, type FormRules } from 'element-plus'
import {
getExternalPrintPlatforms,
type ExternalPrintPlatformItem,
} from '@/api/externalPrintPlatform'
import {
getExternalPrintAccounts,
createExternalPrintAccount,
updateExternalPrintAccount,
deleteExternalPrintAccount,
checkExternalPrintAccount,
loginExternalPrintAccount,
getExternalPrintMerchants,
refreshExternalPrintShopsArea,
createShopsFromMerchants,
type ExternalPrintAccountItem,
type ExternalPrintAccountPayload,
type ExternalPrintMerchantItem,
} from '@/api/externalPrintAccount'
import DataTable from '@/components/DataTable.vue'
import ListPager from '@/components/ListPager.vue'
import ExpressInterfaceDialog from './ExpressInterfaceDialog.vue'
import { DEFAULT_PAGE_SIZE, useListPage } from '@/composables/useListPage'
import { getPrincipalSubsidiaryOptions } from '@/api/principalSubsidiary'
import { formatDateTime } from '@/utils/datetime'
/* -------------------- 平台字典(开发者维护,仅用于账号下拉) -------------------- */
const platforms = ref<ExternalPrintPlatformItem[]>([])
async function fetchPlatforms() {
try {
platforms.value = (await getExternalPrintPlatforms()) || []
} catch {
platforms.value = []
}
}
function platformName(platformId: number) {
return platforms.value.find((p) => p.id === platformId)?.name || '-'
}
/** 下拉标签:禁用平台加后缀,便于识别 */
function platformOptionLabel(p: ExternalPrintPlatformItem) {
return p.status === 1 ? p.name : `${p.name}(已禁用)`
}
/* -------------------- 打单账号 -------------------- */
const query = reactive({
page: 1,
pageSize: DEFAULT_PAGE_SIZE,
platform_id: '' as string | number,
keyword: '',
})
const { loading, list, total, elapsed, fetchList, search, handlePageChange, handleSizeChange } = useListPage({
query,
fetcher: () => {
const params: Record<string, unknown> = { ...query }
if (!params.keyword) delete params.keyword
if (params.platform_id === '') delete params.platform_id
return getExternalPrintAccounts(params)
},
})
function reset() {
query.platform_id = ''
query.keyword = ''
search()
}
/* -------------------- 新增 / 修改 -------------------- */
const dialogVisible = ref(false)
const submitting = ref(false)
const formRef = ref<FormInstance>()
const editId = ref<number | null>(null)
const form = reactive({
platform_id: '' as string | number,
account: '',
password: '',
})
const rules = computed<FormRules>(() => ({
platform_id: [{ required: true, message: '请选择外部打单平台', trigger: 'change' }],
account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
password: [
{
// 修改时不回显密码:留空表示不修改
required: editId.value == null,
validator: (_rule: unknown, value: string, callback: (e?: string | Error) => void) => {
if (editId.value == null && !value) callback(new Error('请输入密码'))
else callback()
},
trigger: 'blur',
},
],
}))
const dialogTitle = computed(() => (editId.value == null ? '新增打单账号' : '修改打单账号'))
function openCreate() {
if (!platforms.value.length) {
ElMessage.warning('暂无可用的外部打单平台,请联系开发维护平台资料')
return
}
editId.value = null
form.platform_id = platforms.value[0].id
form.account = ''
form.password = ''
dialogVisible.value = true
}
function openEdit(row: ExternalPrintAccountItem) {
editId.value = row.id
form.platform_id = row.platform_id
form.account = row.account
form.password = ''
dialogVisible.value = true
}
async function submitForm() {
if (!formRef.value) return
const id = editId.value
await formRef.value.validate(async (valid) => {
if (!valid) return
submitting.value = true
try {
const platform_id = Number(form.platform_id)
const account = form.account.trim()
if (id == null) {
await createExternalPrintAccount({ platform_id, account, password: form.password })
ElMessage.success('新增成功')
} else {
// 密码留空表示不修改
const patch: ExternalPrintAccountPayload = { platform_id, account }
if (form.password) patch.password = form.password
await updateExternalPrintAccount(id, patch)
ElMessage.success('保存成功')
}
dialogVisible.value = false
await fetchList()
} catch {
// 拦截器已提示
} finally {
submitting.value = false
}
})
}
async function handleDelete(row: ExternalPrintAccountItem) {
try {
await ElMessageBox.confirm(
`确认删除「${row.platform?.name || platformName(row.platform_id)}」下的账号「${row.account}」?`,
'删除确认',
{ type: 'warning', confirmButtonText: '删除', cancelButtonText: '取消' },
)
} catch {
return
}
try {
await deleteExternalPrintAccount(row.id)
ElMessage.success('已删除')
await fetchList()
} catch {
// 拦截器已提示
}
}
/* -------------------- 登录态检查 / 登录 -------------------- */
const loginStatus = reactive<Record<number, { loggedIn: boolean; message?: string; shopName?: string }>>({})
const acting = reactive<{ id: number | null; type: 'check' | 'login' | '' }>({ id: null, type: '' })
function isActing(row: ExternalPrintAccountItem, type?: 'check' | 'login') {
if (acting.id !== row.id) return false
return type ? acting.type === type : acting.type !== ''
}
async function handleCheck(row: ExternalPrintAccountItem) {
acting.id = row.id
acting.type = 'check'
try {
const res = await checkExternalPrintAccount(row.id)
loginStatus[row.id] = { loggedIn: res.logged_in, message: res.message, shopName: res.shop_name }
if (res.logged_in) ElMessage.success(res.shop_name ? `登录态有效:${res.shop_name}` : res.message || '登录态有效')
else ElMessage.warning(res.message || '未登录')
} catch {
loginStatus[row.id] = { loggedIn: false }
} finally {
acting.id = null
acting.type = ''
}
}
async function handleLogin(row: ExternalPrintAccountItem) {
acting.id = row.id
acting.type = 'login'
try {
const res = await loginExternalPrintAccount(row.id)
loginStatus[row.id] = {
loggedIn: true,
message: res.reused ? '已复用登录态' : '登录成功',
shopName: res.shop_name,
}
const shop = res.shop_name ? `(${res.shop_name})` : ''
ElMessage.success(res.reused ? `当前已是登录态,无需重新登录${shop}` : `登录成功,已保存登录态${shop}`)
await fetchList()
} catch {
loginStatus[row.id] = { loggedIn: false }
} finally {
acting.id = null
acting.type = ''
}
}
/* -------------------- 快递接口(面单模板) -------------------- */
const expressVisible = ref(false)
const expressAccount = ref<ExternalPrintAccountItem | null>(null)
function openExpressInterfaces(row: ExternalPrintAccountItem) {
expressAccount.value = row
expressVisible.value = true
}
/* -------------------- 平台店铺(商家) -------------------- */
const shopsDialogVisible = ref(false)
const shopsAccount = ref<ExternalPrintAccountItem | null>(null)
const merchantLoading = ref(false)
const merchantList = ref<ExternalPrintMerchantItem[]>([])
/** 添加店铺时选择的货主(下属公司),必选 */
const merchantSubsidiaryId = ref<number | null>(null)
const subsidiaryOptions = ref<{ id: number; name: string }[]>([])
async function fetchSubsidiaryOptions() {
try {
subsidiaryOptions.value = await getPrincipalSubsidiaryOptions({ mine: 1 })
} catch {
subsidiaryOptions.value = []
}
}
const merchantTotal = ref(0)
const merchantQuery = reactive({ page: 1, pageSize: 20, keyword: '' })
const selectedMerchants = ref<ExternalPrintMerchantItem[]>([])
const addingShops = ref(false)
/** 表格高度跟随视口:内容区固定高度、表格内部滚动,保证底部操作按钮始终可见 */
const merchantTableHeight = ref(320)
function syncMerchantTableHeight() {
merchantTableHeight.value = Math.min(520, Math.max(120, Math.floor(window.innerHeight * 0.6) - 220))
}
async function fetchMerchants() {
if (!shopsAccount.value) return
merchantLoading.value = true
try {
const res = await getExternalPrintMerchants(shopsAccount.value.id, {
page: merchantQuery.page,
pageSize: merchantQuery.pageSize,
keyword: merchantQuery.keyword || undefined,
})
merchantList.value = res.list || []
merchantTotal.value = res.total || 0
selectedMerchants.value = []
} catch {
merchantList.value = []
merchantTotal.value = 0
} finally {
merchantLoading.value = false
}
}
function openMerchants(row: ExternalPrintAccountItem) {
shopsAccount.value = row
merchantQuery.page = 1
merchantQuery.pageSize = 20
merchantQuery.keyword = ''
selectedMerchants.value = []
syncMerchantTableHeight()
shopsDialogVisible.value = true
fetchMerchants()
}
function searchMerchants() {
merchantQuery.page = 1
fetchMerchants()
}
function handleMerchantPageChange(page: number) {
merchantQuery.page = page
fetchMerchants()
}
function handleMerchantSizeChange(size: number) {
merchantQuery.pageSize = size
merchantQuery.page = 1
fetchMerchants()
}
function handleMerchantSelection(rows: ExternalPrintMerchantItem[]) {
selectedMerchants.value = rows
}
function canSelectMerchant(row: ExternalPrintMerchantItem) {
return !row.added
}
async function handleAddShops() {
if (!shopsAccount.value || !selectedMerchants.value.length) return
if (!merchantSubsidiaryId.value) {
ElMessage.warning('请先选择所属货主(下属公司)')
return
}
addingShops.value = true
try {
const res = await createShopsFromMerchants(
shopsAccount.value.id,
selectedMerchants.value,
Number(merchantSubsidiaryId.value),
)
const extra = res.skipped ? `,跳过 ${res.skipped} 个(已添加或无效)` : ''
ElMessage.success(`已添加 ${res.created} 个店铺${extra}`)
await fetchMerchants()
} catch {
// 拦截器已提示
} finally {
addingShops.value = false
}
}
/** 正在刷新所在区的账号ID */
const refreshingAreaAccountId = ref<number | null>(null)
/** 刷新账号下店铺的「所在区」:一次拉取账号全部商家,商家已解绑的店铺自动禁用 */
async function handleRefreshShopsArea(row: ExternalPrintAccountItem) {
refreshingAreaAccountId.value = row.id
try {
const res = await refreshExternalPrintShopsArea(row.id)
const parts = [ `平台商家 ${res.merchants} 个`, `${res.shops} 个店铺中更新 ${res.updated} 个` ]
if (res.disabled.length) parts.push(`自动禁用 ${res.disabled.length} 个(已不在合作列表)`)
if (res.voided) parts.push(`作废待审核订单 ${res.voided} 个(启用后自动恢复)`)
if (res.restored.length) parts.push(`${res.restored.length} 个已重新回到合作列表,可手动启用`)
ElMessage.success(`刷新完成:${parts.join(',')}`)
await fetchList()
} catch {
// 拦截器已提示
} finally {
refreshingAreaAccountId.value = null
}
}
/** 行登录状态:优先用本次检查/登录结果,否则用后端记录的最新状态 */
function isRowLoggedIn(row: ExternalPrintAccountItem) {
const local = loginStatus[row.id]
return local ? local.loggedIn : Number(row.login_status) === 1
}
function rowStatusTitle(row: ExternalPrintAccountItem) {
return loginStatus[row.id]?.shopName || ''
}
onMounted(async () => {
syncMerchantTableHeight()
window.addEventListener('resize', syncMerchantTableHeight)
await fetchPlatforms()
await fetchSubsidiaryOptions()
await fetchList()
})
onBeforeUnmount(() => {
window.removeEventListener('resize', syncMerchantTableHeight)
})
</script>
<template>
<el-card class="external-print-card">
<template #header>
<div class="page-header">
<span>外部打单平台</span>
<div class="header-actions">
<el-button type="primary" :disabled="!platforms.length" @click="openCreate">新增账号</el-button>
</div>
</div>
</template>
<el-alert
v-if="!platforms.length"
type="info"
show-icon
:closable="false"
title="暂无外部打单平台,平台资料由开发者维护,请联系开发者开通后再添加账号"
/>
<el-form :inline="true" class="search-bar">
<el-form-item label="平台">
<el-select v-model="query.platform_id" placeholder="全部" clearable style="width: 180px">
<el-option v-for="p in platforms" :key="p.id" :label="platformOptionLabel(p)" :value="p.id" />
</el-select>
</el-form-item>
<el-form-item label="账号">
<el-input v-model="query.keyword" placeholder="账号关键词" clearable @keyup.enter="search" />
</el-form-item>
<el-form-item>
<el-button type="primary" @click="search">查询</el-button>
<el-button @click="reset">重置</el-button>
</el-form-item>
</el-form>
<div class="list-table-wrap">
<DataTable :loading="loading" :data="list" height="100%">
<el-table-column label="平台" min-width="140">
<template #default="{ row }">{{ row.platform?.name || platformName(row.platform_id) }}</template>
</el-table-column>
<el-table-column prop="account" label="账号" min-width="160" show-overflow-tooltip />
<el-table-column label="密码" width="100">
<template #default="{ row }">{{ row.has_password ? '已设置' : '-' }}</template>
</el-table-column>
<el-table-column label="登录状态" width="100">
<template #default="{ row }">
<el-tag :type="isRowLoggedIn(row) ? 'success' : 'info'" :title="rowStatusTitle(row)">
{{ isRowLoggedIn(row) ? '已登录' : '未登录' }}
</el-tag>
</template>
</el-table-column>
<el-table-column label="创建时间" width="180">
<template #default="{ row }">{{ formatDateTime(row.created_at) }}</template>
</el-table-column>
<el-table-column label="操作" width="500" fixed="right">
<template #default="{ row }">
<el-button link type="primary" @click="openMerchants(row)">查看店铺</el-button>
<el-button
link
type="primary"
:loading="refreshingAreaAccountId === row.id"
@click="handleRefreshShopsArea(row)"
>
刷新所在区
</el-button>
<el-button link type="primary" @click="openExpressInterfaces(row)">快递接口</el-button>
<el-button
link
type="primary"
:loading="isActing(row, 'check')"
:disabled="isActing(row) && !isActing(row, 'check')"
@click="handleCheck(row)"
>
检查状态
</el-button>
<el-button
link
type="primary"
:loading="isActing(row, 'login')"
:disabled="isActing(row) && !isActing(row, 'login')"
@click="handleLogin(row)"
>
登录
</el-button>
<el-button link type="primary" @click="openEdit(row)">修改</el-button>
<el-button link type="danger" @click="handleDelete(row)">删除</el-button>
</template>
</el-table-column>
</DataTable>
</div>
<ListPager
:total="total"
:elapsed="elapsed"
:page="query.page"
:page-size="query.pageSize"
@page-change="handlePageChange"
@size-change="handleSizeChange"
/>
<el-dialog v-model="dialogVisible" :title="dialogTitle" width="480px">
<el-form ref="formRef" :model="form" :rules="rules" label-width="96px">
<el-form-item label="打单平台" prop="platform_id">
<el-select v-model="form.platform_id" placeholder="请选择" style="width: 100%">
<el-option v-for="p in platforms" :key="p.id" :label="platformOptionLabel(p)" :value="p.id" />
</el-select>
</el-form-item>
<el-form-item label="账号" prop="account">
<el-input v-model="form.account" placeholder="外部打单平台登录账号" />
</el-form-item>
<el-form-item label="密码" prop="password">
<el-input
v-model="form.password"
type="password"
show-password
:placeholder="editId == null ? '外部打单平台登录密码' : '留空表示不修改密码'"
/>
</el-form-item>
</el-form>
<template #footer>
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" :loading="submitting" @click="submitForm">确定</el-button>
</template>
</el-dialog>
<el-dialog
v-model="shopsDialogVisible"
:title="shopsAccount ? `店铺列表 - ${shopsAccount.account}` : '店铺列表'"
width="920px"
top="6vh"
>
<el-form :inline="true" class="merchant-search">
<el-form-item label="关键词">
<el-input
v-model="merchantQuery.keyword"
placeholder="商家名称/手机号"
clearable
@keyup.enter="searchMerchants"
/>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="searchMerchants">查询</el-button>
</el-form-item>
</el-form>
<DataTable
class="merchant-table"
:height="merchantTableHeight"
:loading="merchantLoading"
:data="merchantList"
@selection-change="handleMerchantSelection"
>
<el-table-column type="selection" width="45" :selectable="canSelectMerchant" />
<el-table-column prop="name" label="商家名称" min-width="160" show-overflow-tooltip />
<el-table-column prop="contact_name" label="联系人" width="120" />
<el-table-column prop="contact_phone" label="联系电话" width="130" />
<el-table-column label="状态" width="80">
<template #default="{ row }">
<el-tag :type="row.status === 0 ? 'info' : 'success'">{{ row.status === 0 ? '停用' : '正常' }}</el-tag>
</template>
</el-table-column>
<el-table-column label="是否已添加" width="110">
<template #default="{ row }">
<el-tag :type="row.added ? 'success' : 'info'">{{ row.added ? '已添加' : '未添加' }}</el-tag>
</template>
</el-table-column>
</DataTable>
<ListPager
:total="merchantTotal"
:page="merchantQuery.page"
:page-size="merchantQuery.pageSize"
@page-change="handleMerchantPageChange"
@size-change="handleMerchantSizeChange"
/>
<template #footer>
<span class="merchant-owner">
所属货主
<el-select
v-model="merchantSubsidiaryId"
placeholder="请选择货主"
filterable
style="width: 180px"
>
<el-option v-for="s in subsidiaryOptions" :key="s.id" :label="s.name" :value="s.id" />
</el-select>
</span>
<span class="merchant-selected">已选 {{ selectedMerchants.length }} 项</span>
<el-button @click="shopsDialogVisible = false">取消</el-button>
<el-button
type="primary"
:loading="addingShops"
:disabled="!selectedMerchants.length || !merchantSubsidiaryId"
@click="handleAddShops"
>
添加到店铺
</el-button>
</template>
</el-dialog>
<!-- 快递接口:打开读取已落库数据,可手动同步并绑定快递编码 -->
<ExpressInterfaceDialog v-model="expressVisible" :account="expressAccount" />
</el-card>
</template>
<style scoped>
/* 列表页统一版式:卡片撑满可视高度,筛选与分页固定,只有表格主体滚动 */
.external-print-card {
flex: 1;
min-height: 0;
display: flex;
flex-direction: column;
}
.external-print-card :deep(.el-card__body) {
flex: 1;
min-height: 0;
display: flex;
flex-direction: column;
overflow: hidden;
padding: 10px 12px;
}
.list-table-wrap {
flex: 1;
min-height: 0;
display: flex;
flex-direction: column;
overflow: hidden;
}
.page-header {
display: flex;
align-items: center;
justify-content: space-between;
font-weight: 600;
}
.external-print-card :deep(.el-alert) {
margin-bottom: 12px;
}
.search-bar {
flex: 0 0 auto;
margin-bottom: 12px;
}
.merchant-search {
margin-bottom: 8px;
}
.merchant-table {
width: 100%;
}
.merchant-owner {
margin-right: 12px;
font-size: 12px;
color: var(--el-text-color-secondary);
}
.merchant-selected {
margin-right: 12px;
font-size: 12px;
color: var(--el-text-color-secondary);
}
</style>