<template>
    <div class="app-redirect-view">
        <img src="@/assets/img/redirect.svg" />
        <div class="context">页面跳转中~</div>
    </div>
</template>

<script lang="ts">
import { ModelTool, Util } from 'ibiz-core';
import qs from 'qs';
import { Vue, Component } from 'vue-property-decorator';

@Component({})
export default class AppRedirectView extends Vue {
    /**
     * vue生命周期
     *
     * @type {*}
     * @memberof AppRedirectView
     */
    public created() {
        let tempViewParam = this.parseViewParam(window.location.href);
        this.executeRedirectLogic(tempViewParam);
    }

    /**
     * 处理路径数据
     *
     * @param {*} [urlStr] 路径
     *
     * @memberof AppRedirectView
     */
    public parseViewParam(urlStr: string): any {
        let tempViewParam: any = {};
        const tempViewparam: any = urlStr.slice(urlStr.lastIndexOf('?') + 1);
        const viewparamArray: Array<string> = decodeURIComponent(tempViewparam).split(';');
        if (viewparamArray.length > 0) {
            viewparamArray.forEach((item: any) => {
                Object.assign(tempViewParam, qs.parse(item));
            });
        }
        return tempViewParam;
    }

    /**
     * 执行数据重定向数据逻辑
     *
     *
     * @memberof AppRedirectView
     */
    public executeRedirectLogic(viewparam: any) {
        let tempViewParam: any = Util.deepCopy(viewparam);
        const { srfdename }: { srfdename: string } = viewparam;
        // name转化成codename
        const srfdecodename = ModelTool.getAppEntityCodeNameByName(srfdename);
        if (!viewparam || !srfdecodename) {
            this.$throw('跳转参数不足，无法完成跳转操作!', 'executeRedirectLogic');
            return;
        }
        // 添加全局重定向标识redirectType和redirectDEName
        Object.assign(tempViewParam,{redirectType:'APPREDIRECT',redirectDEName:tempViewParam['srfdename']});
        // 避免数据污染
        delete tempViewParam['srfdename'];
        delete tempViewParam['srfindexname'];
        // 计算路径
        let viewPath: string = `/index/${Util.srfpluralize(srfdecodename)}/${
            viewparam[srfdename]
        }/views/redirectview`;
        if (Object.keys(tempViewParam).length > 0) {
            viewPath = `${viewPath}?${qs.stringify(tempViewParam, { delimiter: ';' })}`;
        }
        this.$router.push({ path: viewPath });
    }
}
</script>

<style lang='less'>
@import './app-redirect-view.less';
</style>