userApp.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // import { onMounted, ref, unref } from 'vue'
  2. import consola from 'consola';
  3. import { KanKanInstance } from '/#/sdk';
  4. import browser from '/@/utils/browser';
  5. const instance = (window as any).KanKan;
  6. let app: KanKanInstance;
  7. let _num: string;
  8. const deferred = instance.Deferred();
  9. interface appOptions {
  10. region?: string;
  11. resource?: string;
  12. server?: string;
  13. dom: HTMLElement;
  14. mobile?: boolean;
  15. num: string;
  16. }
  17. // onMounted(() => {
  18. // })
  19. // let deferred = KanKan.Deferred()
  20. export function createApp(options: appOptions): Promise<KanKanInstance> {
  21. if (!options?.region) {
  22. options.region = import.meta.env.VITE_APP_REGION_URL;
  23. }
  24. if (!options?.resource) {
  25. options.resource = import.meta.env.VITE_APP_RESOURCE_URL;
  26. }
  27. if (!options?.server) {
  28. options.server = '/';
  29. }
  30. console.log('options', options);
  31. _num = options.num;
  32. if (!instance) {
  33. consola.error('kankan SDK 没有引入!');
  34. }
  35. const _app = new instance(options);
  36. deferred.resolve(_app);
  37. (window as any).__sdk = _app;
  38. if (browser.isMobile()) {
  39. document.body.setAttribute('is-mobile', 'true');
  40. }
  41. app = _app;
  42. return Promise.resolve(app);
  43. }
  44. export function useApp(): Promise<KanKanInstance> {
  45. if (app) {
  46. return Promise.resolve(app);
  47. }
  48. return deferred;
  49. }
  50. export function getApp(): KanKanInstance {
  51. return app;
  52. }
  53. export function getNum(): string {
  54. return _num;
  55. }