view-open-service.ts 7.6 KB
Newer Older
zcdtk's avatar
zcdtk committed
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
import Router from 'vue-router';
import qs from 'qs';
import { Http, Util, HttpResponse } from '@/ibiz-core/utils';
import { AppModal } from '../app-modal/app-modal';
import { AppDrawer } from '../app-drawer/app-drawer';
import { ViewTool } from '../view-tool/view-tool';

/**
 * 界面打开服务
 *
 * @export
 * @class ViewOpenService
 */
export class ViewOpenService {

    /**
     * 
     *
     * @private
     * @type {(Router | any)}
     * @memberof ViewOpenService
     */
    private router: Router | any;

    /**
     * 唯一实例
     *
     * @private
     * @static
     * @type {ViewOpenService}
     * @memberof ViewOpenService
     */
    private static readonly instance: ViewOpenService = new ViewOpenService();

    /**
     * http请求服务
     *
     * @protected
     * @type {Http}
     * @memberof ViewOpenService
     */
    protected http: Http = Http.getInstance();

    /**
     * Creates an instance of ViewOpenService.
     * @memberof ViewOpenService
     */
    private constructor() {
        if (ViewOpenService.instance) {
            return ViewOpenService.instance;
        }
    }

    /**
     * 格式化路由数据
     *
     * @param {*} context
     * @param {*} deResParameters
     * @param {*} parameters
     * @param {*} args
     * @param {*} data
     * @returns {*}
     * @memberof ViewOpenService
     */
    public formatRouteParam(context: any, deResParameters: any, parameters: any, args: any, data: any): any {
        let urlStr: string = '';
        if (Array.isArray(deResParameters) && deResParameters.length > 0) {
            deResParameters.forEach(({ pathName: _pathName, parameterName: _parameterName }: { pathName: string, parameterName: string }) => {
                let value: any = null;
                if (context[_parameterName] && !Object.is(context[_parameterName], '') && !Object.is(context[_parameterName], 'null')) {
                    value = context[_parameterName];
                }
                urlStr = `${urlStr}/${_pathName}/${value}`;
            });
        }
        if (Array.isArray(deResParameters) && parameters.length === 2) {
            const [{ pathName: _pathName, parameterName: _parameterName }, { pathName: _pathName2, parameterName: _parameterName2 }] = parameters;
            const _value: any = context[_parameterName] && !Object.is(context[_parameterName], '') ? context[_parameterName] : null;
            urlStr = `${urlStr}/${_pathName}/${_value}/${_pathName2}`;
            if (Object.keys(data).length > 0) {
                urlStr = `${urlStr}?${qs.stringify(data, { delimiter: '&' })}`;
            }
        } else if (Array.isArray(deResParameters) && parameters.length === 1) {
            const [{ pathName: _pathName, parameterName: _parameterName }] = parameters;
            urlStr = `${urlStr}/${_pathName}`;
            if (Object.keys(data).length > 0) {
                urlStr = `${urlStr}?${qs.stringify(data, { delimiter: '&' })}`;
            }
        }
        if (!Object.is(urlStr, '')) {
KK's avatar
KK committed
91
            let indexViewContext = {};
zcdtk's avatar
zcdtk committed
92
            if (Object.keys(indexViewContext).length > 0) {
KK's avatar
KK committed
93 94
                let indexUrl = qs.stringify(indexViewContext, { delimiter: ';' })?qs.stringify(indexViewContext, { delimiter: ';' }):null;
                urlStr = `/viewshell/${indexUrl}${urlStr}`;
zcdtk's avatar
zcdtk committed
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
            } else {
                urlStr = `/viewshell/null${urlStr}`;
            }
        }
        return urlStr;
    }

    /**
     * 路由打开
     *
     * @param {*} opts
     * @returns {Promise<any>}
     * @memberof ViewOpenService
     */
    public async openView(opts: any): Promise<any> {
        if (opts && !Object.is(opts, '')) {
            this.router.push(opts);
        }
        Promise.resolve();
    }

    /**
     * 模态打开
     *
     * @param {*} view
     * @param {*} context
     * @param {*} param
     * @returns {Promise<any>}
     * @memberof ViewOpenService
     */
    public async openModal(view: any, context: any, param: any): Promise<any> {
        return await AppModal.getInstance().openModal(view, context, param);
    }

    /**
     * 抽屉打开
     *
     * @param {*} view
     * @param {*} context
     * @param {*} param
     * @returns {Promise<any>}
     * @memberof ViewOpenService
     */
    public async openDrawer(view: any, context: any, param: any): Promise<any> {
KK's avatar
KK committed
139
        return await AppDrawer.getInstance().openDrawer(view, context, param);
zcdtk's avatar
zcdtk committed
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
    }

    /**
     * 气泡卡片打开
     *
     * @param {*} view
     * @param {*} context
     * @param {*} param
     * @returns {Promise<any>}
     * @memberof ViewOpenService
     */
    public async openPopOver(view: any, context: any, param: any): Promise<any> {
        throw new Error('气泡打开界面未实现');
    }

    /**
     * 浏览器新标签页打开
     *
     * @param {string} url
     * @returns {Promise<any>}
     * @memberof ViewOpenService
     */
    public async openPopupApp(url: string): Promise<any> {
        window.open(url, '_blank');
        Promise.resolve();
    }

    /**
     * 打开重定向视图
     *
     * @param {string} url
     * @param {*} context
     * @param {*} data
     * @returns {Promise<any>}
     * @memberof ViewOpenService
     */
    public async openRedirect(url: string, context: any, data: any): Promise<any> {
        const response: HttpResponse = await this.http.post(url, data);
        if (response.status === 200 && response.data) {
            const result: any = response.data;
            // 添加额外参数
            if (result.viewparams && Object.keys(result.viewparams)) {
                Object.assign(data, result.viewparams);
            }
            if (Object.is(result.openmode, 'POPUPAPP')) {
                return this.openPopupApp(result.url);
            } else if (Object.is(result.openmode, 'INDEXVIEWTAB') || Object.is(result.openmode, '')) {
                const viewPath: string = `${result.viewmodule}_${result.viewname}`.toLowerCase();
                return this.openView({ viewpath: viewPath, params: data });
            } else if (Object.is(result.openmode, 'POPUPMODAL')) {
                const viewname: string = Util.srfFilePath2(result.viewname);
                const view: any = {
                    viewname,
                    title: result.title,
                    width: result.width,
                    height: result.height,
                }
                return this.openModal(view, context, data);
            } else if (result.openmode.startsWith('DRAWER')) {
                const viewname: string = Util.srfFilePath2(result.viewname);
                const view: any = {
                    viewname: viewname,
                    title: result.title,
                    width: result.width,
                    height: result.height,
                    placement: result.openmode,
                }
                return this.openDrawer(view, context, data);
            } else if (Object.is(result.openmode, 'POPOVER')) {
                const viewname: string = Util.srfFilePath2(result.viewname);
                const view: any = {
                    viewname: viewname,
                    title: result.title,
                    width: result.width,
                    height: result.height,
                    placement: result.openmode,
                }
                return this.openPopOver(view, context, data);
            }
        }
        return response;
    }


    /**
     * 获取实例
     *
     * @static
     * @param {Route} router
     * @returns {ViewOpenService}
     * @memberof ViewOpenService
     */
    public static getInstance(router?: Router): ViewOpenService {
        if (router) {
            this.instance.router = router;
        }
        return this.instance;
    }

}