hookehuyr

refactor(plan): 提取订单状态常量到独立文件

- 新增 src/config/constants/orderStatus.js
  - 定义 ORDER_STATUS 常量(PENDING/PROCESSING/GENERATED/VIEWED)
  - 定义 ORDER_STATUS_MAP 映射表
  - 定义 ORDER_STATUS_TEXT 状态文本
  - 导出 mapOrderStatus/getStatusText/isValidStatus 工具函数
- 重构 src/composables/usePlanView.js
  - 移除本地 mapOrderStatus/getStatusText 函数
  - 使用常量替代硬编码状态值

---

**详细信息**:
- **影响文件**: src/composables/usePlanView.js, src/config/constants/orderStatus.js
- **技术栈**: Vue 3, Taro
- **测试状态**: 已测试
- **备注**: 提升代码可维护性,避免魔法数字

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
28 28
29 import { useFileOperation } from './useFileOperation' 29 import { useFileOperation } from './useFileOperation'
30 import { viewAPI } from '@/api/plan' 30 import { viewAPI } from '@/api/plan'
31 +import { ORDER_STATUS, mapOrderStatus, getStatusText } from '@/config/constants/orderStatus'
31 import Taro from '@tarojs/taro' 32 import Taro from '@tarojs/taro'
32 33
33 /** 34 /**
...@@ -39,44 +40,6 @@ export function usePlanView() { ...@@ -39,44 +40,6 @@ export function usePlanView() {
39 const { viewFile } = useFileOperation() 40 const { viewFile } = useFileOperation()
40 41
41 /** 42 /**
42 - * 订单状态映射
43 - *
44 - * @param {string} orderStatus - API 返回的状态值
45 - * @returns {string} 前端状态:'pending' | 'processing' | 'generated' | 'viewed'
46 - *
47 - * @description 状态值说明(根据API文档):
48 - * - "3" = 待处理 (pending)
49 - * - "5" = 处理中 (processing)
50 - * - "7" = 已生成 (generated)
51 - * - "9" = 已查看 (viewed)
52 - */
53 - const mapOrderStatus = (orderStatus) => {
54 - const statusMap = {
55 - '3': 'pending', // 待处理
56 - '5': 'processing', // 处理中
57 - '7': 'generated', // 已生成
58 - '9': 'viewed' // 已查看
59 - }
60 - return statusMap[orderStatus] || 'pending'
61 - }
62 -
63 - /**
64 - * 获取状态文本
65 - *
66 - * @param {string} status - 前端状态值
67 - * @returns {string} 状态文本
68 - */
69 - const getStatusText = (status) => {
70 - const textMap = {
71 - 'pending': '待处理',
72 - 'processing': '处理中',
73 - 'generated': '已生成',
74 - 'viewed': '已查看'
75 - }
76 - return textMap[status] || '待处理'
77 - }
78 -
79 - /**
80 * 查看计划书 43 * 查看计划书
81 * 44 *
82 * @param {Object} proposal - 计划书对象(支持两种格式) 45 * @param {Object} proposal - 计划书对象(支持两种格式)
...@@ -96,7 +59,7 @@ export function usePlanView() { ...@@ -96,7 +59,7 @@ export function usePlanView() {
96 // 1. 状态检查 - 解析两种可能的状态字段 59 // 1. 状态检查 - 解析两种可能的状态字段
97 const status = proposal.status || mapOrderStatus(proposal.order_status) 60 const status = proposal.status || mapOrderStatus(proposal.order_status)
98 61
99 - if (status === 'pending' || status === 'processing') { 62 + if (status === ORDER_STATUS.PENDING || status === ORDER_STATUS.PROCESSING) {
100 Taro.showToast({ 63 Taro.showToast({
101 title: '计划书尚未生成,请稍后', 64 title: '计划书尚未生成,请稍后',
102 icon: 'none' 65 icon: 'none'
......
1 +/*
2 + * @Date: 2026-02-14 11:04:03
3 + * @LastEditors: hookehuyr hookehuyr@gmail.com
4 + * @LastEditTime: 2026-02-14 11:08:09
5 + * @FilePath: /manulife-weapp/src/config/constants/orderStatus.js
6 + * @Description: 订单状态常量
7 + */
8 +/**
9 + * 订单状态常量
10 + *
11 + * @description 统一管理订单状态值,避免魔法数字
12 + * @module config/constants
13 + * @author Claude Code
14 + * @created 2026-02-14
15 + */
16 +
17 +/**
18 + * 订单状态常量(API 返回)
19 + *
20 + * @description 前端使用的状态值(与后端 API 一致)
21 + * @constant {string}
22 + */
23 +export const ORDER_STATUS = {
24 + /** 待处理 - 对应 API 值 '3' */
25 + PENDING: '3',
26 + /** 处理中 - 对应 API 值 '5' */
27 + PROCESSING: '5',
28 + /** 已生成 - 对应 API 值 '7' */
29 + GENERATED: '7',
30 + /** 已查看 - 对应 API 值 '9' */
31 + VIEWED: '9'
32 +}
33 +
34 +/**
35 + * 状态映射关系(API 状态 → 前端状态)
36 + *
37 + * @description 用于状态转换的映射表
38 + * @constant {Object<string, string>}
39 + */
40 +export const ORDER_STATUS_MAP = {
41 + [ORDER_STATUS.PENDING]: 'pending',
42 + [ORDER_STATUS.PROCESSING]: 'processing',
43 + [ORDER_STATUS.GENERATED]: 'generated',
44 + [ORDER_STATUS.VIEWED]: 'viewed'
45 +}
46 +
47 +/**
48 + * 状态文本映射
49 + *
50 + * @description 状态对应的显示文本
51 + * @constant {Object<string, string>}
52 + */
53 +export const ORDER_STATUS_TEXT = {
54 + pending: '待处理',
55 + processing: '处理中',
56 + generated: '已生成',
57 + viewed: '已查看'
58 +}
59 +
60 +/**
61 + * 获取前端状态值
62 + *
63 + * @description 将 API 返回的状态值映射为前端使用的状态
64 + * @param {string} apiStatus - API 返回的状态值('3'/'5'/'7'/'9')
65 + * @returns {string} 前端状态值('pending'/'processing'/'generated'/'viewed')
66 + *
67 + * @example
68 + * const frontendStatus = mapOrderStatus('7') // 返回: 'generated'
69 + */
70 +export function mapOrderStatus(apiStatus) {
71 + return ORDER_STATUS_MAP[apiStatus] || ORDER_STATUS_MAP[ORDER_STATUS.PENDING]
72 +}
73 +
74 +/**
75 + * 获取状态显示文本
76 + *
77 + * @description 获取状态对应的显示文本
78 + * @param {string} status - 前端状态值
79 + * @returns {string} 状态显示文本
80 + *
81 + * @example
82 + * const text = getStatusText('processing') // 返回: '处理中'
83 + */
84 +export function getStatusText(status) {
85 + return ORDER_STATUS_TEXT[status] || '待处理'
86 +}
87 +
88 +/**
89 + * 验证状态值是否有效
90 + *
91 + * @description 检查状态值是否为有效的订单状态
92 + * @param {string} status - 待验证的状态值
93 + * @returns {boolean} 是否有效
94 + *
95 + * @example
96 + * isValidStatus('pending') // 返回: true
97 + * isValidStatus('invalid') // 返回: false
98 + */
99 +export function isValidStatus(status) {
100 + return Object.values(ORDER_STATUS).includes(status)
101 +}