app-func-service.ts 15.6 KB
Newer Older
1 2 3 4
import { IPSAppDataEntity } from '@ibiz/dynamic-model-api';
import { ViewTool, Util, LogUtil, AppServiceBase, Http, removeSessionStorage } from 'ibiz-core';
import { clearCookie } from 'qx-util';
import { CreateElement } from 'vue';
5
import { ViewCacheService } from '..';
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

/**
 * 应用功能服务
 *
 * @memberof AppFuncService
 */
export class AppFuncService {
    /**
     * 单例变量声明
     *
     * @memberof AppFuncService
     */
    private static appFuncService: AppFuncService;

    /**
     * 构造 AppFuncService 对象
     *
     * @memberof AppViewLogicService
     */
    constructor() { }

    /**
     * vue对象
     *
     * @memberof AppFuncService
     */
    public v!: any;

    /**
     * 初始化
     *
     * @memberof AppFuncService
     */
    public init(vueInstance: any): void {
        this.v = vueInstance;
    }

    /**
     * 获取 AppFuncService 单例对象
     *
     * @memberof AppFuncService
     */
    public static getInstance() {
        if (!this.appFuncService) {
            this.appFuncService = new AppFuncService();
        }
        return this.appFuncService;
    }

    /**
     * 执行应用功能
     *
     * @memberof AppFuncService
     */
    public async executeApplication(appFunc: any, context: any) {
        if (appFunc) {
            let viewParam: any = {};
            if (appFunc?.getPSAppView) {
                await appFunc.getPSAppView.fill();
            }
            if (appFunc.getPSNavigateContexts) {
                const localContext = Util.formatNavParam(appFunc.getPSNavigateContexts);
                Object.assign(context, localContext);
            }
            if (appFunc.getPSNavigateParams) {
                const localViewParam = Util.formatNavParam(appFunc.getPSNavigateParams);
                Object.assign(viewParam, localViewParam);
            }
            switch (appFunc.appFuncType) {
                case 'APPVIEW':
76
                    this.openAppView(appFunc.getPSAppView, context, viewParam);
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
                    return;
                case 'OPENHTMLPAGE':
                    this.openHtmlPage(appFunc, context, viewParam);
                    return;
                case 'PDTAPPFUNC':
                    this.openPdAppFunc(appFunc, context, viewParam);
                    return;
                case 'JAVASCRIPT':
                    this.executeJavaScript(appFunc, context, viewParam);
                    return;
                case 'CUSTOM':
                    this.custom(appFunc, context, viewParam);
                    return;
                default:
                    LogUtil.warn('无该应用功能');
            }
        }
    }

    /**
     * 打开应用视图
     *
     * @memberof AppFuncService
     */
101
    public async openAppView(appView: any, context: any, viewparam: any = {}, openMode?: string) {
102 103 104 105 106 107 108 109 110 111 112 113
        if (Object.values(appView).length == 0) {
            console.error('未找到应用视图');
            return;
        }
        if (appView.redirectView) {
            this.v.$warning('重定向视图暂不支持应用功能打开', 'openAppView');
        } else {
            if (appView.openViewParam) {
                Object.assign(viewparam, appView.openViewParam);
            }
            const deResParameters: any[] = [];
            const parameters: any[] = [];
114 115 116 117 118
            const viewOpenMode = openMode ? openMode : appView.openMode;
            await this.processingParameter(context, appView, deResParameters, parameters, viewOpenMode);
            if (viewOpenMode && Object.is(viewOpenMode, 'INDEXVIEWTAB')) {
                this.openIndexViewTab(appView, context, viewparam, deResParameters, parameters);
            } else if (viewOpenMode && Object.is(viewOpenMode, 'POPUP')) {
119
                this.openPopup(viewparam, deResParameters, parameters);
120
            } else if (viewOpenMode && Object.is(viewOpenMode, 'POPUPMODAL')) {
121
                this.openModal(context, viewparam, appView);
122
            } else if (viewOpenMode && Object.is(viewOpenMode, 'POPUPAPP')) {
123
                this.openApp(context, viewparam, deResParameters, parameters);
124 125 126 127 128
            } else if (viewOpenMode && Object.is(viewOpenMode, 'POPOVER')) {
                this.openPopover(context, viewparam, appView, viewOpenMode);
            } else if (viewOpenMode && viewOpenMode.indexOf('DRAWER') != -1) {
                this.openDrawer(context, viewparam, appView, viewOpenMode);
            } else if (viewOpenMode && viewOpenMode.indexOf('USER') != -1) {
129 130
                this.openUser(context, viewparam, deResParameters, parameters);
            } else {
131
                this.openIndexViewTab(appView, context, viewparam, deResParameters, parameters);
132 133 134 135 136 137 138 139 140
            }
        }
    }

