refactor(请求工具): 重构请求参数合并逻辑,使用工具函数解析URL参数
将原有的merge_request_params函数移除,改用parseQueryString工具函数解析URL参数 合并参数的优先级调整为:调用传参 > URL参数 > 默认参数
Showing
1 changed file
with
16 additions
and
52 deletions
| ... | @@ -13,6 +13,7 @@ import Taro from '@tarojs/taro' | ... | @@ -13,6 +13,7 @@ import Taro from '@tarojs/taro' |
| 13 | import { refreshSession, saveCurrentPagePath, navigateToAuth } from './authRedirect' | 13 | import { refreshSession, saveCurrentPagePath, navigateToAuth } from './authRedirect' |
| 14 | import { has_offline_booking_cache } from '@/composables/useOfflineBookingCache' | 14 | import { has_offline_booking_cache } from '@/composables/useOfflineBookingCache' |
| 15 | import { get_weak_network_modal_no_cache_options } from '@/utils/uiText' | 15 | import { get_weak_network_modal_no_cache_options } from '@/utils/uiText' |
| 16 | +import { parseQueryString } from './tools' | ||
| 16 | 17 | ||
| 17 | // import { ProgressStart, ProgressEnd } from '@/components/axios-progress/progress'; | 18 | // import { ProgressStart, ProgressEnd } from '@/components/axios-progress/progress'; |
| 18 | // import store from '@/store' | 19 | // import store from '@/store' |
| ... | @@ -51,56 +52,6 @@ service.defaults.params = { | ... | @@ -51,56 +52,6 @@ service.defaults.params = { |
| 51 | let has_shown_timeout_modal = false | 52 | let has_shown_timeout_modal = false |
| 52 | 53 | ||
| 53 | /** | 54 | /** |
| 54 | - * 合并请求参数,将默认参数与请求参数合并 | ||
| 55 | - * @param {Object} config - axios请求配置对象 | ||
| 56 | - * @returns {Object} 合并后的请求配置对象 | ||
| 57 | - */ | ||
| 58 | - | ||
| 59 | -const merge_request_params = (config) => { | ||
| 60 | - const url = config?.url || '' | ||
| 61 | - if (!url || url.indexOf('?') === -1) { | ||
| 62 | - const params = config?.params || {} | ||
| 63 | - return { | ||
| 64 | - ...config, | ||
| 65 | - params: { | ||
| 66 | - ...REQUEST_DEFAULT_PARAMS, | ||
| 67 | - ...params, | ||
| 68 | - }, | ||
| 69 | - } | ||
| 70 | - } | ||
| 71 | - | ||
| 72 | - const parts = url.split('?') | ||
| 73 | - const base_url = parts[0] | ||
| 74 | - const search = parts.slice(1).join('?') | ||
| 75 | - const url_params = {} | ||
| 76 | - if (search) { | ||
| 77 | - const pairs = search.split('&') | ||
| 78 | - for (let i = 0; i < pairs.length; i++) { | ||
| 79 | - const pair = pairs[i] | ||
| 80 | - if (!pair) continue | ||
| 81 | - const index = pair.indexOf('=') | ||
| 82 | - const raw_key = index >= 0 ? pair.slice(0, index) : pair | ||
| 83 | - const raw_value = index >= 0 ? pair.slice(index + 1) : '' | ||
| 84 | - const key = decodeURIComponent(raw_key || '') | ||
| 85 | - const value = decodeURIComponent(raw_value || '') | ||
| 86 | - if (!key) continue | ||
| 87 | - url_params[key] = value | ||
| 88 | - } | ||
| 89 | - } | ||
| 90 | - const merged_params = { | ||
| 91 | - ...REQUEST_DEFAULT_PARAMS, | ||
| 92 | - ...url_params, | ||
| 93 | - ...(config?.params || {}), | ||
| 94 | - } | ||
| 95 | - | ||
| 96 | - return { | ||
| 97 | - ...config, | ||
| 98 | - url: base_url, | ||
| 99 | - params: merged_params, | ||
| 100 | - } | ||
| 101 | -} | ||
| 102 | - | ||
| 103 | -/** | ||
| 104 | * 判断是否为超时错误 | 55 | * 判断是否为超时错误 |
| 105 | * @param {Error} error - 请求错误对象 | 56 | * @param {Error} error - 请求错误对象 |
| 106 | * @returns {boolean} 是否为超时错误 | 57 | * @returns {boolean} 是否为超时错误 |
| ... | @@ -188,6 +139,21 @@ service.interceptors.request.use( | ... | @@ -188,6 +139,21 @@ service.interceptors.request.use( |
| 188 | // console.warn(config) | 139 | // console.warn(config) |
| 189 | // console.warn(store) | 140 | // console.warn(store) |
| 190 | 141 | ||
| 142 | + // 解析 URL 参数并合并 | ||
| 143 | + const url = config.url || '' | ||
| 144 | + let url_params = {} | ||
| 145 | + if (url.includes('?')) { | ||
| 146 | + url_params = parseQueryString(url) | ||
| 147 | + config.url = url.split('?')[0] | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + // 优先级:调用传参 > URL参数 > 默认参数 | ||
| 151 | + config.params = { | ||
| 152 | + ...REQUEST_DEFAULT_PARAMS, | ||
| 153 | + ...url_params, | ||
| 154 | + ...(config.params || {}) | ||
| 155 | + } | ||
| 156 | + | ||
| 191 | /** | 157 | /** |
| 192 | * 动态获取sessionid并设置到请求头 | 158 | * 动态获取sessionid并设置到请求头 |
| 193 | * 确保每个请求都带上最新的sessionid | 159 | * 确保每个请求都带上最新的sessionid |
| ... | @@ -197,8 +163,6 @@ service.interceptors.request.use( | ... | @@ -197,8 +163,6 @@ service.interceptors.request.use( |
| 197 | config.headers.cookie = sessionid; | 163 | config.headers.cookie = sessionid; |
| 198 | } | 164 | } |
| 199 | 165 | ||
| 200 | - config = merge_request_params(config) | ||
| 201 | - | ||
| 202 | // 增加时间戳 | 166 | // 增加时间戳 |
| 203 | if (config.method === 'get') { | 167 | if (config.method === 'get') { |
| 204 | config.params = { ...config.params, timestamp: (new Date()).valueOf() } | 168 | config.params = { ...config.params, timestamp: (new Date()).valueOf() } | ... | ... |
-
Please register or login to post a comment