mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import getRealRoute from 'uni-helpers/get-real-route'
 
const SCHEME_RE = /^([a-z-]+:)?\/\//i
const DATA_RE = /^data:.*,.*/
 
// 处理 Android 平台解压与非解压模式下获取的路径不一致的情况
function handleLocalPath (filePath) {
  return plus.io.convertLocalFileSystemURL(filePath)
    .replace(/^\/?apps\//, '/android_asset/apps/')
    .replace(/\/$/, '')
}
 
let wwwPath
 
function addBase (filePath) {
  if (!wwwPath) { // 需要时,初始化一次,外部直接初始化,需要等 plusready
    wwwPath = 'file://' + handleLocalPath('_www') + '/'
  }
  return wwwPath + filePath
}
 
export default function getRealPath (filePath) {
  if (filePath.indexOf('/') === 0) {
    if (filePath.indexOf('//') === 0) {
      return 'https:' + filePath
    }
    // 平台绝对路径 安卓、iOS
    if (filePath.startsWith('/storage/') || filePath.startsWith('/sdcard/') || filePath.includes('/Containers/Data/Application/')) {
      return 'file://' + filePath
    }
    return addBase(filePath.substr(1))
  }
  // 网络资源或base64
  if (SCHEME_RE.test(filePath) || DATA_RE.test(filePath) || filePath.indexOf('blob:') === 0) {
    return filePath
  }
 
  // _do=>_doc,_documents,_downloads
  if (filePath.indexOf('_www') === 0 || filePath.indexOf('_do') === 0) {
    return 'file://' + handleLocalPath(filePath)
  }
 
  const pages = getCurrentPages()
  if (pages.length) {
    return addBase(getRealRoute(pages[pages.length - 1].$page.route, filePath).substr(1))
  }
 
  return filePath
}