    /**
     * 整合参数
     *
     * @memberof AppFuncService
     */
141
    public async processingParameter(context: any, appView: any, deResParameters: any[], parameters: any[], viewOpenMode: string) {
142 143 144 145 146 147 148 149 150
        let params = [];
        if (appView.getPSAppDataEntity()) {
            let result: IPSAppDataEntity = appView.getPSAppDataEntity();
            await result.fill();
            if (!result) {
                console.error('未找到应用实体');
                return;
            }
            if (
151 152 153
                !viewOpenMode ||
                viewOpenMode == 'INDEXVIEWTAB' ||
                viewOpenMode== 'POPUPAPP'
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
            ) {
                params = [
                    {
                        pathName: Util.srfpluralize(result.codeName).toLowerCase(),
                        parameterName: result.codeName.toLowerCase(),
                    },
                    { pathName: 'views', parameterName: appView.getPSDEViewCodeName().toLowerCase() },
                ];
            } else {
                params = [
                    {
                        pathName: Util.srfpluralize(result.codeName).toLowerCase(),
                        parameterName: result.codeName.toLowerCase(),
                    },
                ];
            }
        } else {
            params = [{ pathName: 'views', parameterName: appView.name.toLowerCase() }];
        }
        Object.assign(parameters, params);
    }

    /**
     * 顶级分页打开
     *
     * @memberof AppFuncService
     */
181
    public openIndexViewTab(appView: any, context: any, viewparam: any, deResParameters: any[], parameters: any[]) {
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
        if (context && context.srfdynainstid) {
            Object.assign(viewparam, { srfdynainstid: context.srfdynainstid });
        }
        const path: string = ViewTool.buildUpRoutePath(
            this.v.$route,
            context,
            deResParameters,
            parameters,
            [],
            viewparam,
        );
        let isMicroApp: boolean = false;
        const microAppService = AppServiceBase.getInstance().getMicroAppService();
        if (microAppService) {
            isMicroApp = microAppService.getIsMicroApp();
        }
198
        ViewCacheService.setViewCache(appView, path);
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
        if (!isMicroApp) {
            if (Object.is(this.v.$route.fullPath, path)) {
                return;
            }
            this.v.$nextTick(() => {
                this.v.$router.push(path);
            });
        } else {
            this.v.$nextTick(() => {
                this.v.$router.push(path);
            });
        }
    }

    /**
     * 非模式弹出
     *
     * @memberof AppFuncService
     */
    public openPopup(viewparam: any, deResParameters: any[], parameters: any[]) {
        LogUtil.log('-----POPUP-----非模式弹出,暂时不实现');
    }

    /**
     * 模态打开
     *
     * @memberof AppFuncService
     */
    public openModal(context: any, viewparam: any, appView: any) {
        const view = {
            viewname: 'app-view-shell',
            title: this.v.$tl(appView.getTitlePSLanguageRes()?.lanResTag, appView.title),
            height: appView.height,
            width: appView.width,
        };
        if (appView.modelPath) {
            Object.assign(context, { viewpath: appView.modelPath });
        }
        const appmodal = this.v.$appmodal.openModal(view, Util.deepCopy(context), viewparam);
        appmodal.subscribe((result: any) => {
            LogUtil.log(result);
        });
    }

    /**
     * 独立程序弹出
     *
     * @memberof AppFuncService
     */
    public openApp(context: any, viewparam: any, deResParameters: any[], parameters: any[]) {
        if (context && context.srfdynainstid) {
            Object.assign(viewparam, { srfdynainstid: context.srfdynainstid });
        }
        const routePath = ViewTool.buildUpRoutePath(this.v.$route, context, deResParameters, parameters, [], viewparam);
        window.open('./#' + routePath, '_blank');
    }

    /**
     * 气泡打开
     *
     * @memberof AppFuncService
     */
261
    public openPopover(context: any, viewparam: any, appView: any, viewOpenMode: string) {
262 263 264 265 266
        const view = {
            viewname: 'app-view-shell',
            title: this.v.$tl(appView.getTitlePSLanguageRes()?.lanResTag, appView.title),
            height: appView.height,
            width: appView.width,
267
            placement: viewOpenMode,
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
        };
        if (appView.modelPath) {
            Object.assign(context, { viewpath: appView.modelPath });
        }
        const appPopover = this.v.$apppopover.openPop({}, view, Util.deepCopy(context), viewparam);
        appPopover.subscribe((result: any) => {
            LogUtil.log(result);
        });
    }

    /**
     * 抽屉打开
     *
     * @memberof AppFuncService
     */
283
    public openDrawer(context: any, viewparam: any, appView: any, viewOpenMode: string) {
284 285 286 287 288
        const view = {
            viewname: 'app-view-shell',
            title: this.v.$tl(appView.getTitlePSLanguageRes()?.lanResTag, appView.title),
            height: appView.height,
            width: appView.width,
289
            placement: viewOpenMode,
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
        };
        const appdrawer = this.v.$appdrawer.openDrawer(view, Util.getViewProps(context, viewparam), {
            viewModelData: appView,
        });
        appdrawer.subscribe((result: any) => {
            LogUtil.log(result);
        });
    }

    /**
     * 用户自定义
     *
     * @memberof AppFuncService
     */
    public openUser(context: any, viewparam: any, deResParameters: any[], parameters: any[]) {
        LogUtil.log('用户自定义,暂时不实现');
    }

