@zfqh/uniapp 0.3.17 → 0.3.18
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/CHANGELOG.md +8 -0
- package/docs/api/utils/cos.md +55 -2
- package/lib/utils/cos.d.ts +3 -1
- package/lib/utils/cos.js +5 -2
- package/package.json +2 -3
- package/LICENSE +0 -21
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [0.3.18](http://10.10.10.16/caoben/front-end/compare/@zfqh/uniapp@0.3.17...@zfqh/uniapp@0.3.18) (2026-09-16)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @zfqh/uniapp
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
6
14
|
## [0.3.17](http://10.10.10.16/caoben/front-end/compare/@zfqh/uniapp@0.3.16...@zfqh/uniapp@0.3.17) (2026-09-03)
|
|
7
15
|
|
|
8
16
|
**Note:** Version bump only for package @zfqh/uniapp
|
package/docs/api/utils/cos.md
CHANGED
|
@@ -45,7 +45,52 @@ const { url } = await cos.uploadFile(file, { key: 'avatar/user-1.png' });
|
|
|
45
45
|
| H5 File 对象 | 直接传入 File |
|
|
46
46
|
| 自定义文件名 | `{ name: 'photo.jpg', path: '...' }` |
|
|
47
47
|
|
|
48
|
-
未指定 `key` 时会自动生成:`<path><
|
|
48
|
+
未指定 `key` 时会自动生成:`<path><yyyymmdd>/<hhMMss><guid>.<ext>`(`yyyymmdd` 用于按日期分目录,`hhMMss` 便于看出日内上传先后)
|
|
49
|
+
|
|
50
|
+
## 取消上传
|
|
51
|
+
|
|
52
|
+
传入 `getTask` 可以拿到 `uni.uploadFile` 返回的 `UploadTask` 实例,需要时调用 `task.abort()` 取消本次上传:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import { cos } from '@zfqh/uniapp';
|
|
56
|
+
|
|
57
|
+
let task;
|
|
58
|
+
|
|
59
|
+
const { url } = await cos.uploadFile(file, {
|
|
60
|
+
path: 'avatar/',
|
|
61
|
+
getTask(obj) {
|
|
62
|
+
task = obj;
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// 需要取消时(例如用户点了删除、离开页面)
|
|
67
|
+
task?.abort();
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
`getTask(obj)` 的写法也可以直接使用:
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
let task;
|
|
74
|
+
|
|
75
|
+
cos.uploadFile({
|
|
76
|
+
Bucket: 'xxx-1250000000',
|
|
77
|
+
Region: 'ap-nanjing',
|
|
78
|
+
Key: 'avatar/user-1.png',
|
|
79
|
+
FilePath: tempFilePath,
|
|
80
|
+
getTask(obj) {
|
|
81
|
+
task = obj;
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### 注意事项
|
|
87
|
+
|
|
88
|
+
- `getTask` 在**每次** `uni.uploadFile` 调用时都会回调。token 过期(`ExpiredToken`)触发内部重试时会创建新的 task 并再次回调,因此请始终使用**最近一次**回调收到的 task,不要缓存第一次的。
|
|
89
|
+
- 取消会让该次上传的 Promise reject,但各端错误文案不一致:H5 为 `uploadFile:fail abort`,App 为 `uploadFile:fail statusCode: 0`(**不含 `abort` 字样**),微信小程序通常为 `uploadFile:fail abort`。请不要通过匹配错误文案判断是否被取消。
|
|
90
|
+
- App 端调用 `abort()` 后不保证触发 `fail` 回调,Promise 可能一直 pending,取消后的 UI 状态需要业务自行切换。
|
|
91
|
+
- 取消支持的范围为 App / H5 / 微信小程序。支付宝小程序 `my.uploadFile` 的 `abort` 需要传参对象,无参 `abort()` 可能无效。
|
|
92
|
+
- 上传已完成后再调用 `abort()` 是空操作。
|
|
93
|
+
- H5 端请勿调用 `task.onHeadersReceived` / `task.offHeadersReceived`(H5 实现为直接抛错)。
|
|
49
94
|
|
|
50
95
|
## 完整上传流程
|
|
51
96
|
|
|
@@ -131,6 +176,14 @@ await checkRefreshToken();
|
|
|
131
176
|
| --- | --- | --- |
|
|
132
177
|
| `checkRefreshToken` | `export async function checkRefreshToken(): Promise<boolean> {` | 调用 upload.request({ mode: "cos" }) 获取并缓存 COS 临时密钥;1 秒内并发上传复用同一个请求。 |
|
|
133
178
|
| `uploadFile` | `export async function uploadFile(file: any, opts: UploadFileOptions = {}): Promise<{ url: string }> {` | 上传文件到腾讯 COS,自动生成 key、刷新过期 token,并返回 https url。 |
|
|
179
|
+
|
|
180
|
+
`UploadFileOptions` 支持的选项:
|
|
181
|
+
|
|
182
|
+
| 选项 | 类型 | 说明 |
|
|
183
|
+
| --- | --- | --- |
|
|
184
|
+
| `path` | `string` | key 前缀(子目录),如 `'avatar/'`。 |
|
|
185
|
+
| `key` | `string` | 完整 key,传入时忽略自动生成的 key。 |
|
|
186
|
+
| `getTask` | `(task: UniNamespace.UploadTask) => void` | 获取本次上传的 `UploadTask`,可用 `task.abort()` 取消。每次上传尝试(含 token 过期重试)都会回调。 |
|
|
134
187
|
| `cosCompress` | `export function cosCompress(url: string, rule: string, type?: string): string {` | 基于腾讯云 COS `imageMogr2` 拼接图片压缩 URL。`rule` 为尺寸规则(如 `'200x'`、`'200x200'`),`type` 可选 `'cut'`(裁剪)或默认(缩放)。 |
|
|
135
188
|
|
|
136
189
|
`cosCompress` 常用示例:
|
|
@@ -251,7 +304,7 @@ defineConfig({
|
|
|
251
304
|
未指定 `key` 时会自动生成:
|
|
252
305
|
|
|
253
306
|
```text
|
|
254
|
-
<path><
|
|
307
|
+
<path><yyyymmdd>/<hhMMss><guid>.<ext>
|
|
255
308
|
```
|
|
256
309
|
|
|
257
310
|
## 图片压缩 URL
|
package/lib/utils/cos.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { cosCompress } from './index';
|
|
2
2
|
export declare function checkRefreshToken(): Promise<boolean>;
|
|
3
|
-
interface UploadFileOptions {
|
|
3
|
+
export interface UploadFileOptions {
|
|
4
4
|
key?: string;
|
|
5
5
|
path?: string;
|
|
6
|
+
/** 获取上传任务实例,可用于 task.abort() 取消上传;token 过期重试时会再次回调 */
|
|
7
|
+
getTask?: ((task: UniNamespace.UploadTask) => void) | ((task: any) => void);
|
|
6
8
|
}
|
|
7
9
|
export declare function uploadFile(file: any, opts?: UploadFileOptions): Promise<{
|
|
8
10
|
url: string;
|
package/lib/utils/cos.js
CHANGED
|
@@ -54,7 +54,7 @@ class COS {
|
|
|
54
54
|
'q-key-time': qKeyTime,
|
|
55
55
|
'q-signature': qSignature,
|
|
56
56
|
};
|
|
57
|
-
uni.uploadFile({
|
|
57
|
+
const task = uni.uploadFile({
|
|
58
58
|
url: `https://${cosHost}`,
|
|
59
59
|
filePath: FilePath,
|
|
60
60
|
file: File,
|
|
@@ -102,6 +102,7 @@ class COS {
|
|
|
102
102
|
});
|
|
103
103
|
},
|
|
104
104
|
});
|
|
105
|
+
params.getTask?.(task);
|
|
105
106
|
});
|
|
106
107
|
}
|
|
107
108
|
}
|
|
@@ -114,12 +115,14 @@ export async function uploadFile(file, opts = {}) {
|
|
|
114
115
|
ext = match[0];
|
|
115
116
|
}
|
|
116
117
|
}
|
|
117
|
-
const
|
|
118
|
+
const [day, clock] = uni.$uv.timeFormat(new Date().getTime(), 'yyyymmdd hhMMss').split(' ');
|
|
119
|
+
const key = `${opts.path || ''}${day}/${clock}${uni.$uv.guid(30)}${ext}`;
|
|
118
120
|
await checkRefreshToken();
|
|
119
121
|
const params = {
|
|
120
122
|
Bucket: data.bucket,
|
|
121
123
|
Region: data.region,
|
|
122
124
|
Key: opts.key || key,
|
|
125
|
+
getTask: opts.getTask,
|
|
123
126
|
};
|
|
124
127
|
if (file.path) {
|
|
125
128
|
params.FilePath = file.path;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zfqh/uniapp",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.18",
|
|
4
4
|
"description": "uniapp公共模块",
|
|
5
5
|
"author": "yinjiazeng@163.com",
|
|
6
6
|
"license": "MIT",
|
|
@@ -45,6 +45,5 @@
|
|
|
45
45
|
"@types/crypto-js": "^4.2.2",
|
|
46
46
|
"@types/qs": "^6.9.16",
|
|
47
47
|
"@types/url-parse": "^1.4.11"
|
|
48
|
-
}
|
|
49
|
-
"gitHead": "d56ac10822cd04b832695122cc7daa21e67d6628"
|
|
48
|
+
}
|
|
50
49
|
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 aniu
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|