app.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { Component, signal } from '@angular/core';
  2. import { Router, RouterModule, RouterOutlet } from '@angular/router';
  3. import { AlertController } from '@ionic/angular/standalone';
  4. @Component({
  5. selector: 'app-root',
  6. standalone: true,
  7. imports: [RouterOutlet,RouterModule],
  8. templateUrl: './app.html',
  9. styleUrl: './app.scss',
  10. // providers:[AuthService]
  11. })
  12. export class App {
  13. protected readonly title = signal('yss-project');
  14. constructor(
  15. private router: Router,
  16. private alertController: AlertController
  17. ){
  18. this.initParse();
  19. this.initAuth();
  20. this.initFmodeApi();
  21. this.registerGlobalAlertEvents();
  22. }
  23. // 初始化Parse配置
  24. private initParse(): void {
  25. try {
  26. // 延迟加载 FmodeParse 以避免初始化问题
  27. import('fmode-ng/parse').then(({ FmodeParse }) => {
  28. const Parse = FmodeParse.with("nova");
  29. console.log('✅ FmodeParse 初始化成功');
  30. }).catch(error => {
  31. console.error('❌ FmodeParse 初始化失败:', error);
  32. });
  33. } catch (error) {
  34. console.error('❌ FmodeParse 初始化失败:', error);
  35. }
  36. }
  37. // 初始化企业微信认证
  38. private initAuth(): void {
  39. try {
  40. // 可以在这里做一些全局的认证配置
  41. console.log('✅ 企业微信认证模块初始化成功');
  42. } catch (error) {
  43. console.error('❌ 企业微信认证初始化失败:', error);
  44. }
  45. }
  46. // 注册全局 Alert 事件(信息提示、确认、输入)
  47. private registerGlobalAlertEvents(): void {
  48. window.addEventListener('globalAlert', (e: Event) => {
  49. const evt = e as CustomEvent<any>;
  50. this.handleGlobalAlert(evt.detail);
  51. });
  52. window.addEventListener('globalPrompt', (e: Event) => {
  53. const evt = e as CustomEvent<any>;
  54. const detail = evt.detail || {};
  55. this.presentPrompt(detail);
  56. });
  57. }
  58. // 挂载到 window.fmode API,支持直接函数调用
  59. private initFmodeApi(): void {
  60. try {
  61. const fmode: any = (window as any).fmode || {};
  62. fmode.alert = (detail: any) => this.handleGlobalAlert(detail);
  63. // 新增:confirm 简化方法,返回 boolean(true 确认,false 取消)
  64. fmode.confirm = async (messageOrDetail: any): Promise<boolean> => {
  65. const detail = typeof messageOrDetail === 'string' ? {
  66. header: '确认',
  67. message: messageOrDetail,
  68. buttons: [
  69. { text: '取消', role: 'cancel' },
  70. { text: '确定', role: 'confirm' }
  71. ]
  72. } : (messageOrDetail || {});
  73. const result = await this.handleGlobalAlert(detail);
  74. return result?.role === 'confirm';
  75. };
  76. // 支持两种用法:
  77. // 1) 对象用法:window.fmode.input({ header, message, inputs, buttons, callback }) 保留原功能
  78. // 2) 字符串简化用法:window.fmode.input('描述', '默认值'),行为与await window?.fmode?.input() 一致,返回输入值或 null
  79. fmode.input = (detailOrMessage: any, defaultValue?: string) => {
  80. if (typeof detailOrMessage === 'string') {
  81. return this.presentPromptSimple(detailOrMessage, defaultValue);
  82. }
  83. return this.presentPrompt(detailOrMessage);
  84. };
  85. (window as any).fmode = fmode;
  86. console.log('✅ window.fmode API 已挂载');
  87. } catch (error) {
  88. console.error('❌ 挂载 window.fmode 失败:', error);
  89. }
  90. }
  91. // 处理全局信息提示/确认
  92. private async handleGlobalAlert(detail: any): Promise<any> {
  93. const header = detail?.header ?? '提示';
  94. const subHeader = detail?.subHeader ?? undefined;
  95. const message = detail?.message ?? '';
  96. const buttons = detail?.buttons ?? [
  97. { text: '确定', role: 'confirm' }
  98. ];
  99. const alert = await this.alertController.create({
  100. header,
  101. subHeader,
  102. message,
  103. buttons
  104. });
  105. await alert.present();
  106. const result = await alert.onDidDismiss();
  107. this.returnResult(detail, result);
  108. return result;
  109. }
  110. // 全局输入弹窗
  111. private async presentPrompt(detail: any): Promise<any> {
  112. const header = detail?.header ?? '请输入';
  113. const subHeader = detail?.subHeader ?? undefined;
  114. const message = detail?.message ?? '';
  115. const inputs = detail?.inputs ?? [
  116. { name: 'value', type: 'text', placeholder: '请输入内容' }
  117. ];
  118. const buttons = detail?.buttons ?? [
  119. { text: '取消', role: 'cancel' },
  120. { text: '确定', role: 'confirm' }
  121. ];
  122. const alert = await this.alertController.create({
  123. header,
  124. subHeader,
  125. message,
  126. inputs,
  127. buttons
  128. });
  129. await alert.present();
  130. const result = await alert.onDidDismiss();
  131. this.returnResult(detail, result);
  132. return result;
  133. }
  134. // 字符串简化用法:行为与原生await window?.fmode?.input() 相同,返回输入字符串或 null
  135. private async presentPromptSimple(message: string, defaultValue?: string): Promise<string | null> {
  136. const alert = await this.alertController.create({
  137. header: '请输入',
  138. message,
  139. inputs: [
  140. { name: 'value', type: 'text', placeholder: '请输入内容', value: defaultValue ?? '' }
  141. ],
  142. buttons: [
  143. { text: '取消', role: 'cancel' },
  144. {
  145. text: '确定',
  146. role: 'confirm',
  147. handler: (value: any) => {
  148. // 主动传递输入值作为 data,确保 onDidDismiss() 可获取字符串
  149. const v = value?.value ?? '';
  150. alert.dismiss(v, 'confirm');
  151. return false; // 阻止默认关闭,使用我们主动 dismiss
  152. }
  153. }
  154. ]
  155. });
  156. await alert.present();
  157. const result = await alert.onDidDismiss();
  158. if (result.role === 'confirm') {
  159. return (result.data as string) ?? '';
  160. }
  161. return null;
  162. }
  163. // 结果返回:优先调用回调,其次派发结果事件
  164. private returnResult(detail: any, result: any): void {
  165. try {
  166. if (typeof detail?.callback === 'function') {
  167. detail.callback(result);
  168. return;
  169. }
  170. const requestId = detail?.requestId ?? undefined;
  171. window.dispatchEvent(new CustomEvent('globalAlert:result', {
  172. detail: { requestId, result }
  173. }));
  174. } catch (err) {
  175. console.error('全局Alert结果处理异常:', err);
  176. }
  177. }
  178. }