appmenu-control-base.tsx 17.8 KB
Newer Older
1 2 3 4 5 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 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
import { IPSAppMenu } from '@ibiz/dynamic-model-api';
import { AppServiceBase, AuthService, LogUtil, MenuControlInterface, Util } from 'ibiz-core';
import { ControlBase } from "./control-base";
import { AppFuncService, NavDataService } from '../app-service';
import { AppMenuService } from "../ctrl-service";
import { Subscription } from 'rxjs';

/**
 * 菜单部件基类
 *
 * @export
 * @class AppMenuControlBase
 * @extends {ControlBase}
 */
export class AppMenuControlBase extends ControlBase implements MenuControlInterface {

    /**
     * 菜单部件实例
     * 
     * @memberof AppMenuControlBase
     */
    public declare controlInstance: IPSAppMenu;

    /**
     * 显示处理提示
     * 
     * @memberof AppMenuControlBase
     */
    public showBusyIndicator: boolean = true;

    /**
     * 菜单收缩改变
     *
     * @type {boolean}
     * @memberof AppMenuControlBase
     */
    public collapseChange!: boolean;

    /**
     * 当前模式,菜单在顶部还是在底部
     *
     * @type {*}
     * @memberof AppMenuControlBase
     */
    public mode!: string;

    /**
     * 当前选中主题
     *
     * @type {*}
     * @memberof AppMenuControlBase
     */
    public selectTheme!: string;

    /**
     * 默认激活的index
     *
     * @type {*}
     * @memberof AppMenuControlBase
     */
    public defaultActive: any = null;

    /**
     * 触发方式,默认click
     *
     * @type {string}
     * @memberof AppMenuControlBase
     */
    public trigger: string = 'click';

    /**
     * 默认打开的index数组
     *
     * @type {any[]}
     * @memberof AppMenuControlBase
     */
    public defaultOpeneds: any[] = [];

    /**
     * 是否计算好默认打开项
     */
    public isDefaultOpenedsReady: boolean = false;

    /**
     * 应用起始页面
     *
     * @type {boolean}
     * @memberof AppMenuControlBase
     */
    public isDefaultPage: boolean = false;

    /**
     * 空白视图模式
     *
     * @type {boolean}
     * @memberof AppMenuControlBase
     */
    public isBlankMode: boolean = false;

    /**
     * 应用导航服务
     *
     * @type {*}
     * @memberof AppMenuControlBase
     */
    public navDataService: NavDataService = NavDataService.getInstance(this.$store);

    /**
     * 默认打开视图
     *
     * @type {boolean}
     * @memberof AppMenuControlBase
     */
    public defPSAppView: any;

    /**
     * 计数器数据
     *
     * @type {*}
     * @memberof AppMenuControlBase
     */
    public counterdata: any;

    /**
     * 菜单模型
     *
     * @public
     * @type 
     * @memberof AppMenuControlBase
     */
    public menuMode: any;

    /**
     * 菜单数据
     *
     * @public
     * @type {any[]}
     * @memberof AppMenuControlBase
     */
    public menus: any[] = [];

    /**
     * 建构权限服务对象
     *
     * @type {AuthService}
     * @memberof AppMenuControlBase
     */
    public authService!: AuthService;

    /**
     * 是否需要使用计数器
     *
     * @private
     * @type {boolean}
     * @memberof AppMenuControlBase
     */
    private needUseCounter: boolean = false;

    /**
     * 微应用状态订阅对象
     *
     * @private
     * @type {*}
     * @memberof AppMenuControlBase
     */
    private MicroAppStateEvent: Subscription | undefined;

    /**
     * @description 菜单部件事件
     * @public
     * @type {(Subscription | undefined)}
     * @memberof AppMenuControlBase
     */
    public menuControlEvent: Subscription | undefined;

    /**
     * 当前应用是否为微应用
     *
     * @memberof AppMenuControlBase
     */
    public isMicroApp: boolean = false;

    /**
     * 提示框主题样式
     *
     * @type {string}
     * @memberof AppMenuControlBase
     */
    public popperClass(): string {
        return 'app-control-menu__popper ' + this.selectTheme;
    }

    /**
     * 监听部件动态参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof AppMenuControlBase
     */
    public onDynamicPropsChange(newVal: any, oldVal: any) {
        super.onDynamicPropsChange(newVal, oldVal);
        this.collapseChange = newVal.collapseChange;
        this.$forceUpdate();
    }