    /**
     * 打开HTML页面
     *
     * @memberof AppFuncService
     */
    public openHtmlPage(appFunc: any, context: any, viewparam: any) {
        // openMode: INDEXVIEWTAB(应用容器分页)/INDEXVIEWPOPUP(应用容器弹出)/INDEXVIEWPOPUPMODAL(应用容器弹出(模式))/HTMLPOPUP(独立网页弹出)/TOP(顶级页面)
        const { htmlPageUrl, openMode, menuItem } = appFunc;
        switch (openMode) {
            case 'INDEXVIEWTAB':
            case 'TOP':
                const indexPath = ViewTool.getIndexRoutePath(this.v.$route);
                let path = `${indexPath}/apphtmlview/${encodeURIComponent(htmlPageUrl)}`;
                if (menuItem && menuItem.caption) {
                    path += `?caption=${encodeURIComponent(menuItem.caption)}`;
                }
                this.v.$router.push(path);
                break;
            case 'INDEXVIEWPOPUP':
            case 'INDEXVIEWPOPUPMODAL':
                const view = {
                    viewname: 'app-html-container',
                    title: menuItem && menuItem.caption ? menuItem.caption : '应用HTML视图',
                    height: 800,
                    width: 1200
                };
                const tempViewParam = Util.deepCopy(viewparam);
                Object.assign(tempViewParam, { caption: menuItem?.caption, htmlPageUrl });
                const appmodal = this.v.$appmodal.openModal(view, Util.deepCopy(context), tempViewParam);
                appmodal.subscribe((result: any) => {
                    LogUtil.log(result);
                });
                break;
            case 'HTMLPOPUP':
                window.open(htmlPageUrl, '_blank');
                break;
            default:
                window.open(htmlPageUrl, '_blank');
                break;
        }
    }

    /**
     * 预置应用功能
     *
     * @memberof AppFuncService
     */
    public openPdAppFunc(appFunc: any, context: any, viewparam: any) {
        // todo 类型标识待补充
        if (appFunc.name) {
            switch (appFunc.name) {
                case '系统登录':
                    this.login();
                    break;
                case '系统登出':
                    this.logout();
                    break;
                case '清空缓存':
                    this.clearAppData();
                    break;
                case '检查系统更新':
                    this.v.$warning('暂未实现', 'openPdAppFunc');
                    break;
                case '关于系统':
                    this.openAbout();
                    break;
                case '我的收藏':
                    this.v.$warning('暂未实现', 'openPdAppFunc');
                    break;
            }
        }
    }

    /**
     * 执行JS
     *
     * @memberof AppFuncService
     */
    public executeJavaScript(appFunc: any, context: any, viewparam: any) {
        this.v.$warning('执行JS暂不支持', 'executeJavaScript');
    }

    /**
     * 自定义
     *
     * @memberof AppFuncService
     */
    public custom(appFunc: any, context: any, viewparam: any) {
        this.v.$warning('自定义暂不支持', 'custom');
    }

    /**
     * @description 登录
     * @memberof AppFuncService
     */
    public login() {
        this.v.$router.push({ name: 'login' });
    }

    /**
     * @description 登出
     * @memberof AppFuncService
     */
    public logout() {
        this.v.$Modal.confirm({
            title: this.v.$t('components.appuser.surelogout'),
            onOk: () => {
                Http.getInstance().get('/v7/logout').then((response: any) => {
                    if (response && response.status === 200) {
                        this.clearAppData(true);
                    }
                }).catch((error: any) => {
                    console.error(error);
                });
            },
        });
    }

    /**
     * @description 关于系统
     * @memberof AppFuncService
     */
    public openAbout() {
        this.v.$Modal.info({
            title: '系统信息',
            render: (h: CreateElement) => {
                return h('app-about');
            }
        })
    }

    /**
     * 清除应用数据
     *
     * @param isLogout 是否为登出
     * @memberof AppFuncService
     */
    public clearAppData(isLogout: boolean = false) {
        // 清除user、token
        clearCookie('ibzuaa-token', true);
        clearCookie('ibzuaa-expired', true);
        clearCookie('ibzuaa-user', true);
        // 清除应用级数据
        localStorage.removeItem('localdata');
        this.v.$store.commit('addAppData', {});
        this.v.$store.dispatch('authresource/commitAuthData', {});
        // 清除租户相关信息
        removeSessionStorage('activeOrgData');
        removeSessionStorage('srfdynaorgid');
        removeSessionStorage('dcsystem');
        removeSessionStorage('orgsData');
        if (isLogout) {
            let leftTime = new Date();
            leftTime.setTime(leftTime.getSeconds() - 1);
            // 重置路由缓存
            const navHistory: any = AppServiceBase.getInstance().getAppNavDataService();
            navHistory.reset();
        }
        const loginUrl = AppServiceBase.getInstance().getAppEnvironment().loginUrl;
        if (loginUrl) {
            window.location.href = `${loginUrl}?redirect=${window.location.href}`;
        } else {
            this.v.$router.push({ name: 'login' });
        }
    }
}