mirror of
https://gitee.com/baidu/amis.git
synced 2024-12-02 03:58:07 +08:00
Merge remote-tracking branch 'amis/master'
This commit is contained in:
commit
29daf95b9a
@ -35,6 +35,90 @@ export function embed(
|
||||
container.classList.add('amis-scope');
|
||||
let scoped: any;
|
||||
|
||||
const attachmentAdpator = (response: any) => {
|
||||
if (
|
||||
response &&
|
||||
response.headers &&
|
||||
response.headers['content-disposition']
|
||||
) {
|
||||
const disposition = response.headers['content-disposition'];
|
||||
let filename = '';
|
||||
if (disposition && disposition.indexOf('attachment') !== -1) {
|
||||
let filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
|
||||
let matches = filenameRegex.exec(disposition);
|
||||
if (matches != null && matches[1]) {
|
||||
filename = matches[1].replace(/['"]/g, '');
|
||||
}
|
||||
|
||||
// 很可能是中文被 url-encode 了
|
||||
if (filename && filename.replace(/[^%]/g, '').length > 2) {
|
||||
filename = decodeURIComponent(filename);
|
||||
}
|
||||
|
||||
let type = response.headers['content-type'];
|
||||
let blob =
|
||||
response.data.toString() === '[object Blob]'
|
||||
? response.data
|
||||
: new Blob([response.data], {type: type});
|
||||
if (typeof window.navigator.msSaveBlob !== 'undefined') {
|
||||
// IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed."
|
||||
window.navigator.msSaveBlob(blob, filename);
|
||||
} else {
|
||||
let URL = window.URL || (window as any).webkitURL;
|
||||
let downloadUrl = URL.createObjectURL(blob);
|
||||
if (filename) {
|
||||
// use HTML5 a[download] attribute to specify filename
|
||||
let a = document.createElement('a');
|
||||
// safari doesn't support this yet
|
||||
if (typeof a.download === 'undefined') {
|
||||
(window as any).location = downloadUrl;
|
||||
} else {
|
||||
a.href = downloadUrl;
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
}
|
||||
} else {
|
||||
(window as any).location = downloadUrl;
|
||||
}
|
||||
setTimeout(function () {
|
||||
URL.revokeObjectURL(downloadUrl);
|
||||
}, 100); // cleanup
|
||||
}
|
||||
|
||||
return {
|
||||
...response,
|
||||
data: {
|
||||
status: 0,
|
||||
msg: '文件即将开始下载。。'
|
||||
}
|
||||
};
|
||||
}
|
||||
} else if (response.data.toString() === '[object Blob]') {
|
||||
return new Promise((resolve, reject) => {
|
||||
let reader = new FileReader();
|
||||
reader.addEventListener('loadend', e => {
|
||||
const text = reader.result as string;
|
||||
|
||||
try {
|
||||
resolve({
|
||||
...response,
|
||||
data: {
|
||||
...JSON.parse(text)
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
|
||||
reader.readAsText(response.data);
|
||||
});
|
||||
}
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
const responseAdpater = (api: any) => (value: any) => {
|
||||
let response = value.data;
|
||||
if (env && env.responseAdpater) {
|
||||
@ -173,7 +257,9 @@ export function embed(
|
||||
}
|
||||
|
||||
data && (config.data = data);
|
||||
return axios(url, config).then(responseAdpater(api));
|
||||
return axios(url, config)
|
||||
.then(attachmentAdpator)
|
||||
.then(responseAdpater(api));
|
||||
},
|
||||
isCancel: (value: any) => (axios as any).isCancel(value),
|
||||
copy: (contents: string, options: any = {}) => {
|
||||
|
@ -11,6 +11,7 @@ const mapping: {
|
||||
'react-dropzone': __moduleId('react-dropzone'),
|
||||
'classnames': __moduleId('classnames'),
|
||||
'axios': __moduleId('axios'),
|
||||
'exceljs': __moduleId('exceljs'),
|
||||
'moment': __moduleId('moment'),
|
||||
'mobx': __moduleId('mobx'),
|
||||
'mobx-state-tree': __moduleId('mobx-state-tree'),
|
||||
|
@ -110,7 +110,7 @@ export default class ButtonGroupControl extends React.Component<
|
||||
}
|
||||
);
|
||||
});
|
||||
} else if (buttons) {
|
||||
} else if (Array.isArray(buttons)) {
|
||||
body = buttons.map((button, key) =>
|
||||
render(
|
||||
`button/${key}`,
|
||||
|
@ -377,6 +377,7 @@ export class CityPicker extends React.Component<
|
||||
onChange={this.handleStreetChange}
|
||||
onBlur={this.handleStreetEnd}
|
||||
placeholder={__('请输入街道信息')}
|
||||
disabled={disabled}
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
@ -405,7 +406,8 @@ export class LocationControl extends React.Component<LocationControlProps> {
|
||||
allowDistrict,
|
||||
extractValue,
|
||||
joinValues,
|
||||
allowStreet
|
||||
allowStreet,
|
||||
disabled
|
||||
} = this.props;
|
||||
return (
|
||||
<ThemedCity
|
||||
@ -416,6 +418,7 @@ export class LocationControl extends React.Component<LocationControlProps> {
|
||||
extractValue={extractValue}
|
||||
joinValues={joinValues}
|
||||
allowStreet={allowStreet}
|
||||
disabled={disabled}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
@ -232,10 +232,18 @@ export function wrapAdaptor(promise: Promise<fetcherResult>, api: ApiObject) {
|
||||
const adaptor = api.adaptor;
|
||||
return adaptor
|
||||
? promise
|
||||
.then(response => ({
|
||||
...response,
|
||||
data: adaptor((response as any).data, response, api)
|
||||
}))
|
||||
.then(async response => {
|
||||
let result = adaptor((response as any).data, response, api);
|
||||
|
||||
if (result?.then) {
|
||||
result = await result;
|
||||
}
|
||||
|
||||
return {
|
||||
...response,
|
||||
data: result
|
||||
};
|
||||
})
|
||||
.then(responseAdaptor)
|
||||
: promise.then(responseAdaptor);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user