    /**
     * 监听部件参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof AppMenuControlBase
     */
    public onStaticPropsChange(newVal: any, oldVal: any) {
        this.mode = newVal.mode ? newVal.mode : 'LEFT';
        this.selectTheme = newVal.selectTheme;
        this.isDefaultPage = newVal.isDefaultPage ? newVal.isDefaultPage : this.isDefaultPage;
        this.isBlankMode = newVal.isBlankMode ? newVal.isBlankMode : this.isBlankMode;
        this.defPSAppView = newVal.defPSAppView;
        this.counterdata = newVal.counterdata;
        super.onStaticPropsChange(newVal, oldVal)
    }

    /**
     * 部件模型数据初始化
     *
     * @memberof AppMenuControlBase
     */
    public async ctrlModelInit(args?: any) {
        await super.ctrlModelInit();
        this.service = new AppMenuService(this.controlInstance, this.context);
        await this.service.initServiceParam(this.context, this.controlInstance);
        const microAppService = AppServiceBase.getInstance().getMicroAppService();
        if (microAppService) {
            this.isMicroApp = microAppService.getIsMicroApp();
            if (microAppService.getAppState()) {
                this.MicroAppStateEvent = microAppService.getAppState().subscribe((state: any) => {
                    const { tag, action, data } = state;
                    if (Object.is(tag, this.Environment.microAppName)) {
                        if (Object.is(action, 'MENUCLICK')) {
                            const appFuncs: Array<any> = this.service.getAllFuncs();
                            const appFunc = appFuncs.find((element: any) => {
                                return element.appfunctag === data?.funtag;
                            });
                            if (appFunc) {
                                const menuItem = this.service.getAllMenuItems().find((item:any) =>{
                                    return item.getPSAppFunc.codeName === data?.funtag;
                                })
                                Object.assign(appFunc, { menuItem });
                                AppFuncService.getInstance().executeApplication(appFunc, Util.deepCopy(this.context));
                            }
                        }
                    }
                })
            }
        }
    }

    /**
     * 应用菜单初始化
     *
     * @memberof AppMenuControlBase
     */
    public ctrlInit(args?: any) {
        super.ctrlInit();
        let _this: any = this;
        this.authService = new AuthService();
        if (this.viewState) {
            this.menuControlEvent = this.viewState.subscribe(({ tag, action, data }: any) => {
                if (!Object.is(tag, this.name)) {
                    return;
                }
                this.load();
            });
        }
    }

    /**
     * 部件销毁
     *
     * @memberof AppMenuControlBase
     */
    public ctrlDestroyed(args?: any) {
        super.ctrlDestroyed(args);
        if (this.MicroAppStateEvent) {
            this.MicroAppStateEvent.unsubscribe();
        }
        if (this.menuControlEvent) {
            this.menuControlEvent.unsubscribe();
        }
    }

    /**
     * 数据加载
     *  
     * @memberof AppMenuControlBase
     */
    public load() {
        this.handleMenusResource(this.service.getAllMenuItems());
    }

    /**
     * 菜单项选中处理
     *
     * @param {menuName } 选中菜单名称
     * @returns
     * @memberof AppMenuControlBase
     */
    public select(menuName: any) {
        let item = this.compute(this.menus, menuName);
        if (Object.keys(item).length === 0) {
            return;
        }
        this.click(item);
    }

    /**
     * 处理菜单默认选中项
     *
     * @public
     * @memberof AppMenuControlBase
     */
    public defaultMenuSelect(): void {
        if (!this.isDefaultPage || this.isBlankMode) {
            return;
        }
        const appFuncs: Array<any> = this.service.getAllFuncs();
        if (this.$route && this.$route.matched && this.$route.matched.length == 2) { // 存在二级路由
            const [{ }, matched] = this.$route.matched;
            const appfunc: any = appFuncs?.find((_appfunc: any) => Object.is(_appfunc.routepath, matched.path) && Object.is(_appfunc.appfunctype, 'APPVIEW'));
            if (appfunc) {
                this.computeMenuSelect(this.menus, appfunc.appfunctag);
            }
            return;
        } else if (this.defPSAppView && Object.keys(this.defPSAppView).length > 0) { // 存在默认视图
            const appfunc: any = appFuncs?.find((_appfunc: any) => Object.is(_appfunc.routepath, this.defPSAppView.routepath) && Object.is(_appfunc.appfunctype, 'APPVIEW'));
            if (appfunc) {
                this.computeMenuSelect(this.menus, appfunc.appfunctag);
            }
            const viewparam: any = {};
            const path: string = this.$viewTool.buildUpRoutePath(this.$route, {}, this.defPSAppView.deResParameters, this.defPSAppView.parameters, [], viewparam);
            this.$router.push(path);
            return;
        }
        this.computeMenuSelect(this.menus, '');
        let item = this.compute(this.menus, this.defaultActive);
        if (Object.keys(item).length === 0) {
            return;
        }
        if (!item.hidden) {
            this.click(item);
        }
    }

