import _ from 'lodash'; 
 | 
import { setOSSLink } from '@life-payment/utils'; 
 | 
import Taro from '@tarojs/taro'; 
 | 
import { FileItem } from '@nutui/nutui-taro/dist/types/__VUE/uploader/type'; 
 | 
  
 | 
/** 
 | 
 * 获取文件扩展名 
 | 
 */ 
 | 
export function getFileExtension(url: string): string { 
 | 
  return _.last(url.split('.')); 
 | 
} 
 | 
  
 | 
export function getFileNameInfo(fileUrl: string) { 
 | 
  const fileNameList = fileUrl.split('/').pop().split('.'); 
 | 
  const extension = fileNameList.pop(); 
 | 
  const newFileNameList = fileNameList.join('.').split('_'); 
 | 
  if (newFileNameList.length > 1) { 
 | 
    newFileNameList.pop(); 
 | 
  } 
 | 
  return { 
 | 
    fileName: decodeURI(newFileNameList.join('_')), 
 | 
    extension, 
 | 
  }; 
 | 
} 
 | 
  
 | 
export function extractFileNameFromUrl(fileUrl: string) { 
 | 
  const { fileName, extension } = getFileNameInfo(fileUrl); 
 | 
  return `${fileName}.${extension}`; 
 | 
} 
 | 
  
 | 
export function urlOmitDomain(url: string) { 
 | 
  return url.replace(/^https?:\/\/[^/]+/, ''); 
 | 
} 
 | 
  
 | 
const imgReg = /\.(png|jpeg|jpg|webp|gif)$/i; 
 | 
  
 | 
/** 
 | 
 * 把api返回的路径转换为upload的路径 
 | 
 */ 
 | 
export function convertApiPath2Url(path: string) { 
 | 
  return { 
 | 
    name: extractFileNameFromUrl(path), 
 | 
    path: urlOmitDomain(path), 
 | 
    url: setOSSLink(path), 
 | 
    status: 'success', 
 | 
    type: imgReg.test(path) ? 'image' : '', 
 | 
    uid: _.uniqueId(), 
 | 
  } as FileItem; 
 | 
} 
 | 
  
 | 
export function convertApi2FormUrlOnlyOne(path: string) { 
 | 
  return path ? [convertApiPath2Url(path)] : []; 
 | 
} 
 | 
  
 | 
/** 
 | 
 * 把upload的路径转换为上传到api的路径 
 | 
 */ 
 | 
export function convertFormUrl2Api< 
 | 
  T extends { path?: string; url?: string; status?: FileItem['status'] } 
 | 
>(urls: T[]) { 
 | 
  return urls.filter((x) => x.path && x.status === 'success').map((x) => urlOmitDomain(x.path)); 
 | 
} 
 | 
  
 | 
export function convertFilterFormUrl2Api<T extends { path?: string }>(urls: T[]) { 
 | 
  return urls.filter((x) => x.path); 
 | 
} 
 | 
  
 | 
export function isFileAllUploaded(fileList: FileItem[]) { 
 | 
  return fileList.every((x) => !!x.path && x.status === 'success'); 
 | 
} 
 | 
  
 | 
type Base64ListItem = { 
 | 
  base64: string; 
 | 
  fileName: string; 
 | 
}; 
 | 
  
 | 
export function base64SaveTolocal(base64List: Base64ListItem[]) { 
 | 
  const fsm = Taro.getFileSystemManager(); 
 | 
  
 | 
  return base64List.map((base64ListItem) => { 
 | 
    const arrayBuffer = Taro.base64ToArrayBuffer( 
 | 
      base64ListItem.base64.replace('data:image/png;base64,', '') 
 | 
    ); 
 | 
    const fileName = `${Taro.env.USER_DATA_PATH}/${base64ListItem.fileName}.png`; 
 | 
    fsm.writeFileSync(fileName, arrayBuffer, 'binary'); 
 | 
    return fileName; 
 | 
  }); 
 | 
} 
 | 
  
 | 
export function downloadBase64File(base64Data: string, filename: string) { 
 | 
  // 将base64数据分割,获取mime-type 
 | 
  const [, mime, b64data] = base64Data.match(/^data:([^;]+);base64,(.+)$/); 
 | 
  
 | 
  // 解码base64数据 
 | 
  const byteString = atob(b64data); 
 | 
  const arrayBuffer = new ArrayBuffer(byteString.length); 
 | 
  const intArray = new Uint8Array(arrayBuffer); 
 | 
  
 | 
  for (let i = 0; i < byteString.length; i++) { 
 | 
    intArray[i] = byteString.charCodeAt(i); 
 | 
  } 
 | 
  
 | 
  // 创建Blob对象 
 | 
  const blob = new Blob([intArray], { type: mime }); 
 | 
  
 | 
  //@ts-ignore 
 | 
  if (typeof navigator !== 'undefined' && navigator.msSaveOrOpenBlob) { 
 | 
    //@ts-ignore 兼容IE 
 | 
    navigator.msSaveOrOpenBlob(blob, filename); 
 | 
  } else { 
 | 
    // 创建一个临时的a标签用于触发下载 
 | 
    const a = document.createElement('a'); 
 | 
    const url = window.URL.createObjectURL(blob); 
 | 
    a.href = url; 
 | 
    a.download = filename; // 设置下载后的文件名 
 | 
    document.body.appendChild(a); 
 | 
    a.click(); // 触发下载 
 | 
    document.body.removeChild(a); 
 | 
    setTimeout(() => { 
 | 
      window.URL.revokeObjectURL(url); // 清除创建的对象URL 
 | 
    }, 0); 
 | 
  } 
 | 
} 
 |