Commit 8551790b by caoyanzhi

update: 更新请求封装

parent 319eb4bd
...@@ -7,7 +7,9 @@ export default function getFetch(api, baseUrl) { ...@@ -7,7 +7,9 @@ export default function getFetch(api, baseUrl) {
let opt = api[key]; let opt = api[key];
if (typeof opt === 'string') opt = { url: opt }; if (typeof opt === 'string') opt = { url: opt };
opt.url = `${baseUrl}${opt.url}`; opt.url = `${baseUrl}${opt.url}`;
api[key] = params => request(opt, params); // requestConfig提供请求发起之前动态设置axios配置的能力
// requestConfig的所有属性同request.js的opt参数
api[key] = (params, requestConfig) => request(opt, params, requestConfig);
}); });
return api; return api;
} }
...@@ -16,7 +16,7 @@ import { origin, succCode, notAuthCode } from '@/config/index.js'; ...@@ -16,7 +16,7 @@ import { origin, succCode, notAuthCode } from '@/config/index.js';
axios.defaults.baseURL = origin; axios.defaults.baseURL = origin;
axios.defaults.withCredentials = true; axios.defaults.withCredentials = true;
const request = (opt, params) => { const request = (opt, params, config) => {
params = Object.assign({}, params); params = Object.assign({}, params);
opt = Object.assign({}, opt); opt = Object.assign({}, opt);
let requestConfig = { let requestConfig = {
...@@ -33,7 +33,7 @@ const request = (opt, params) => { ...@@ -33,7 +33,7 @@ const request = (opt, params) => {
// 设置headers // 设置headers
headers: {} headers: {}
}; };
requestConfig = Object.assign(requestConfig, opt); Object.assign(requestConfig, opt, config);
if (requestConfig.useFormData) params = qs.stringify(params); if (requestConfig.useFormData) params = qs.stringify(params);
switch (requestConfig.method.toLowerCase()) { switch (requestConfig.method.toLowerCase()) {
case 'get': case 'get':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment