Source: packages/utils/util.js


import IS from './is'

/**
 * 判断是否是Blob地址 
 * @param {*} url 
 * @returns {boolean} 否是Blob地址 true 是 false 否
 */
export const isBlobUrl = (url)=>{
  return /^blob:(.*)/.test(url)
}

/**
 * 获取水印文本信息
 * @returns {Object} {title:'',subtitle:'中雄机密文件禁止外传'}
 */
export const getWatermarkConfig = ()=>{
  const str = localStorage.getItem('pro__Login_Userinfo')
  const userInfo = str ? JSON.parse(str) : {};
  // console.log('@@@@@@@@@@@@@@@@@@@@@', userInfo.value);
  let WatermarkText = '请勿外传'
  if (userInfo.value) {
    const { phone, realname } = userInfo.value
    if(phone){
      WatermarkText = `${realname} ${phone&&phone.substr(7, 4)}`
    }else{
      WatermarkText = realname
    }
  }
  return { title:WatermarkText, subtitle:'中雄机密文件禁止外传' }
}

/**
 * 获取文件后缀
 * @param {string} path 文件地址
 * @returns {string} 文件后缀
 */
export function getFileExtension(path) {
  if(IS.isNullOrUnDef(path) || IS.isEmpty(path)){
    return undefined
  }
  const matches = path.match(/^.*\.(.*)$/);
  return matches ? matches[1] : undefined;
}

/**
 * 截取文件名
 * @param {*} url 
 * @returns {string} 文件名
 */
export function getCaptionFileName(url) {
    let aName = '';
    if(IS.isNullOrUnDef(url) || IS.isEmpty(url)){
      return aName
    }
    if (aName == '' || aName.length == 0) {
        aName = getFileName(decodeURI(encodeSearchKey(url)))
    }
    if (aName == '' || aName.length == 0) {
        aName = getFileName2(decodeURI(url))
    }
    return aName
}

function encodeSearchKey(key) {
    const encodeArr = [{
      code: '%',
      encode: '%25'
    }, {
      code: '?',
      encode: '%3F'
    }, {
      code: '#',
      encode: '%23'
    }, {
      code: '&',
      encode: '%26'
    }, {
      code: '=',
      encode: '%3D'
    }];
    return key.replace(/[%?#&=]/g, ($, index, str) => {
      for (const k of encodeArr) {
        if (k.code === $) {
          return k.encode;
        }
      }
    });
  }

function getFileName(path) {
    var pos1 = path.lastIndexOf('/');
    var pos2 = path.lastIndexOf('\\');
    var pos = Math.max(pos1, pos2);
    if (pos < 0)
        return path;
    else
        return path.substring(pos + 1);
}
function getFileName2(path) {
    var pos1 = path.lastIndexOf('/');
    var pos2 = path.lastIndexOf('\\');
    var pos = Math.max(pos1, pos2);
    if (pos < 0) {
        return path;
    } else {
        let tempPath = path.substring(pos + 1);
        return tempPath.substring(0, tempPath.lastIndexOf("."));
    }
}
/**
 * 获取文件原名称
 * @param {*} name_ 
 * @returns {string} 文件原名称
 */
export const getFileOldName = name_ => {
    let res = ''
    if (name_.indexOf('_') >= 0) {
        const tempArr = name_.split('_')
        res = tempArr[tempArr.length - 1]
    }else{
        res = name_
    }
    return res
}