@@ -1,5 +1,5 @@
< script setup lang = "ts" >
import { computed , onMounted , onUnmounted , ref } from 'vue'
import { computed , onMounted , onUnmounted , ref , watch } from 'vue'
import { ElMessage , ElMessageBox } from 'element-plus'
import TaskCenterPanel from '@/shared/components/tasks/TaskCenterPanel.vue'
import type { TaskItemView , TaskStatCard } from '@/shared/components/tasks/types'
@@ -15,6 +15,7 @@ import {
fetchJdPublishProducts ,
fetchJdPublishTaskProgressBatch ,
interruptJdPublishTask ,
retryJdPublishBrand ,
type JdPublishHistoryItemVo ,
type JdPublishProductVo ,
} from '@/shared/api/types/modules/jd-publish'
@@ -36,7 +37,7 @@ import NotificationBell from '@/shared/components/NotificationBell.vue'
* 同一规则折算 SKU 价(铁律 27a 改价同源);
* 3. 网页原样转交建单响应的 queuePayload;认领只由客户端与后端完成,投递失败仍可由客户端拉取;
* 4. 进度只读服务端:行级四档计数来自发布明细表聚合,total 缺失时只报已发布行数,不编造百分比;
* 5. 失败/中断「重试/继续」走通用原位复位(铁律 26,不克隆),新一轮由客户端拉取认领 ;
* 5. 失败/中断在原任务上重试,目标品牌校验失败时只允许符合条件的任务改品牌后重试 ;
* 6. 京麦未登录等凭据致命失败(铁律 31):服务端整单判死后 useLiveTaskBoard 经
* reportCredentialFatal 弹一次窗(credential-fatal 同形接入,AC-5 用户端半边;弹窗组件
* 挂 App.vue,本页无需重复挂)。
@@ -152,6 +153,14 @@ const PRICE_RULE_MODES = [
] as const
const priceMode = ref < 'none' | 'percent' | 'fixed' > ( 'none' )
const priceValue = ref < number | null > ( null )
const targetBrandInput = ref ( '' )
const TARGET _BRAND _MAX = 120
const TARGET _BRAND _ERROR = '请输入目标品牌名称,去掉首尾空格后须为 1–120 个字符。'
const targetBrandName = computed ( ( ) => targetBrandInput . value . trim ( ) )
function isValidBrand ( name : string ) {
return name . length > 0 && name . length <= TARGET _BRAND _MAX
}
const priceValueMissing = computed ( ( ) => priceMode . value !== 'none' && ( priceValue . value == null || priceValue . value < 0 ) )
@@ -159,9 +168,23 @@ const submitDisabled = computed(() =>
submitting . value
|| ! selectedGroupId . value
|| ! selectedProducts . value . size
|| ! isValidBrand ( targetBrandName . value )
|| priceValueMissing . value )
// 重试回执不带代数:旧代 + 1 只用作读侧下限,不生成任务或队列数据 。
// 此闸只约束京东品牌发布,不改变客户端其他功能的全局版本门槛 。
const JD _PUBLISH _PROTOCOL = 3
const JD _UPGRADE _NOTICE = '请升级到支持目标品牌发布的数富AI客户端,并在客户端内打开本页。'
async function supportsBrandPublishing ( ) {
try {
return await getPywebviewApi ( ) ? . jd _publish _protocol ? . ( ) === JD _PUBLISH _PROTOCOL
} catch ( error ) {
console . error ( '[jd-publish] 读取本机发布协议失败' , error )
return false
}
}
// 通用重试回执不带代数:旧代 + 1 只用作读侧下限,不生成任务或队列数据。
const retryAttemptFloor = new Map < number , number > ( )
const awaitingRetryProgress = new Set < number > ( )
const retryingTaskIds = ref ( new Set < number > ( ) )
@@ -192,12 +215,22 @@ async function fetchCurrentProgress(taskIds: number[], { force }: { force: boole
} )
return {
... batch ,
items : batch . items . filter ( ( { task , items } ) => {
if ( task ? . id == null || ! unchanged ( task . id ) || ! acceptsAttempt ( task . id , task . resumeAttempt ) ) return false
items : batch . items . flatMap ( ( detail ) => {
const { task , items } = detail
if ( task ? . id == null || ! unchanged ( task . id ) || ! acceptsAttempt ( task . id , task . resumeAttempt ) ) return [ ]
const row = items ? . [ 0 ]
if ( row && ! acceptsAttempt ( task . id , row . resumeAttempt ) ) return false
if ( row && ! acceptsAttempt ( task . id , row . resumeAttempt ) ) return [ ]
awaitingRetryProgress . delete ( task . id )
return true
// 稳定分档来自任务头,而非商品行错误;缺行时仍显示失败,但不猜商品计数。
if ( task . status ? . toUpperCase ( ) !== 'FAILED' || ! validationFailureKind ( task . errorMessage ) ) return [ detail ]
// 京东校验提示由本页处理,不把原文交给通用凭据分类器。
return [ { ... detail , task : { ... task , errorMessage : undefined } , items : [ {
... row ,
taskId : task . id ,
taskStatus : task . status ,
resumeAttempt : task . resumeAttempt ,
errorMessage : task . errorMessage ,
} ] } ]
} ) ,
missingTaskIds : batch . missingTaskIds . filter ( unchanged ) ,
}
@@ -249,6 +282,7 @@ function toJdPublishTaskView(item: JdPublishHistoryItemVo): TaskItemView {
const showProgress = isActiveTask ( item )
const total = typeof item . productTotal === 'number' && item . productTotal > 0 ? item . productTotal : null
const published = ( item . successCount ? ? 0 ) + ( item . failedCount ? ? 0 )
const failureNotice = validationFailureNotice ( item )
return {
key : ` jd-publish- ${ item . taskId ? ? item . taskNo ? ? item . updatedAt ? ? '' } ` ,
title : ` 商品上架 · ${ total ? ? '—' } 个商品 ` ,
@@ -261,7 +295,8 @@ function toJdPublishTaskView(item: JdPublishHistoryItemVo): TaskItemView {
` 成功 ${ item . successCount ? ? 0 } / 失败 ${ item . failedCount ? ? 0 } `
+ ( item . publishingCount ? ` / 发布中 ${ item . publishingCount } ` : '' ) ,
... ( showProgress && total ? [ ` 已发布 ${ Math . min ( published , total ) } / ${ total } 个商品 ` ] : [ ] ) ,
... ( item . errorMessage ? [ ` 错误: ${ userNotice ( item . errorMessage ) } ` ] : [ ] ) ,
... ( failureNotice ? [ ` 错误: ${ failureNotice . title } : ${ failureNotice . body } ` ]
: item . errorMessage ? [ ` 错误: ${ userNotice ( item . errorMessage ) } ` ] : [ ] ) ,
] ,
progress : showProgress ? { percent : progressPercent ( item ) , countLabel : progressCountLabel ( item ) } : null ,
source : item ,
@@ -317,15 +352,73 @@ function progressCountLabel(item: JdPublishHistoryItemVo) {
return ` 已发布 ${ published } / ${ total } 个商品( ${ progressPercent ( item ) } %) `
}
/** 失败/已中断的任务能续跑:在原任务上复位(不新建任务)。 */
function validationFailureKind ( message : string | undefined ) {
if ( message ? . startsWith ( '目标品牌校验失败:' ) ) return 'brand'
if ( message ? . startsWith ( '本店源商品校验失败:' ) ) return 'source'
return null
}
function canChangeBrand ( item : JdPublishHistoryItemVo ) {
return item . successCount === 0 && item . publishingCount === 0
}
function validationFailureDetail ( message : string ) {
// 只去掉发布校验机器码,不把商品编码或英文品牌名误当错误码。
const detail = message . slice ( message . indexOf ( ': ' ) + 1 )
. replace ( /[( (]\s*JD_(?:BRAND|SOURCE)_[A-Z0-9_]+\s*[) )]|\bJD_(?:BRAND|SOURCE)_[A-Z0-9_]+\b/g , '' )
. trim ( )
return /\p{Script=Han}/u . test ( detail ) ? detail : ''
}
function validationFailureNotice ( item : JdPublishHistoryItemVo ) {
if ( normalizeTaskStatus ( item ) !== 'FAILED' ) return null
const kind = validationFailureKind ( item . errorMessage )
if ( ! kind || ! item . errorMessage ) return null
const detail = validationFailureDetail ( item . errorMessage )
const addAction = ( action : string ) => detail ? ` ${ detail } \ n ${ action } ` : action
if ( kind === 'brand' ) {
return {
title : '目标品牌校验失败' ,
body : addAction ( canChangeBrand ( item )
? '请核对当前京麦店铺可用的品牌名称,然后选择「修改品牌并重试」。'
: '请保留已发布结果,核对京麦中的发布状态,然后重新选择未发布商品建单。' ) ,
}
}
return {
title : '本店源商品校验失败' ,
body : addAction ( '请切换到正确店铺后重试,或重新选择本店商品建单。' ) ,
}
}
const shownValidationFailures = new Set < string > ( )
watch (
( ) => board . historyItems . value . map ( ( item ) => ( {
taskId : item . taskId , attempt : item . resumeAttempt ? ? 0 , notice : validationFailureNotice ( item ) ,
} ) ) ,
( items ) => {
for ( const { taskId , attempt , notice } of items ) {
if ( ! taskId || ! notice ) continue
const key = ` ${ taskId } : ${ attempt } `
if ( shownValidationFailures . has ( key ) ) continue
shownValidationFailures . add ( key )
void ElMessageBox . alert ( notice . body , notice . title , {
type : 'warning' , confirmButtonText : '知道了' , dangerouslyUseHTMLString : false ,
} )
. then ( ( ) => undefined , ( ) => undefined )
}
} ,
)
/** 失败/已中断的任务能续跑;品牌失败仅在既无成功也无待确认商品时允许改品牌。 */
function canRetryTask ( item : JdPublishHistoryItemVo | null | undefined ) : boolean {
if ( ! item ? . taskId ) return false
const status = normalizeTaskStatus ( item )
if ( status === 'FAILED' && validationFailureKind ( item . errorMessage ) === 'brand' ) return canChangeBrand ( item )
return status === 'FAILED' || status === 'CANCELLED'
}
/** 按钮文案:已中断的任务显示「继续」(从断点接着跑),失败任务仍是「重试」。 */
function retryLabel ( item : JdPublishHistoryItemVo | null | undefined ) : string {
if ( normalizeTaskStatus ( item ) === 'FAILED' && validationFailureKind ( item ? . errorMessage ) === 'brand' ) return '修改品牌并重试'
return normalizeTaskStatus ( item ) === 'CANCELLED' ? '继续' : '重试'
}
@@ -358,24 +451,53 @@ async function interruptTaskRecord(item: JdPublishHistoryItemVo | null | undefin
}
}
async function retryTaskRecord ( item : JdPublishHistoryItemVo | null | undefined ) {
const taskId = item ? . taskId
if ( ! taskId || retryingTaskIds . value . has ( taskId ) ) return
const isResume = normalizeTaskStatus ( item ) === 'CANCELLED'
retryingTaskIds . value . add ( taskId )
const retried = await retryClientTask ( taskId ) . then ( ( ) => true , ( error : unknown ) => {
ElMessage . error ( error instanceof Error ? error . message : '重试失败 ' )
return false
} )
retryingTaskIds . value . delete ( taskId )
if ( ! retried ) return
async function promptRetryBrand ( ) : Promise < string | null > {
if ( ! await supportsBrandPublishing ( ) ) {
ElMessage . error ( JD _UPGRADE _NOTICE )
return null
}
const name = await ElMessageBox . prompt ( '请输入当前京麦店铺可用的目标品牌名称。' , '修改品牌并重试' , {
confirmButtonText : '重试' ,
cancelButtonText : '取消' ,
inputValidator : ( value : string ) => isValidBrand ( value . trim ( ) ) || TARGET _BRAND _ERROR ,
} ) . then ( ( { value } ) => value . trim ( ) , ( ) => null )
return name && isValidBrand ( name ) ? name : null
}
async function retryTaskRecord ( item : JdPublishHistoryItemVo | null | undefined ) {
if ( ! item ? . taskId || ! canRetryTask ( item ) || retryingTaskIds . value . has ( item . taskId ) ) return
const { taskId , resumeAttempt = 0 } = item
const changeBrand = normalizeTaskStatus ( item ) === 'FAILED' && validationFailureKind ( item . errorMessage ) === 'brand'
const isResume = normalizeTaskStatus ( item ) === 'CANCELLED'
let nextPayload : Record < string , unknown > | undefined
retryingTaskIds . value . add ( taskId )
try {
if ( changeBrand ) {
const brand = await promptRetryBrand ( )
if ( ! brand ) return
const vo = await retryJdPublishBrand ( taskId , brand , resumeAttempt )
nextPayload = vo . queuePayload
} else {
await retryClientTask ( taskId )
}
retryAttemptFloor . set ( taskId , resumeAttempt + 1 )
} catch ( error ) {
ElMessage . error ( error instanceof Error ? userNotice ( error . message ) : '重试失败' )
return
} finally {
retryingTaskIds . value . delete ( taskId )
}
retryAttemptFloor . set ( taskId , ( item . resumeAttempt ? ? 0 ) + 1 )
awaitingRetryProgress . add ( taskId )
board . removePollingTask ( taskId )
board . historyItems . value = board . historyItems . value . filter ( ( row ) => row . taskId !== taskId )
board . addPollingTask ( taskId )
ElMessage . success ( isResume ? '已提交继续,等待本机客户端领取' : '已提交原任务重试,等待本机客户端领取' )
if ( changeBrand ) {
const pushNote = await pushToClientQueue ( nextPayload , '已修改品牌并重试' )
ElMessage . success ( pushNote ? ? '已修改品牌并重试,任务已送入本机队列' )
} else {
ElMessage . success ( isResume ? '已提交继续,等待本机客户端领取' : '已提交原任务重试,等待本机客户端领取' )
}
await refreshBoard ( ) . catch ( ( error ) => console . error ( '[jd-publish] 重试后刷新任务失败' , error ) )
}
@@ -388,21 +510,20 @@ async function refreshBoard() {
] )
}
const WAIT _FOR _CLIENT = '任务已创建,等待本机客户端领取。请保持客户端在线。'
async function pushToClientQueue ( payload : Record < string , unknown > | undefined ) : Promise < string | null > {
if ( ! payload ) return WAIT _FOR _CLIENT
async function pushToClientQueue ( payload : Record < string , unknown > | undefined , action = '任务已创建' ) : Promise < string | null > {
if ( ! payload ) return ` ${ action } ,等待本机客户端领取。请保持客户端在线。 `
const api = getPywebviewApi ( )
if ( ! api ? . enqueue _json ) {
return '任务已创建 ,等待本机客户端领取。请打开本机客户端并登录同一账号。'
return ` ${ action } ,等待本机客户端领取。请打开本机客户端并登录同一账号。 `
}
const result = await api . enqueue _json ( payload ) . catch ( ( error ) => {
try {
const result = await api . enqueue _json ( payload )
if ( result ? . success ) return null
if ( result ? . error ) console . error ( '[jd-publish] enqueue_json 被拒' , result . error )
} catch ( error ) {
console . error ( '[jd-publish] 推送本机队列失败' , error )
return null
} )
if ( result ? . success ) return null
if ( result ? . error ) console . error ( '[jd-publish] enqueue_json 被拒' , result . error )
return '任务已创建,但未能推送到本机队列。请保持客户端在线,客户端会自动领取。'
}
return ` ${ action } ,但未能推送到本机队列。请保持客户端在线,客户端会自动领取。 `
}
const isRunning = computed ( ( ) => Boolean ( board . dashboard . value . pendingTaskCount ) || submitting . value )
@@ -426,7 +547,12 @@ async function startPublish() {
// (服务端按 owner 校验,选了他人商品当场拒,AC-9)。
const categoryId = selectedGroupId . value
const productIds = selectedProductIds . value
const vo = await createJdPublishTask ( categoryId , productIds , priceRule )
const brand = targetBrandName . value
if ( ! await supportsBrandPublishing ( ) ) {
errorMessage . value = JD _UPGRADE _NOTICE
return
}
const vo = await createJdPublishTask ( categoryId , productIds , priceRule , brand )
board . addPollingTask ( vo . taskId )
const pushNote = await pushToClientQueue ( vo . queuePayload )
successMessage . value = pushNote
@@ -542,6 +668,14 @@ onUnmounted(() => {
< p class = "category-hint" > { { priceRuleHint } } < / p >
< / div >
< div class = "category-block" >
< label class = "category-label" for = "jd-publish-target-brand" > 目标品牌名称 ( 必填 ) < / label >
< input id = "jd-publish-target-brand" v-model = "targetBrandInput" class="target-brand-input" type="text"
required : aria -invalid = " Boolean ( targetBrandInput & & ! isValidBrand ( targetBrandName ) ) "
aria -describedby = " jd -publish -brand -hint " placeholder = "输入当前京麦店铺可用的品牌名称" / >
< p id = "jd-publish-brand-hint" class = "category-hint" > 本次选中的商品统一使用此品牌 , 不会修改京东数据库中的原品牌 。 < / p >
< / div >
< button class = "collect-btn" type = "button" :disabled = "submitDisabled" @click ="startPublish" >
{{ submitting ? ' 创建中 … ' : ` 上架已选 $ { selectedProducts.size } 个商品 ` }}
< / button >
@@ -630,6 +764,8 @@ onUnmounted(() => {
. product - pager { margin - top : 8 px ; justify - content : center ; }
. product - pager : deep ( . el - pager li ) , . product - pager : deep ( . btn - prev ) , . product - pager : deep ( . btn - next ) { background : transparent ; color : # c8d2e2 ; }
. product - pager : deep ( . el - pager li . is - active ) { color : # 6 ea8ff ; }
. target - brand - input { box - sizing : border - box ; width : 100 % ; padding : 8 px 10 px ; border : 1 px solid # 3 e4a62 ; border - radius : 6 px ; background : # 232 b3d ; color : # c8d2e2 ; font : inherit ; }
. target - brand - input [ aria - invalid = "true" ] { border - color : # fca5a5 ; }
. price - input { width : 100 % ; margin - top : 8 px ; }
. price - input : deep ( . el - input _ _wrapper ) { background - color : # 232 b3d ; box - shadow : 0 0 0 1 px # 3 e4a62 inset ; }
. price - input : deep ( . el - input _ _inner ) { color : # c8d2e2 ; }