qt.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { QWebChannel } from '@/utils/qt/qwebchannel.js';
  2. declare global {
  3. interface Window {
  4. qt: {
  5. webChannelTransport: {
  6. send: (payload: any) => void;
  7. onmessage: (payload: any) => void;
  8. };
  9. };
  10. }
  11. }
  12. function base64ToUint8Array(base64String: string) {
  13. const padding = '='.repeat((4 - (base64String.length % 4)) % 4);
  14. const base64 = (base64String + padding).replace(/\-/g, '+').replace(/_/g, '/');
  15. const rawData = window.atob(base64);
  16. const outputArray = new Uint8Array(rawData.length);
  17. for (let i = 0; i < rawData.length; ++i) {
  18. outputArray[i] = rawData.charCodeAt(i);
  19. }
  20. return outputArray;
  21. }
  22. export function qtAction() {
  23. // 获取 与qt交互实例
  24. let qtWebChannel: any = null;
  25. const webChannelTransport = window.qt?.webChannelTransport;
  26. if (webChannelTransport) {
  27. new QWebChannel(webChannelTransport, (channel: any) => {
  28. // all published objects are available in channel.objects under
  29. // the identifier set in their attached WebChannel.id property
  30. qtWebChannel = channel.objects.bridge;
  31. console.log('qtWebChannel', qtWebChannel);
  32. });
  33. }
  34. function getQtInfo(): Promise<Uint8Array[]> {
  35. if (qtWebChannel) {
  36. return qtWebChannel.getSystemInfo().then((res: any) => {
  37. return base64ToUint8Array(res);
  38. });
  39. } else {
  40. console.warn('qtWebChannel is null');
  41. return Promise.resolve([]);
  42. }
  43. }
  44. return { getQtInfo };
  45. }