    /**
     * 计算菜单选中项
     *
     * @public
     * @param {Array<any>} items 菜单数据
     * @param {string} appfunctag 应用功能tag
     * @return {*}  {boolean}
     * @memberof AppMenuControlBase
     */
    public computeMenuSelect(items: Array<any>, appfunctag: string): boolean {
        const appFuncs: Array<any> = this.service.getAllFuncs();
        const result = items.some((item: any) => {
            if (Object.is(appfunctag, '') && item.getPSAppFunc && item.openDefault && !item.hidden) {
                const appfunc = appFuncs?.find((_appfunc: any) => Object.is(_appfunc.appfunctag, item.getPSAppFunc.codeName));
                if (appfunc) {
                    this.defaultActive = item.name;
                    this.setHideSideBar(item);
                    return true;
                }
            }
            if (item.getPSAppFunc && Object.is(item.getPSAppFunc.codeName, appfunctag)) {
                this.setHideSideBar(item);
                this.defaultActive = item.name;
                return true;
            }
            if (item.getPSAppMenuItems && item.getPSAppMenuItems.length > 0) {
                const state = this.computeMenuSelect(item.getPSAppMenuItems, appfunctag);
                if (state) {
                    this.defaultOpeneds.push(item.name);
                    return true;
                }
            }
            return false;
        });
        this.isDefaultOpenedsReady = true;
        return result;
    }

    /**
     * 获取菜单项数据
     *
     * @public
     * @param {any[]} items
     * @param {string} name
     * @returns
     * @memberof AppMenuControlBase
     */
    public compute(items: any[], name: string) {
        const item: any = {};
        items.some((_item: any) => {
            if (name && Object.is(_item.name, name)) {
                Object.assign(item, _item);
                this.setHideSideBar(_item);
                return true;
            }
            if (_item.getPSAppMenuItems && Array.isArray(_item.getPSAppMenuItems)) {
                const subItem = this.compute(_item.getPSAppMenuItems, name);
                if (Object.keys(subItem).length > 0) {
                    Object.assign(item, subItem);
                    return true;
                }
            }
            return false;
        });
        return item;
    }

    /**
     * 设置是否隐藏菜单栏
     *
     * @public
     * @param {*} item
     * @memberof AppMenuControlBase
     */
    public setHideSideBar(item: any): void {
        if (this.isDefaultPage && item.hidesidebar) {
            this.$emit('collapsechange', true);
        }
    }

    /**
     * 菜单点击
     *  
     * @param {*} item 点击项
     * @memberof AppMenuControlBase
     */
    public click(item: any) {
        this.handleCtrlEvents('onclick', { action: 'MenuClick', data: item }).then((res: boolean) => {
            if (res) {
443
                this.defaultActive = item.name;
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 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
                if (Object.is((this.controlInstance as any)?.parentModel?.viewStyle, 'STYLE4') && (!this.mode || Object.is(this.mode, 'LEFT'))) {
                    this.$store.commit('removeAllPage');
                    this.navDataService.removeNavDataFrist();
                }
                let tempContext: any = Util.deepCopy(this.context);
                if (item.getPSNavigateContexts) {
                    const localContext = Util.formatNavParam(item.getPSNavigateContexts);
                    Object.assign(tempContext, localContext);
                } else {
                    if (tempContext.hasOwnProperty("srfdynainstid")) {
                        delete tempContext.srfdynainstid;
                    }
                }
                if (item.getPSAppFunc) {
                    const appFuncs: Array<any> = this.service.getAllFuncs();
                    const appFunc = appFuncs.find((element: any) => {
                        return element.appfunctag === item.getPSAppFunc.codeName;
                    });
                    if (appFunc) {
                        Object.assign(appFunc, { menuItem: item });
                        AppFuncService.getInstance().executeApplication(appFunc, tempContext);
                    }
                } else {
                    LogUtil.warn(this.$t('app.commonwords.noassign'));
                }
            }
        })
    }

