PublicUtil.js 8.09 KB
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")
}