PublicUtil.js
8.09 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import {isDate, isEmptyValue, isNoneOfTheseValues, isObject, isOneOfTheseValues} from "@/PublicUtil/Judgment"
import moment from "moment"
import _ from "lodash"
import Cookies from 'js-cookie'
export const getFolderColor = function(status) {
switch (status)
{
case 1:
{
return '#44D7B6'
}
case 2:
{
return '#F7B500'
}
case 3:
{
return '#6DD400'
}
case -1:
{
return '#6495ED'
}
case 4:
{
return '#FF69B4'
}
case 7:
{
return '#8B4513'
}
default:
{
return ''
}
}
}
export const getFolderTitle = function(status) {
switch (status)
{
case 1:
{
return '待标注'
}
case 2:
{
return '标注中'
}
case 3:
{
return '完成标注'
}
case -1:
{
return '被标注'
}
case 4:
{
return '优质'
}
case 7:
{
return '低质'
}
default:
{
return ''
}
}
}
export const getImageUrl = function(imageId) {
return `${window._baseUrl}/pics/image/${imageId}?token=${Cookies.get('token')}`
}
export const previewImage = function(imageUrl) {
window.open(imageUrl)
}
export const goBack = function() {
window.history.back()
}
export const getFormDataByObject = function(object) {
const formData = new FormData()
for (const key in object)
{
const value = object[key]
formData.append(key, value)
}
return formData
}
export const getCountGte = function() {
if (isEmptyValue(localStorage.getItem('countGTE')))
{
return 1
}
else
{
return localStorage.getItem('countGTE')
}
}
export const getCountLte = function() {
if (isEmptyValue(localStorage.getItem('countLTE')))
{
return 100
}
else
{
return localStorage.getItem('countLTE')
}
}
export const getFolderNum = function() {
if (isEmptyValue(localStorage.getItem('folderNum')))
{
return 10
}
else
{
return localStorage.getItem('folderNum')
}
}
export const getMinuteNum = function() {
if (isEmptyValue(localStorage.getItem('minuteNum')))
{
return 60
}
else
{
return localStorage.getItem('minuteNum')
}
}
export const getPagedList = function(list, columnNum) {
const baseArray = list
const len = baseArray.length
const lineNum = len % columnNum === 0 ? len / columnNum : Math.floor((len / columnNum) + 1)
let result = []
for (let i = 0; i < lineNum; i++)
{
// slice() 方法返回一个从开始到结束(不包括结束)选择的数组的一部分浅拷贝到一个新数组对象。且原始数组不会被修改。
const temp = baseArray.slice(i * columnNum, i * columnNum + columnNum)
result.push(temp)
}
return result
}
export const getKeyByValueInObject = function(object, value) {
return Object.keys(object).find(
(key) => {
return object[key] === value
}
)
}
export const filterEmptyValueInObject = function(object) {
if (isObject(object))
{
for (let key in object)
{
let value = object[key]
if (isOneOfTheseValues(value, ['', NaN, undefined, null]))
{
delete object[key]
}
}
return object
}
}
export const getTodayDate = function() {
return moment().startOf("day").format("yyyy-MM-DD")
}
export const setAccessedMenu = function(menu) {
localStorage.setItem('accessedMenu', encryptString(JSON.stringify(menu)))
}
export const getAccessedMenu = function() {
return JSON.parse(decryptString(localStorage.getItem('accessedMenu')))
}
/**
* encrypto 加密程序
* @param {Strng} str 待加密字符串
* @param {Number} xor 异或值
* @param {Number} hex 加密后的进制数
* @return {Strng} 加密后的字符串
*/
const encryptString = function(str, xor = 123, hex = 25) {
if (typeof str !== 'string' || typeof xor !== 'number' || typeof hex !== 'number')
{
return
}
let resultList = []
hex = hex <= 25 ? hex : hex % 25
for (let i = 0; i < str.length; i++)
{
// 提取字符串每个字符的ascll码
let charCode = str.charCodeAt(i)
// 进行异或加密
charCode = (charCode * 1) ^ xor
// 异或加密后的字符转成 hex 位数的字符串
charCode = charCode.toString(hex)
resultList.push(charCode)
}
let splitStr = String.fromCharCode(hex + 97)
let resultStr = resultList.join(splitStr)
return resultStr
}
/**
* decrypto 解密程序
* @param {Strng} str 待加密字符串
* @param {Number} xor 异或值
* @param {Number} hex 加密后的进制数
* @return {Strng} 加密后的字符串
*/
const decryptString = function(str, xor = 123, hex = 25) {
if (typeof str !== 'string' || typeof xor !== 'number' || typeof hex !== 'number')
{
return
}
let strCharList = []
let resultList = []
hex = hex <= 25 ? hex : hex % 25
// 解析出分割字符
let splitStr = String.fromCharCode(hex + 97)
// 分割出加密字符串的加密后的每个字符
strCharList = str.split(splitStr)
for (let i = 0; i < strCharList.length; i++)
{
// 将加密后的每个字符转成加密后的ascll码
let charCode = parseInt(strCharList[i], hex)
// 异或解密出原字符的ascll码
charCode = (charCode * 1) ^ xor
let strChar = String.fromCharCode(charCode)
resultList.push(strChar)
}
let resultStr = resultList.join('')
return resultStr
}
export const resetForm = function(form) {
for (let key in form)
{
form[key] = ""
}
}
export const emptyList = function(list) {
list.splice(0, list.length)
}
export const getTimestampString = function() {
return new Date().getTime().toString()
}
export const emptyObject = function(object) {
for (let key in object)
{
delete object[key]
}
}
export const cloneObject = function(targetObject, sourceObject) {
emptyObject(targetObject)
for (const key in sourceObject)
{
const value = sourceObject[key]
targetObject[key] = value
}
}
export const createFormData = function(object, fileName, file) {
let formData = new FormData()
if (isObject(object))
{
for (let key in object)
{
let value = object[key]
// if (![undefined, null].includes(value))
if (isNoneOfTheseValues(value, [undefined, null]))
{
formData.append(key, value)
}
}
}
if (file !== undefined)
{
formData.append(fileName, file)
}
return formData
}
export const copyDataList = function(targetDataList, sourceDataList) {
targetDataList.value.splice(0, targetDataList.value.length)
targetDataList.value.push(...sourceDataList)
}
export const formatPaginatedTableData = function(targetData, sourceData, total) {
const list = sourceData.list
// 添加序号
if (list !== undefined)
{
let start = (sourceData.pageNum - 1) * sourceData.pageSize
let end = start + list.length
for (let i = start; i < end; i++)
{
let item = list[i - start]
item["serialNumber"] = i + 1
}
}
copyDataList(targetData, list)
total.value = sourceData.total
}
export const addSerialNumber = function(sourceData) {
if (sourceData !== undefined)
{
for (let i = 0; i < sourceData.length; i++)
{
let item = sourceData[i]
item["serialNumber"] = i + 1
}
}
}
// 把 date 对象转换为 0000-00-00 这种日期形式
export const formatDate = function(date) {
return moment(date).format("YYYY-MM-DD")
}
export const formatTime = function(date) {
return moment(date).format("HH:mm:ss")
}