hookehuyr

refactor(请求工具): 重构请求参数合并逻辑,使用工具函数解析URL参数

将原有的merge_request_params函数移除,改用parseQueryString工具函数解析URL参数
合并参数的优先级调整为:调用传参 > URL参数 > 默认参数
......@@ -13,6 +13,7 @@ import Taro from '@tarojs/taro'
import { refreshSession, saveCurrentPagePath, navigateToAuth } from './authRedirect'
import { has_offline_booking_cache } from '@/composables/useOfflineBookingCache'
import { get_weak_network_modal_no_cache_options } from '@/utils/uiText'
import { parseQueryString } from './tools'
// import { ProgressStart, ProgressEnd } from '@/components/axios-progress/progress';
// import store from '@/store'
......@@ -51,56 +52,6 @@ service.defaults.params = {
let has_shown_timeout_modal = false
/**
* 合并请求参数,将默认参数与请求参数合并
* @param {Object} config - axios请求配置对象
* @returns {Object} 合并后的请求配置对象
*/
const merge_request_params = (config) => {
const url = config?.url || ''
if (!url || url.indexOf('?') === -1) {
const params = config?.params || {}
return {
...config,
params: {
...REQUEST_DEFAULT_PARAMS,
...params,
},
}
}
const parts = url.split('?')
const base_url = parts[0]
const search = parts.slice(1).join('?')
const url_params = {}
if (search) {
const pairs = search.split('&')
for (let i = 0; i < pairs.length; i++) {
const pair = pairs[i]
if (!pair) continue
const index = pair.indexOf('=')
const raw_key = index >= 0 ? pair.slice(0, index) : pair
const raw_value = index >= 0 ? pair.slice(index + 1) : ''
const key = decodeURIComponent(raw_key || '')
const value = decodeURIComponent(raw_value || '')
if (!key) continue
url_params[key] = value
}
}
const merged_params = {
...REQUEST_DEFAULT_PARAMS,
...url_params,
...(config?.params || {}),
}
return {
...config,
url: base_url,
params: merged_params,
}
}
/**
* 判断是否为超时错误
* @param {Error} error - 请求错误对象
* @returns {boolean} 是否为超时错误
......@@ -188,6 +139,21 @@ service.interceptors.request.use(
// console.warn(config)
// console.warn(store)
// 解析 URL 参数并合并
const url = config.url || ''
let url_params = {}
if (url.includes('?')) {
url_params = parseQueryString(url)
config.url = url.split('?')[0]
}
// 优先级:调用传参 > URL参数 > 默认参数
config.params = {
...REQUEST_DEFAULT_PARAMS,
...url_params,
...(config.params || {})
}
/**
* 动态获取sessionid并设置到请求头
* 确保每个请求都带上最新的sessionid
......@@ -197,8 +163,6 @@ service.interceptors.request.use(
config.headers.cookie = sessionid;
}
config = merge_request_params(config)
// 增加时间戳
if (config.method === 'get') {
config.params = { ...config.params, timestamp: (new Date()).valueOf() }
......