桌面端 API

注意

使用桌面端 API 的前提是在更多配置中,开启全局 TauriApi,然后才可以打包发布使用!

桌面端 API 主要是集成了 tauri2 的所有 api 接口,还有 PakePlus 自定义的一些 api 接口,一定要开启全局 TauriApi,然后才可以打包发布使用,否则不生效的。开发过程中,可以通过 window.TAURI 来查看有哪些 api 接口,并可以查看接口类型。

JS 脚本中使用

  1. // core
  2. const {
  3. addPluginListener,
  4. invoke,
  5. Channel,
  6. checkPermissions,
  7. convertFileSrc,
  8. isTauri,
  9. PluginListener,
  10. requestPermissions,
  11. Resource,
  12. transformCallback,
  13. SERIALIZE_TO_IPC_FN,
  14. } = window.__TAURI__.core
  15. // app
  16. const {
  17. defaultWindowIcon,
  18. fetchDataStoreIdentifiers,
  19. getIdentifier,
  20. getName,
  21. getTauriVersion,
  22. getVersion,
  23. hide,
  24. removeDataStore,
  25. setDockVisibility,
  26. setTheme,
  27. show,
  28. } = window.__TAURI__.app
  29. // event
  30. const { emit, emitTo, listen, once } = window.__TAURI__.event
  31. // dpi
  32. const {
  33. LogicalPosition,
  34. LogicalSize,
  35. PhysicalPosition,
  36. PhysicalSize,
  37. Position,
  38. Size,
  39. } = window.__TAURI__.dpi
  40. // image
  41. const { Image, transformImage } = window.__TAURI__.image
  42. // menu
  43. const {
  44. CheckMenuItem,
  45. IconMenuItem,
  46. itemFromKind,
  47. Menu,
  48. MenuItem,
  49. NativeIcon,
  50. PredefinedMenuItem,
  51. Submenu,
  52. } = window.__TAURI__.menu
  53. // path
  54. const {
  55. appDataDir,
  56. appConfigDir,
  57. appLocalDataDir,
  58. appCacheDir,
  59. appLogDir,
  60. audioDir,
  61. cacheDir,
  62. configDir,
  63. dataDir,
  64. desktopDir,
  65. documentDir,
  66. downloadDir,
  67. executableDir,
  68. fontDir,
  69. homeDir,
  70. pictureDir,
  71. publicDir,
  72. resourceDir,
  73. runtimeDir,
  74. templateDir,
  75. videoDir,
  76. sep,
  77. delimiter,
  78. basename,
  79. dirname,
  80. extname,
  81. join,
  82. normalize,
  83. resolve,
  84. isAbsolute,
  85. localDataDir,
  86. resolveResource,
  87. tempDir,
  88. } = window.__TAURI__.path
  89. // 调用新建窗口等函数
  90. const { WebviewWindow } = window.__TAURI__.webviewWindow
  91. // 等等接口,请参考Tauri2官方文档:https://v2.tauri.app/reference/javascript/api/#vanilla-js-api

Vue/React/Next/等使用

  1. // 安装依赖,就可以支持类型提示等
  2. // core api
  3. pnpm install @tauri-apps/api
  4. // dialog api
  5. pnpm install @tauri-apps/plugin-dialog
  6. // fs api
  7. pnpm install @tauri-apps/plugin-fs
  8. // os api
  9. pnpm install @tauri-apps/plugin-os
  10. // 等等接口,请参考Tauri2官方文档:https://v2.tauri.app/reference/javascript/api/#vanilla-js-api

Tauri2Api

文档待更新……

Tauri2PluginApi

文档待更新……

PakePlusApi

本章后面的 API 接口是 PakePlus 开发的一些 rust 后端接口,可以在 js 中直接调用使用,也仅仅只能在 PakePlus 项目中打包使用。请勿将此 API 用于原生的 tauri 项目。

打开 URL(本窗口)

在脚本中添加以下代码,即可实现打开 URL(本窗口)

  1. const hookClick = (e) => {
  2. const origin = e.target.closest('a')
  3. const isBaseTargetBlank = document.querySelector(
  4. 'head base[target="_blank"]'
  5. )
  6. if (
  7. (origin && origin.href && origin.target === '_blank') ||
  8. (origin && origin.href && isBaseTargetBlank)
  9. ) {
  10. e.preventDefault()
  11. location.href = origin.href
  12. }
  13. }
  14. window.open = function (url, target, features) {
  15. console.log('open', url, target, features)
  16. location.href = url
  17. }
  18. document.addEventListener('click', hookClick, { capture: true })

打开 URL(新窗口)

在脚本中添加以下代码,即可实现打开 URL(新窗口)

  1. const { WebviewWindow } = window.__TAURI__.webviewWindow
  2. const webview = new WebviewWindow('my-label', {
  3. url: 'https://pakeplus.com/',
  4. x: 500,
  5. y: 500,
  6. width: 800,
  7. height: 400,
  8. focus: true,
  9. title: 'PakePlus Window',
  10. alwaysOnTop: true,
  11. center: true,
  12. resizable: true,
  13. transparent: false,
  14. visible: true,
  15. })
  16. webview.once('tauri://created', function () {
  17. // webview successfully created
  18. console.log('new webview created')
  19. })
  20. webview.once('tauri://error', function (e) {
  21. // an error happened creating the webview
  22. console.log('new webview error', e)
  23. })

打开 URL(默认浏览器)

在脚本中添加以下代码,即可实现打开 URL(默认浏览器)

  1. const { invoke } = window.__TAURI__.core
  2. const hookClick = (e) => {
  3. const origin = e.target.closest('a')
  4. const isBaseTargetBlank = document.querySelector(
  5. 'head base[target="_blank"]'
  6. )
  7. if (
  8. (origin && origin.href && origin.target === '_blank') ||
  9. (origin && origin.href && isBaseTargetBlank)
  10. ) {
  11. e.preventDefault()
  12. invoke('open_url', { url: origin.href })
  13. }
  14. }
  15. window.open = function (url, target, features) {
  16. invoke('open_url', { url: url })
  17. }
  18. document.addEventListener('click', hookClick, { capture: true })

下载文件

下载网络链接文件到本地,支持多文件下载,以及下载进度回调

js

  1. const { invoke } = window.__TAURI__.core
  2. if ('__TAURI__' in window) {
  3. await invoke('download_file', {
  4. url: 'https://www.baidu.com/img/flexible/logo/pc/result.png',
  5. savePath: 'test.png',
  6. fileId: 'test',
  7. })
  8. }

下载进度

在脚本中添加以下代码,即可监听下载进度回调

js

  1. const { listen } = window.__TAURI__.event
  2. listen('download_progress', (event: any) => {
  3. downloadProgress.value = Number(
  4. ((event.payload.downloaded / event.payload.total) * 100).toFixed(2)
  5. )
  6. })

执行命令

在脚本中添加以下代码,即可实现运行命令

js

  1. const { invoke } = window.__TAURI__.core
  2. if ('__TAURI__' in window) {
  3. invoke('run_command', { command: 'ls -l' })
  4. }