usePlanSubmit.js
3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* 计划书提交后处理 Composable
*
* @description 统一处理计划书提交后的弹窗关闭、状态清空、导航跳转逻辑
* @author Claude Code
* @example
* const { handlePlanSubmit } = usePlanSubmit({
* getPopupState: () => showPlanPopup.value,
* setPopupState: (state) => { showPlanPopup.value = state }
* })
*
* // 使用
* await handlePlanSubmit(result, {
* beforeNav: async () => { console.log('导航前') },
* afterNav: async () => { console.log('导航后') }
* })
*/
import { nextTick } from 'vue'
import Taro from '@tarojs/taro'
import { useGo } from '@/hooks/useGo'
/**
* 计划书提交后处理 Hook
*
* @param {Object} options - 配置选项
* @param {Function} options.getPopupState - 获取弹窗状态(必需)
* @param {Function} options.setPopupState - 设置弹窗状态(必需)
* @param {Function} options.clearSelectedProduct - 清空选中产品(可选)
* @param {boolean} options.useGoUtil - 是否使用 go() 工具(默认 true)
* @param {string} options.pageName - 页面名称(用于日志,默认 'Page')
* @returns {Object} 包含 handlePlanSubmit 方法的对象
*/
export function usePlanSubmit(options = {}) {
const {
getPopupState,
setPopupState,
clearSelectedProduct,
useGoUtil = true,
pageName = 'Page'
} = options
/**
* 处理计划书提交结果
*
* @param {Object} result - 提交结果
* @param {boolean} result.success - 是否成功
* @param {string} result.message - 错误信息(失败时)
* @param {number} result.order_id - 订单 ID(成功时)
* @param {number} result.product_id - 产品 ID(成功时)
* @param {string} result.form_sn - 表单标识(成功时)
* @param {Object} callbacks - 回调函数
* @param {Function} callbacks.beforeNav - 导航前回调
* @param {Function} callbacks.afterNav - 导航后回调
*/
const handlePlanSubmit = async (result, callbacks = {}) => {
console.log(`[${pageName}] 计划书提交结果:`, result)
// 1. 关闭弹窗
setPopupState(false)
// 2. 等待 DOM 更新
await nextTick()
// 3. 清空选中产品(如果提供)
if (clearSelectedProduct) {
clearSelectedProduct()
}
// 4. 构建结果页面参数
const params = {
success: result.success ? 'true' : 'false'
}
// 失败时传递错误信息
if (!result.success && result.message) {
params.message = result.message
}
console.log(`[${pageName}] 跳转到结果页面,参数:`, params)
// 5. 执行导航前回调
if (callbacks.beforeNav) {
await callbacks.beforeNav(result)
}
// 6. 跳转到结果页面
if (useGoUtil) {
// 使用 go() 工具函数
const go = useGo()
go('/pages/plan-submit-result/index', params)
} else {
// 直接使用 Taro API
Taro.navigateTo({
url: `/pages/plan-submit-result/index?${new URLSearchParams(params).toString()}`
})
}
// 7. 执行导航后回调
if (callbacks.afterNav) {
await callbacks.afterNav()
}
}
return {
handlePlanSubmit
}
}