mirror of
https://gitee.com/baidu/amis.git
synced 2024-11-29 18:48:45 +08:00
chore: 补充自定义 fetcher 时可能漏了 attachmentAdpator 实现的说明文档
This commit is contained in:
parent
af806d553c
commit
9e81e6b15c
@ -871,6 +871,13 @@ Content-Disposition: attachment; filename="download.pdf"
|
||||
Access-Control-Expose-Headers: Content-Disposition
|
||||
```
|
||||
|
||||
如果自己覆盖了 `fetcher` 函数,需要有类似如下代码,具体可以参考 `embed.tsx` 里的实现
|
||||
|
||||
```javascript
|
||||
let response = await axios(config);
|
||||
response = await attachmentAdpator(response, __);
|
||||
```
|
||||
|
||||
### 配置提示信息
|
||||
|
||||
可以通过`messages`自定义接口请求提示信息。
|
||||
|
@ -5,7 +5,7 @@ import Portal from 'react-overlays/Portal';
|
||||
import {normalizeLink} from 'amis-core';
|
||||
import {withRouter} from 'react-router';
|
||||
import copy from 'copy-to-clipboard';
|
||||
import {qsparse, parseQuery} from 'amis-core';
|
||||
import {qsparse, parseQuery, attachmentAdpator} from 'amis-core';
|
||||
import isPlainObject from 'lodash/isPlainObject';
|
||||
|
||||
function loadEditor() {
|
||||
@ -106,16 +106,29 @@ export default function (schema, schemaProps, showCode, envOverrides) {
|
||||
|
||||
return false;
|
||||
},
|
||||
fetcher: ({url, method, data, config, headers}) => {
|
||||
fetcher: async ({
|
||||
url,
|
||||
method,
|
||||
data,
|
||||
responseType,
|
||||
config,
|
||||
headers
|
||||
}) => {
|
||||
config = config || {};
|
||||
config.headers = headers || {};
|
||||
config.url = url;
|
||||
responseType && (config.responseType = responseType);
|
||||
|
||||
if (config.cancelExecutor) {
|
||||
config.cancelToken = new axios.CancelToken(config.cancelExecutor);
|
||||
}
|
||||
|
||||
if (data && data instanceof FormData) {
|
||||
// config.headers = config.headers || {};
|
||||
config.headers = headers || {};
|
||||
config.method = method;
|
||||
config.data = data;
|
||||
|
||||
if (method === 'get' && data) {
|
||||
config.params = data;
|
||||
} else if (data && data instanceof FormData) {
|
||||
// config.headers['Content-Type'] = 'multipart/form-data';
|
||||
} else if (
|
||||
data &&
|
||||
@ -127,19 +140,40 @@ export default function (schema, schemaProps, showCode, envOverrides) {
|
||||
config.headers['Content-Type'] = 'application/json';
|
||||
}
|
||||
|
||||
if (method !== 'post' && method !== 'put' && method !== 'patch') {
|
||||
if (data) {
|
||||
if (method === 'delete') {
|
||||
config.data = data;
|
||||
// 支持返回各种报错信息
|
||||
config.validateStatus = function () {
|
||||
return true;
|
||||
};
|
||||
|
||||
let response = await axios(config);
|
||||
response = await attachmentAdpator(response, s => {
|
||||
return s;
|
||||
});
|
||||
|
||||
if (response.status >= 400) {
|
||||
if (response.data) {
|
||||
// 主要用于 raw: 模式下,后端自己校验登录,
|
||||
if (
|
||||
response.status === 401 &&
|
||||
response.data.location &&
|
||||
response.data.location.startsWith('http')
|
||||
) {
|
||||
location.href = response.data.location.replace(
|
||||
'{{redirect}}',
|
||||
encodeURIComponent(location.href)
|
||||
);
|
||||
return new Promise(() => {});
|
||||
} else if (response.data.msg) {
|
||||
throw new Error(response.data.msg);
|
||||
} else {
|
||||
config.params = data;
|
||||
throw new Error(JSON.stringify(response.data, null, 2));
|
||||
}
|
||||
} else {
|
||||
throw new Error(`${response.status}`);
|
||||
}
|
||||
}
|
||||
|
||||
return axios[method](url, config);
|
||||
}
|
||||
|
||||
return axios[method](url, data, config);
|
||||
return response;
|
||||
},
|
||||
isCancel: value => axios.isCancel(value),
|
||||
copy: (content, options) => {
|
||||
|
@ -49,6 +49,18 @@ function mockResponse(event, context, callback) {
|
||||
json: false,
|
||||
body: res
|
||||
});
|
||||
},
|
||||
download(file) {
|
||||
callback(null, {
|
||||
statusCode: 200,
|
||||
headers: {
|
||||
...createHeaders(event.headers),
|
||||
'Content-Type': 'application/octet-stream',
|
||||
'Content-Disposition': `attachment; filename="${file}"`
|
||||
},
|
||||
json: false,
|
||||
download: file
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
BIN
mock/cfc/mock/download.zip
Normal file
BIN
mock/cfc/mock/download.zip
Normal file
Binary file not shown.
@ -3,7 +3,10 @@ const fs = require('fs');
|
||||
const DIRNAME = path.dirname(__filename);
|
||||
|
||||
module.exports = function (req, res) {
|
||||
const subpath = (req.originalUrl || req.url).replace(/^\/(api\/mock2|api)\/|\?.*$/g, '');
|
||||
const subpath = (req.originalUrl || req.url).replace(
|
||||
/^\/(api\/mock2|api)\/|\?.*$/g,
|
||||
''
|
||||
);
|
||||
const jsonFile = subpath + '.json';
|
||||
const jsFile = subpath + '.js';
|
||||
|
||||
@ -12,7 +15,9 @@ module.exports = function(req, res) {
|
||||
let term = req.query.term;
|
||||
|
||||
if (term) {
|
||||
json.data = json.data.filter(item => item.label.substring(0, term.length) === term);
|
||||
json.data = json.data.filter(
|
||||
item => item.label.substring(0, term.length) === term
|
||||
);
|
||||
}
|
||||
|
||||
return res.json(json);
|
||||
@ -31,7 +36,8 @@ module.exports = function(req, res) {
|
||||
}
|
||||
|
||||
return require(file)(req, res);
|
||||
} if (exist(jsonFile)) {
|
||||
}
|
||||
if (exist(jsonFile)) {
|
||||
if (req.query.waitSeconds) {
|
||||
return setTimeout(function () {
|
||||
res.json(readJson(jsonFile));
|
||||
@ -43,11 +49,13 @@ module.exports = function(req, res) {
|
||||
return res.json({
|
||||
status: 0,
|
||||
msg: '删除成功'
|
||||
})
|
||||
});
|
||||
} else if (/download/.test(subpath)) {
|
||||
return res.download(path.join(DIRNAME, 'download.zip'));
|
||||
}
|
||||
|
||||
res.json(readJson('notFound.json'));
|
||||
}
|
||||
};
|
||||
|
||||
function exist(subpath) {
|
||||
return fs.existsSync(path.join(DIRNAME, subpath));
|
||||
|
@ -26,7 +26,9 @@ module.exports = function (req, res) {
|
||||
res.status(500).json({status: 500, msg: err});
|
||||
} else {
|
||||
res.set(result.headers);
|
||||
if (!result.json) {
|
||||
if (result.download) {
|
||||
res.download(result.download);
|
||||
} else if (!result.json) {
|
||||
res.status(result.statusCode).send(result.body);
|
||||
} else {
|
||||
res.status(result.statusCode).json(JSON.parse(result.body));
|
||||
|
Loading…
Reference in New Issue
Block a user