@pnt-team/pnt-component-frontend 0.0.92 → 0.0.96
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 +26 -4
- package/lib/cjs/api/cos.d.ts +103 -0
- package/lib/cjs/api/cos.js +137 -0
- package/lib/cjs/components/PntCosUploader/index.d.ts +135 -0
- package/lib/cjs/components/PntCosUploader/index.js +209 -0
- package/lib/cjs/components/PntImage/index.d.ts +9 -3
- package/lib/cjs/components/PntImage/index.js +9 -1
- package/lib/cjs/components/PntSecureBox/index.js +8 -3
- package/lib/cjs/components/PntUpload/index.d.ts +149 -0
- package/lib/cjs/components/PntUpload/index.js +228 -0
- package/lib/cjs/components/PntUpload/index.scss +101 -0
- package/lib/cjs/hooks/useHighSecurity.d.ts +55 -15
- package/lib/cjs/hooks/useHighSecurity.js +163 -35
- package/lib/cjs/index.d.ts +6 -0
- package/lib/cjs/index.js +18 -2
- package/lib/esm/api/cos.d.ts +103 -0
- package/lib/esm/api/cos.js +189 -0
- package/lib/esm/components/PntCosUploader/index.d.ts +135 -0
- package/lib/esm/components/PntCosUploader/index.js +289 -0
- package/lib/esm/components/PntImage/index.d.ts +9 -3
- package/lib/esm/components/PntImage/index.js +22 -3
- package/lib/esm/components/PntSecureBox/index.js +9 -3
- package/lib/esm/components/PntUpload/index.d.ts +149 -0
- package/lib/esm/components/PntUpload/index.js +276 -0
- package/lib/esm/components/PntUpload/index.scss +101 -0
- package/lib/esm/hooks/useHighSecurity.d.ts +55 -15
- package/lib/esm/hooks/useHighSecurity.js +174 -32
- package/lib/esm/index.d.ts +6 -0
- package/lib/esm/index.js +9 -1
- package/lib/umd/pnt-component-frontend.min.css +1 -1
- package/lib/umd/pnt-component-frontend.min.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { type CosScene, type GetCosCredentialParams, type NormalizeCredential, type NormalizedCosCredential } from "../../api/cos";
|
|
2
|
+
import { usePntConfig } from "../../config";
|
|
3
|
+
import type { AxiosInstance } from 'axios';
|
|
4
|
+
/**
|
|
5
|
+
* 文件名生成策略
|
|
6
|
+
* @description 内置三种策略;如需自定义可传函数
|
|
7
|
+
* - uuid: UUID v4 + 原扩展名(默认,最简单)
|
|
8
|
+
* - md5: 文件内容 MD5 + 原扩展名(与 pnt-tools CosApi 原策略一致)
|
|
9
|
+
* - timestamp: 时间戳(36进制)+自增计数器 + 原扩展名(与 SDK 定制场景原策略一致)
|
|
10
|
+
*/
|
|
11
|
+
export type FileNameStrategy = 'uuid' | 'md5' | 'timestamp' | ((file: File) => string | Promise<string>);
|
|
12
|
+
/**
|
|
13
|
+
* COS 上传配置
|
|
14
|
+
*/
|
|
15
|
+
export interface PntCosUploadOptions {
|
|
16
|
+
/**
|
|
17
|
+
* 业务场景,决定走哪个凭证接口
|
|
18
|
+
* @description 内置 4 个 scene,消费方可传 string 自定义并配合 getCredential 使用
|
|
19
|
+
*/
|
|
20
|
+
scene: CosScene;
|
|
21
|
+
/**
|
|
22
|
+
* 业务类型路径 key
|
|
23
|
+
* @description 通用场景透传给后端 path map 选取对应业务路径;
|
|
24
|
+
* sdk_customize 场景作为受控目录(filePath)下的子目录继续拼接
|
|
25
|
+
*/
|
|
26
|
+
pathKey?: string;
|
|
27
|
+
/**
|
|
28
|
+
* 业务上下文参数(gameId/env/updType 等)
|
|
29
|
+
* @description 按 scene 不同传递给后端;default/default_download 下 gameId 自动映射到 x-service-type 请求头
|
|
30
|
+
*/
|
|
31
|
+
context?: Record<string, string | number>;
|
|
32
|
+
/**
|
|
33
|
+
* 网关 audience
|
|
34
|
+
* @default '/player-network'
|
|
35
|
+
* @description 账号管理等子系统需覆盖为对应 audience(如 '/laas-pnt')
|
|
36
|
+
*/
|
|
37
|
+
audience?: string;
|
|
38
|
+
/**
|
|
39
|
+
* 文件名生成策略
|
|
40
|
+
* @default 'uuid'
|
|
41
|
+
*/
|
|
42
|
+
fileNameStrategy?: FileNameStrategy;
|
|
43
|
+
/**
|
|
44
|
+
* 是否走 CDN 域名拼接
|
|
45
|
+
* @description true 且凭证返回 cosHost 时用 cosHost + cosKey;否则用 COS putObject 返回的 Location
|
|
46
|
+
* @default true
|
|
47
|
+
*/
|
|
48
|
+
useCdn?: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* 自定义凭证获取函数(覆盖内置 scene 路由)
|
|
51
|
+
* @description 后端接口非内置 scene 时使用;返回归一化后的凭证
|
|
52
|
+
*/
|
|
53
|
+
getCredential?: (http: ReturnType<typeof usePntConfig>['http'], params: GetCosCredentialParams) => Promise<NormalizedCosCredential>;
|
|
54
|
+
/**
|
|
55
|
+
* 自定义凭证响应归一化
|
|
56
|
+
* @description 后端字段不标准时使用;入参为内置 scene 接口返回的原始 data,需返回归一化凭证
|
|
57
|
+
*/
|
|
58
|
+
normalizeCredential?: NormalizeCredential;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* COS 上传结果
|
|
62
|
+
*/
|
|
63
|
+
export interface PntCosUploadResult {
|
|
64
|
+
/** 上传后的可访问 URL */
|
|
65
|
+
fileUrl: string;
|
|
66
|
+
/** COS 对象 Key */
|
|
67
|
+
cosKey: string;
|
|
68
|
+
/** 原始文件名 */
|
|
69
|
+
fileName: string;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* 单次请求覆盖参数
|
|
73
|
+
* @description 上传/下载入口的 gameId/task_id/pathKey 常来自表单或表格行数据,
|
|
74
|
+
* 可通过该参数在单次调用覆盖 hook 初始化时的配置
|
|
75
|
+
*/
|
|
76
|
+
export interface PntCosRequestOverrides {
|
|
77
|
+
/** 覆盖业务上下文(与初始化 context 浅合并,如行级 gameId/task_id) */
|
|
78
|
+
context?: Record<string, string | number>;
|
|
79
|
+
/** 覆盖网关 audience */
|
|
80
|
+
audience?: string;
|
|
81
|
+
/** 覆盖凭证场景 */
|
|
82
|
+
scene?: CosScene;
|
|
83
|
+
/** 覆盖业务路径 key */
|
|
84
|
+
pathKey?: string;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* COS 上传器实例
|
|
88
|
+
*/
|
|
89
|
+
export interface PntCosUploader {
|
|
90
|
+
/**
|
|
91
|
+
* 上传文件到 COS
|
|
92
|
+
* @param file 原始文件
|
|
93
|
+
* @param onProgress 上传进度回调(0~100)
|
|
94
|
+
* @param overrides 单次调用覆盖配置(表单/行级 gameId 等)
|
|
95
|
+
*/
|
|
96
|
+
upload: (file: File, onProgress?: (percent: number) => void, overrides?: PntCosRequestOverrides) => Promise<PntCosUploadResult>;
|
|
97
|
+
/**
|
|
98
|
+
* 获取文件预签名下载 URL(临时)
|
|
99
|
+
* @param cosKey COS 对象 Key,或完整 URL(自动剥离域名)
|
|
100
|
+
* @param overrides 单次调用覆盖配置(行级 gameId/task_id 等)
|
|
101
|
+
*/
|
|
102
|
+
getDownloadUrl: (cosKey: string, overrides?: PntCosRequestOverrides) => Promise<string>;
|
|
103
|
+
}
|
|
104
|
+
export type { CosScene, GetCosCredentialParams, NormalizeCredential, NormalizedCosCredential, };
|
|
105
|
+
export { normalizeSnakeCredential } from "../../api/cos";
|
|
106
|
+
/**
|
|
107
|
+
* 创建 COS 直传器实例(不依赖 React)
|
|
108
|
+
* @description 与 usePntCosUploader 共用同一套核心逻辑,适用于 class 兼容层、
|
|
109
|
+
* 工具函数、非 React 入口等场景;React 组件请优先使用 usePntCosUploader
|
|
110
|
+
*
|
|
111
|
+
* @param http 消费方 axios 实例(约定响应拦截器已解包 response.data)
|
|
112
|
+
* @param options 上传配置(固定值),或返回配置的 getter(需要动态配置时使用)
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```ts
|
|
116
|
+
* const uploader = createPntCosUploader(http, { scene: 'default', pathKey: 'marketing_icon' });
|
|
117
|
+
* const { fileUrl } = await uploader.upload(file);
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
export declare const createPntCosUploader: (http: AxiosInstance, options: PntCosUploadOptions | (() => PntCosUploadOptions)) => PntCosUploader;
|
|
121
|
+
/**
|
|
122
|
+
* COS 直传 Service Hook
|
|
123
|
+
* @description 内置凭证获取 + COS SDK 初始化 + 文件名策略 + URL 拼接,
|
|
124
|
+
* 收敛各项目重复的 4 份自实现;http 实例由 PntProvider 注入
|
|
125
|
+
*
|
|
126
|
+
* @param options 上传配置
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```ts
|
|
130
|
+
* const uploader = usePntCosUploader({ scene: 'default', pathKey: 'marketing_icon' });
|
|
131
|
+
* const { fileUrl, cosKey } = await uploader.upload(file, (p) => setProgress(p));
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
export declare const usePntCosUploader: (options: PntCosUploadOptions) => PntCosUploader;
|
|
135
|
+
export default usePntCosUploader;
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { fetchCosCredential } from "../../api/cos";
|
|
2
|
+
import { usePntConfig } from "../../config";
|
|
3
|
+
import COS from 'cos-js-sdk-v5';
|
|
4
|
+
import CryptoJS from 'crypto-js';
|
|
5
|
+
import { useMemo, useRef } from 'react';
|
|
6
|
+
import { v4 as uuidV4 } from 'uuid';
|
|
7
|
+
|
|
8
|
+
// ==========================================
|
|
9
|
+
// 类型定义
|
|
10
|
+
// ==========================================
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 文件名生成策略
|
|
14
|
+
* @description 内置三种策略;如需自定义可传函数
|
|
15
|
+
* - uuid: UUID v4 + 原扩展名(默认,最简单)
|
|
16
|
+
* - md5: 文件内容 MD5 + 原扩展名(与 pnt-tools CosApi 原策略一致)
|
|
17
|
+
* - timestamp: 时间戳(36进制)+自增计数器 + 原扩展名(与 SDK 定制场景原策略一致)
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* COS 上传配置
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* COS 上传结果
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 单次请求覆盖参数
|
|
30
|
+
* @description 上传/下载入口的 gameId/task_id/pathKey 常来自表单或表格行数据,
|
|
31
|
+
* 可通过该参数在单次调用覆盖 hook 初始化时的配置
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* COS 上传器实例
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
export { normalizeSnakeCredential } from "../../api/cos";
|
|
39
|
+
|
|
40
|
+
// ==========================================
|
|
41
|
+
// 工具函数
|
|
42
|
+
// ==========================================
|
|
43
|
+
|
|
44
|
+
/** 去除首尾斜杠 */
|
|
45
|
+
const trimSlash = (seg, leading = true) => seg.replace(/\/$/, '').replace(leading ? /^\// : '', '');
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 提取文件扩展名(含点号)
|
|
49
|
+
*/
|
|
50
|
+
const getFileExt = filename => {
|
|
51
|
+
const idx = filename.lastIndexOf('.');
|
|
52
|
+
return idx > -1 ? filename.slice(idx) : '';
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* timestamp 策略计数器(同一会话内自增,避免同毫秒碰撞)
|
|
57
|
+
*/
|
|
58
|
+
let _timestampCounter = 0;
|
|
59
|
+
const generateTimestampName = () => `${Date.now().toString(36)}${(_timestampCounter++).toString(36)}`;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 计算文件内容 MD5(基于 crypto-js,与 pnt-tools CosApi 原策略一致)
|
|
63
|
+
*/
|
|
64
|
+
const computeFileMd5 = async file => {
|
|
65
|
+
const buffer = await file.arrayBuffer();
|
|
66
|
+
return CryptoJS.MD5(CryptoJS.lib.WordArray.create(buffer)).toString();
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 根据策略生成文件名(不含路径前缀)
|
|
71
|
+
*/
|
|
72
|
+
const generateFileName = async (file, strategy) => {
|
|
73
|
+
if (typeof strategy === 'function') {
|
|
74
|
+
return strategy(file);
|
|
75
|
+
}
|
|
76
|
+
const ext = getFileExt(file.name);
|
|
77
|
+
switch (strategy) {
|
|
78
|
+
case 'md5':
|
|
79
|
+
{
|
|
80
|
+
const md5 = await computeFileMd5(file);
|
|
81
|
+
return `${md5}${ext}`;
|
|
82
|
+
}
|
|
83
|
+
case 'timestamp':
|
|
84
|
+
return `${generateTimestampName()}${ext}`;
|
|
85
|
+
case 'uuid':
|
|
86
|
+
default:
|
|
87
|
+
return `${uuidV4()}${ext}`;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 拼接 COS Key 的目录前缀
|
|
93
|
+
* @description 优先级:filePath(可继续拼 pathKey 子目录) > path[pathKey] > 空
|
|
94
|
+
*/
|
|
95
|
+
const resolveBasePath = (cred, pathKey) => {
|
|
96
|
+
if (cred.filePath) {
|
|
97
|
+
// sdk_customize:受控目录前缀 + 业务自定义子目录
|
|
98
|
+
const prefix = cred.filePath.replace(/\/$/, '');
|
|
99
|
+
return pathKey ? `${prefix}/${trimSlash(pathKey)}` : prefix;
|
|
100
|
+
}
|
|
101
|
+
if (pathKey && cred.path) {
|
|
102
|
+
return cred.path[pathKey] || '';
|
|
103
|
+
}
|
|
104
|
+
return '';
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 从 URL 或完整 Key 中剥离域名,得到纯 COS Key
|
|
109
|
+
*/
|
|
110
|
+
const stripHost = key => key.replace(/^https?:\/\/[^/]+/, '');
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 用归一化凭证构造 COS SDK 实例
|
|
114
|
+
*/
|
|
115
|
+
const createCosClient = cred => new COS({
|
|
116
|
+
getAuthorization: (_, callback) => {
|
|
117
|
+
callback({
|
|
118
|
+
TmpSecretId: cred.secretId,
|
|
119
|
+
TmpSecretKey: cred.secretKey,
|
|
120
|
+
SecurityToken: cred.sessionToken,
|
|
121
|
+
StartTime: cred.startTime,
|
|
122
|
+
ExpiredTime: cred.expiredTime
|
|
123
|
+
});
|
|
124
|
+
},
|
|
125
|
+
Protocol: 'https:',
|
|
126
|
+
FileParallelLimit: 3,
|
|
127
|
+
ChunkParallelLimit: 3,
|
|
128
|
+
ChunkSize: 1024 * 1024,
|
|
129
|
+
ProgressInterval: 1
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// ==========================================
|
|
133
|
+
// createPntCosUploader 工厂(框架无关,可在非 React 上下文使用)
|
|
134
|
+
// ==========================================
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* 创建 COS 直传器实例(不依赖 React)
|
|
138
|
+
* @description 与 usePntCosUploader 共用同一套核心逻辑,适用于 class 兼容层、
|
|
139
|
+
* 工具函数、非 React 入口等场景;React 组件请优先使用 usePntCosUploader
|
|
140
|
+
*
|
|
141
|
+
* @param http 消费方 axios 实例(约定响应拦截器已解包 response.data)
|
|
142
|
+
* @param options 上传配置(固定值),或返回配置的 getter(需要动态配置时使用)
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* const uploader = createPntCosUploader(http, { scene: 'default', pathKey: 'marketing_icon' });
|
|
147
|
+
* const { fileUrl } = await uploader.upload(file);
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
export const createPntCosUploader = (http, options) => {
|
|
151
|
+
const getOptions = () => typeof options === 'function' ? options() : options;
|
|
152
|
+
const upload = async (file, onProgress, overrides) => {
|
|
153
|
+
const opts = getOptions();
|
|
154
|
+
const {
|
|
155
|
+
scene,
|
|
156
|
+
pathKey,
|
|
157
|
+
context,
|
|
158
|
+
audience,
|
|
159
|
+
fileNameStrategy = 'uuid',
|
|
160
|
+
useCdn = true,
|
|
161
|
+
getCredential,
|
|
162
|
+
normalizeCredential
|
|
163
|
+
} = opts;
|
|
164
|
+
// 单次调用覆盖(表单/行级 gameId、动态 pathKey 等),context 浅合并
|
|
165
|
+
const mergedContext = {
|
|
166
|
+
...context,
|
|
167
|
+
...overrides?.context
|
|
168
|
+
};
|
|
169
|
+
const mergedScene = overrides?.scene ?? scene;
|
|
170
|
+
const mergedAudience = overrides?.audience ?? audience;
|
|
171
|
+
const mergedPathKey = overrides?.pathKey ?? pathKey;
|
|
172
|
+
|
|
173
|
+
// 1. 获取凭证
|
|
174
|
+
const credParams = {
|
|
175
|
+
scene: mergedScene,
|
|
176
|
+
pathKey: mergedPathKey,
|
|
177
|
+
context: mergedContext,
|
|
178
|
+
audience: mergedAudience,
|
|
179
|
+
contentLength: file.size
|
|
180
|
+
};
|
|
181
|
+
const cred = getCredential ? await getCredential(http, credParams) : await fetchCosCredential(http, credParams, normalizeCredential);
|
|
182
|
+
|
|
183
|
+
// 2. 构造 COS Key
|
|
184
|
+
const basePath = resolveBasePath(cred, mergedPathKey);
|
|
185
|
+
const fileName = await generateFileName(file, fileNameStrategy);
|
|
186
|
+
const cosKey = basePath ? `${basePath.replace(/\/$/, '')}/${fileName}` : fileName;
|
|
187
|
+
|
|
188
|
+
// 3. putObject 上传
|
|
189
|
+
const cos = createCosClient(cred);
|
|
190
|
+
return new Promise((resolve, reject) => {
|
|
191
|
+
cos.putObject({
|
|
192
|
+
Bucket: cred.bucket,
|
|
193
|
+
Region: cred.region,
|
|
194
|
+
Key: cosKey,
|
|
195
|
+
Body: file,
|
|
196
|
+
onProgress: onProgress ? progressData => {
|
|
197
|
+
onProgress(Math.round(progressData.percent * 100));
|
|
198
|
+
} : undefined
|
|
199
|
+
}, (err, data) => {
|
|
200
|
+
if (err) {
|
|
201
|
+
reject(err);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
// URL 拼接:useCdn 且凭证带 cosHost 时走 CDN;否则用 COS Location
|
|
205
|
+
const fileUrl = useCdn && cred.cosHost ? `${cred.cosHost.replace(/\/$/, '')}/${cosKey}` : `https://${data.Location}`;
|
|
206
|
+
resolve({
|
|
207
|
+
fileUrl,
|
|
208
|
+
cosKey,
|
|
209
|
+
fileName: file.name
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
};
|
|
214
|
+
const getDownloadUrl = async (cosKey, overrides) => {
|
|
215
|
+
const opts = getOptions();
|
|
216
|
+
const {
|
|
217
|
+
scene,
|
|
218
|
+
pathKey,
|
|
219
|
+
context,
|
|
220
|
+
audience,
|
|
221
|
+
getCredential
|
|
222
|
+
} = opts;
|
|
223
|
+
// 单次调用覆盖(表格行级 gameId/task_id 等),context 浅合并
|
|
224
|
+
const mergedContext = {
|
|
225
|
+
...context,
|
|
226
|
+
...overrides?.context
|
|
227
|
+
};
|
|
228
|
+
const mergedScene = overrides?.scene ?? scene;
|
|
229
|
+
const mergedAudience = overrides?.audience ?? audience;
|
|
230
|
+
const mergedPathKey = overrides?.pathKey ?? pathKey;
|
|
231
|
+
// 下载凭证默认走 default_download 接口;sdk_customize 保持原 scene;
|
|
232
|
+
// 消费方传了 getCredential 时完全由其决定
|
|
233
|
+
const credParams = {
|
|
234
|
+
scene: getCredential ? mergedScene : mergedScene === 'sdk_customize' ? mergedScene : 'default_download',
|
|
235
|
+
pathKey: mergedPathKey,
|
|
236
|
+
context: mergedContext,
|
|
237
|
+
audience: mergedAudience,
|
|
238
|
+
fileUrl: cosKey
|
|
239
|
+
};
|
|
240
|
+
const cred = getCredential ? await getCredential(http, credParams) : await fetchCosCredential(http, credParams);
|
|
241
|
+
const cos = createCosClient(cred);
|
|
242
|
+
return new Promise((resolve, reject) => {
|
|
243
|
+
cos.getObjectUrl({
|
|
244
|
+
Bucket: cred.bucket,
|
|
245
|
+
Region: cred.region,
|
|
246
|
+
Key: stripHost(cosKey),
|
|
247
|
+
Sign: true
|
|
248
|
+
}, (err, data) => {
|
|
249
|
+
if (err) {
|
|
250
|
+
reject(err);
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
resolve(data.Url);
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
};
|
|
257
|
+
return {
|
|
258
|
+
upload,
|
|
259
|
+
getDownloadUrl
|
|
260
|
+
};
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
// ==========================================
|
|
264
|
+
// usePntCosUploader Hook
|
|
265
|
+
// ==========================================
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* COS 直传 Service Hook
|
|
269
|
+
* @description 内置凭证获取 + COS SDK 初始化 + 文件名策略 + URL 拼接,
|
|
270
|
+
* 收敛各项目重复的 4 份自实现;http 实例由 PntProvider 注入
|
|
271
|
+
*
|
|
272
|
+
* @param options 上传配置
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* ```ts
|
|
276
|
+
* const uploader = usePntCosUploader({ scene: 'default', pathKey: 'marketing_icon' });
|
|
277
|
+
* const { fileUrl, cosKey } = await uploader.upload(file, (p) => setProgress(p));
|
|
278
|
+
* ```
|
|
279
|
+
*/
|
|
280
|
+
export const usePntCosUploader = options => {
|
|
281
|
+
const {
|
|
282
|
+
http
|
|
283
|
+
} = usePntConfig();
|
|
284
|
+
// 用 ref 持有配置,避免 options 内联字面量导致 uploader 重建
|
|
285
|
+
const optionsRef = useRef(options);
|
|
286
|
+
optionsRef.current = options;
|
|
287
|
+
return useMemo(() => createPntCosUploader(http, () => optionsRef.current), [http]);
|
|
288
|
+
};
|
|
289
|
+
export default usePntCosUploader;
|
|
@@ -28,9 +28,15 @@ export interface PntImageProps {
|
|
|
28
28
|
* @description src 为空时展示的占位图;不传则使用内置的 uploadImg
|
|
29
29
|
*/
|
|
30
30
|
defaultImg?: string;
|
|
31
|
+
/**
|
|
32
|
+
* 快捷尺寸(正方形)
|
|
33
|
+
* @description 预设尺寸:`small`=36px、`medium`=60px、`large`=136px;默认 `large`
|
|
34
|
+
* 若 `style` 中显式指定了 width/height,则以 style 为准;非正方形场景请使用 `style`
|
|
35
|
+
*/
|
|
36
|
+
size?: 'small' | 'medium' | 'large';
|
|
31
37
|
/**
|
|
32
38
|
* 自定义样式
|
|
33
|
-
* @description
|
|
39
|
+
* @description 一般用于控制图片宽高;其中 width/height 优先级高于 size
|
|
34
40
|
*/
|
|
35
41
|
style?: React.CSSProperties;
|
|
36
42
|
/**
|
|
@@ -69,14 +75,14 @@ export interface PntImageProps {
|
|
|
69
75
|
* loading={loading}
|
|
70
76
|
* defaultImg={defaultImg}
|
|
71
77
|
* onRemove={handleRemove}
|
|
72
|
-
*
|
|
78
|
+
* size="large"
|
|
73
79
|
* />
|
|
74
80
|
*
|
|
75
81
|
* // 纯预览模式
|
|
76
82
|
* <PntImage
|
|
77
83
|
* src={url}
|
|
78
84
|
* alt="avatar"
|
|
79
|
-
*
|
|
85
|
+
* size="medium"
|
|
80
86
|
* container={container}
|
|
81
87
|
* />
|
|
82
88
|
* ```
|
|
@@ -33,14 +33,14 @@ const DEFAULT_TEXTS = {
|
|
|
33
33
|
* loading={loading}
|
|
34
34
|
* defaultImg={defaultImg}
|
|
35
35
|
* onRemove={handleRemove}
|
|
36
|
-
*
|
|
36
|
+
* size="large"
|
|
37
37
|
* />
|
|
38
38
|
*
|
|
39
39
|
* // 纯预览模式
|
|
40
40
|
* <PntImage
|
|
41
41
|
* src={url}
|
|
42
42
|
* alt="avatar"
|
|
43
|
-
*
|
|
43
|
+
* size="medium"
|
|
44
44
|
* container={container}
|
|
45
45
|
* />
|
|
46
46
|
* ```
|
|
@@ -51,6 +51,7 @@ const PntImage = ({
|
|
|
51
51
|
src,
|
|
52
52
|
alt = '',
|
|
53
53
|
defaultImg,
|
|
54
|
+
size,
|
|
54
55
|
style,
|
|
55
56
|
loading = false,
|
|
56
57
|
onRemove,
|
|
@@ -60,6 +61,24 @@ const PntImage = ({
|
|
|
60
61
|
// 模式判定:传入 onRemove 即为上传预览模式
|
|
61
62
|
const isUploadMode = typeof onRemove === 'function';
|
|
62
63
|
const finalSrc = src?.length ? src : defaultImg || uploadImg;
|
|
64
|
+
|
|
65
|
+
// 预设尺寸映射:small=36 / medium=60 / large=136(默认 large)
|
|
66
|
+
const SIZE_MAP = {
|
|
67
|
+
small: 36,
|
|
68
|
+
medium: 60,
|
|
69
|
+
large: 136
|
|
70
|
+
};
|
|
71
|
+
const sizeValue = SIZE_MAP[size ?? 'large'];
|
|
72
|
+
|
|
73
|
+
// 合并尺寸:size 提供正方形宽高,style 中显式的 width/height 优先级更高
|
|
74
|
+
const sizeStyle = {
|
|
75
|
+
width: `${sizeValue}px`,
|
|
76
|
+
height: `${sizeValue}px`
|
|
77
|
+
};
|
|
78
|
+
const mergedStyle = {
|
|
79
|
+
...sizeStyle,
|
|
80
|
+
...style
|
|
81
|
+
};
|
|
63
82
|
const attach = container ? typeof container === 'function' ? container : () => container : undefined;
|
|
64
83
|
// 合并文案:texts 字段覆盖默认中文
|
|
65
84
|
const finalTexts = {
|
|
@@ -172,7 +191,7 @@ const PntImage = ({
|
|
|
172
191
|
overlayTrigger: "hover",
|
|
173
192
|
fit: "contain",
|
|
174
193
|
className: "pnt-image",
|
|
175
|
-
style:
|
|
194
|
+
style: mergedStyle
|
|
176
195
|
});
|
|
177
196
|
}
|
|
178
197
|
})
|
|
@@ -46,15 +46,21 @@ const PntSecureBox = prop => {
|
|
|
46
46
|
pageId
|
|
47
47
|
} = prop;
|
|
48
48
|
|
|
49
|
-
// 通过 PntProvider 注入的 http
|
|
49
|
+
// 通过 PntProvider 注入的 http 实例请求高敏数据,缓存按 gameId+env 隔离
|
|
50
50
|
const {
|
|
51
51
|
securityData,
|
|
52
|
+
isUnlocked,
|
|
52
53
|
requestSecurityData
|
|
53
|
-
} = useHighSecurity(
|
|
54
|
+
} = useHighSecurity({
|
|
55
|
+
gameId,
|
|
56
|
+
env
|
|
57
|
+
});
|
|
58
|
+
const isSecurity = isSecurityData(dataValue);
|
|
59
|
+
const unlocked = isSecurity && isUnlocked(dataKey);
|
|
54
60
|
return /*#__PURE__*/_jsx("div", {
|
|
55
61
|
className: "pnt-secure-box",
|
|
56
62
|
children: /*#__PURE__*/_jsxs(_Fragment, {
|
|
57
|
-
children: [renderDom(
|
|
63
|
+
children: [renderDom(unlocked ? securityData[dataKey] || '******' : isSecurity ? '******' : dataValue), isSecurity && !isUnlocked(dataKey) && /*#__PURE__*/_jsx("span", {
|
|
58
64
|
className: "view-icon",
|
|
59
65
|
onClick: () => {
|
|
60
66
|
requestSecurityData({
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import type { CosScene } from "../../api/cos";
|
|
2
|
+
import type { FileNameStrategy, PntCosUploadOptions } from "../PntCosUploader";
|
|
3
|
+
import React, { FC } from 'react';
|
|
4
|
+
import './index.scss';
|
|
5
|
+
/**
|
|
6
|
+
* PntUpload 文案集合
|
|
7
|
+
* @description 所有内部展示文案均可通过 texts prop 自定义,用于多语言场景
|
|
8
|
+
*/
|
|
9
|
+
export interface PntUploadTexts {
|
|
10
|
+
/** 上传中文案 */
|
|
11
|
+
uploading?: string;
|
|
12
|
+
/** 上传失败文案 */
|
|
13
|
+
uploadFailed?: string;
|
|
14
|
+
/** 文件大小超出限制文案 */
|
|
15
|
+
sizeInvalid?: string;
|
|
16
|
+
/** 文件类型不支持文案 */
|
|
17
|
+
typeInvalid?: string;
|
|
18
|
+
/** 选择文件文案(无文件时) */
|
|
19
|
+
selectFile?: string;
|
|
20
|
+
/** 重新选择文案(已有文件时,非图片模式) */
|
|
21
|
+
reselect?: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 校验/上传失败阶段
|
|
25
|
+
*/
|
|
26
|
+
export type PntUploadErrorPhase = 'validate' | 'upload';
|
|
27
|
+
export interface PntUploadProps {
|
|
28
|
+
/**
|
|
29
|
+
* 当前已上传文件 URL(受控)
|
|
30
|
+
*/
|
|
31
|
+
value?: string;
|
|
32
|
+
/**
|
|
33
|
+
* 上传成功回调
|
|
34
|
+
* @param fileUrl 上传后的可访问 URL
|
|
35
|
+
* @param cosKey COS 对象 Key
|
|
36
|
+
* @param file 原始文件
|
|
37
|
+
*/
|
|
38
|
+
onChange?: (fileUrl: string, cosKey: string, file: File) => void;
|
|
39
|
+
/**
|
|
40
|
+
* 删除回调
|
|
41
|
+
* @description 由消费方清空 value
|
|
42
|
+
*/
|
|
43
|
+
onRemove?: () => void;
|
|
44
|
+
/**
|
|
45
|
+
* 异常回调(校验失败 / 上传失败)
|
|
46
|
+
* @param err 错误对象(校验失败时 message 为对应文案)
|
|
47
|
+
* @param phase 失败阶段:validate=本地校验,upload=凭证获取或 COS 上传
|
|
48
|
+
* @param file 原始文件
|
|
49
|
+
*/
|
|
50
|
+
onError?: (err: Error, phase: PntUploadErrorPhase, file: File) => void;
|
|
51
|
+
/**
|
|
52
|
+
* 业务场景(透传给 PntCosUploader)
|
|
53
|
+
*/
|
|
54
|
+
scene: CosScene;
|
|
55
|
+
/**
|
|
56
|
+
* 业务类型路径 key(透传给 PntCosUploader)
|
|
57
|
+
*/
|
|
58
|
+
pathKey?: string;
|
|
59
|
+
/**
|
|
60
|
+
* 业务上下文(透传给 PntCosUploader)
|
|
61
|
+
*/
|
|
62
|
+
context?: Record<string, string | number>;
|
|
63
|
+
/**
|
|
64
|
+
* 网关 audience(透传给 PntCosUploader)
|
|
65
|
+
* @default '/player-network'
|
|
66
|
+
*/
|
|
67
|
+
audience?: string;
|
|
68
|
+
/**
|
|
69
|
+
* 文件类型限制
|
|
70
|
+
* @description 支持 MIME('image/png')、通配('image/*')、扩展名('.zip'),可传数组
|
|
71
|
+
*/
|
|
72
|
+
accept?: string | string[];
|
|
73
|
+
/**
|
|
74
|
+
* 文件大小限制(字节)
|
|
75
|
+
*/
|
|
76
|
+
sizeLimit?: number;
|
|
77
|
+
/**
|
|
78
|
+
* 是否为图片预览模式
|
|
79
|
+
* @description true 使用 PntImage 预览;false 使用文件名 + 选择区域
|
|
80
|
+
* @default true
|
|
81
|
+
*/
|
|
82
|
+
imagePreview?: boolean;
|
|
83
|
+
/**
|
|
84
|
+
* 图片预览区样式(imagePreview=true 生效,一般控制宽高)
|
|
85
|
+
*/
|
|
86
|
+
previewStyle?: React.CSSProperties;
|
|
87
|
+
/**
|
|
88
|
+
* 默认占位图(imagePreview=true 生效)
|
|
89
|
+
*/
|
|
90
|
+
defaultImg?: string;
|
|
91
|
+
/**
|
|
92
|
+
* 文件名生成策略(透传给 PntCosUploader)
|
|
93
|
+
* @default 'uuid'
|
|
94
|
+
*/
|
|
95
|
+
fileNameStrategy?: FileNameStrategy;
|
|
96
|
+
/**
|
|
97
|
+
* 是否走 CDN 域名拼接(透传给 PntCosUploader)
|
|
98
|
+
* @default true
|
|
99
|
+
*/
|
|
100
|
+
useCdn?: boolean;
|
|
101
|
+
/**
|
|
102
|
+
* 自定义凭证获取函数(透传给 PntCosUploader)
|
|
103
|
+
*/
|
|
104
|
+
getCredential?: PntCosUploadOptions['getCredential'];
|
|
105
|
+
/**
|
|
106
|
+
* 自定义凭证响应归一化(透传给 PntCosUploader)
|
|
107
|
+
*/
|
|
108
|
+
normalizeCredential?: PntCosUploadOptions['normalizeCredential'];
|
|
109
|
+
/**
|
|
110
|
+
* 多语言文案
|
|
111
|
+
* @description 传入即覆盖默认中文文案;仅对应字段会覆盖,未传字段仍用默认值
|
|
112
|
+
*/
|
|
113
|
+
texts?: PntUploadTexts;
|
|
114
|
+
/**
|
|
115
|
+
* 禁用上传与删除
|
|
116
|
+
* @default false
|
|
117
|
+
*/
|
|
118
|
+
disabled?: boolean;
|
|
119
|
+
/**
|
|
120
|
+
* 自定义样式
|
|
121
|
+
*/
|
|
122
|
+
style?: React.CSSProperties;
|
|
123
|
+
/**
|
|
124
|
+
* 自定义类名
|
|
125
|
+
*/
|
|
126
|
+
className?: string;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* PntUpload 业务上传组件
|
|
130
|
+
* @description 基于 TDesign Upload + PntImage + usePntCosUploader,
|
|
131
|
+
* 内置 COS 凭证获取、直传、进度、校验、错误处理;支持图片预览 / 文件两种模式
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```tsx
|
|
135
|
+
* <PntUpload
|
|
136
|
+
* scene="default"
|
|
137
|
+
* pathKey="marketing_icon"
|
|
138
|
+
* context={{ gameId }}
|
|
139
|
+
* accept="image/png"
|
|
140
|
+
* sizeLimit={500 * 1024}
|
|
141
|
+
* imagePreview
|
|
142
|
+
* value={iconUrl}
|
|
143
|
+
* onChange={(url) => setIconUrl(url)}
|
|
144
|
+
* onRemove={() => setIconUrl('')}
|
|
145
|
+
* />
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
declare const PntUpload: FC<PntUploadProps>;
|
|
149
|
+
export default PntUpload;
|