material-preview.uts
10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import { toStr } from "@/utils/common.uts";
export type MaterialPreviewPayload = {
title: string
kind: string
url: string
contentType: number
unsupportedMessage: string
}
function createPreviewPayload(
title: string,
kind: string,
url: string,
contentType: number,
unsupportedMessage: string = "该素材暂不支持预览"
): MaterialPreviewPayload {
return {
title: title,
kind: kind,
url: url,
contentType: contentType,
unsupportedMessage: unsupportedMessage
};
}
export function readMaterialTitle(item: any | null): string {
if (item == null) {
return "";
}
const obj = item as UTSJSONObject;
return (
(obj["detailTitle"] as null | string)
?? (obj["materialTitle"] as null | string)
?? (obj["shortTitle"] as null | string)
?? (obj["materialName"] as null | string)
?? (obj["title"] as null | string)
?? (obj["name"] as null | string)
?? ""
);
}
export function readMaterialType(item: any | null): number {
if (item == null) {
return -1;
}
const obj = item as UTSJSONObject;
const raw = obj["contentType"];
if (raw == null) {
return -1;
}
if (typeof raw === "number") {
return raw as number;
}
const parsed = parseInt(`${raw}`);
return isNaN(parsed) ? -1 : parsed;
}
export function readMaterialCoverUrl(item: any | null): string {
if (item == null) {
return "";
}
const obj = item as UTSJSONObject;
const candidates = [
obj["coverUrl"],
obj["bannerImg"],
obj["thumbnail"],
obj["thumbnailUrl"],
obj["contentPicUrl"],
obj["imageUrl"],
obj["imgUrl"],
obj["coverImg"],
obj["detailUrl"]
];
for (let i = 0; i < candidates.length; i++) {
const value = candidates[i];
if (value != null && typeof value === "string" && `${value}` !== "") {
return `${value}`;
}
}
return "";
}
export function readMaterialPreviewUrl(item: any | null): string {
if (item == null) {
return "";
}
const obj = item as UTSJSONObject;
const candidates = [
obj["previewUrl"],
obj["materialUrl"],
obj["detailUrl"],
obj["bodyUrl"],
obj["contentPicUrl"],
obj["coverUrl"],
obj["thumbnailUrl"]
];
for (let i = 0; i < candidates.length; i++) {
const value = candidates[i];
if (value != null && typeof value === "string" && `${value}` !== "") {
return `${value}`;
}
}
return "";
}
function isWebUrl(url: string): boolean {
return url.startsWith("http://") || url.startsWith("https://");
}
function getUrlWithoutQuery(url: string): string {
let result = url.toLowerCase();
const hashIndex = result.indexOf("#");
if (hashIndex >= 0) {
result = result.substring(0, hashIndex);
}
const queryIndex = result.indexOf("?");
if (queryIndex >= 0) {
result = result.substring(0, queryIndex);
}
return result;
}
function endsWithOneOf(url: string, suffixes: string[]): boolean {
const normalized = getUrlWithoutQuery(url);
for (let i = 0; i < suffixes.length; i++) {
const suffix = suffixes[i];
if (normalized.endsWith(suffix)) {
return true;
}
}
return false;
}
function inferPreviewKindFromUrl(url: string): string {
if (url == "") {
return "unsupported";
}
if (endsWithOneOf(url, [".png", ".jpg", ".jpeg", ".webp", ".gif", ".bmp"])) {
return "image";
}
if (endsWithOneOf(url, [".mp4", ".mov", ".m4v", ".webm", ".avi"])) {
return "video";
}
if (isWebUrl(url) && endsWithOneOf(url, [".pdf", ".html", ".htm"])) {
return "web";
}
return "unsupported";
}
function isDirectOfficeFileUrl(url: string): boolean {
return endsWithOneOf(url, [".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx"]);
}
function getUnsupportedMessage(contentType: number, previewUrl: string): string {
if (contentType == 4 || contentType == 5) {
if (previewUrl == "") {
return "Office 素材缺少预览地址";
}
if (isDirectOfficeFileUrl(previewUrl)) {
return "Office 原文件不建议直接预览,请配置在线预览地址";
}
}
if (contentType == 2 && !isWebUrl(previewUrl)) {
return "PDF 素材链接无效";
}
if (contentType == 1 && previewUrl == "") {
return "视频地址不存在";
}
return "该素材暂不支持预览";
}
export function resolveMaterialPreviewPayload(item: any | null): MaterialPreviewPayload {
const title = readMaterialTitle(item);
const contentType = readMaterialType(item);
const previewUrl = readMaterialPreviewUrl(item);
const coverUrl = readMaterialCoverUrl(item);
const inferredPreviewKind = inferPreviewKindFromUrl(previewUrl);
const inferredCoverKind = inferPreviewKindFromUrl(coverUrl);
if (contentType == 0) {
const imageUrl = previewUrl != "" ? previewUrl : coverUrl;
if (imageUrl != "") {
return createPreviewPayload(title, "image", imageUrl, contentType);
}
return createPreviewPayload(title, "unsupported", "", contentType, "图片地址不存在");
}
if (contentType == 1) {
if (previewUrl != "") {
return createPreviewPayload(title, "video", previewUrl, contentType);
}
return createPreviewPayload(title, "unsupported", "", contentType, "视频地址不存在");
}
if (contentType == 2 || contentType == 3) {
if ((inferredPreviewKind == "image" || inferredPreviewKind == "video") && previewUrl != "") {
return createPreviewPayload(title, inferredPreviewKind, previewUrl, contentType);
}
if (previewUrl != "" && isWebUrl(previewUrl)) {
return createPreviewPayload(title, "web", previewUrl, contentType);
}
if ((inferredCoverKind == "image" || inferredCoverKind == "video") && coverUrl != "") {
return createPreviewPayload(title, inferredCoverKind, coverUrl, contentType);
}
return createPreviewPayload(title, "unsupported", "", contentType, getUnsupportedMessage(contentType, previewUrl));
}
if (contentType == 4 || contentType == 5) {
if ((inferredPreviewKind == "image" || inferredPreviewKind == "video") && previewUrl != "") {
return createPreviewPayload(title, inferredPreviewKind, previewUrl, contentType);
}
if (previewUrl != "" && isWebUrl(previewUrl) && !isDirectOfficeFileUrl(previewUrl)) {
return createPreviewPayload(title, "web", previewUrl, contentType);
}
if ((inferredCoverKind == "image" || inferredCoverKind == "video") && coverUrl != "") {
return createPreviewPayload(title, inferredCoverKind, coverUrl, contentType);
}
return createPreviewPayload(title, "unsupported", "", contentType, getUnsupportedMessage(contentType, previewUrl));
}
const inferredUrl = previewUrl != "" ? previewUrl : coverUrl;
const inferredKind = inferPreviewKindFromUrl(inferredUrl);
if (inferredKind != "unsupported") {
return createPreviewPayload(title, inferredKind, inferredUrl, contentType);
}
return createPreviewPayload(title, "unsupported", "", contentType, getUnsupportedMessage(contentType, inferredUrl));
}
export function canPreviewMaterial(item: any | null): boolean {
const payload = resolveMaterialPreviewPayload(item);
return payload.kind != "unsupported" && payload.url != "";
}
export function buildMaterialPreviewRoute(item: any | null): string {
const payload = resolveMaterialPreviewPayload(item);
if (payload.kind == "unsupported" || payload.url == "") {
return "";
}
const titleText = payload.title != "" ? payload.title : "素材预览";
const title = encodeURIComponent(toStr(titleText));
const url = encodeURIComponent(payload.url);
if (payload.kind == "web") {
return `/pages/common/webview/index?title=${title}&url=${url}`;
}
const kind = encodeURIComponent(payload.kind);
return `/pages/common/materialPreview/index?title=${title}&kind=${kind}&url=${url}`;
}
export function openMaterialPreview(item: any | null, showUnsupportedToast: boolean = true): boolean {
const payload = resolveMaterialPreviewPayload(item);
if (payload.kind == "image" && payload.url != "") {
uni.previewImage({
current: payload.url,
urls: [payload.url]
});
return true;
}
const route = buildMaterialPreviewRoute(item);
if (route == "") {
if (showUnsupportedToast) {
uni.showToast({
title: payload.unsupportedMessage,
icon: "none"
});
}
return false;
}
uni.navigateTo({ url: route });
return true;
}
/**
* UTSJSONObject 版本的预览解析,避免跨文件传递自定义 type 导致运行时 NoSuchMethodError
*/
export function resolvePreviewObj(item: any | null): UTSJSONObject {
const p = resolveMaterialPreviewPayload(item);
return {
title: p.title,
kind: p.kind,
url: p.url,
contentType: p.contentType,
unsupportedMessage: p.unsupportedMessage
} as UTSJSONObject;
}