utils.js
8.33 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import CryptoJS from 'crypto-js'
import config from '@/config.js'
export function szToHz(num) {
const hzArr = ['〇', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十']
return hzArr[parseInt(num)]
}
export function AESEncrypt(data) {
const key = CryptoJS.enc.Utf8.parse('abcdefgabcdegf21')
// 将数据转换为字符串
const parseByte2HexStr = (wordArray) => {
const hexStr = wordArray.ciphertext.toString(CryptoJS.enc.Hex)
return hexStr
}
let dataStr
if (typeof data === 'object') {
dataStr = JSON.stringify(data)
} else {
dataStr = String(data)
}
// 加密
const encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(dataStr), key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
})
return parseByte2HexStr(encrypted)
}
export function AESDecrypt(str) {
const key = CryptoJS.enc.Utf8.parse('abcdefgabcdegf21')
const decrypt = CryptoJS.AES.decrypt(str, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
})
const aesStr = CryptoJS.enc.Utf8.stringify(decrypt).toString()
try {
return JSON.parse(aesStr)
} catch (e) {
return aesStr
}
}
export function isDaoGuanRole() {
const app = getApp()
const deptType = app.globalData?.deptType
const userType = app.globalData?.userType
return deptType == null || deptType == 6 || deptType == '6' || userType == '4'
}
export function reLaunchHomeByRole() {
uni.reLaunch({
url: isDaoGuanRole() ? '/pages/index/daoGuanPerson' : '/pages/index/home'
})
}
export function fillImgUrl(url, prefix) {
if (!url) return ''
const trimmedUrl = String(url).trim()
if (!trimmedUrl) return ''
if (trimmedUrl === 'null' || trimmedUrl === 'undefined') return ''
if (trimmedUrl.startsWith('msr:')) {
return `${trimBaseUrl(config.baseUrl_api)}/fileServer/download?file=${encodeURIComponent(trimmedUrl)}&downFlag=0`
}
if (/^(data:|https?:\/\/)/.test(trimmedUrl)) {
return trimmedUrl
}
const baseUrl = prefix
? `${trimBaseUrl(config.baseUrl_api)}/${String(prefix).replace(/^\/+|\/+$/g, '')}`
: trimBaseUrl(config.baseUrl_api)
const path = trimmedUrl.startsWith('/') ? trimmedUrl : `/${trimmedUrl}`
return baseUrl + path
}
export function isImageUrl(url) {
const value = String(url || '').trim()
if (!value) return false
const decodedValue = safeDecodeURIComponent(value)
return /\.(png|jpe?g|gif|webp|bmp|svg)(\?.*)?$/i.test(decodedValue) ||
/[?&]file=[^&]*\.(png|jpe?g|gif|webp|bmp|svg)(&|$)/i.test(decodedValue)
}
export function getMemberPhotoValue(item = {}) {
return [item.photo, item.perPhoto, item.photo2, item.perPhoto2].find(isValidUrlValue) || ''
}
export function fillMemberPhoto(item = {}, fallback = '') {
const photo = getMemberPhotoValue(item)
return photo ? fillImgUrl(photo) : fallback
}
export function previewAttachment(file, options = {}) {
const item = normalizeAttachment(file)
const rawUrl = item.rawUrl || item.url
const previewUrl = getAttachmentPreviewUrl(rawUrl, 0)
if (!previewUrl) {
uni.showToast({
title: '暂无可预览附件',
icon: 'none'
})
return
}
if (isAttachmentImage(item) || isImageUrl(rawUrl) || isImageUrl(previewUrl)) {
uni.previewImage({
urls: [previewUrl],
current: previewUrl,
fail: () => {
uni.showToast({
title: '图片预览失败',
icon: 'none'
})
}
})
return
}
if (getAttachmentExt(item) === 'zip') {
uni.showToast({
title: '压缩包暂不支持在线预览',
icon: 'none'
})
return
}
if (getAttachmentExt(item) === 'pdf' && options.preferWebView !== false) {
openAttachmentWebView(previewUrl, options.title || '附件预览')
return
}
openDocumentAttachment(item, getAttachmentPreviewUrl(rawUrl, 1), false, options)
}
export function normalizeAttachment(file) {
if (Array.isArray(file)) return normalizeAttachment(file[0])
if (typeof file === 'string') {
const parsed = parseAttachmentJson(file)
if (parsed !== file) return normalizeAttachment(parsed)
return {
rawUrl: file,
url: file,
name: ''
}
}
if (file && typeof file === 'object') {
const rawUrl = file.rawUrl || file.url || file.fileUrl || file.path || ''
return {
...file,
rawUrl,
url: rawUrl
}
}
return {}
}
export function parseAttachmentJson(value) {
if (!value || typeof value !== 'string') return value
try {
return JSON.parse(value)
} catch (e) {
return value
}
}
export function getAttachmentPreviewUrl(url, downFlag = 0) {
if (!url) return ''
const value = String(url).trim()
if (!value || value === 'null' || value === 'undefined') return ''
if (value.startsWith('msr:')) {
return `${trimBaseUrl(config.baseUrl_api)}/fileServer/download?file=${value}&downFlag=${downFlag}`
}
return fillImgUrl(value)
}
function openDocumentAttachment(item, url, retried = false, options = {}) {
if (!url) {
uni.showToast({
title: '文件下载失败',
icon: 'none'
})
return
}
uni.showLoading({
title: '打开中',
mask: true
})
uni.downloadFile({
url,
header: {
Authorization: uni.getStorageSync('token')
},
success: res => {
if (res.statusCode && res.statusCode !== 200) {
if ((res.statusCode === 301 || res.statusCode === 302) && !retried) {
const redirectUrl = getRedirectUrl(res.header)
uni.hideLoading()
if (redirectUrl) {
openDocumentAttachment(item, redirectUrl, true, options)
} else if (getAttachmentExt(item) === 'pdf') {
openAttachmentWebView(getAttachmentPreviewUrl(item.rawUrl || item.url, 0), options.title || '附件预览')
} else {
uni.showToast({
title: '文件下载失败',
icon: 'none'
})
}
return
}
uni.showToast({
title: '文件下载失败',
icon: 'none'
})
return
}
uni.openDocument({
filePath: res.tempFilePath,
fileType: getDocumentFileType(item),
showMenu: true,
fail: () => {
if (getAttachmentExt(item) === 'pdf') {
openAttachmentWebView(getAttachmentPreviewUrl(item.rawUrl || item.url, 0), options.title || '附件预览')
return
}
uni.showToast({
title: '文件暂不支持预览',
icon: 'none'
})
}
})
},
fail: () => {
if (getAttachmentExt(item) === 'pdf') {
openAttachmentWebView(getAttachmentPreviewUrl(item.rawUrl || item.url, 0), options.title || '附件预览')
return
}
uni.showToast({
title: '文件下载失败',
icon: 'none'
})
},
complete: () => {
uni.hideLoading()
}
})
}
function openAttachmentWebView(url, title = '附件预览') {
if (!url) {
uni.showToast({
title: '文件下载失败',
icon: 'none'
})
return
}
uni.navigateTo({
url: `/pages/webview/webview?title=${encodeURIComponent(title)}&url=${encodeURIComponent(url)}`,
fail: () => {
uni.showToast({
title: '附件预览失败',
icon: 'none'
})
}
})
}
function getAttachmentExt(file) {
const name = `${file?.extname || file?.name || file?.rawUrl || file?.url || ''}`
const ext = name.includes('.') ? name.split('.').pop() : name
return ext.split('?')[0].toLowerCase()
}
function isAttachmentImage(file) {
return ['png', 'jpg', 'jpeg', 'gif', 'bmp', 'webp', 'svg'].includes(getAttachmentExt(file))
}
function getDocumentFileType(file) {
const ext = getAttachmentExt(file)
const types = ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf']
return types.includes(ext) ? ext : undefined
}
function getRedirectUrl(header = {}) {
const location = header.Location || header.location
if (!location) return ''
if (/^https?:\/\//.test(location)) return location
return `${trimBaseUrl(config.baseUrl_api)}/${String(location).replace(/^\/+/, '')}`
}
function isValidUrlValue(value) {
if (value === undefined || value === null) return false
const url = String(value).trim()
return url !== '' && url !== 'null' && url !== 'undefined'
}
function trimBaseUrl(url) {
return String(url || '').replace(/\/+$/, '')
}
function safeDecodeURIComponent(value) {
try {
return decodeURIComponent(value)
} catch (e) {
return value
}
}