feat(axios): 自动从页面URL携带client_id到接口请求
恢复parseQueryString工具函数的导入,将页面URL中的client_id作为兜底参数透传给所有接口请求,若请求未主动指定该参数则自动添加
Showing
1 changed file
with
7 additions
and
3 deletions
| ... | @@ -9,8 +9,7 @@ | ... | @@ -9,8 +9,7 @@ |
| 9 | import axios from 'axios'; | 9 | import axios from 'axios'; |
| 10 | import router from '@/router'; | 10 | import router from '@/router'; |
| 11 | import qs from 'Qs' | 11 | import qs from 'Qs' |
| 12 | -import { strExist } from '@/utils/tools' | 12 | +import { strExist, parseQueryString } from '@/utils/tools' |
| 13 | -// import { parseQueryString } from '@/utils/tools' | ||
| 14 | 13 | ||
| 15 | axios.defaults.params = { | 14 | axios.defaults.params = { |
| 16 | f: 'tools', | 15 | f: 'tools', |
| ... | @@ -21,7 +20,7 @@ axios.defaults.params = { | ... | @@ -21,7 +20,7 @@ axios.defaults.params = { |
| 21 | */ | 20 | */ |
| 22 | axios.interceptors.request.use( | 21 | axios.interceptors.request.use( |
| 23 | config => { | 22 | config => { |
| 24 | - // const url_params = parseQueryString(location.href); | 23 | + const pageParams = parseQueryString(location.href); |
| 25 | // GET请求默认打上时间戳,避免从缓存中拿数据。 | 24 | // GET请求默认打上时间戳,避免从缓存中拿数据。 |
| 26 | const timestamp = config.method === 'get' ? (new Date()).valueOf() : ''; | 25 | const timestamp = config.method === 'get' ? (new Date()).valueOf() : ''; |
| 27 | 26 | ||
| ... | @@ -35,6 +34,11 @@ axios.interceptors.request.use( | ... | @@ -35,6 +34,11 @@ axios.interceptors.request.use( |
| 35 | mergedParams[key] = value; | 34 | mergedParams[key] = value; |
| 36 | } | 35 | } |
| 37 | 36 | ||
| 37 | + // 页面地址上的 client_id 作为兜底参数透传给所有接口请求 | ||
| 38 | + if (pageParams.client_id && !mergedParams.client_id && !config.params?.client_id) { | ||
| 39 | + mergedParams.client_id = pageParams.client_id; | ||
| 40 | + } | ||
| 41 | + | ||
| 38 | // 清理URL,移除查询参数(因为参数会通过params传递) | 42 | // 清理URL,移除查询参数(因为参数会通过params传递) |
| 39 | config.url = baseUrl; | 43 | config.url = baseUrl; |
| 40 | 44 | ... | ... |
-
Please register or login to post a comment