    /**
     * 通过统一资源标识计算菜单
     *
     * @param {*} data
     * @memberof AppMenuControlBase
     */
    public handleMenusResource(inputMenus: Array<any>) {
        let _this: any = this;
        if (_this.$store.getters['authresource/getEnablePermissionValid']) {
            this.computedEffectiveMenus(inputMenus);
            //  不使用计数器则抛出事件让视图销毁计数器
            if (!this.needUseCounter) {
                this.ctrlEvent({ controlname: this.name, action: 'destoryCounter', data: null });
            }
            // this.computeParentMenus(inputMenus);
            //  不使用计数器则抛出事件让视图销毁计数器
            if (!this.needUseCounter) {
                this.ctrlEvent({ controlname: this.name, action: 'destoryCounter', data: null });
            }
        }
        this.dataProcess(inputMenus);
        this.isDefaultOpenedsReady = true;
        this.menus = inputMenus;
        if(!this.isMicroApp){
            this.defaultMenuSelect();
        }
    }

    /**
     * 计算有效菜单项
     *
     * @param {*} inputMenus
     * @memberof AppMenuControlBase
     */
    public computedEffectiveMenus(inputMenus: Array<any>) {
        inputMenus.forEach((_item: any) => {
            //  无权限
            if (!this.authService.getMenusPermission(_item)) {
                _item.hidden = true;
            } else {
                if (Util.isExistAndNotEmpty(_item.counterid)) {
                    this.needUseCounter = true;
                }
            }
            //  父隐藏时不计算子权限
            if (_item.getPSAppMenuItems && _item.getPSAppMenuItems.length > 0 && !_item.hidden) {
                this.computedEffectiveMenus(_item.getPSAppMenuItems);
            }
        })
    }

    // /**
    //  * 计算父项菜单项是否隐藏
    //  *
    //  * @param {*} inputMenus
    //  * @memberof AppMenuControlBase
    //  */
    // public computeParentMenus(inputMenus:Array<any>){
    //     if(inputMenus && inputMenus.length >0){
    //         inputMenus.forEach((item:any) =>{
    //             if(item.hidden && item.getPSAppMenuItems && item.getPSAppMenuItems.length >0){
    //                 item.getPSAppMenuItems.map((singleItem:any) =>{
    //                     if(!singleItem.hidden){
    //                         item.hidden = false;
    //                     }else{
    //                         if(singleItem.getPSAppMenuItems && singleItem.getPSAppMenuItems.length >0){
    //                             singleItem.getPSAppMenuItems.map((grandsonItem:any) =>{
    //                                 if(!grandsonItem.hidden){
    //                                     item.hidden = false;
    //                                 }
    //                             })
    //                         }
    //                     }
    //                     if(item.getPSAppMenuItems && item.getPSAppMenuItems.length >0){
    //                         this.computeParentMenus(item.getPSAppMenuItems);
    //                     }
    //                 })
    //             }
    //         })
    //     }
    // }

    /**
     * 数据处理
     *
     * @public
     * @param {any[]} items
     * @memberof AppMenuControlBase
     */
    public dataProcess(items: any[]): void {
        items.forEach((_item: any) => {
            if (_item.expanded) {
                this.defaultOpeneds.push(_item.name);
            }
            if (_item.getPSAppMenuItems && _item.getPSAppMenuItems.length > 0) {
                this.dataProcess(_item.getPSAppMenuItems)
            }
        });
    }

    /**
     * @description 处理关闭菜单
     * @param {*} key 菜单项标识
     * @param {*} keyPath 标识集合
     * @memberof AppMenuControlBase
     */
    public handleCloseMenu(key: any, keyPath: any) {
        const refs: any = this.$refs;
        if (refs[this.controlInstance?.codeName] && refs[this.controlInstance?.codeName].close instanceof Function) {
            refs[this.controlInstance?.codeName].close(key);
        }
    }
}