diff --git a/src/api/index/index.ts b/src/api/index/index.ts index 00e25686..568c28fe 100644 --- a/src/api/index/index.ts +++ b/src/api/index/index.ts @@ -185,7 +185,7 @@ export function getPlatformList(data?: Record) { } as any) } -export function getPrintPlatformTemplateList(data?: { platform_code?: string; express_code: string }) { +export function getPrintPlatformTemplateList(data?: { platform_code?: string; express_code: string; shopId?: string | number; shop_id?: string | number }) { return request({ url: '/system', method: 'post', diff --git a/src/api/order.print/orderprint.ts b/src/api/order.print/orderprint.ts index ee0febed..e1bde05a 100644 --- a/src/api/order.print/orderprint.ts +++ b/src/api/order.print/orderprint.ts @@ -19,6 +19,21 @@ export type GetWavePlatformPrintDataParams = { wave_ids: Array } +export type UnlockWavePlatformPrintGroupItem = { + wave_id: number | string + platform?: string + sys_logistics_code?: string + order_ids?: Array +} + +export type UnlockWavePlatformPrintDataByGroupParams = { + warehouse_id: number + wave_ids: Array + group_type?: 'platform' | 'platform_express' | 'order' + unlock_scope?: 'group' | 'wave' + groups: UnlockWavePlatformPrintGroupItem[] +} + /** 接口文档:summary / wave_ids / waveGroups(主)/ locked_wave_groups(锁定)/ platform_groups、order_list(兼容)/ ext */ export type WavePlatformPrintDataPayload = { summary?: Record @@ -63,3 +78,18 @@ export function getWavePlatformPrintData(data: GetWavePlatformPrintDataParams) { isSystemApi: true } as any) } + +/** 按分组解锁波次平台打印锁(order.print.unlockWavePlatformPrintDataByGroup) */ +export function unlockWavePlatformPrintDataByGroup(data: UnlockWavePlatformPrintDataByGroupParams) { + return request({ + url: '/system', + method: 'post', + params: { + method: 'order.print.unlockWavePlatformPrintDataByGroup', + v, + timestamp: new Date().getTime() + }, + data, + isSystemApi: true + } as any) +} diff --git a/src/views/order/waveOrder/GroupWaveBatchListPanel.vue b/src/views/order/waveOrder/GroupWaveBatchListPanel.vue index f7785c33..82b9c137 100644 --- a/src/views/order/waveOrder/GroupWaveBatchListPanel.vue +++ b/src/views/order/waveOrder/GroupWaveBatchListPanel.vue @@ -824,6 +824,8 @@ :error-message="platformPrintErrorMessage" :payload="platformPrintPayload" :request-meta="platformPrintRequestMeta" + @refresh="handlePlatformPrintDialogRefresh" + @print-success="handlePlatformPrintListRefresh" /> @@ -1914,6 +1916,30 @@ async function handleBatchPlatformPrint() { } } +async function handlePlatformPrintDialogRefresh() { + const meta = platformPrintRequestMeta.value + if (!meta) return + try { + const res: any = await getWavePlatformPrintData({ + warehouse_id: Number(meta.warehouse_id), + wave_ids: meta.wave_ids + }) + if (res?.code !== undefined && res?.code !== null && !okCode(res.code)) { + throw new Error(res?.message || '刷新平台打印数据失败') + } + platformPrintPayload.value = unwrapWavePlatformPrintPayload(res) + platformPrintErrorMessage.value = '' + } catch (error: any) { + platformPrintPayload.value = null + platformPrintErrorMessage.value = error?.message || '刷新平台打印数据失败' + ElMessage.error(platformPrintErrorMessage.value) + } +} + +async function handlePlatformPrintListRefresh() { + await loadData() +} + function stopFetchTaskPolling() { if (fetchTaskPollTimer.value) { clearTimeout(fetchTaskPollTimer.value) diff --git a/src/views/order/waveOrder/SandanWaveBatchListPanel.vue b/src/views/order/waveOrder/SandanWaveBatchListPanel.vue index 41475ab1..93d925cb 100644 --- a/src/views/order/waveOrder/SandanWaveBatchListPanel.vue +++ b/src/views/order/waveOrder/SandanWaveBatchListPanel.vue @@ -410,6 +410,8 @@ :error-message="platformPrintErrorMessage" :payload="platformPrintPayload" :request-meta="platformPrintRequestMeta" + @refresh="handlePlatformPrintDialogRefresh" + @print-success="handlePlatformPrintListRefresh" /> @@ -1232,6 +1234,30 @@ async function handleBatchPlatformPrint() { } } +async function handlePlatformPrintDialogRefresh() { + const meta = platformPrintRequestMeta.value + if (!meta) return + try { + const res: any = await getWavePlatformPrintData({ + warehouse_id: Number(meta.warehouse_id), + wave_ids: meta.wave_ids + }) + if (res?.code !== undefined && res?.code !== null && !okCode(res.code)) { + throw new Error(res?.message || '刷新平台打印数据失败') + } + platformPrintPayload.value = unwrapWavePlatformPrintPayload(res) + platformPrintErrorMessage.value = '' + } catch (error: any) { + platformPrintPayload.value = null + platformPrintErrorMessage.value = error?.message || '刷新平台打印数据失败' + ElMessage.error(platformPrintErrorMessage.value) + } +} + +async function handlePlatformPrintListRefresh() { + await loadData() +} + async function handleBatchPrint() { if (!isGroupWaveTab.value) { ElMessage.warning('批量打印仅在「团购波次」页签可用') diff --git a/src/views/order/waveOrder/component/WavePlatformPrintPreviewDialog.vue b/src/views/order/waveOrder/component/WavePlatformPrintPreviewDialog.vue index eb83959c..0527fa05 100644 --- a/src/views/order/waveOrder/component/WavePlatformPrintPreviewDialog.vue +++ b/src/views/order/waveOrder/component/WavePlatformPrintPreviewDialog.vue @@ -63,7 +63,7 @@ }} | {{ platform.platform_name }} - {{ platform.platform }} + {{ platform.platform_display }} 订单 {{ platform.order_count }} @@ -76,7 +76,19 @@
-

锁定波次 / 平台列表

+
+

锁定波次 / 平台列表

+ + 一键解锁 + +
@@ -102,9 +114,21 @@ }} | {{ platform.platform_name }} - {{ platform.platform }} + {{ platform.platform_display }} +
+
+ 锁定订单 {{ platform.order_count }} + + 解锁 +
- 锁定订单 {{ platform.order_count }} @@ -135,6 +159,7 @@ import { computed, reactive, ref, watch } from 'vue' import { ElMessage } from 'element-plus' import type { WavePlatformPrintDataPayload } from '@/api/order.print/orderprint' +import { unlockWavePlatformPrintDataByGroup } from '@/api/order.print/orderprint' import * as stationApi from '@/api/print/print' import type { PrintSendParams } from '@/api/print/print' @@ -144,9 +169,12 @@ type AnyRecord = Record type WaveGroupPlatform = { platformKey: string platform: string + platform_display: string platform_name: string /** 快递 / 物流公司名称(与波次列表 sys_logistics_name 一致) */ sys_logistics_name: string + sys_logistics_code: string + order_ids: Array order_count: number } type WaveGroupView = { @@ -166,6 +194,8 @@ const props = defineProps<{ const emit = defineEmits<{ 'update:modelValue': [boolean] + refresh: [] + 'print-success': [] }>() const printRootRef = ref(null) @@ -180,6 +210,9 @@ type StationOption = { const stationOptions = ref([]) const stationLoading = ref(false) const stationPrinting = ref(false) +const unlockAllLoading = ref(false) +const unlockingPlatformKey = ref('') +const unlockedPlatformKeys = ref>(new Set()) const printSettings = reactive({ station: '' as string }) @@ -202,17 +235,23 @@ function buildWaveGroupList(groups: unknown[] | null | undefined, groupKeyPrefix .filter((x) => x && typeof x === 'object') .map((rawPlatform, platformIndex) => { const platform = rawPlatform as AnyRecord - const platformCode = toText(platform.platform, `platform-${platformIndex + 1}`) - const platformName = toText(platform.platform_name, platformCode) + const platformCode = toRawText(platform.platform) + const platformDisplay = platformCode || `platform-${platformIndex + 1}` + const platformName = toText(platform.platform_name, platformDisplay) const logisticsRaw = platform.sys_logistics_name ?? platform.logistics_name ?? platform.express_name ?? platform.logisticsName const sys_logistics_name = logisticsRaw != null && String(logisticsRaw).trim() !== '' ? String(logisticsRaw).trim() : '' + const sys_logistics_code = extractSysLogisticsCode(platform) + const order_ids = extractOrderIds(platform) return { - platformKey: `${groupKeyPrefix}-${wave_id}-${platformCode}-${platformIndex}`, + platformKey: `${groupKeyPrefix}-${wave_id}-${platformDisplay}-${platformIndex}`, platform: platformCode, + platform_display: platformDisplay, platform_name: platformName, sys_logistics_name, + sys_logistics_code, + order_ids, order_count: toNum(platform.order_count, 0) } }) @@ -230,19 +269,42 @@ watch( () => props.modelValue, (visible) => { if (visible) { + unlockedPlatformKeys.value = new Set() + unlockingPlatformKey.value = '' + unlockAllLoading.value = false void loadStationOptions() } } ) +watch( + () => props.payload, + () => { + // 父级刷新到新 payload 后,清空本地已解锁过滤,确保页面展示后端最新状态 + unlockedPlatformKeys.value = new Set() + unlockingPlatformKey.value = '' + } +) + const waveGroupList = computed(() => { return buildWaveGroupList(props.payload?.waveGroups, 'normal') }) -const lockedWaveGroupList = computed(() => { +const rawLockedWaveGroupList = computed(() => { return buildWaveGroupList(props.payload?.locked_wave_groups, 'locked') }) +const lockedWaveGroupList = computed(() => { + const unlocked = unlockedPlatformKeys.value + if (!unlocked.size) return rawLockedWaveGroupList.value + return rawLockedWaveGroupList.value + .map((wave) => ({ + ...wave, + platforms: wave.platforms.filter((platform) => !unlocked.has(platform.platformKey)) + })) + .filter((wave) => wave.platforms.length > 0) +}) + const hasWaveGroups = computed(() => { return waveGroupList.value.length > 0 }) @@ -257,11 +319,252 @@ function toText(v: unknown, fallback = '—'): string { return s || fallback } +function toRawText(v: unknown): string { + if (v === null || v === undefined) return '' + return String(v).trim() +} + function toNum(v: unknown, fallback = 0): number { const n = Number(v) return Number.isFinite(n) ? n : fallback } +function extractOrderIds(platform: AnyRecord): Array { + const direct = platform.order_ids + if (Array.isArray(direct)) { + return direct + .map(normalizeIdValue) + .filter((id) => !!id) + } + + const rawOrders = platform.orders + if (!Array.isArray(rawOrders)) return [] + return rawOrders + .map((item) => { + if (!item || typeof item !== 'object') return '' + const rec = item as AnyRecord + return normalizeIdValue(rec.order_id ?? rec.id ?? '') + }) + .filter((id) => !!id) +} + +function extractSysLogisticsCode(platform: AnyRecord): string { + const direct = toRawText( + platform.sys_logistics_code ?? + platform.sysLogisticsCode ?? + platform.logistics_code ?? + platform.logisticsCode ?? + platform.express_code ?? + platform.expressCode + ) + if (direct) return direct + + const sysLogistics = platform.sys_logistics + if (sysLogistics && typeof sysLogistics === 'object') { + const code = toRawText((sysLogistics as AnyRecord).code) + if (code) return code + } + return '' +} + +function normalizeIdValue(v: unknown): number | string { + const raw = String(v ?? '').trim() + if (!raw) return '' + if (/^\d+$/.test(raw)) { + const n = Number(raw) + if (Number.isSafeInteger(n)) { + return n + } + } + return raw +} + +type UnlockGroupPayload = { + wave_id: number | string + platform?: string + sys_logistics_code?: string + order_ids?: Array +} + +type UnlockGroupMode = 'platform_express' | 'order' + +function buildUnlockGroupsForPlatforms(wave: WaveGroupView, platforms: WaveGroupPlatform[]): UnlockGroupPayload[] { + return platforms.map((platform) => { + const item: UnlockGroupPayload = { + wave_id: normalizeIdValue(wave.wave_id), + platform: platform.platform || undefined, + sys_logistics_code: platform.sys_logistics_code || undefined + } + if (platform.order_ids.length > 0) { + item.order_ids = platform.order_ids + } + return item + }) +} + +function buildWaveIdsFromGroups(groups: UnlockGroupPayload[]): Array { + const uniq = new Set() + const result: Array = [] + groups.forEach((group) => { + const normalized = normalizeIdValue(group.wave_id) + const waveId = String(normalized ?? '').trim() + if (!waveId || uniq.has(waveId)) return + uniq.add(waveId) + result.push(normalized) + }) + return result +} + +function splitUnlockGroupsByMode( + wave: WaveGroupView, + platforms: WaveGroupPlatform[] +): { + platformExpressGroups: UnlockGroupPayload[] + orderGroups: UnlockGroupPayload[] + platformExpressKeys: string[] + orderKeys: string[] +} { + const platformExpressGroups: UnlockGroupPayload[] = [] + const orderGroups: UnlockGroupPayload[] = [] + const platformExpressKeys: string[] = [] + const orderKeys: string[] = [] + const waveId = normalizeIdValue(wave.wave_id) + + platforms.forEach((platform) => { + if (platform.platform && platform.sys_logistics_code) { + platformExpressGroups.push({ + wave_id: waveId, + platform: platform.platform, + sys_logistics_code: platform.sys_logistics_code + }) + platformExpressKeys.push(platform.platformKey) + return + } + + if (platform.order_ids.length > 0) { + orderGroups.push({ + wave_id: waveId, + order_ids: platform.order_ids + }) + orderKeys.push(platform.platformKey) + } + }) + + return { + platformExpressGroups, + orderGroups, + platformExpressKeys, + orderKeys + } +} + +async function unlockGroupsByMode( + mode: UnlockGroupMode, + groups: UnlockGroupPayload[], + unlockedPlatformKeysToMark: string[] +) { + const warehouseId = Number(props.requestMeta?.warehouse_id || 0) + if (!warehouseId) { + ElMessage.warning('缺少仓库ID,无法解锁') + return false + } + if (!groups.length) { + return true + } + + const waveIds = buildWaveIdsFromGroups(groups) + if (!waveIds.length) { + ElMessage.warning('缺少波次ID,无法解锁') + return false + } + + const res: any = await unlockWavePlatformPrintDataByGroup({ + warehouse_id: warehouseId, + wave_ids: waveIds, + group_type: mode, + unlock_scope: 'group', + groups + }) + + if (!isApiSuccess(res)) { + ElMessage.error(res?.message || '解锁失败') + return false + } + + const next = new Set(unlockedPlatformKeys.value) + unlockedPlatformKeysToMark.forEach((key) => next.add(key)) + unlockedPlatformKeys.value = next + + const unlockedCount = + Number(res?.data?.unlocked_count ?? res?.data?.unlock_count ?? unlockedPlatformKeysToMark.length) || + unlockedPlatformKeysToMark.length + ElMessage.success(`解锁成功,已解锁 ${unlockedCount} 组`) + return true +} + +async function unlockSinglePlatform(wave: WaveGroupView, platform: WaveGroupPlatform) { + if (unlockAllLoading.value) return + const key = platform.platformKey + unlockingPlatformKey.value = key + try { + const { platformExpressGroups, orderGroups, platformExpressKeys, orderKeys } = splitUnlockGroupsByMode(wave, [platform]) + if (!platformExpressGroups.length && !orderGroups.length) { + ElMessage.warning('当前分组缺少解锁条件:需提供平台+物流编码,或订单号') + return + } + if (platformExpressGroups.length) { + const ok = await unlockGroupsByMode('platform_express', platformExpressGroups, platformExpressKeys) + if (ok) emit('refresh') + return + } + const ok = await unlockGroupsByMode('order', orderGroups, orderKeys) + if (ok) emit('refresh') + } finally { + unlockingPlatformKey.value = '' + } +} + +async function unlockAllLockedGroups() { + if (unlockAllLoading.value || !lockedWaveGroupList.value.length) return + unlockAllLoading.value = true + try { + const platformExpressGroups: UnlockGroupPayload[] = [] + const orderGroups: UnlockGroupPayload[] = [] + const platformExpressKeys: string[] = [] + const orderKeys: string[] = [] + lockedWaveGroupList.value.forEach((wave) => { + const split = splitUnlockGroupsByMode(wave, wave.platforms) + platformExpressGroups.push(...split.platformExpressGroups) + orderGroups.push(...split.orderGroups) + platformExpressKeys.push(...split.platformExpressKeys) + orderKeys.push(...split.orderKeys) + }) + + if (!platformExpressGroups.length && !orderGroups.length) { + ElMessage.warning('未找到可解锁分组,请检查物流编码或订单号数据') + return + } + + let successCount = 0 + if (platformExpressGroups.length) { + const ok = await unlockGroupsByMode('platform_express', platformExpressGroups, platformExpressKeys) + if (ok) successCount += 1 + } + if (orderGroups.length) { + const ok = await unlockGroupsByMode('order', orderGroups, orderKeys) + if (ok) successCount += 1 + } + + if (!successCount) { + ElMessage.error('批量解锁失败') + return + } + emit('refresh') + } finally { + unlockAllLoading.value = false + } +} + function isApiSuccess(res: any) { const c = res?.code return !!res && (c === 0 || c === 200 || c === 1 || c === 20000) @@ -420,6 +723,8 @@ async function printToStation() { const res: any = await stationApi.sendPrint(printPayload) if (res && (res.code === 0 || res.code === 200 || res.flag === 'success' || res.status === 'success')) { ElMessage.success(res?.message || '已发送到打印台') + emit('refresh') + emit('print-success') return } ElMessage.error(res?.msg || res?.message || '发送打印失败') @@ -621,6 +926,12 @@ function escapeHtml(s: string) { margin-bottom: 0; } + &__actions { + display: inline-flex; + align-items: center; + gap: 6px; + } + &__title { font-size: 12px; color: #303133; @@ -648,8 +959,16 @@ function escapeHtml(s: string) { .wave-print-section { margin-bottom: 18px; + &__header { + display: flex; + align-items: center; + justify-content: space-between; + gap: 8px; + margin-bottom: 6px; + } + &__title { - margin: 0 0 6px; + margin: 0; font-size: 13px; font-weight: 600; color: #303133; diff --git a/src/views/system/setting/expressinterface/index.vue b/src/views/system/setting/expressinterface/index.vue index d13e0d39..c140b21f 100644 --- a/src/views/system/setting/expressinterface/index.vue +++ b/src/views/system/setting/expressinterface/index.vue @@ -418,7 +418,7 @@ filterable clearable :loading="printTemplateLoading" - :disabled="!formData.express_code" + :disabled="!activeTemplateExpressCode" placeholder="请选择打印模板" > ([]) const selectedShopId = ref(undefined) const useCustomExpressCode = ref(false) const printTemplateLoading = ref(false) +const activeTemplateExpressCode = computed(() => + String(useCustomExpressCode.value ? formData.express_code : formData.sys_express_code).trim() +) const serviceValueMap = ref>({}) const shopExpressSitesMap = ref>({}) const selectedSiteKey = ref('') @@ -1069,8 +1072,8 @@ async function loadPlatformOptions() { } } -async function loadPrintPlatformTemplateOptions(sourceExpressCode?: string, sourcePlatformCode?: string) { - const expressCode = String(sourceExpressCode ?? formData.express_code ?? '').trim().toUpperCase() +async function loadPrintPlatformTemplateOptions(sourceExpressCode?: string, sourcePlatformCode?: string, sourceShopId?: string | number) { + const expressCode = String(sourceExpressCode ?? activeTemplateExpressCode.value ?? '').trim().toUpperCase() if (!expressCode) { templateOptions.value = [] formData.template_url = '' @@ -1082,7 +1085,9 @@ async function loadPrintPlatformTemplateOptions(sourceExpressCode?: string, sour const platformCode = String(sourcePlatformCode ?? formData.platform_code ?? '').trim().toUpperCase() const res: any = await getPrintPlatformTemplateList({ platform_code: platformCode || undefined, - express_code: expressCode + express_code: expressCode, + shopId: sourceShopId ?? selectedShopId.value ?? formData.shop_id, + shop_id: sourceShopId ?? selectedShopId.value ?? formData.shop_id }) if (!isSuccess(res)) { templateOptions.value = [] @@ -1561,7 +1566,7 @@ async function handleExpressCodeChange() { serviceValueMap.value = {} syncExtDataByServiceSelection() console.log('handleExpressCodeChange', { currentExpressCode, platformCode: formData.platform_code }) - await loadPrintPlatformTemplateOptions(currentExpressCode, formData.sys_platform_code) + await loadPrintPlatformTemplateOptions(currentExpressCode, formData.sys_platform_code, selectedShopId.value ?? formData.shop_id) if (!useCustomExpressCode.value || !currentExpressCode) return await loadCustomExpressOptions(currentExpressCode) } @@ -1767,7 +1772,7 @@ async function openEdit(row: ExpressInterfaceRecord) { } ] } - await loadPrintPlatformTemplateOptions(formData.express_code, formData.platform_code) + await loadPrintPlatformTemplateOptions(activeTemplateExpressCode.value, formData.platform_code, selectedShopId.value ?? formData.shop_id) formRef.value?.clearValidate() }