|
@@ -1,56 +1,60 @@
|
|
import { processMethod, scanAccountMethod } from '@/request/api';
|
|
import { processMethod, scanAccountMethod } from '@/request/api';
|
|
import router from '@/router';
|
|
import router from '@/router';
|
|
|
|
|
|
-import type { RouteLocation } from 'vue-router';
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-export interface globalAIO {
|
|
|
|
- scan(value: string): number;
|
|
|
|
- print(value: string): void;
|
|
|
|
|
|
+import { platformIsAIO } from '@/platform';
|
|
|
|
+
|
|
|
|
+export function waitFor(condition: () => boolean | Promise<boolean>, timeout: number = 300 * 1000) {
|
|
|
|
+ const start = Date.now();
|
|
|
|
+ const { promise, resolve, reject } = Promise.withResolvers<void>();
|
|
|
|
+ const check = async () => {
|
|
|
|
+ try {
|
|
|
|
+ if (await condition()) resolve();
|
|
|
|
+ else if (timeout && Date.now() - start >= timeout) reject({ message: 'waitForBridge timeout' });
|
|
|
|
+ else requestAnimationFrame(check);
|
|
|
|
+ } catch (e) {
|
|
|
|
+ reject(e);
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+ return check().then(
|
|
|
|
+ () => promise,
|
|
|
|
+ () => promise
|
|
|
|
+ );
|
|
}
|
|
}
|
|
|
|
|
|
-declare var window: Window & typeof globalThis & { AIO: globalAIO };
|
|
|
|
-
|
|
|
|
-
|
|
|
|
export default function bridgeLoader(): DEV.Loader {
|
|
export default function bridgeLoader(): DEV.Loader {
|
|
- async function scan(value: string, route?: RouteLocation) {
|
|
|
|
- const { Toast } = await import(`@/platform/toast.ui`);
|
|
|
|
- const toast = Toast.loading(100, { message: '加载中' });
|
|
|
|
- const data = await scanAccountMethod(value).catch(() => {});
|
|
|
|
|
|
+ window.AIO ??= {};
|
|
|
|
+ window.AIO.scan ??= (value) => {
|
|
|
|
+ if (!value) return -1;
|
|
|
|
+ const event = new CustomEvent('scan', { detail: { code: value, state: 0, type: -1 } });
|
|
|
|
+ Bridge.getInstance().dispatchEvent(event);
|
|
|
|
+ return 0;
|
|
|
|
+ };
|
|
|
|
+ window.AIO.print ??= (value) => {
|
|
|
|
+ (window as any).sixWisdom.printPdfByUrl(value);
|
|
|
|
+ };
|
|
|
|
|
|
- if ( data ) {
|
|
|
|
- const path = data?.path ?? (
|
|
|
|
- route?.path === '/screen' ? await processMethod() : route?.path
|
|
|
|
- );
|
|
|
|
- const key = Date.now();
|
|
|
|
- sessionStorage.setItem(`scan_${ key }`, JSON.stringify(data));
|
|
|
|
- await router.replace({ path, query: { scan: key } });
|
|
|
|
- Toast.success('扫码成功');
|
|
|
|
|
|
+ return async function () {
|
|
|
|
+ if (platformIsAIO()) {
|
|
|
|
+ await waitFor(() => window.bridge != null);
|
|
|
|
+ Bridge.getInstance().addEventListener('scan', async ({ detail }) => {
|
|
|
|
+ if (detail.code !== 0 || detail.data?.code == null) return;
|
|
|
|
+ const route = unref(router.currentRoute);
|
|
|
|
+ if (route.meta?.scan) {
|
|
|
|
+ const { Toast } = await import(`@/platform/toast.ui`);
|
|
|
|
+ const toast = Toast.loading(100, { message: '加载中' });
|
|
|
|
+ const data = await scanAccountMethod(detail.data.code).catch(() => {});
|
|
|
|
+
|
|
|
|
+ if (data) {
|
|
|
|
+ const path = data?.path ?? (route?.path === '/screen' ? await processMethod() : route?.path);
|
|
|
|
+ const key = Date.now();
|
|
|
|
+ sessionStorage.setItem(`scan_${key}`, JSON.stringify(data));
|
|
|
|
+ await router.replace({ path, query: { scan: key } });
|
|
|
|
+ Toast.success('扫码成功');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ toast.close();
|
|
|
|
+ } else import(`@/platform/notify.ui`).then(({ Notify }) => { Notify.warning(`请返回首页后,再进行扫码!`); });
|
|
|
|
+ });
|
|
}
|
|
}
|
|
- toast.close();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- return async function() {
|
|
|
|
- window.AIO ??= {} as globalAIO;
|
|
|
|
-
|
|
|
|
- window.AIO.scan = (value) => {
|
|
|
|
- if ( !value ) return -1;
|
|
|
|
-
|
|
|
|
- const route = unref(router.currentRoute);
|
|
|
|
-
|
|
|
|
- if ( !route.meta?.scan ) {
|
|
|
|
- import(`@/platform/notify.ui`).then(({ Notify }) => { Notify.warning(`请返回首页后,再进行扫码!`); });
|
|
|
|
- return 1;
|
|
|
|
- }
|
|
|
|
- scan(value, route);
|
|
|
|
- return 0;
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- window.AIO.print = (value) => {
|
|
|
|
- (
|
|
|
|
- window as any
|
|
|
|
- ).sixWisdom.printPdfByUrl(value);
|
|
|
|
- };
|
|
|
|
};
|
|
};
|
|
}
|
|
}
|