@sadais/uploader 0.3.4 → 0.3.5
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 +80 -80
- package/dist/index.es.js +281 -281
- package/dist/index.min.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,80 +1,80 @@
|
|
|
1
|
-
## 开始
|
|
2
|
-
|
|
3
|
-
```bash
|
|
4
|
-
# 安装依赖
|
|
5
|
-
npx yarn
|
|
6
|
-
# 监听实时构建`/dist`
|
|
7
|
-
npm run dev
|
|
8
|
-
# 只构建`/dist`一次,不作监听
|
|
9
|
-
npm run build
|
|
10
|
-
```
|
|
11
|
-
|
|
12
|
-
### 发布包到私服
|
|
13
|
-
|
|
14
|
-
```bash
|
|
15
|
-
# 登录npm服务器
|
|
16
|
-
npm login --registry https://registry.npmjs.org
|
|
17
|
-
# 发布
|
|
18
|
-
npm publish
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
## 发布之前测试
|
|
22
|
-
|
|
23
|
-
在当前工程,没有创建组件的单元测试,只能通过把构建后的东西复制到某个项目的 node_modules
|
|
24
|
-
|
|
25
|
-
比如当前 package.json 的包名是 "@sadais/uploader" , 发布前要在 A 项目中测试 "@sadais/uploader", 在 A 项目的 node_modules 新建 @sadais/uploader , 再把 /dist/ 和 package.json 拷贝到 A 项目的 node_modules/@sadais/uploader
|
|
26
|
-
|
|
27
|
-
使用
|
|
28
|
-
|
|
29
|
-
```js
|
|
30
|
-
|
|
31
|
-
import { UploaderInstance,UPLOADER_TYPE } from '@sadais/uploader';
|
|
32
|
-
const uploader = new UploaderInstance(
|
|
33
|
-
{type:UPLOADER_TYPE.ALIYUN,getOptionFun:async (type) => {
|
|
34
|
-
// 获取option示例
|
|
35
|
-
const { head, data } = await apiGetUploadOption(type)
|
|
36
|
-
if (head.ret !== this.$consts.RET_CODE.SUCCESS) return null
|
|
37
|
-
return data
|
|
38
|
-
}}
|
|
39
|
-
)
|
|
40
|
-
|
|
41
|
-
// 获取上传options示例
|
|
42
|
-
export const apiGetUploadOption = async type => {
|
|
43
|
-
const params = {
|
|
44
|
-
storageenum: type
|
|
45
|
-
}
|
|
46
|
-
const { data } = await request.get('/api/manage/v1/storage/getstoragesignature', params)
|
|
47
|
-
return data
|
|
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:上传成功的路径 或 上传失败的文件名
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
## 发布完成后
|
|
71
|
-
|
|
72
|
-
在 A 项目安装
|
|
73
|
-
|
|
74
|
-
```bash
|
|
75
|
-
# use yarn
|
|
76
|
-
npx yarn add @sadais/uploader -S
|
|
77
|
-
|
|
78
|
-
# use npm or pnpm
|
|
79
|
-
npx pnpm install @sadais/uploader -S
|
|
80
|
-
```
|
|
1
|
+
## 开始
|
|
2
|
+
|
|
3
|
+
```bash
|
|
4
|
+
# 安装依赖
|
|
5
|
+
npx yarn
|
|
6
|
+
# 监听实时构建`/dist`
|
|
7
|
+
npm run dev
|
|
8
|
+
# 只构建`/dist`一次,不作监听
|
|
9
|
+
npm run build
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
### 发布包到私服
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
# 登录npm服务器
|
|
16
|
+
npm login --registry https://registry.npmjs.org
|
|
17
|
+
# 发布
|
|
18
|
+
npm publish
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## 发布之前测试
|
|
22
|
+
|
|
23
|
+
在当前工程,没有创建组件的单元测试,只能通过把构建后的东西复制到某个项目的 node_modules
|
|
24
|
+
|
|
25
|
+
比如当前 package.json 的包名是 "@sadais/uploader" , 发布前要在 A 项目中测试 "@sadais/uploader", 在 A 项目的 node_modules 新建 @sadais/uploader , 再把 /dist/ 和 package.json 拷贝到 A 项目的 node_modules/@sadais/uploader
|
|
26
|
+
|
|
27
|
+
使用
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
|
|
31
|
+
import { UploaderInstance,UPLOADER_TYPE } from '@sadais/uploader';
|
|
32
|
+
const uploader = new UploaderInstance(
|
|
33
|
+
{type:UPLOADER_TYPE.ALIYUN,getOptionFun:async (type) => {
|
|
34
|
+
// 获取option示例
|
|
35
|
+
const { head, data } = await apiGetUploadOption(type)
|
|
36
|
+
if (head.ret !== this.$consts.RET_CODE.SUCCESS) return null
|
|
37
|
+
return data
|
|
38
|
+
}}
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
// 获取上传options示例
|
|
42
|
+
export const apiGetUploadOption = async type => {
|
|
43
|
+
const params = {
|
|
44
|
+
storageenum: type
|
|
45
|
+
}
|
|
46
|
+
const { data } = await request.get('/api/manage/v1/storage/getstoragesignature', params)
|
|
47
|
+
return data
|
|
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:上传成功的路径 或 上传失败的文件名
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## 发布完成后
|
|
71
|
+
|
|
72
|
+
在 A 项目安装
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
# use yarn
|
|
76
|
+
npx yarn add @sadais/uploader -S
|
|
77
|
+
|
|
78
|
+
# use npm or pnpm
|
|
79
|
+
npx pnpm install @sadais/uploader -S
|
|
80
|
+
```
|
package/dist/index.es.js
CHANGED
|
@@ -1,284 +1,284 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @Author: fuwenlong
|
|
3
|
-
* @Date: 2022-01-14 14:22:39
|
|
4
|
-
* @LastEditTime: 2022-
|
|
5
|
-
* @LastEditors: zhangzhenfei
|
|
6
|
-
* @Description: 文件上传
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
// 对象存储类型
|
|
10
|
-
const UPLOADER_TYPE = {
|
|
11
|
-
ALIYUN: 'ALIYUN', // 阿里云
|
|
12
|
-
TXYUN: 'TXYUN', // 腾讯云
|
|
13
|
-
HUAWEI: 'HUAWEI' // 华为云
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
const DEFAULT_OPTIONS = {
|
|
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
|
-
};
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* @param {String} params.type
|
|
30
|
-
* @param {String} params.getOptionFun
|
|
31
|
-
*/
|
|
32
|
-
const UploaderInstance = (function () {
|
|
33
|
-
let instance;
|
|
34
|
-
return function (params) {
|
|
35
|
-
if (!instance) {
|
|
36
|
-
instance = new SadaisUploader(params);
|
|
37
|
-
}
|
|
38
|
-
return instance
|
|
39
|
-
}
|
|
40
|
-
})();
|
|
41
|
-
|
|
42
|
-
class SadaisUploader {
|
|
43
|
-
constructor(params) {
|
|
44
|
-
this._init(params);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* 初始化
|
|
49
|
-
* @param {Object} options
|
|
50
|
-
*/
|
|
51
|
-
async _init(params) {
|
|
52
|
-
this.params = params;
|
|
53
|
-
// 不在初始化的时候调用接口,上传文件时再调用,故注释以下代码
|
|
54
|
-
// this._initOptions()
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* 获取配置信息
|
|
59
|
-
*/
|
|
60
|
-
async initOptions(customParams) {
|
|
61
|
-
if (!this.params) this.params = {};
|
|
62
|
-
|
|
63
|
-
if (customParams) {
|
|
64
|
-
return (this.params = {
|
|
65
|
-
...this.params,
|
|
66
|
-
...customParams
|
|
67
|
-
})
|
|
68
|
-
}
|
|
69
|
-
let { type, getOptionFun } = this.params;
|
|
70
|
-
|
|
71
|
-
if (getOptionFun) {
|
|
72
|
-
const options = await getOptionFun(type);
|
|
73
|
-
if (!options) return
|
|
74
|
-
if (options.path && !options.path.endsWith('/')) {
|
|
75
|
-
options.path = options.path + '/';
|
|
76
|
-
}
|
|
77
|
-
this.options = { ...options, type };
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* 是否超时
|
|
83
|
-
*/
|
|
84
|
-
_isTimeOut() {
|
|
85
|
-
// 比服务器timeOut时间提前10s过期
|
|
86
|
-
const timeout = this.options.timeOut - 1000 * 10;
|
|
87
|
-
return new Date().getTime() > timeout
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* 文件上传
|
|
92
|
-
* @param { File | Array<file> } file 文件或文件数组
|
|
93
|
-
*/
|
|
94
|
-
async upload(file) {
|
|
95
|
-
if (!file) return
|
|
96
|
-
let files = Array.isArray(file) ? file : [file];
|
|
97
|
-
const result = [];
|
|
98
|
-
for (let i = 0; i < files.length; i++) {
|
|
99
|
-
const item = files[i];
|
|
100
|
-
const { data, head } = await this.uploadFile(item);
|
|
101
|
-
result.push({
|
|
102
|
-
index: i,
|
|
103
|
-
url: data,
|
|
104
|
-
ret: head.ret
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
return result
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* 单文件上传
|
|
112
|
-
* @param {File} file
|
|
113
|
-
* @return Promise
|
|
114
|
-
*/
|
|
115
|
-
async uploadFile(file) {
|
|
116
|
-
if (!this.options || this._isTimeOut()) {
|
|
117
|
-
await this.initOptions();
|
|
118
|
-
}
|
|
119
|
-
if (!file.name && file.path) {
|
|
120
|
-
const suffix = file.path.substring(file.path.lastIndexOf('.'));
|
|
121
|
-
file.name = this._guid() + suffix;
|
|
122
|
-
}
|
|
123
|
-
return new Promise((resolve, reject) => {
|
|
124
|
-
const { domain } = this.options;
|
|
125
|
-
|
|
126
|
-
// 请求地址
|
|
127
|
-
const formData = this._buildParams(file);
|
|
128
|
-
|
|
129
|
-
const apiUrl = formData.url;
|
|
130
|
-
const uploadedFilePath = `${domain}${domain.endsWith('/') ? '' : '/'}${formData.key}`;
|
|
131
|
-
const success = {
|
|
132
|
-
data: uploadedFilePath,
|
|
133
|
-
head: { ret: 0, msg: 'success' }
|
|
134
|
-
};
|
|
135
|
-
const error = {
|
|
136
|
-
data: file.name,
|
|
137
|
-
head: { ret: 1, msg: 'fail' }
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
try {
|
|
141
|
-
// uniapp
|
|
142
|
-
uni.uploadFile({
|
|
143
|
-
url: apiUrl,
|
|
144
|
-
filePath: file.path, // uni api提供的blob格式图片地址
|
|
145
|
-
name: 'file',
|
|
146
|
-
formData,
|
|
147
|
-
success: (res) => {
|
|
148
|
-
const { statusCode } = res;
|
|
149
|
-
if (statusCode == 200) {
|
|
150
|
-
resolve(success);
|
|
151
|
-
} else {
|
|
152
|
-
error.head.msg = res;
|
|
153
|
-
reject(error);
|
|
154
|
-
}
|
|
155
|
-
},
|
|
156
|
-
fail: (error) => {
|
|
157
|
-
console.log(error);
|
|
158
|
-
reject(error);
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
// H5 input:file接收内容
|
|
162
|
-
} catch (e) {
|
|
163
|
-
console.log('uni报错,调用XMLHttpRequest', e);
|
|
164
|
-
const data = new FormData();
|
|
165
|
-
Object.keys(formData).forEach((key) => {
|
|
166
|
-
data.append(key, formData[key]);
|
|
167
|
-
});
|
|
168
|
-
data.append('file', file);
|
|
169
|
-
const xhr = new XMLHttpRequest();
|
|
170
|
-
xhr.open('POST', apiUrl, true);
|
|
171
|
-
xhr.onreadystatechange = () => {
|
|
172
|
-
if (xhr.readyState === XMLHttpRequest.DONE) {
|
|
173
|
-
if (xhr.status === 200) {
|
|
174
|
-
resolve(success);
|
|
175
|
-
} else {
|
|
176
|
-
reject(error);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
};
|
|
180
|
-
xhr.send(data);
|
|
181
|
-
}
|
|
182
|
-
})
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* 多文件上传
|
|
187
|
-
* @param {Array<File>} files
|
|
188
|
-
* @return Promise
|
|
189
|
-
*/
|
|
190
|
-
async uploadFiles(files) {
|
|
191
|
-
const successUrls = [];
|
|
192
|
-
const failUrls = [];
|
|
193
|
-
for (const file of files) {
|
|
194
|
-
const { data, head } = await this.uploadFile(file);
|
|
195
|
-
if (head.ret === 0) {
|
|
196
|
-
successUrls.push(data);
|
|
197
|
-
} else {
|
|
198
|
-
failUrls.push(data);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
return { successUrls, failUrls }
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// 按类型组装过参数
|
|
205
|
-
_buildParams(file) {
|
|
206
|
-
const opt = this.options;
|
|
207
|
-
const type = opt.type;
|
|
208
|
-
const params = {
|
|
209
|
-
name: file.name,
|
|
210
|
-
policy: opt.policy,
|
|
211
|
-
success_action_status: '200',
|
|
212
|
-
key: this._getUploadFilePath(file.name)
|
|
213
|
-
};
|
|
214
|
-
const endPoint = opt.endPoint.replace('https://', '').replace('http://', '');
|
|
215
|
-
if (UPLOADER_TYPE.TXYUN === type) {
|
|
216
|
-
params['q-ak'] = opt.accessKeyId;
|
|
217
|
-
params['q-signature'] = opt.signature; // 签名
|
|
218
|
-
params['q-sign-algorithm'] = 'sha1'; // 签名算法
|
|
219
|
-
params['q-key-time'] = opt.param['q-sign-time'];
|
|
220
|
-
// params['q-sign-time'] = opt.param['q-sign-time']
|
|
221
|
-
params.url = `https://${opt.bucketName}.cos.${endPoint}.myqcloud.com`;
|
|
222
|
-
} else if (UPLOADER_TYPE.HUAWEI === type) {
|
|
223
|
-
params.AccessKeyId = opt.accessKeyId;
|
|
224
|
-
params.signature = opt.signature;
|
|
225
|
-
params.url = `https://${opt.bucketName}.obs.${endPoint}.myhuaweicloud.com`;
|
|
226
|
-
} else {
|
|
227
|
-
params.signature = opt.signature;
|
|
228
|
-
params.OSSAccessKeyId = opt.accessKeyId;
|
|
229
|
-
params.bucket = opt.bucketName;
|
|
230
|
-
|
|
231
|
-
params.url = `https://${opt.bucketName}.${endPoint}`;
|
|
232
|
-
}
|
|
233
|
-
return params
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* 获取上传文件路径
|
|
238
|
-
* @param {String} name 文件名
|
|
239
|
-
* @returns
|
|
240
|
-
*/
|
|
241
|
-
_getUploadFilePath(name) {
|
|
242
|
-
const { useOriginalName, path } = this.options;
|
|
243
|
-
const lastIndexOf = name.lastIndexOf('.');
|
|
244
|
-
const originalName = name.substring(0, lastIndexOf);
|
|
245
|
-
const suffix = name.substring(lastIndexOf + 1);
|
|
246
|
-
const fileName = useOriginalName ? `/${originalName}` : '';
|
|
247
|
-
// 为了保证使用文件原名时,文件不被覆盖,仍然使用随机生成方式
|
|
248
|
-
// ${this._getNowDate()}/
|
|
249
|
-
return `${path}${this._getNowDate()}/${this._guid()}${fileName}.${suffix}`
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* 获取当前日期
|
|
254
|
-
* @returns YYYY-MM-DD
|
|
255
|
-
*/
|
|
256
|
-
_getNowDate() {
|
|
257
|
-
const date = new Date();
|
|
258
|
-
const year = date.getFullYear();
|
|
259
|
-
let month = date.getMonth() + 1;
|
|
260
|
-
let day = date.getDate();
|
|
261
|
-
if (month < 10) {
|
|
262
|
-
month = `0${month}`;
|
|
263
|
-
}
|
|
264
|
-
if (day < 10) {
|
|
265
|
-
day = `0${day}`;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
return `${year}
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* 生成guid,用于生成随机文件名
|
|
273
|
-
* @return String
|
|
274
|
-
*/
|
|
275
|
-
_guid() {
|
|
276
|
-
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
277
|
-
var r = (Math.random() * 16) | 0,
|
|
278
|
-
v = c == 'x' ? r : (r & 0x3) | 0x8;
|
|
279
|
-
return v.toString(16)
|
|
280
|
-
})
|
|
281
|
-
}
|
|
1
|
+
/*
|
|
2
|
+
* @Author: fuwenlong
|
|
3
|
+
* @Date: 2022-01-14 14:22:39
|
|
4
|
+
* @LastEditTime: 2022-07-21 11:44:54
|
|
5
|
+
* @LastEditors: zhangzhenfei
|
|
6
|
+
* @Description: 文件上传
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
// 对象存储类型
|
|
10
|
+
const UPLOADER_TYPE = {
|
|
11
|
+
ALIYUN: 'ALIYUN', // 阿里云
|
|
12
|
+
TXYUN: 'TXYUN', // 腾讯云
|
|
13
|
+
HUAWEI: 'HUAWEI' // 华为云
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const DEFAULT_OPTIONS = {
|
|
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
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {String} params.type
|
|
30
|
+
* @param {String} params.getOptionFun
|
|
31
|
+
*/
|
|
32
|
+
const UploaderInstance = (function () {
|
|
33
|
+
let instance;
|
|
34
|
+
return function (params) {
|
|
35
|
+
if (!instance) {
|
|
36
|
+
instance = new SadaisUploader(params);
|
|
37
|
+
}
|
|
38
|
+
return instance
|
|
39
|
+
}
|
|
40
|
+
})();
|
|
41
|
+
|
|
42
|
+
class SadaisUploader {
|
|
43
|
+
constructor(params) {
|
|
44
|
+
this._init(params);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 初始化
|
|
49
|
+
* @param {Object} options
|
|
50
|
+
*/
|
|
51
|
+
async _init(params) {
|
|
52
|
+
this.params = params;
|
|
53
|
+
// 不在初始化的时候调用接口,上传文件时再调用,故注释以下代码
|
|
54
|
+
// this._initOptions()
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 获取配置信息
|
|
59
|
+
*/
|
|
60
|
+
async initOptions(customParams) {
|
|
61
|
+
if (!this.params) this.params = {};
|
|
62
|
+
|
|
63
|
+
if (customParams) {
|
|
64
|
+
return (this.params = {
|
|
65
|
+
...this.params,
|
|
66
|
+
...customParams
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
let { type, getOptionFun } = this.params;
|
|
70
|
+
|
|
71
|
+
if (getOptionFun) {
|
|
72
|
+
const options = await getOptionFun(type);
|
|
73
|
+
if (!options) return
|
|
74
|
+
if (options.path && !options.path.endsWith('/')) {
|
|
75
|
+
options.path = options.path + '/';
|
|
76
|
+
}
|
|
77
|
+
this.options = { ...options, type };
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 是否超时
|
|
83
|
+
*/
|
|
84
|
+
_isTimeOut() {
|
|
85
|
+
// 比服务器timeOut时间提前10s过期
|
|
86
|
+
const timeout = this.options.timeOut - 1000 * 10;
|
|
87
|
+
return new Date().getTime() > timeout
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 文件上传
|
|
92
|
+
* @param { File | Array<file> } file 文件或文件数组
|
|
93
|
+
*/
|
|
94
|
+
async upload(file) {
|
|
95
|
+
if (!file) return
|
|
96
|
+
let files = Array.isArray(file) ? file : [file];
|
|
97
|
+
const result = [];
|
|
98
|
+
for (let i = 0; i < files.length; i++) {
|
|
99
|
+
const item = files[i];
|
|
100
|
+
const { data, head } = await this.uploadFile(item);
|
|
101
|
+
result.push({
|
|
102
|
+
index: i,
|
|
103
|
+
url: data,
|
|
104
|
+
ret: head.ret
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return result
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* 单文件上传
|
|
112
|
+
* @param {File} file
|
|
113
|
+
* @return Promise
|
|
114
|
+
*/
|
|
115
|
+
async uploadFile(file) {
|
|
116
|
+
if (!this.options || this._isTimeOut()) {
|
|
117
|
+
await this.initOptions();
|
|
118
|
+
}
|
|
119
|
+
if (!file.name && file.path) {
|
|
120
|
+
const suffix = file.path.substring(file.path.lastIndexOf('.'));
|
|
121
|
+
file.name = this._guid() + suffix;
|
|
122
|
+
}
|
|
123
|
+
return new Promise((resolve, reject) => {
|
|
124
|
+
const { domain } = this.options;
|
|
125
|
+
|
|
126
|
+
// 请求地址
|
|
127
|
+
const formData = this._buildParams(file);
|
|
128
|
+
|
|
129
|
+
const apiUrl = formData.url;
|
|
130
|
+
const uploadedFilePath = `${domain}${domain.endsWith('/') ? '' : '/'}${formData.key}`;
|
|
131
|
+
const success = {
|
|
132
|
+
data: uploadedFilePath,
|
|
133
|
+
head: { ret: 0, msg: 'success' }
|
|
134
|
+
};
|
|
135
|
+
const error = {
|
|
136
|
+
data: file.name,
|
|
137
|
+
head: { ret: 1, msg: 'fail' }
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
try {
|
|
141
|
+
// uniapp
|
|
142
|
+
uni.uploadFile({
|
|
143
|
+
url: apiUrl,
|
|
144
|
+
filePath: file.path, // uni api提供的blob格式图片地址
|
|
145
|
+
name: 'file',
|
|
146
|
+
formData,
|
|
147
|
+
success: (res) => {
|
|
148
|
+
const { statusCode } = res;
|
|
149
|
+
if (statusCode == 200) {
|
|
150
|
+
resolve(success);
|
|
151
|
+
} else {
|
|
152
|
+
error.head.msg = res;
|
|
153
|
+
reject(error);
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
fail: (error) => {
|
|
157
|
+
console.log(error);
|
|
158
|
+
reject(error);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
// H5 input:file接收内容
|
|
162
|
+
} catch (e) {
|
|
163
|
+
console.log('uni报错,调用XMLHttpRequest', e);
|
|
164
|
+
const data = new FormData();
|
|
165
|
+
Object.keys(formData).forEach((key) => {
|
|
166
|
+
data.append(key, formData[key]);
|
|
167
|
+
});
|
|
168
|
+
data.append('file', file);
|
|
169
|
+
const xhr = new XMLHttpRequest();
|
|
170
|
+
xhr.open('POST', apiUrl, true);
|
|
171
|
+
xhr.onreadystatechange = () => {
|
|
172
|
+
if (xhr.readyState === XMLHttpRequest.DONE) {
|
|
173
|
+
if (xhr.status === 200) {
|
|
174
|
+
resolve(success);
|
|
175
|
+
} else {
|
|
176
|
+
reject(error);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
xhr.send(data);
|
|
181
|
+
}
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* 多文件上传
|
|
187
|
+
* @param {Array<File>} files
|
|
188
|
+
* @return Promise
|
|
189
|
+
*/
|
|
190
|
+
async uploadFiles(files) {
|
|
191
|
+
const successUrls = [];
|
|
192
|
+
const failUrls = [];
|
|
193
|
+
for (const file of files) {
|
|
194
|
+
const { data, head } = await this.uploadFile(file);
|
|
195
|
+
if (head.ret === 0) {
|
|
196
|
+
successUrls.push(data);
|
|
197
|
+
} else {
|
|
198
|
+
failUrls.push(data);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return { successUrls, failUrls }
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// 按类型组装过参数
|
|
205
|
+
_buildParams(file) {
|
|
206
|
+
const opt = this.options;
|
|
207
|
+
const type = opt.type;
|
|
208
|
+
const params = {
|
|
209
|
+
name: file.name,
|
|
210
|
+
policy: opt.policy,
|
|
211
|
+
success_action_status: '200',
|
|
212
|
+
key: this._getUploadFilePath(file.name)
|
|
213
|
+
};
|
|
214
|
+
const endPoint = opt.endPoint.replace('https://', '').replace('http://', '');
|
|
215
|
+
if (UPLOADER_TYPE.TXYUN === type) {
|
|
216
|
+
params['q-ak'] = opt.accessKeyId;
|
|
217
|
+
params['q-signature'] = opt.signature; // 签名
|
|
218
|
+
params['q-sign-algorithm'] = 'sha1'; // 签名算法
|
|
219
|
+
params['q-key-time'] = opt.param['q-sign-time'];
|
|
220
|
+
// params['q-sign-time'] = opt.param['q-sign-time']
|
|
221
|
+
params.url = `https://${opt.bucketName}.cos.${endPoint}.myqcloud.com`;
|
|
222
|
+
} else if (UPLOADER_TYPE.HUAWEI === type) {
|
|
223
|
+
params.AccessKeyId = opt.accessKeyId;
|
|
224
|
+
params.signature = opt.signature;
|
|
225
|
+
params.url = `https://${opt.bucketName}.obs.${endPoint}.myhuaweicloud.com`;
|
|
226
|
+
} else {
|
|
227
|
+
params.signature = opt.signature;
|
|
228
|
+
params.OSSAccessKeyId = opt.accessKeyId;
|
|
229
|
+
params.bucket = opt.bucketName;
|
|
230
|
+
|
|
231
|
+
params.url = `https://${opt.bucketName}.${endPoint}`;
|
|
232
|
+
}
|
|
233
|
+
return params
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* 获取上传文件路径
|
|
238
|
+
* @param {String} name 文件名
|
|
239
|
+
* @returns
|
|
240
|
+
*/
|
|
241
|
+
_getUploadFilePath(name) {
|
|
242
|
+
const { useOriginalName, path } = this.options;
|
|
243
|
+
const lastIndexOf = name.lastIndexOf('.');
|
|
244
|
+
const originalName = name.substring(0, lastIndexOf);
|
|
245
|
+
const suffix = name.substring(lastIndexOf + 1);
|
|
246
|
+
const fileName = useOriginalName ? `/${originalName}` : '';
|
|
247
|
+
// 为了保证使用文件原名时,文件不被覆盖,仍然使用随机生成方式
|
|
248
|
+
// ${this._getNowDate()}/
|
|
249
|
+
return `${path}${this._getNowDate()}/${this._guid()}${fileName}.${suffix}`
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* 获取当前日期
|
|
254
|
+
* @returns YYYY-MM-DD
|
|
255
|
+
*/
|
|
256
|
+
_getNowDate() {
|
|
257
|
+
const date = new Date();
|
|
258
|
+
const year = date.getFullYear();
|
|
259
|
+
let month = date.getMonth() + 1;
|
|
260
|
+
let day = date.getDate();
|
|
261
|
+
if (month < 10) {
|
|
262
|
+
month = `0${month}`;
|
|
263
|
+
}
|
|
264
|
+
if (day < 10) {
|
|
265
|
+
day = `0${day}`;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return `${year}${month}${day}`
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* 生成guid,用于生成随机文件名
|
|
273
|
+
* @return String
|
|
274
|
+
*/
|
|
275
|
+
_guid() {
|
|
276
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
277
|
+
var r = (Math.random() * 16) | 0,
|
|
278
|
+
v = c == 'x' ? r : (r & 0x3) | 0x8;
|
|
279
|
+
return v.toString(16)
|
|
280
|
+
})
|
|
281
|
+
}
|
|
282
282
|
}
|
|
283
283
|
|
|
284
284
|
export { DEFAULT_OPTIONS, UPLOADER_TYPE, UploaderInstance };
|
package/dist/index.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";function e(e,r){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!n){if(Array.isArray(e)||(n=function(e,r){if(!e)return;if("string"==typeof e)return t(e,r);var n=Object.prototype.toString.call(e).slice(8,-1);"Object"===n&&e.constructor&&(n=e.constructor.name);if("Map"===n||"Set"===n)return Array.from(e);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return t(e,r)}(e))||r&&e&&"number"==typeof e.length){n&&(e=n);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(){n=n.call(e)},n:function(){var e=n.next();return u=e.done,e},e:function(e){c=!0,o=e},f:function(){try{u||null==n.return||n.return()}finally{if(c)throw o}}}}function t(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function r(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function n(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?r(Object(n),!0).forEach((function(t){a(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):r(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function a(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function i(e,t,r,n,a,i,o){try{var u=e[i](o),c=u.value}catch(e){return void r(e)}u.done?t(c):Promise.resolve(c).then(n,a)}function o(e){return function(){var t=this,r=arguments;return new Promise((function(n,a){var o=e.apply(t,r);function u(e){i(o,n,a,u,c,"next",e)}function c(e){i(o,n,a,u,c,"throw",e)}u(void 0)}))}}function u(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}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 r,a,i,c,p,l,f,h;return r=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;case 1:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"initOptions",value:(f=o(regeneratorRuntime.mark((function e(t){var r,a,i,o;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(this.params||(this.params={}),!t){e.next=3;break}return e.abrupt("return",this.params=n(n({},this.params),t));case 3:if(r=this.params,a=r.type,!(i=r.getOptionFun)){e.next=12;break}return e.next=7,i(a);case 7:if(o=e.sent){e.next=10;break}return e.abrupt("return");case 10:o.path&&!o.path.endsWith("/")&&(o.path=o.path+"/"),this.options=n(n({},o),{},{type:a});case 12:case"end":return e.stop()}}),e,this)}))),function(e){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 r,n,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:r=Array.isArray(t)?t:[t],n=[],a=0;case 5:if(!(a<r.length)){e.next=16;break}return i=r[a],e.next=9,this.uploadFile(i);case 9:o=e.sent,u=o.data,c=o.head,n.push({index:a,url:u,ret:c.ret});case 13:a++,e.next=5;break;case 16:return e.abrupt("return",n);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 r,n=this;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(this.options&&!this._isTimeOut()){e.next=3;break}return e.next=3,this.initOptions();case 3:return!t.name&&t.path&&(r=t.path.substring(t.path.lastIndexOf(".")),t.name=this._guid()+r),e.abrupt("return",new Promise((function(e,r){var a=n.options.domain,i=n._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,r(c))},fail:function(e){console.log(e),r(e)}})}catch(n){console.log("uni报错,调用XMLHttpRequest",n);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):r(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(r){var n,a,i,o,u,c,s;return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:n=[],a=[],i=e(r),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?n.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:n,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,r=t.type,n={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===r?(n["q-ak"]=t.accessKeyId,n["q-signature"]=t.signature,n["q-sign-algorithm"]="sha1",n["q-key-time"]=t.param["q-sign-time"],n.url="https://".concat(t.bucketName,".cos.").concat(a,".myqcloud.com")):s.HUAWEI===r?(n.AccessKeyId=t.accessKeyId,n.signature=t.signature,n.url="https://".concat(t.bucketName,".obs.").concat(a,".myhuaweicloud.com")):(n.signature=t.signature,n.OSSAccessKeyId=t.accessKeyId,n.bucket=t.bucketName,n.url="https://".concat(t.bucketName,".").concat(a)),n}},{key:"_getUploadFilePath",value:function(e){var t=this.options,r=t.useOriginalName,n=t.path,a=e.lastIndexOf("."),i=e.substring(0,a),o=e.substring(a+1),u=r?"/".concat(i):"";return"".concat(n).concat(this._getNowDate(),"/").concat(this._guid()).concat(u,".").concat(o)}},{key:"_getNowDate",value:function(){var e=new Date,t=e.getFullYear(),r=e.getMonth()+1,n=e.getDate();return r<10&&(r="0".concat(r)),n<10&&(n="0".concat(n)),"".concat(t
|
|
1
|
+
"use strict";function e(e,r){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!n){if(Array.isArray(e)||(n=function(e,r){if(!e)return;if("string"==typeof e)return t(e,r);var n=Object.prototype.toString.call(e).slice(8,-1);"Object"===n&&e.constructor&&(n=e.constructor.name);if("Map"===n||"Set"===n)return Array.from(e);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return t(e,r)}(e))||r&&e&&"number"==typeof e.length){n&&(e=n);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(){n=n.call(e)},n:function(){var e=n.next();return u=e.done,e},e:function(e){c=!0,o=e},f:function(){try{u||null==n.return||n.return()}finally{if(c)throw o}}}}function t(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function r(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function n(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?r(Object(n),!0).forEach((function(t){a(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):r(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function a(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function i(e,t,r,n,a,i,o){try{var u=e[i](o),c=u.value}catch(e){return void r(e)}u.done?t(c):Promise.resolve(c).then(n,a)}function o(e){return function(){var t=this,r=arguments;return new Promise((function(n,a){var o=e.apply(t,r);function u(e){i(o,n,a,u,c,"next",e)}function c(e){i(o,n,a,u,c,"throw",e)}u(void 0)}))}}function u(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}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 r,a,i,c,p,l,f,h;return r=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;case 1:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"initOptions",value:(f=o(regeneratorRuntime.mark((function e(t){var r,a,i,o;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(this.params||(this.params={}),!t){e.next=3;break}return e.abrupt("return",this.params=n(n({},this.params),t));case 3:if(r=this.params,a=r.type,!(i=r.getOptionFun)){e.next=12;break}return e.next=7,i(a);case 7:if(o=e.sent){e.next=10;break}return e.abrupt("return");case 10:o.path&&!o.path.endsWith("/")&&(o.path=o.path+"/"),this.options=n(n({},o),{},{type:a});case 12:case"end":return e.stop()}}),e,this)}))),function(e){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 r,n,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:r=Array.isArray(t)?t:[t],n=[],a=0;case 5:if(!(a<r.length)){e.next=16;break}return i=r[a],e.next=9,this.uploadFile(i);case 9:o=e.sent,u=o.data,c=o.head,n.push({index:a,url:u,ret:c.ret});case 13:a++,e.next=5;break;case 16:return e.abrupt("return",n);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 r,n=this;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(this.options&&!this._isTimeOut()){e.next=3;break}return e.next=3,this.initOptions();case 3:return!t.name&&t.path&&(r=t.path.substring(t.path.lastIndexOf(".")),t.name=this._guid()+r),e.abrupt("return",new Promise((function(e,r){var a=n.options.domain,i=n._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,r(c))},fail:function(e){console.log(e),r(e)}})}catch(n){console.log("uni报错,调用XMLHttpRequest",n);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):r(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(r){var n,a,i,o,u,c,s;return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:n=[],a=[],i=e(r),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?n.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:n,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,r=t.type,n={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===r?(n["q-ak"]=t.accessKeyId,n["q-signature"]=t.signature,n["q-sign-algorithm"]="sha1",n["q-key-time"]=t.param["q-sign-time"],n.url="https://".concat(t.bucketName,".cos.").concat(a,".myqcloud.com")):s.HUAWEI===r?(n.AccessKeyId=t.accessKeyId,n.signature=t.signature,n.url="https://".concat(t.bucketName,".obs.").concat(a,".myhuaweicloud.com")):(n.signature=t.signature,n.OSSAccessKeyId=t.accessKeyId,n.bucket=t.bucketName,n.url="https://".concat(t.bucketName,".").concat(a)),n}},{key:"_getUploadFilePath",value:function(e){var t=this.options,r=t.useOriginalName,n=t.path,a=e.lastIndexOf("."),i=e.substring(0,a),o=e.substring(a+1),u=r?"/".concat(i):"";return"".concat(n).concat(this._getNowDate(),"/").concat(this._guid()).concat(u,".").concat(o)}},{key:"_getNowDate",value:function(){var e=new Date,t=e.getFullYear(),r=e.getMonth()+1,n=e.getDate();return r<10&&(r="0".concat(r)),n<10&&(n="0".concat(n)),"".concat(t).concat(r).concat(n)}},{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(r.prototype,a),i&&u(r,i),Object.defineProperty(r,"prototype",{writable:!1}),t}();exports.DEFAULT_OPTIONS={},exports.UPLOADER_TYPE=s,exports.UploaderInstance=p;
|