divisionTestStepProgress.js 4.29 KB
/** subProgress 进行中:34 步骤1 / 35 已付款 / 36 已分账 / 31 失败(不驱动展示步骤) */
export const DIVISION_TEST_SUB_PROGRESS_IN_PROGRESS = [34, 35, 36, 31]

export function isDivisionTestInProgress(subProgress) {
  const sp = Number(subProgress)
  return DIVISION_TEST_SUB_PROGRESS_IN_PROGRESS.includes(sp)
}

export function isDivisionTestPassed(subProgress) {
  const sp = Number(subProgress)
  return sp > 0 && !isDivisionTestInProgress(sp)
}

function hasOutTradeNo(payload = {}) {
  const v = payload.outTradeNo
  return v != null && v !== ''
}

function isOrderPaidFromPayload(payload, sp) {
  if (payload.orderPaid === true) return true
  if (payload.paymentSuccess === true) return true
  if (payload.payStatus === 1 || payload.payStatus === '1' || payload.payStatus === 'Y') {
    return true
  }
  if (sp === 31 && hasOutTradeNo(payload)) return true
  return sp >= 35
}

/**
 * 仅从 subProgress / 支付状态解析业务态,不映射展示步骤
 * @param {number|string} subProgress
 * @param {{ orderPaid?: boolean, payStatus?: *, paymentSuccess?: boolean, outTradeNo?: string }} [payload]
 */
export function mapSubProgressToUi(subProgress, payload = {}) {
  const sp = Number(subProgress)
  const step1Paid = isOrderPaidFromPayload(payload, sp)

  return {
    step1Paid,
    subProgress: sp,
    isFailed: sp === 31
  }
}

/** 步骤条完成态:[0]=步骤1已付款 [1]=步骤2已到达 [2]=步骤3失败标记(仅 max>=3 时) */
export function buildStepCompletedFlags(step1Paid, maxReachedStep, isFailed = false) {
  const max = Number(maxReachedStep) || 1
  return [
    !!step1Paid,
    max >= 2,
    !!isFailed && max >= 3
  ]
}

/**
 * @param {number} targetStep
 * @param {{ maxReachedStep: number, step1Paid?: boolean }} uiState
 */
export function canNavigateToStep(targetStep, uiState) {
  const step = Number(targetStep)
  const max = Number(uiState.maxReachedStep) || 1
  if (step < 1 || step > 3) return false
  return step <= max
}

/**
 * 回看已完成步骤时为只读
 * @param {number} viewStep
 * @param {{ maxReachedStep: number }} uiState
 */
export function isStepReadOnly(viewStep, uiState) {
  const v = Number(viewStep)
  const max = Number(uiState.maxReachedStep) || 1
  return v < max
}

/**
 * subProgress 决定可达的最大步骤
 * @param {number|string} subProgress
 * @param {number|string} [progress] 服务端主步骤 1/2/3
 * @returns {number} 1 | 2 | 3
 */
export function resolveMaxReachedStepFromSubProgress(subProgress, progress) {
  const sp = Number(subProgress) || 0
  const mp = Number(progress) || 0
  if (sp === 36) return 3
  if (sp >= 35) return 2
  if (sp === 31 && mp >= 1 && mp <= 3) return mp
  return 1
}

/**
 * 打开弹窗时决定初始展示步骤:优先 progress(1/2/3),无 progress 时回退 subProgress
 * @param {number|string} subProgress
 * @param {number|string} [progress] 服务端主步骤 1/2/3
 * @returns {{ viewStep: number, maxReachedStep: number }}
 */
export function resolveInitialWizardSteps(subProgress, progress) {
  const sp = Number(subProgress) || 0
  const mp = Number(progress) || 0
  let maxReachedStep = resolveMaxReachedStepFromSubProgress(sp, mp)

  if (mp >= 1 && mp <= 3) {
    maxReachedStep = Math.max(maxReachedStep, mp)
    return { viewStep: mp, maxReachedStep }
  }

  if (sp === 36) {
    return { viewStep: 3, maxReachedStep: 3 }
  }

  if (sp === 31 && maxReachedStep > 1) {
    return { viewStep: maxReachedStep, maxReachedStep }
  }

  return { viewStep: 1, maxReachedStep }
}

export function isDivisionTestFailed(subProgress) {
  return Number(subProgress) === 31
}

/**
 * subProgress=31/36 等需按服务端 progress 恢复展示步骤
 */
export function shouldSyncWizardStepsFromProgress(subProgress) {
  const sp = Number(subProgress)
  return sp === 31 || sp === 36
}

/**
 * 步骤1是否展示「关联测试订单」(而非二维码)
 * @param {{ step1Paid?: boolean, subProgress?: number|string, outTradeNo?: string, maxReachedStep?: number, viewStep?: number }} params
 */
export function shouldShowStep1OrderLinked({
  step1Paid = false,
  subProgress = 0,
  outTradeNo = '',
  maxReachedStep = 1,
  viewStep = 1
} = {}) {
  if (step1Paid) return true
  if (Number(subProgress) === 31 && outTradeNo) return true
  if (Number(maxReachedStep) > 1 && Number(viewStep) === 1) return true
  return false
}