orderStatus.js 2.55 KB
/*
 * @Date: 2026-02-14 11:04:03
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2026-02-14 11:08:09
 * @FilePath: /manulife-weapp/src/config/constants/orderStatus.js
 * @Description: 订单状态常量
 */
/**
 * 订单状态常量
 *
 * @description 统一管理订单状态值,避免魔法数字
 * @module config/constants
 * @author Claude Code
 * @created 2026-02-14
 */

/**
 * 订单状态常量(API 返回)
 *
 * @description 前端使用的状态值(与后端 API 一致)
 * @constant {string}
 */
export const ORDER_STATUS = {
  /** 待处理 - 对应 API 值 '3' */
  PENDING: '3',
  /** 处理中 - 对应 API 值 '5' */
  PROCESSING: '5',
  /** 已生成 - 对应 API 值 '7' */
  GENERATED: '7',
  /** 已查看 - 对应 API 值 '9' */
  VIEWED: '9'
}

/**
 * 状态映射关系(API 状态 → 前端状态)
 *
 * @description 用于状态转换的映射表
 * @constant {Object<string, string>}
 */
export const ORDER_STATUS_MAP = {
  [ORDER_STATUS.PENDING]: 'pending',
  [ORDER_STATUS.PROCESSING]: 'processing',
  [ORDER_STATUS.GENERATED]: 'generated',
  [ORDER_STATUS.VIEWED]: 'viewed'
}

/**
 * 状态文本映射
 *
 * @description 状态对应的显示文本
 * @constant {Object<string, string>}
 */
export const ORDER_STATUS_TEXT = {
  pending: '待处理',
  processing: '处理中',
  generated: '已生成',
  viewed: '已查看'
}

/**
 * 获取前端状态值
 *
 * @description 将 API 返回的状态值映射为前端使用的状态
 * @param {string} apiStatus - API 返回的状态值('3'/'5'/'7'/'9')
 * @returns {string} 前端状态值('pending'/'processing'/'generated'/'viewed')
 *
 * @example
 * const frontendStatus = mapOrderStatus('7')  // 返回: 'generated'
 */
export function mapOrderStatus(apiStatus) {
  return ORDER_STATUS_MAP[apiStatus] || ORDER_STATUS_MAP[ORDER_STATUS.PENDING]
}

/**
 * 获取状态显示文本
 *
 * @description 获取状态对应的显示文本
 * @param {string} status - 前端状态值
 * @returns {string} 状态显示文本
 *
 * @example
 * const text = getStatusText('processing') // 返回: '处理中'
 */
export function getStatusText(status) {
  return ORDER_STATUS_TEXT[status] || '待处理'
}

/**
 * 验证状态值是否有效
 *
 * @description 检查状态值是否为有效的订单状态
 * @param {string} status - 待验证的状态值
 * @returns {boolean} 是否有效
 *
 * @example
 * isValidStatus('pending') // 返回: true
 * isValidStatus('invalid') // 返回: false
 */
export function isValidStatus(status) {
  return Object.values(ORDER_STATUS).includes(status)
}