|
@@ -11,36 +11,88 @@ export interface globalAIO {
|
|
|
|
|
|
declare var window: Window & typeof globalThis & { AIO: globalAIO };
|
|
|
|
|
|
+if (typeof Promise.withResolvers !== 'function') {
|
|
|
+ Promise.withResolvers = function <T>() {
|
|
|
+ let resolve!: (value: T | PromiseLike<T>) => void;
|
|
|
+ let reject!: (reason?: any) => void;
|
|
|
+
|
|
|
+ const promise = new Promise<T>((res, rej) => {
|
|
|
+ resolve = res;
|
|
|
+ reject = rej;
|
|
|
+ });
|
|
|
+
|
|
|
+ return { promise, resolve, reject };
|
|
|
+ };
|
|
|
+}
|
|
|
+
|
|
|
+const platformInjection: Platform['injection'] = (config) => {
|
|
|
+ const callbackFn = config?.callbackFn ?? ((key) => `AIO_${key}_${Date.now()}_${Math.random().toString(36).slice(2)}`);
|
|
|
+ const clearFn = (key: string) => platform.__execution__map__.delete(key);
|
|
|
+
|
|
|
+ platform.__execution__map__ ??= new Map();
|
|
|
+ // @ts-ignore
|
|
|
+ platform.__execution__ ??= function (key, ...args) {
|
|
|
+ const callbackId = callbackFn(key);
|
|
|
+ const { promise, ...executor } = Promise.withResolvers<string>();
|
|
|
+ try {
|
|
|
+ if (!window.platformAIO[key](callbackId, ...args)) throw new ReferenceError(`执行 ${key} 方法失败`);
|
|
|
+ } catch (e) {
|
|
|
+ executor.reject(e);
|
|
|
+ }
|
|
|
+ this.__execution__map__.set(callbackId, executor);
|
|
|
+ promise.then(
|
|
|
+ () => clearFn(key),
|
|
|
+ () => clearFn(key)
|
|
|
+ );
|
|
|
+ return promise.then((response) => {
|
|
|
+ try {
|
|
|
+ const res = JSON.parse(response);
|
|
|
+ if (res && typeof res === 'object') {
|
|
|
+ if (res?.code === 0) return res.data;
|
|
|
+ return Promise.reject(res);
|
|
|
+ }
|
|
|
+ return res as string;
|
|
|
+ } catch (e) {
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ };
|
|
|
+ platform.pulse ??= (id) => platform.__execution__('pulse', id);
|
|
|
+};
|
|
|
+
|
|
|
|
|
|
export default function bridgeLoader(): DEV.Loader {
|
|
|
+ window.platform ??= new EventTarget() as Platform;
|
|
|
+ if (typeof window.platform.injection !== 'function') window.platform.injection = platformInjection;
|
|
|
+ window.platform.injection();
|
|
|
+
|
|
|
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(() => {});
|
|
|
|
|
|
- if ( data ) {
|
|
|
- const path = data?.path ?? (
|
|
|
- route?.path === '/screen' ? await processMethod() : route?.path
|
|
|
- );
|
|
|
+ if (data) {
|
|
|
+ const path = data?.path ?? (route?.path === '/screen' ? await processMethod() : route?.path);
|
|
|
const key = Date.now();
|
|
|
- sessionStorage.setItem(`scan_${ key }`, JSON.stringify(data));
|
|
|
+ sessionStorage.setItem(`scan_${key}`, JSON.stringify(data));
|
|
|
await router.replace({ path, query: { scan: key } });
|
|
|
Toast.success('扫码成功');
|
|
|
}
|
|
|
toast.close();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
return async function() {
|
|
|
window.AIO ??= {} as globalAIO;
|
|
|
|
|
|
window.AIO.scan = (value) => {
|
|
|
- if ( !value ) return -1;
|
|
|
+ if (!value) return -1;
|
|
|
|
|
|
const route = unref(router.currentRoute);
|
|
|
|
|
|
- if ( !route.meta?.scan ) {
|
|
|
- import(`@/platform/notify.ui`).then(({ Notify }) => { Notify.warning(`请返回首页后,再进行扫码!`); });
|
|
|
+ if (!route.meta?.scan) {
|
|
|
+ import(`@/platform/notify.ui`).then(({ Notify }) => {
|
|
|
+ Notify.warning(`请返回首页后,再进行扫码!`);
|
|
|
+ });
|
|
|
return 1;
|
|
|
}
|
|
|
scan(value, route);
|
|
@@ -48,9 +100,7 @@ export default function bridgeLoader(): DEV.Loader {
|
|
|
};
|
|
|
|
|
|
window.AIO.print = (value) => {
|
|
|
- (
|
|
|
- window as any
|
|
|
- ).sixWisdom.printPdfByUrl(value);
|
|
|
+ (window as any).sixWisdom.printPdfByUrl(value);
|
|
|
};
|
|
|
};
|
|
|
}
|