import { AppConfig, AppFuncConfig, AppViewConfig } from '@/config'; import { AppCodeListConfig } from '@/config/app-code-list-config'; import { AppCounterConfig } from '@/config/app-counter-config'; import { AppEntityConfig } from '@/config/app-entity-config'; import { AppThemeConfig } from '@/config/app-theme-config'; import { AppViewMsgConfig } from '@/config/app-view-msg-config'; import { ProjectSetting } from '@/setting/project-setting'; import { IApp, IAppCodeListHelper, IAppData, IAppFunc, IAppFuncHelper, IContext, IGlobalActionHelper, IDialogHelper, INotificationHelper, IOpenViewHelper, IParam, IUIService, IViewLogicHelper, ILoadingHelper, AppLoadingHelper, IAppViewMsgHelper, IAppCounterHelper, IEntityDetail, IGlobalNotificationHelper, IProjectSetting, IDevActionHelper, IAppConfig, ICustomDialogOptions, } from '@/core'; import { AppController } from '@/core/controller/app/app-controller'; import { AppCodeListHelper, AppFuncHelper, DialogHelper, NotificationHelper, OpenViewHelper, AppViewMsgHelper, AppCounterHelper, GlobalNotificationHelper, GlobalActionHelper, ViewLogicHelper, } from '@/helper'; import { Http } from '@/http'; import { i18n } from '@/locales'; import { EntityServiceRegister, UIServiceRegister } from '@/register'; import { router } from '@/router'; import { useAppStoreWithOut, useAuthResourceStoreWithOut } from '@/store'; import { getDcSystemIdViewParam } from '@/utils'; import { clearCookie, setCookie } from 'qx-util'; import { DevActionHelper } from '@/helper/dev-action-helper'; import { createVNode } from 'vue'; import { QuestionCircleOutlined } from '@ant-design/icons-vue'; /** * 应用 * * @export * @class App * @extends {AppController} */ export class App extends AppController { /** * 应用对象 * * @private * @static * @type {IApp} * @memberof App */ private static app: IApp; /** * 界面应用对象 * * @private * @type {IParam} * @memberof App */ private UIAppInstance: IParam | undefined; /** * 获取实例对象 * * @public * @static * @return {*} {IApp} * @memberof App */ public static getInstance(): IApp { if (!this.app) { this.app = new App(); } return this.app; } /** * 获取应用功能辅助类 * * @return {*} {IAppFuncHelper} * @memberof App */ getAppFuncHelper(): IAppFuncHelper { return AppFuncHelper.getInstance(); } /** * 获取打开视图辅助类 * * @return {*} {IOpenViewHelper} * @memberof App */ getOpenViewHelper(): IOpenViewHelper { return OpenViewHelper.getInstance(); } /** * 获取代码表辅助类 * @returns */ getAppCodeListHelper(): IAppCodeListHelper { return AppCodeListHelper.getInstance(); } /** * 获取视图消息辅助类 * @returns */ getAppViewMsgHelper(): IAppViewMsgHelper { return AppViewMsgHelper.getInstance(); } /** * 获取计数器辅助类 * @returns */ getAppCounterHelper(): IAppCounterHelper { return AppCounterHelper.getInstance(); } /** * 获取应用加载辅助类 * * @return {*} {ILoadingHelper} * @memberof App */ getAppLoadingHelper(): ILoadingHelper { return AppLoadingHelper.getInstance(); } /** * 获取全局行为辅助类 * * @return {*} {IGlobalActionHelper} * @memberof App */ getGlobalActionHelper(): IGlobalActionHelper { return GlobalActionHelper.getInstance(); } /** * 获取全局通知助手类 * * @return {*} {IGlobalNotificationHelper} * @memberof App */ getGlobalNotificationHelper(): IGlobalNotificationHelper { return GlobalNotificationHelper.getInstance(); } /** * 获取视图逻辑辅助类 * * @return {*} {IViewLogicHelper} * @memberof App */ getViewLogicHelper(): IViewLogicHelper { return ViewLogicHelper.getInstance(); } /** * @description 获取消息提示辅助类 * @return {*} {INotificationHelper} * @memberof App */ getNotificationHelper(): INotificationHelper { return NotificationHelper.getInstance(); } /** * @description 获取对话框提示辅助类 * @return {*} {IDialogHelper} * @memberof App */ getDialogHelper(): IDialogHelper { return DialogHelper.getInstance(); } /** * 获取数据服务 * * @param {string} name * @param {(IContext)} [context] * @return {*} {Promise} * @memberof App */ getDataService(name: string, context?: IContext): Promise { return EntityServiceRegister.getInstance().getService(name, context); } /** * 获取界面服务 * * @param {string} name * @param {IContext} [context] * @return {*} {Promise} * @memberof App */ getUIService(name: string, context?: IContext): Promise { return UIServiceRegister.getInstance().getService(name, context); } /** * 设置界面应用对象 * @param UIApp */ setUIAppInstance(UIApp: IParam): void { this.UIAppInstance = UIApp; } /** * 获取界面应用对象 */ getUIAppInstance(): IParam | undefined { return this.UIAppInstance; } /** * 获取视图配置参数 * * @return {*} {IParam} * @memberof App */ getAppViewConfig(): IParam { return AppViewConfig; } /** * 获取主题配置参数 * * @return {*} {IParam} * @memberof App */ getAppThemeConfig(): IParam[] { return AppThemeConfig; } /** * 获取项目设置 * * @return {*} {IProjectSetting} * @memberof App */ getProjectSetting(): IProjectSetting { return ProjectSetting; } /** * 获取应用功能配置参数 * * @return {*} {IAppFunc[]} * @memberof App */ getAppFuncConfig(): IAppFunc[] { return AppFuncConfig; } /** * 获取代码表配置参数 * * @return {*} {IParam} * @memberof App */ getAppCodeListConfig(): IParam { return AppCodeListConfig; } /** * 获取计数器配置参数 * * @return {*} {IParam} * @memberof App */ getAppCounterConfig(): IParam { return AppCounterConfig; } /** * 获取视图消息配置参数 * * @return {*} {IParam} * @memberof App */ getAppViewMsgConfig(): IParam { return AppViewMsgConfig; } /** * 获取应用实体配置信息 * * @return {*} {IEntityDetail[]} * @memberof App */ getAppEntityConfig(): IEntityDetail[] { return AppEntityConfig; } /** * 应用配置 * * @return {*} {IAppConfig} * @memberof App */ getAppConfig(): IAppConfig { return AppConfig; } /** * @description 获取应用是否启用权限 * @return {*} {boolean} * @memberof App */ getEnableAppPermission(): boolean { const { getEnablePermissionValid } = useAuthResourceStoreWithOut(); const enableAppPermission = this.getProjectSetting().enableAppPermission; return getEnablePermissionValid && enableAppPermission; } /** * @description 获取指定菜单是否存在权限 * @param {string} tag * @return {*} {boolean} * @memberof App */ getAppMenuAuth(tag: string): boolean { const { getMenuData, getResourceData } = useAuthResourceStoreWithOut(); if ( Object.is(this.getProjectSetting().menuSetting.menuPermissionMode, 'RT') ) { return getMenuData.some((authTag: string) => Object.is(authTag, tag)); } else { return getResourceData.some((resourceTag: string) => Object.is(resourceTag, tag) ); } } /** * @description 获取指定统一资源是否存在权限 * @param {string} tag * @return {*} {boolean} * @memberof App */ getAppUniresAuth(tag: string): boolean { const { getResourceData } = useAuthResourceStoreWithOut(); return getResourceData.some((codeName: string) => Object.is(codeName, tag)); } /** * 获取debug 控制器辅助器 * * @return {*} {IDevActionHelper} * @memberof App */ getDevActionHelper(): IDevActionHelper { return DevActionHelper.getInstance(); } /** * @description 跳转用户登录页 * @memberof App */ login(loginname?: string, password?: string): void { this.clearAppData(); if (loginname && password) { const headers = {}; const tempViewParam = getDcSystemIdViewParam(); if (tempViewParam && tempViewParam.srfdcsystem) { Object.assign(headers, { srfdcsystem: tempViewParam.srfdcsystem }); } const requestData = { loginname, password }; Http.getInstance() .post('/v7/login', requestData, headers, true) .then((response: any) => { if (response.success) { const data = response.data; if (data && data.token) { setCookie('ibzuaa-token', data.token, 7, true); } // 设置cookie,保存账号密码7天 setCookie('loginname', loginname, 7, true); // 跳转首页 const route: any = router.currentRoute; const url = route?.query?.redirect || '/'; router.push({ path: url }); return { ok: true, data: null }; } else { this.getNotificationHelper().warning(this.ts('app.notificationtitle.warn'), this.ts('function.generic.loginerror')); } }) .catch((error: any) => { this.getNotificationHelper().error('报错', error.data.message); }); } else { router.push({ name: 'login' }); } } /** * @description 用户登出 * @return {*} {Promise} * @memberof App */ logout(): Promise { return new Promise((resolve, reject) => { this.getDialogHelper().confirm( this.ts('function.generic.prompt'), this.ts('function.generic.promptInformation'), () => { const result: Promise = Http.getInstance()[this.getProjectSetting().appRequestMode]('/v7/logout'); result .then((response: any) => { if (response.success) { this.clearAppData(); resolve(response.data); } }) .catch((error: any) => { reject(error); }); } ); }); } /** * @description 获取应用数据 * @return {*} {IAppData} * @memberof App */ getAppData(): IAppData { const { getAppData } = useAppStoreWithOut(); return getAppData; } /** * @description 清除应用数据 * @return {*} {boolean} * @memberof App */ clearAppData(): boolean { try { clearCookie('ibzuaa-token', true); clearCookie('ibzuaa-expired', true); // 清除应用级数据 localStorage.removeItem('localdata'); const { clearAppData } = useAppStoreWithOut(); const { clearAuthData } = useAuthResourceStoreWithOut(); clearAppData(); clearAuthData(); // 清除租户相关信息 sessionStorage.removeItem('activeOrgData'); sessionStorage.removeItem('dcsystem'); sessionStorage.removeItem('orgsData'); return true; } catch (error) { return false; } } /** * 翻译 * @param str 源字符 * @param resTag 语言标识 * @memberof App */ ts(resTag: string, str = ''): string { if (resTag) { return i18n.global.t(resTag, str); } else { return str; } } /** * 关闭应用页面 * @memberof App */ closeView() { const { deletePage, historyPathList } = useAppStoreWithOut(); const { currentRoute, push } = router; // 删除当前页面 deletePage(currentRoute.value.fullPath); // 跳转前一个页面 const length = historyPathList.length; if (length > 0) { const path = historyPathList[length - 1]; if (Object.is(path, currentRoute.value.fullPath)) { return; } else { push(path); } } else { const path: string | null = window.sessionStorage.getItem( this.getAppConfig().appName ); if (path) { push(path); } else { push('/'); } } } /** * 关闭应用loadding * * @memberof App */ closeAppLoadding(): void { setTimeout(() => { const el = document.getElementById('app-loading-x'); if (el) { el.style.display = 'none'; } }, 300); } /** * 获取视图关闭提示 * * @param {string} tag 视图标识 * @return {*} {ICustomDialogOptions} * @memberof App */ getViewCloseHint(tag: string): ICustomDialogOptions { const dialogOption: ICustomDialogOptions = { title: this.ts('app.common.closehinttitle'), icon: createVNode(QuestionCircleOutlined), content: this.ts('app.common.closehintcontent'), okText: this.ts('app.common.confirm'), cancelText: this.ts('app.common.cancel'), } return dialogOption; } }