微信小程序中的url-safe:// URI 是一种特殊的 URL,它可以包含特殊字符(如空格、`%27`、`&`等)和一些特殊的编码格式。这种 URL 主要用于小程序的组件之间传递数据,或者在组件中加载本地文件等场景。
获取加密的 url-safe:// URI 主要有以下几种方式:
1. 直接使用 `wx.getSystemInfoSync()` 方法获取系统信息,其中包含了一个名为 `systemInfo.userAgent.scheme` 的属性。这个属性的值就是当前系统的 URI 协议,包括了 `http://`、`https://`、`file://` 以及 `data:` 等。通过判断这个属性,可以获取到当前的 URI 协议,然后通过拼接其他参数来获取加密的 url-safe:// URI。
```javascript
const systemInfo = wx.getSystemInfoSync();
const scheme = systemInfo.userAgent.scheme;
const url = scheme + '://' + scheme + '://';
```
2. 使用 `wx.createWebView()` 方法创建一个 WebView 组件,并通过监听其 `loadstart` 事件来判断是否已经加载了加密的 url-safe:// URI。
```javascript
const webView = wx.createWebView({
// ...
});
webView.onLoadstart = function (event) {
if (event.detail.url.startsWith('https://')) {
// 如果是 https,则说明已经加载了加密的 url-safe:// URI
console.log(event.detail.url);
}
};
```
3. 使用 `wx.getFileSystemManager().getRootPath()` 方法获取根目录路径,然后通过拼接其他参数来获取加密的 url-safe:// URI。
```javascript
const rootPath = wx.getFileSystemManager().getRootPath();
const urlPath = rootPath + '/' + rootPath + '/' + 'encrypted_url.js';
```
4. 使用 `wx.getFileSystemManager().getFileInfoSync()` 方法获取文件信息,然后通过拼接其他参数来获取加密的 url-safe:// URI。
```javascript
const fileInfo = wx.getFileSystemManager().getFileInfoSync(urlPath);
const url = fileInfo.path + '?scheme=' + scheme;
```
以上四种方法都可以获取到加密的 url-safe:// URI,但第一种方法比较简单,适用于不需要特别处理的场景。而第二种方法则可以用于监听加载开始事件,以便在需要时进行处理。第三种和第四种方法则需要根据具体情况进行选择,因为这两种方法都需要知道根目录路径或文件信息,这可能会带来一定的性能开销。