// statusMachine.ts - 完整修正版 export class OrderStatusMachine { constructor(row, currentTime) { this.row = row this.currentTime = currentTime || Date.now() } // 获取当前时间(带时区处理) getCurrentTime() { // 如果传入了currentTime就直接使用 if (this.currentTime) return this.currentTime let timezone = 2; // 目标时区时间,东3区 let offset_GMT = new Date().getTimezoneOffset(); // 本地时间和格林威治的时间差,单位为分钟 let nowDate = new Date().getTime(); // 本地时间距 1970 年 1 月 1 日午夜(GMT 时间)之间的毫秒数 let now = new Date( nowDate + offset_GMT * 60 * 1000 + timezone * 60 * 60 * 1000 ); return now.getTime() } // 是否未过期(当前时间 < 过期时间) isNotExpired() { // 如果没有过期时间,视为未过期 if (!this.row.expireTime) return true const now = this.getCurrentTime() const expireTime = new Date(this.row.expireTime).getTime() return now < expireTime } // 是否已过期(当前时间 > 过期时间,且不是最终状态) isExpired() { const r = this.row // 如果没有过期时间,不过期 if (!r.expireTime) return false const now = this.getCurrentTime() const expireTime = new Date(r.expireTime).getTime() // 当前时间必须大于过期时间 if (now <= expireTime) return false // 已取消的不过期 if (this.isCancelled()) return false // 已拒绝的不过期 if (this.isRefused()) return false // 已完成的不过期 if (r.type === 1 && this.isCompletedType1()) return false if (r.type === 2 && this.isCompletedType2()) return false // 状态5已经在isCancelled中处理了,这里不需要重复判断 return true } // 判断是否已完成 (type 1) isCompletedType1() { return this.row.status === 2 && this.row.executionStatus === 2 && this.row.callbackStatus === 1 } // 判断是否已完成 (type 2) isCompletedType2() { return this.row.status === 2 && this.row.executionStatus === 2 && this.row.submitStatus === 2 } // 判断是否已拒绝 isRefused() { return this.row.callbackStatus === 2 || this.row.status === 3 || this.row.executionStatus === 3 } // 判断是否已取消 isCancelled() { return this.row.status === 5 } // 判断是否为待处理状态 (type 1) isToBeProcessedType1() { const r = this.row return r.status === 1 && r.callbackStatus === 0 && (r.expireTime === null || this.isNotExpired()) } // 判断是否为待处理状态 (type 2) isToBeProcessedType2() { const r = this.row return r.status === 1 && r.callbackStatus === 0 && (r.expireTime === null || this.isNotExpired()) } // 判断是否为进行中状态 (type 1) isInProgressType1() { const r = this.row // 条件1: status=2, executionStatus=0或1, callbackStatus≠2, 未过期 const condition1 = r.status === 2 && [0, 1].includes(r.executionStatus) && r.callbackStatus !== 2 && (r.expireTime === null || this.isNotExpired()) // 条件2: status=1, callbackStatus=1, 未过期 const condition2 = r.status === 1 && r.callbackStatus === 1 && (r.expireTime === null || this.isNotExpired()) return condition1 || condition2 } // 判断是否为进行中状态 (type 2) isInProgressType2() { const r = this.row // 条件1: status=2, executionStatus=0或1, callbackStatus≠2 const condition1 = r.status === 2 && [0, 1].includes(r.executionStatus) && r.callbackStatus !== 2 // 条件2: status=2, executionStatus=2, callbackStatus=0, submitStatus≠2 const condition2 = r.status === 2 && r.executionStatus === 2 && r.callbackStatus === 0 && r.submitStatus !== 2 return condition1 || condition2 } // 获取状态码 getStatus() { const r = this.row // 1. 优先检查已取消 if (this.isCancelled()) return 6 // 2. 检查已拒绝 if (this.isRefused()) return 4 // 3. 检查是否已完成 if (r.type === 1 && this.isCompletedType1()) return 2 if (r.type === 2 && this.isCompletedType2()) return 2 // 4. 检查是否过期 - 按照原始逻辑,只有不是已完成、已拒绝、已取消才判断过期 if (this.isExpired()) return 5 // 5. 根据类型判断待处理或进行中 if (r.type === 1) { if (this.isToBeProcessedType1()) return 1 if (this.isInProgressType1()) return 3 } else if (r.type === 2) { if (this.isToBeProcessedType2()) return 1 if (this.isInProgressType2()) return 3 } return null } // 获取状态信息 getStatusInfo() { const status = this.getStatus() const statusMap = { 1: { text: '待处理', class: 'warning', value: 1 }, 2: { text: '已完成', class: 'success', value: 2 }, 3: { text: '进行中', class: 'processing', value: 3 }, 4: { text: '已拒绝', class: 'danger', value: 4 }, 5: { text: '已过期', class: 'expired', value: 5 }, 6: { text: '已取消', class: 'cancelled', value: 6 } } return statusMap[status] || { text: '-', class: '', value: null } } // 检查是否可以取消(普通取消) canCancel() { const r = this.row // type=2 且 status=1, callbackStatus=0, 未过期 if (r.type === 2 && r.status === 1 && r.callbackStatus === 0 && (r.expireTime === null || this.isNotExpired())) { return 'normal' } return false } // 检查是否可以取消(后台取消) canCancelBackstage() { const r = this.row // type=2 且 status=2, executionStatus=2, backstageStatus=1 if (r.type === 2 && r.status === 2 && r.executionStatus === 2 && r.backstageStatus === 1) { return 'backstage' } return false } // 获取可执行的操作列表 getAvailableActions() { const actions = [] if (this.canCancel()) { actions.push({ type: 'cancel', text: '取消', handler: 'handleCancel' }) } if (this.canCancelBackstage()) { actions.push({ type: 'cancelBackstage', text: '取消', handler: 'handleCancelBackstage' }) } return actions } // 调试方法 debug() { console.log({ row: this.row, isCancelled: this.isCancelled(), isRefused: this.isRefused(), isCompletedType1: this.isCompletedType1(), isCompletedType2: this.isCompletedType2(), isExpired: this.isExpired(), isNotExpired: this.isNotExpired(), isToBeProcessedType1: this.isToBeProcessedType1(), isToBeProcessedType2: this.isToBeProcessedType2(), isInProgressType1: this.isInProgressType1(), isInProgressType2: this.isInProgressType2(), canCancel: this.canCancel(), canCancelBackstage: this.canCancelBackstage(), finalStatus: this.getStatus(), finalStatusInfo: this.getStatusInfo() }) } } // 工厂函数 export const createOrderStatusMachine = (row, currentTime) => { return new OrderStatusMachine(row, currentTime) }