合同小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.6 KiB

3 months ago
  1. // @ts-nocheck
  2. // fileToBase64, fileToDataURL,dataURLToFile
  3. export function fileToBase64(filePath : string) {
  4. return new Promise((resolve, reject)=>{
  5. if(uni.canIUse('getFileSystemManager')){
  6. uni.getFileSystemManager().readFile({
  7. filePath: path,
  8. encoding: 'base64',
  9. success: (res) => {
  10. resolve(res.data)
  11. },
  12. fail: (error) => {
  13. console.error({ error, path })
  14. reject(error)
  15. }
  16. })
  17. } else {
  18. reject('fileToBase64:环境不支持')
  19. }
  20. })
  21. }
  22. export function fileToDataURL(filePath : string) {
  23. let extension = path.substring(path.lastIndexOf('.') + 1);
  24. const imageExtensions = ["jpg", "jpeg", "png", "gif", "bmp", "svg"];
  25. const isImageFile = imageExtensions.includes(extension.toLowerCase());
  26. let prefix = ''
  27. if (isImageFile) {
  28. prefix = 'image/';
  29. if(extension == 'svg') {
  30. extension += '+xml'
  31. }
  32. } else if (extension === 'pdf') {
  33. prefix = 'application/pdf';
  34. } else if (extension === 'txt') {
  35. prefix = 'text/plain';
  36. } else {
  37. // 添加更多文件类型的判断
  38. // 如果不是图片、PDF、文本等类型,可以设定默认的前缀或采取其他处理
  39. prefix = 'application/octet-stream';
  40. }
  41. return fileToBase64(filePath).then(res => `data:${prefix}${extension};base64,${res}`)
  42. }
  43. function getFileExtensionFromDataURL(dataURL : string) : string {
  44. const commaIndex = dataURL.indexOf(",");
  45. const mimeType = dataURL.substring(0, commaIndex).replace("data:", "").replace(";base64", "");
  46. const mimeTypeParts = mimeType.split("/");
  47. return mimeTypeParts[1];
  48. }
  49. function getPlatform():Uni {
  50. // #ifdef MP-WEIXIN
  51. return wx
  52. // #endif
  53. // #ifdef MP-BAIDU
  54. return swan
  55. // #endif
  56. // #ifdef MP-ALIPAY
  57. return my
  58. // #endif
  59. // #ifdef MP-JD
  60. return jd
  61. // #endif
  62. // #ifdef MP-QQ
  63. return qq
  64. // #endif
  65. // #ifdef MP-360
  66. return qh
  67. // #endif
  68. // #ifdef MP-KUAISHOU
  69. return ks
  70. // #endif
  71. // #ifdef MP-LARK||MP-TOUTIAO
  72. return tt
  73. // #endif
  74. // #ifdef MP-DINGTALK
  75. return dd
  76. // #endif
  77. // #ifdef QUICKAPP-WEBVIEW || QUICKAPP-WEBVIEW-UNION || QUICKAPP-WEBVIEW-HUAWEI
  78. return qa
  79. // #endif
  80. return uni
  81. }
  82. export function dataURLToFile(dataURL : string, filename : NullableString = null) {
  83. return new Promise((resolve, reject) => {
  84. const name = filename ?? `${Date.now()}.${getFileExtensionFromDataURL(dataURL)}`;
  85. const commaIndex = dataURL.indexOf(",");
  86. const base64 = dataURL.substring(commaIndex + 1);
  87. const platform = getPlatform()
  88. const filePath = `${platform.env.USER_DATA_PATH}/${name}`;
  89. fs.writeFile({
  90. filePath,
  91. data: base64,
  92. encoding: 'base64',
  93. success() {
  94. resolve(filePath)
  95. },
  96. fail(err) {
  97. reject(err)
  98. }
  99. })
  100. })
  101. }