@sadais/uploader 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -0
- package/dist/index.es.js +228 -217
- package/dist/index.min.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -38,6 +38,7 @@ const uploader = new UploaderInstance(
|
|
|
38
38
|
}}
|
|
39
39
|
)
|
|
40
40
|
|
|
41
|
+
// 获取上传options示例
|
|
41
42
|
export const apiGetUploadOption = async type => {
|
|
42
43
|
const params = {
|
|
43
44
|
storageenum: type
|
|
@@ -45,6 +46,25 @@ export const apiGetUploadOption = async type => {
|
|
|
45
46
|
const { data } = await request.get('/api/manage/v1/storage/getstoragesignature', params)
|
|
46
47
|
return data
|
|
47
48
|
}
|
|
49
|
+
|
|
50
|
+
// 调用说明
|
|
51
|
+
//单文件上传
|
|
52
|
+
const {data,head:{msg,ret}} = await uploader.uploadFile(file)
|
|
53
|
+
// 0-成功 1-失败
|
|
54
|
+
if(ret===this.$consts.RET_CODE.SUCCESS){
|
|
55
|
+
const url = data
|
|
56
|
+
}
|
|
57
|
+
//多文件上传
|
|
58
|
+
const {successUrls,failUrls} = await uploader.uploadFiles(files)
|
|
59
|
+
// successUrls-上传成功的url数组
|
|
60
|
+
// failUrls-上传失败的fileName数组
|
|
61
|
+
|
|
62
|
+
//通用上传,支持单文件和多文件
|
|
63
|
+
const [{index,url,ret}] = await uploader.upload(file|files)
|
|
64
|
+
// 返回数组
|
|
65
|
+
// index:上传文件的下标;
|
|
66
|
+
// ret:状态,0-成功,1-失败;
|
|
67
|
+
// url:上传成功的路径 或 上传失败的文件名
|
|
48
68
|
```
|
|
49
69
|
|
|
50
70
|
## 发布完成后
|
package/dist/index.es.js
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* @Author: fuwenlong
|
|
3
3
|
* @Date: 2022-01-14 14:22:39
|
|
4
|
-
* @LastEditTime: 2022-01-
|
|
4
|
+
* @LastEditTime: 2022-01-24 17:48:03
|
|
5
5
|
* @LastEditors: zhangzhenfei
|
|
6
6
|
* @Description: 文件上传
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
// 对象存储类型
|
|
10
10
|
const UPLOADER_TYPE = {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
ALIYUN: 'ALIYUN', // 阿里云
|
|
12
|
+
TXYUN: 'TXYUN', // 腾讯云
|
|
13
|
+
HUAWEI: 'HUAWEI' // 华为云
|
|
14
14
|
};
|
|
15
15
|
|
|
16
16
|
const DEFAULT_OPTIONS = {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
17
|
+
// type: UPLOADER_TYPE.ALIYUN, //
|
|
18
|
+
// accessKeyId: 'LTAIXeOzClQBtKs3',
|
|
19
|
+
// bucketName: 'sadais-oss',
|
|
20
|
+
// endPoint: 'https://oss-cn-hangzhou.aliyuncs.com',
|
|
21
|
+
// path: 'file',
|
|
22
|
+
// signature: '6Y5l5k56y9VgxDU1vhsyCLC99QM=',
|
|
23
|
+
// domain: 'https://m.antibao.cn',
|
|
24
|
+
// policy:'',
|
|
25
|
+
// timeOut: 1642476191415
|
|
26
26
|
};
|
|
27
27
|
|
|
28
28
|
/**
|
|
@@ -30,236 +30,247 @@ const DEFAULT_OPTIONS = {
|
|
|
30
30
|
* @param {String} params.getOptionFun
|
|
31
31
|
*/
|
|
32
32
|
const UploaderInstance = (function () {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
33
|
+
let instance;
|
|
34
|
+
return function (params) {
|
|
35
|
+
if (!instance) {
|
|
36
|
+
instance = new SadaisUploader(params);
|
|
37
|
+
}
|
|
38
|
+
return instance
|
|
39
|
+
}
|
|
40
40
|
})();
|
|
41
41
|
|
|
42
42
|
class SadaisUploader {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
constructor(params) {
|
|
44
|
+
this._init(params);
|
|
45
|
+
}
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
47
|
+
/**
|
|
48
|
+
* 初始化
|
|
49
|
+
* @param {Object} options
|
|
50
|
+
*/
|
|
51
|
+
async _init(params) {
|
|
52
|
+
this.params = params;
|
|
53
|
+
this._initOptions(params);
|
|
54
|
+
}
|
|
55
55
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
56
|
+
/**
|
|
57
|
+
* 获取配置信息
|
|
58
|
+
*/
|
|
59
|
+
async _initOptions() {
|
|
60
|
+
let { type, getOptionFun } = this.params;
|
|
61
61
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
62
|
+
if (getOptionFun) {
|
|
63
|
+
const options = await getOptionFun(type);
|
|
64
|
+
if (!options) return
|
|
65
|
+
if (options.path && !options.path.endsWith('/')) {
|
|
66
|
+
options.path = options.path + '/';
|
|
67
|
+
}
|
|
68
|
+
this.options = { ...options, type };
|
|
70
69
|
}
|
|
70
|
+
}
|
|
71
71
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
72
|
+
/**
|
|
73
|
+
* 是否超时
|
|
74
|
+
*/
|
|
75
|
+
_isTimeOut() {
|
|
76
|
+
// 比服务器timeOut时间提前10s过期
|
|
77
|
+
const timeout = this.options.timeOut - 1000 * 10;
|
|
78
|
+
return new Date().getTime() > timeout
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 文件上传
|
|
83
|
+
* @param { File | Array<file> } file 文件或文件数组
|
|
84
|
+
*/
|
|
85
|
+
async upload(file) {
|
|
86
|
+
if (!file) return
|
|
87
|
+
let files = Array.isArray(file) ? file : [file];
|
|
88
|
+
const result = [];
|
|
89
|
+
for (let i = 0; i < files.length; i++) {
|
|
90
|
+
const item = files[i];
|
|
91
|
+
const { data, head } = await this.uploadFile(item);
|
|
92
|
+
result.push({
|
|
93
|
+
index: i,
|
|
94
|
+
url: data,
|
|
95
|
+
ret: head.ret
|
|
96
|
+
});
|
|
79
97
|
}
|
|
98
|
+
return result
|
|
99
|
+
}
|
|
80
100
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
101
|
+
/**
|
|
102
|
+
* 单文件上传
|
|
103
|
+
* @param {File} file
|
|
104
|
+
* @return Promise
|
|
105
|
+
*/
|
|
106
|
+
async uploadFile(file) {
|
|
107
|
+
// 超时判断
|
|
108
|
+
if (this._isTimeOut()) {
|
|
109
|
+
await this._initOptions();
|
|
110
|
+
}
|
|
111
|
+
if (!file.name && file.path) {
|
|
112
|
+
const suffix = file.path.substring(file.path.lastIndexOf('.'));
|
|
113
|
+
file.name = this._guid() + suffix;
|
|
114
|
+
}
|
|
115
|
+
return new Promise((resolve, reject) => {
|
|
116
|
+
const { domain } = this.options;
|
|
97
117
|
|
|
98
|
-
|
|
99
|
-
|
|
118
|
+
// 请求地址
|
|
119
|
+
const formData = this._buildParams(file);
|
|
100
120
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
success: 0,
|
|
112
|
-
url: file.name,
|
|
113
|
-
errMsg: 'fail',
|
|
114
|
-
};
|
|
121
|
+
const apiUrl = formData.url;
|
|
122
|
+
const uploadedFilePath = `${domain}${domain.endsWith('/') ? '' : '/'}${formData.key}`;
|
|
123
|
+
const success = {
|
|
124
|
+
data: uploadedFilePath,
|
|
125
|
+
head: { ret: 0, msg: 'success' }
|
|
126
|
+
};
|
|
127
|
+
const error = {
|
|
128
|
+
data: file.name,
|
|
129
|
+
head: { ret: 1, msg: 'fail' }
|
|
130
|
+
};
|
|
115
131
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
},
|
|
132
|
-
fail: (error) => {
|
|
133
|
-
console.log(error);
|
|
134
|
-
reject(error);
|
|
135
|
-
},
|
|
136
|
-
});
|
|
137
|
-
// H5 input:file接收内容
|
|
138
|
-
} catch (e) {
|
|
139
|
-
console.log('uni报错,调用XMLHttpRequest', e);
|
|
140
|
-
const data = new FormData();
|
|
141
|
-
Object.keys(formData).forEach((key) => {
|
|
142
|
-
data.append(key, formData[key]);
|
|
143
|
-
});
|
|
144
|
-
data.append('file', file);
|
|
145
|
-
const xhr = new XMLHttpRequest();
|
|
146
|
-
xhr.open('POST', apiUrl, true);
|
|
147
|
-
xhr.onreadystatechange = () => {
|
|
148
|
-
if (xhr.readyState === XMLHttpRequest.DONE) {
|
|
149
|
-
if (xhr.status === 200) {
|
|
150
|
-
resolve(success);
|
|
151
|
-
} else {
|
|
152
|
-
reject(error);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
xhr.send(data);
|
|
132
|
+
try {
|
|
133
|
+
// uniapp
|
|
134
|
+
uni.uploadFile({
|
|
135
|
+
url: apiUrl,
|
|
136
|
+
filePath: file.path, // uni api提供的blob格式图片地址
|
|
137
|
+
name: 'file',
|
|
138
|
+
formData,
|
|
139
|
+
success: (res) => {
|
|
140
|
+
const { statusCode } = res;
|
|
141
|
+
if (statusCode == 200) {
|
|
142
|
+
resolve(success);
|
|
143
|
+
} else {
|
|
144
|
+
error.head.msg = res;
|
|
145
|
+
reject(error);
|
|
157
146
|
}
|
|
147
|
+
},
|
|
148
|
+
fail: (error) => {
|
|
149
|
+
console.log(error);
|
|
150
|
+
reject(error);
|
|
151
|
+
}
|
|
158
152
|
});
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
if (
|
|
172
|
-
|
|
153
|
+
// H5 input:file接收内容
|
|
154
|
+
} catch (e) {
|
|
155
|
+
console.log('uni报错,调用XMLHttpRequest', e);
|
|
156
|
+
const data = new FormData();
|
|
157
|
+
Object.keys(formData).forEach((key) => {
|
|
158
|
+
data.append(key, formData[key]);
|
|
159
|
+
});
|
|
160
|
+
data.append('file', file);
|
|
161
|
+
const xhr = new XMLHttpRequest();
|
|
162
|
+
xhr.open('POST', apiUrl, true);
|
|
163
|
+
xhr.onreadystatechange = () => {
|
|
164
|
+
if (xhr.readyState === XMLHttpRequest.DONE) {
|
|
165
|
+
if (xhr.status === 200) {
|
|
166
|
+
resolve(success);
|
|
173
167
|
} else {
|
|
174
|
-
|
|
168
|
+
reject(error);
|
|
175
169
|
}
|
|
176
|
-
|
|
177
|
-
return { successUrls, failUrls };
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// 按类型组装过参数
|
|
181
|
-
_buildParams(file) {
|
|
182
|
-
const opt = this.options;
|
|
183
|
-
const type = opt.type;
|
|
184
|
-
const params = {
|
|
185
|
-
name: file.name,
|
|
186
|
-
policy: opt.policy,
|
|
187
|
-
success_action_status: '200',
|
|
188
|
-
key: this._getUploadFilePath(file.name),
|
|
170
|
+
}
|
|
189
171
|
};
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
params['q-ak'] = opt.accessKeyId;
|
|
195
|
-
params['q-signature'] = opt.signature; // 签名
|
|
196
|
-
params['q-sign-algorithm'] = 'sha1'; // 签名算法
|
|
197
|
-
params['q-key-time'] = opt.param['q-sign-time'];
|
|
198
|
-
// params['q-sign-time'] = opt.param['q-sign-time']
|
|
199
|
-
params.url = `https://${opt.bucketName}.cos.${endPoint}.myqcloud.com`;
|
|
200
|
-
} else if (UPLOADER_TYPE.HUAWEI === type) {
|
|
201
|
-
params.AccessKeyId = opt.accessKeyId;
|
|
202
|
-
params.signature = opt.signature;
|
|
203
|
-
params.url = `https://${opt.bucketName}.obs.${endPoint}.myhuaweicloud.com`;
|
|
204
|
-
} else {
|
|
205
|
-
params.signature = opt.signature;
|
|
206
|
-
params.OSSAccessKeyId = opt.accessKeyId;
|
|
207
|
-
params.bucket = opt.bucketName;
|
|
172
|
+
xhr.send(data);
|
|
173
|
+
}
|
|
174
|
+
})
|
|
175
|
+
}
|
|
208
176
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
177
|
+
/**
|
|
178
|
+
* 多文件上传
|
|
179
|
+
* @param {Array<File>} files
|
|
180
|
+
* @return Promise
|
|
181
|
+
*/
|
|
182
|
+
async uploadFiles(files) {
|
|
183
|
+
const successUrls = [];
|
|
184
|
+
const failUrls = [];
|
|
185
|
+
for (const file of files) {
|
|
186
|
+
const { data, head } = await this.uploadFile(file);
|
|
187
|
+
if (head.ret === 0) {
|
|
188
|
+
successUrls.push(data);
|
|
189
|
+
} else {
|
|
190
|
+
failUrls.push(data);
|
|
191
|
+
}
|
|
212
192
|
}
|
|
193
|
+
return { successUrls, failUrls }
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// 按类型组装过参数
|
|
197
|
+
_buildParams(file) {
|
|
198
|
+
const opt = this.options;
|
|
199
|
+
const type = opt.type;
|
|
200
|
+
const params = {
|
|
201
|
+
name: file.name,
|
|
202
|
+
policy: opt.policy,
|
|
203
|
+
success_action_status: '200',
|
|
204
|
+
key: this._getUploadFilePath(file.name)
|
|
205
|
+
};
|
|
206
|
+
const endPoint = opt.endPoint.replace('https://', '').replace('http://', '');
|
|
207
|
+
if (UPLOADER_TYPE.TXYUN === type) {
|
|
208
|
+
params['q-ak'] = opt.accessKeyId;
|
|
209
|
+
params['q-signature'] = opt.signature; // 签名
|
|
210
|
+
params['q-sign-algorithm'] = 'sha1'; // 签名算法
|
|
211
|
+
params['q-key-time'] = opt.param['q-sign-time'];
|
|
212
|
+
// params['q-sign-time'] = opt.param['q-sign-time']
|
|
213
|
+
params.url = `https://${opt.bucketName}.cos.${endPoint}.myqcloud.com`;
|
|
214
|
+
} else if (UPLOADER_TYPE.HUAWEI === type) {
|
|
215
|
+
params.AccessKeyId = opt.accessKeyId;
|
|
216
|
+
params.signature = opt.signature;
|
|
217
|
+
params.url = `https://${opt.bucketName}.obs.${endPoint}.myhuaweicloud.com`;
|
|
218
|
+
} else {
|
|
219
|
+
params.signature = opt.signature;
|
|
220
|
+
params.OSSAccessKeyId = opt.accessKeyId;
|
|
221
|
+
params.bucket = opt.bucketName;
|
|
213
222
|
|
|
214
|
-
|
|
215
|
-
* 获取上传文件路径
|
|
216
|
-
* @param {String} name 文件名
|
|
217
|
-
* @returns
|
|
218
|
-
*/
|
|
219
|
-
_getUploadFilePath(name) {
|
|
220
|
-
const { useOriginalName, path } = this.options;
|
|
221
|
-
const lastIndexOf = name.lastIndexOf('.');
|
|
222
|
-
const originalName = name.substring(0, lastIndexOf);
|
|
223
|
-
const suffix = name.substring(lastIndexOf + 1);
|
|
224
|
-
const fileName = useOriginalName ? `/${originalName}` : '';
|
|
225
|
-
// 为了保证使用文件原名时,文件不被覆盖,仍然使用随机生成方式
|
|
226
|
-
// ${this._getNowDate()}/
|
|
227
|
-
return `${path}${this._getNowDate()}/${this._guid()}${fileName}.${suffix}`;
|
|
223
|
+
params.url = `https://${opt.bucketName}.${endPoint}`;
|
|
228
224
|
}
|
|
225
|
+
return params
|
|
226
|
+
}
|
|
229
227
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
228
|
+
/**
|
|
229
|
+
* 获取上传文件路径
|
|
230
|
+
* @param {String} name 文件名
|
|
231
|
+
* @returns
|
|
232
|
+
*/
|
|
233
|
+
_getUploadFilePath(name) {
|
|
234
|
+
const { useOriginalName, path } = this.options;
|
|
235
|
+
const lastIndexOf = name.lastIndexOf('.');
|
|
236
|
+
const originalName = name.substring(0, lastIndexOf);
|
|
237
|
+
const suffix = name.substring(lastIndexOf + 1);
|
|
238
|
+
const fileName = useOriginalName ? `/${originalName}` : '';
|
|
239
|
+
// 为了保证使用文件原名时,文件不被覆盖,仍然使用随机生成方式
|
|
240
|
+
// ${this._getNowDate()}/
|
|
241
|
+
return `${path}${this._getNowDate()}/${this._guid()}${fileName}.${suffix}`
|
|
242
|
+
}
|
|
245
243
|
|
|
246
|
-
|
|
244
|
+
/**
|
|
245
|
+
* 获取当前日期
|
|
246
|
+
* @returns YYYY-MM-DD
|
|
247
|
+
*/
|
|
248
|
+
_getNowDate() {
|
|
249
|
+
const date = new Date();
|
|
250
|
+
const year = date.getFullYear();
|
|
251
|
+
let month = date.getMonth() + 1;
|
|
252
|
+
let day = date.getDate();
|
|
253
|
+
if (month < 10) {
|
|
254
|
+
month = `0${month}`;
|
|
247
255
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
* 生成guid,用于生成随机文件名
|
|
251
|
-
* @return String
|
|
252
|
-
*/
|
|
253
|
-
_guid() {
|
|
254
|
-
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
|
|
255
|
-
/[xy]/g,
|
|
256
|
-
function (c) {
|
|
257
|
-
var r = (Math.random() * 16) | 0,
|
|
258
|
-
v = c == 'x' ? r : (r & 0x3) | 0x8;
|
|
259
|
-
return v.toString(16);
|
|
260
|
-
}
|
|
261
|
-
);
|
|
256
|
+
if (day < 10) {
|
|
257
|
+
day = `0${day}`;
|
|
262
258
|
}
|
|
259
|
+
|
|
260
|
+
return `${year}-${month}-${day}`
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* 生成guid,用于生成随机文件名
|
|
265
|
+
* @return String
|
|
266
|
+
*/
|
|
267
|
+
_guid() {
|
|
268
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
269
|
+
var r = (Math.random() * 16) | 0,
|
|
270
|
+
v = c == 'x' ? r : (r & 0x3) | 0x8;
|
|
271
|
+
return v.toString(16)
|
|
272
|
+
})
|
|
273
|
+
}
|
|
263
274
|
}
|
|
264
275
|
|
|
265
276
|
export { DEFAULT_OPTIONS, UPLOADER_TYPE, UploaderInstance };
|
package/dist/index.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";function e(e,n){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!r){if(Array.isArray(e)||(r=function(e,n){if(!e)return;if("string"==typeof e)return t(e,n);var r=Object.prototype.toString.call(e).slice(8,-1);"Object"===r&&e.constructor&&(r=e.constructor.name);if("Map"===r||"Set"===r)return Array.from(e);if("Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r))return t(e,n)}(e))||n&&e&&"number"==typeof e.length){r&&(e=r);var a=0,
|
|
1
|
+
"use strict";function e(e,n){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!r){if(Array.isArray(e)||(r=function(e,n){if(!e)return;if("string"==typeof e)return t(e,n);var r=Object.prototype.toString.call(e).slice(8,-1);"Object"===r&&e.constructor&&(r=e.constructor.name);if("Map"===r||"Set"===r)return Array.from(e);if("Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r))return t(e,n)}(e))||n&&e&&"number"==typeof e.length){r&&(e=r);var a=0,i=function(){};return{s:i,n:function(){return a>=e.length?{done:!0}:{done:!1,value:e[a++]}},e:function(e){throw e},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,u=!0,c=!1;return{s:function(){r=r.call(e)},n:function(){var e=r.next();return u=e.done,e},e:function(e){c=!0,o=e},f:function(){try{u||null==r.return||r.return()}finally{if(c)throw o}}}}function t(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function n(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function r(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?n(Object(r),!0).forEach((function(t){a(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):n(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function a(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(e,t,n,r,a,i,o){try{var u=e[i](o),c=u.value}catch(e){return void n(e)}u.done?t(c):Promise.resolve(c).then(r,a)}function o(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var o=e.apply(t,n);function u(e){i(o,r,a,u,c,"next",e)}function c(e){i(o,r,a,u,c,"throw",e)}u(void 0)}))}}function u(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}Object.defineProperty(exports,"__esModule",{value:!0});var c,s={ALIYUN:"ALIYUN",TXYUN:"TXYUN",HUAWEI:"HUAWEI"},p=function(e){return c||(c=new l(e)),c},l=function(){function t(e){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t),this._init(e)}var n,a,i,c,p,l,f,h;return n=t,a=[{key:"_init",value:(h=o(regeneratorRuntime.mark((function e(t){return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:this.params=t,this._initOptions(t);case 2:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"_initOptions",value:(f=o(regeneratorRuntime.mark((function e(){var t,n,a,i;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(t=this.params,n=t.type,!(a=t.getOptionFun)){e.next=9;break}return e.next=4,a(n);case 4:if(i=e.sent){e.next=7;break}return e.abrupt("return");case 7:i.path&&!i.path.endsWith("/")&&(i.path=i.path+"/"),this.options=r(r({},i),{},{type:n});case 9:case"end":return e.stop()}}),e,this)}))),function(){return f.apply(this,arguments)})},{key:"_isTimeOut",value:function(){var e=this.options.timeOut-1e4;return(new Date).getTime()>e}},{key:"upload",value:(l=o(regeneratorRuntime.mark((function e(t){var n,r,a,i,o,u,c;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(t){e.next=2;break}return e.abrupt("return");case 2:n=Array.isArray(t)?t:[t],r=[],a=0;case 5:if(!(a<n.length)){e.next=16;break}return i=n[a],e.next=9,this.uploadFile(i);case 9:o=e.sent,u=o.data,c=o.head,r.push({index:a,url:u,ret:c.ret});case 13:a++,e.next=5;break;case 16:return e.abrupt("return",r);case 17:case"end":return e.stop()}}),e,this)}))),function(e){return l.apply(this,arguments)})},{key:"uploadFile",value:(p=o(regeneratorRuntime.mark((function e(t){var n,r=this;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!this._isTimeOut()){e.next=3;break}return e.next=3,this._initOptions();case 3:return!t.name&&t.path&&(n=t.path.substring(t.path.lastIndexOf(".")),t.name=this._guid()+n),e.abrupt("return",new Promise((function(e,n){var a=r.options.domain,i=r._buildParams(t),o=i.url,u={data:"".concat(a).concat(a.endsWith("/")?"":"/").concat(i.key),head:{ret:0,msg:"success"}},c={data:t.name,head:{ret:1,msg:"fail"}};try{uni.uploadFile({url:o,filePath:t.path,name:"file",formData:i,success:function(t){200==t.statusCode?e(u):(c.head.msg=t,n(c))},fail:function(e){console.log(e),n(e)}})}catch(r){console.log("uni报错,调用XMLHttpRequest",r);var s=new FormData;Object.keys(i).forEach((function(e){s.append(e,i[e])})),s.append("file",t);var p=new XMLHttpRequest;p.open("POST",o,!0),p.onreadystatechange=function(){p.readyState===XMLHttpRequest.DONE&&(200===p.status?e(u):n(c))},p.send(s)}})));case 5:case"end":return e.stop()}}),e,this)}))),function(e){return p.apply(this,arguments)})},{key:"uploadFiles",value:(c=o(regeneratorRuntime.mark((function t(n){var r,a,i,o,u,c,s;return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:r=[],a=[],i=e(n),t.prev=3,i.s();case 5:if((o=i.n()).done){t.next=15;break}return u=o.value,t.next=9,this.uploadFile(u);case 9:c=t.sent,s=c.data,0===c.head.ret?r.push(s):a.push(s);case 13:t.next=5;break;case 15:t.next=20;break;case 17:t.prev=17,t.t0=t.catch(3),i.e(t.t0);case 20:return t.prev=20,i.f(),t.finish(20);case 23:return t.abrupt("return",{successUrls:r,failUrls:a});case 24:case"end":return t.stop()}}),t,this,[[3,17,20,23]])}))),function(e){return c.apply(this,arguments)})},{key:"_buildParams",value:function(e){var t=this.options,n=t.type,r={name:e.name,policy:t.policy,success_action_status:"200",key:this._getUploadFilePath(e.name)},a=t.endPoint.replace("https://","").replace("http://","");return s.TXYUN===n?(r["q-ak"]=t.accessKeyId,r["q-signature"]=t.signature,r["q-sign-algorithm"]="sha1",r["q-key-time"]=t.param["q-sign-time"],r.url="https://".concat(t.bucketName,".cos.").concat(a,".myqcloud.com")):s.HUAWEI===n?(r.AccessKeyId=t.accessKeyId,r.signature=t.signature,r.url="https://".concat(t.bucketName,".obs.").concat(a,".myhuaweicloud.com")):(r.signature=t.signature,r.OSSAccessKeyId=t.accessKeyId,r.bucket=t.bucketName,r.url="https://".concat(t.bucketName,".").concat(a)),r}},{key:"_getUploadFilePath",value:function(e){var t=this.options,n=t.useOriginalName,r=t.path,a=e.lastIndexOf("."),i=e.substring(0,a),o=e.substring(a+1),u=n?"/".concat(i):"";return"".concat(r).concat(this._getNowDate(),"/").concat(this._guid()).concat(u,".").concat(o)}},{key:"_getNowDate",value:function(){var e=new Date,t=e.getFullYear(),n=e.getMonth()+1,r=e.getDate();return n<10&&(n="0".concat(n)),r<10&&(r="0".concat(r)),"".concat(t,"-").concat(n,"-").concat(r)}},{key:"_guid",value:function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(e){var t=16*Math.random()|0;return("x"==e?t:3&t|8).toString(16)}))}}],a&&u(n.prototype,a),i&&u(n,i),Object.defineProperty(n,"prototype",{writable:!1}),t}();exports.DEFAULT_OPTIONS={},exports.UPLOADER_TYPE=s,exports.UploaderInstance=p;
|