utils.js 2.72 KB
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 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(/\/+$/, '')
}