Skip to content
This repository has been archived by the owner on Aug 14, 2022. It is now read-only.

Commit

Permalink
feat(uploader/custom): support set token
Browse files Browse the repository at this point in the history
  • Loading branch information
njzydark committed Sep 4, 2020
1 parent 3744507 commit 147a9e4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
22 changes: 19 additions & 3 deletions packages/aragorn-uploader-custom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ interface Config {
url: string;
method: 'POST' | 'GET' | 'PUT' | 'PATCH' | 'DELETE';
contentType: 'multipart/form-data' | 'application/x-www-form-urlencoded' | 'application/json';
token?: string;
fileFieldName: string;
responseUrlFieldName: string;
responseMessageName?: string;
requestParams?: string;
requestBody?: string;
}
Expand Down Expand Up @@ -42,14 +44,21 @@ export class CustomUploader implements Uploader {
method: uploaderOptions.method,
headers: {
...formData.getHeaders(),
'Content-Length': length
'Content-Length': length,
Authorization: uploaderOptions.token || ''
},
params: uploaderOptions.requestParams ? JSON.parse(uploaderOptions.requestParams) : {},
data: uploaderOptions.contentType === 'multipart/form-data' ? formData : uploaderOptions.requestBody
};
// 发起请求
const { data: res } = await axios(requestOpetion);
let imageUrl = res?.data?.[uploaderOptions.responseUrlFieldName];
let imageUrl = uploaderOptions.responseUrlFieldName?.split('.').reduce((pre, cur) => {
try {
return pre[cur];
} catch (err) {
return undefined;
}
}, res);
if (imageUrl) {
return {
success: true,
Expand All @@ -58,9 +67,16 @@ export class CustomUploader implements Uploader {
}
};
} else {
const message = uploaderOptions?.responseMessageName?.split('.').reduce((pre, cur) => {
try {
return pre[cur];
} catch (err) {
return undefined;
}
}, res);
return {
success: false,
desc: '上传失败'
desc: message || '上传失败'
};
}
} catch (err) {
Expand Down
20 changes: 16 additions & 4 deletions packages/aragorn-uploader-custom/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ export const options: UploaderOptions = [
valueType: 'input',
required: true
},
{
label: 'Token',
name: 'token',
value: '',
valueType: 'input'
},
{
label: '请求方式',
name: 'method',
Expand Down Expand Up @@ -40,7 +46,7 @@ export const options: UploaderOptions = [
{
label: 'Content-Type',
name: 'contentType',
value: '',
value: 'multipart/form-data',
valueType: 'select',
options: [
{
Expand All @@ -59,19 +65,25 @@ export const options: UploaderOptions = [
required: true
},
{
label: '文件字段名',
label: '上传文件字段名',
name: 'fileFieldName',
value: 'file',
valueType: 'input',
required: true
},
{
label: '响应字段名',
label: '响应文件字段名',
name: 'responseUrlFieldName',
value: '',
value: 'data.url',
valueType: 'input',
required: true
},
{
label: '响应提示字段名',
name: 'responseMessageName',
value: 'message',
valueType: 'input'
},
{
label: '请求头参数',
name: 'requestParams',
Expand Down

0 comments on commit 147a9e4

Please sign in to comment.