utils.js
3.14 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
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
}
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
}
}