duplicate-recharge-confirm.vue 2.71 KB
<template>
  <span class="duplicate-recharge-confirm-host" />
</template>

<script>
import { checkSameDayDuplicate } from '@/api/businessUnitDashboard'

function formatBizDate(bizDate) {
  if (!bizDate) {
    const d = new Date()
    const m = `${d.getMonth() + 1}`.padStart(2, '0')
    const day = `${d.getDate()}`.padStart(2, '0')
    return `${d.getFullYear()}-${m}-${day}`
  }
  if (typeof bizDate === 'string') {
    return bizDate.length >= 10 ? bizDate.substring(0, 10) : bizDate
  }
  try {
    const d = new Date(bizDate)
    const m = `${d.getMonth() + 1}`.padStart(2, '0')
    const day = `${d.getDate()}`.padStart(2, '0')
    return `${d.getFullYear()}-${m}-${day}`
  } catch (e) {
    return ''
  }
}

/**
 * 同自然日重复充值确认组件(软提醒)
 * 用法:ref.checkAndConfirm({ memberId, bizDate, scene, excludeId }) => Promise<boolean>
 */
export default {
  name: 'DuplicateRechargeConfirm',
  methods: {
    /**
     * @param {Object} options
     * @param {string} options.memberId
     * @param {string|Date} options.bizDate
     * @param {'Billing'|'Consume'} options.scene
     * @param {string} [options.excludeId]
     * @returns {Promise<boolean>} true 继续提交;false 取消
     */
    async checkAndConfirm(options = {}) {
      const memberId = options.memberId
      const scene = options.scene || 'Billing'
      if (!memberId) return true

      const bizDate = formatBizDate(options.bizDate)

      try {
        const res = await checkSameDayDuplicate({
          memberId,
          bizDate,
          scene,
          excludeId: options.excludeId || undefined
        })
        const data = (res && res.data) || {}
        const hasDuplicate = !!(data.HasDuplicate || data.hasDuplicate)
        if (!hasDuplicate) return true

        const existingCount = data.ExistingCount != null
          ? data.ExistingCount
          : (data.existingCount || 0)
        const defaultMessage = scene === 'Consume'
          ? '该会员当日已有有效耗卡记录,请确认是否继续操作'
          : '该会员当日已有有效开单记录,请确认是否继续操作'
        const message = data.Message || data.message || defaultMessage

        try {
          await this.$confirm(
            `${message}${existingCount ? `(已有 ${existingCount} 笔)` : ''}`,
            '重复充值提醒',
            {
              confirmButtonText: '继续提交',
              cancelButtonText: '取消',
              type: 'warning'
            }
          )
          return true
        } catch (e) {
          return false
        }
      } catch (e) {
        // 校验接口失败不阻断业务提交
        console.error('重复充值校验失败:', e)
        return true
      }
    }
  }
}
</script>