| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- import { Component, signal } from '@angular/core';
- import { Router, RouterModule, RouterOutlet } from '@angular/router';
- import { AlertController } from '@ionic/angular/standalone';
- @Component({
- selector: 'app-root',
- standalone: true,
- imports: [RouterOutlet,RouterModule],
- templateUrl: './app.html',
- styleUrl: './app.scss',
- // providers:[AuthService]
- })
- export class App {
- protected readonly title = signal('yss-project');
- constructor(
- private router: Router,
- private alertController: AlertController
- ){
- this.initParse();
- this.initAuth();
- this.initFmodeApi();
- this.registerGlobalAlertEvents();
- }
- // 初始化Parse配置
- private initParse(): void {
- try {
- // 延迟加载 FmodeParse 以避免初始化问题
- import('fmode-ng/parse').then(({ FmodeParse }) => {
- const Parse = FmodeParse.with("nova");
- console.log('✅ FmodeParse 初始化成功');
- }).catch(error => {
- console.error('❌ FmodeParse 初始化失败:', error);
- });
- } catch (error) {
- console.error('❌ FmodeParse 初始化失败:', error);
- }
- }
- // 初始化企业微信认证
- private initAuth(): void {
- try {
- // 可以在这里做一些全局的认证配置
- console.log('✅ 企业微信认证模块初始化成功');
- } catch (error) {
- console.error('❌ 企业微信认证初始化失败:', error);
- }
- }
- // 注册全局 Alert 事件(信息提示、确认、输入)
- private registerGlobalAlertEvents(): void {
- window.addEventListener('globalAlert', (e: Event) => {
- const evt = e as CustomEvent<any>;
- this.handleGlobalAlert(evt.detail);
- });
- window.addEventListener('globalPrompt', (e: Event) => {
- const evt = e as CustomEvent<any>;
- const detail = evt.detail || {};
- this.presentPrompt(detail);
- });
- }
- // 挂载到 window.fmode API,支持直接函数调用
- private initFmodeApi(): void {
- try {
- const fmode: any = (window as any).fmode || {};
- fmode.alert = (detail: any) => this.handleGlobalAlert(detail);
- // 新增:confirm 简化方法,返回 boolean(true 确认,false 取消)
- fmode.confirm = async (messageOrDetail: any): Promise<boolean> => {
- const detail = typeof messageOrDetail === 'string' ? {
- header: '确认',
- message: messageOrDetail,
- buttons: [
- { text: '取消', role: 'cancel' },
- { text: '确定', role: 'confirm' }
- ]
- } : (messageOrDetail || {});
- const result = await this.handleGlobalAlert(detail);
- return result?.role === 'confirm';
- };
- // 支持两种用法:
- // 1) 对象用法:window.fmode.input({ header, message, inputs, buttons, callback }) 保留原功能
- // 2) 字符串简化用法:window.fmode.input('描述', '默认值'),行为与await window?.fmode?.input() 一致,返回输入值或 null
- fmode.input = (detailOrMessage: any, defaultValue?: string) => {
- if (typeof detailOrMessage === 'string') {
- return this.presentPromptSimple(detailOrMessage, defaultValue);
- }
- return this.presentPrompt(detailOrMessage);
- };
- (window as any).fmode = fmode;
- console.log('✅ window.fmode API 已挂载');
- } catch (error) {
- console.error('❌ 挂载 window.fmode 失败:', error);
- }
- }
- // 处理全局信息提示/确认
- private async handleGlobalAlert(detail: any): Promise<any> {
- const header = detail?.header ?? '提示';
- const subHeader = detail?.subHeader ?? undefined;
- const message = detail?.message ?? '';
- const buttons = detail?.buttons ?? [
- { text: '确定', role: 'confirm' }
- ];
- const alert = await this.alertController.create({
- header,
- subHeader,
- message,
- buttons
- });
- await alert.present();
- const result = await alert.onDidDismiss();
- this.returnResult(detail, result);
- return result;
- }
- // 全局输入弹窗
- private async presentPrompt(detail: any): Promise<any> {
- const header = detail?.header ?? '请输入';
- const subHeader = detail?.subHeader ?? undefined;
- const message = detail?.message ?? '';
- const inputs = detail?.inputs ?? [
- { name: 'value', type: 'text', placeholder: '请输入内容' }
- ];
- const buttons = detail?.buttons ?? [
- { text: '取消', role: 'cancel' },
- { text: '确定', role: 'confirm' }
- ];
- const alert = await this.alertController.create({
- header,
- subHeader,
- message,
- inputs,
- buttons
- });
- await alert.present();
- const result = await alert.onDidDismiss();
- this.returnResult(detail, result);
- return result;
- }
- // 字符串简化用法:行为与原生await window?.fmode?.input() 相同,返回输入字符串或 null
- private async presentPromptSimple(message: string, defaultValue?: string): Promise<string | null> {
- const alert = await this.alertController.create({
- header: '请输入',
- message,
- inputs: [
- { name: 'value', type: 'text', placeholder: '请输入内容', value: defaultValue ?? '' }
- ],
- buttons: [
- { text: '取消', role: 'cancel' },
- {
- text: '确定',
- role: 'confirm',
- handler: (value: any) => {
- // 主动传递输入值作为 data,确保 onDidDismiss() 可获取字符串
- const v = value?.value ?? '';
- alert.dismiss(v, 'confirm');
- return false; // 阻止默认关闭,使用我们主动 dismiss
- }
- }
- ]
- });
- await alert.present();
- const result = await alert.onDidDismiss();
- if (result.role === 'confirm') {
- return (result.data as string) ?? '';
- }
- return null;
- }
- // 结果返回:优先调用回调,其次派发结果事件
- private returnResult(detail: any, result: any): void {
- try {
- if (typeof detail?.callback === 'function') {
- detail.callback(result);
- return;
- }
- const requestId = detail?.requestId ?? undefined;
- window.dispatchEvent(new CustomEvent('globalAlert:result', {
- detail: { requestId, result }
- }));
- } catch (err) {
- console.error('全局Alert结果处理异常:', err);
- }
- }
- }
|