app-user.vue 6.3 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
<template>
    <div class="app-header-user">
        <dropdown transfer-class-name="app-header-user__dropdown-menu" @on-click="userSelect" :transfer="true">
            <div class="app-header-user__caption">
                <span>{{ user.name ? user.name : $t('components.appuser.name') }}</span>
                &nbsp;&nbsp;<avatar :src="user.avatar" />
            </div>
            <dropdown-menu class="menu" slot="list">
                <dropdown-item name="lockscren">
                    <app-lock-scren />
                </dropdown-item>
                <dropdown-item name="changetheme" >
                    <app-custom-theme :viewStyle="this.viewStyle"></app-custom-theme>
                </dropdown-item>
                <dropdown-item v-if="enabledChangePassword" name="updatepwd">
                    <span><Icon type="ios-create-outline" /></span>
                    <span>{{ $t('components.appuser.changepwd') }}</span>
                </dropdown-item>
                <dropdown-item name="logout">
                    <span><i aria-hidden="true" class="ivu-icon ivu-icon-md-power" ></i></span>
                    <span>{{ $t('components.appuser.logout') }}</span>
                </dropdown-item>
            </dropdown-menu>
        </dropdown>
    </div>
</template>
<script lang = 'ts'>
import { Vue, Component, Prop } from 'vue-property-decorator';
import { Subject, Subscription } from 'rxjs';
import { Environment } from '@/environments/environment';
import { AppServiceBase, removeSessionStorage } from 'ibiz-core';
import { clearCookie, getCookie } from 'qx-util';
@Component({})
export default class AppUser extends Vue {

    /**
     * 视图样式类型
     *
     * @type {any}
     * @memberof AppUser
     */ 
    @Prop() public viewStyle!: string;

    /**
     * 视图样式类型
     *
     * @type {any}
     * @memberof AppUser
     */ 
    @Prop({default: true}) public enabledChangePassword!: boolean;

    /**
     * 用户信息
     *
     * @memberof AppUser
     */
    public user = {
        name: '',
        avatar: './assets/img/avatar.png',
    };

    /**
     * 状态事件
     *
     * @private
     * @type {(Subscription | undefined)}
     * @memberof AppImagePreview
     */
    private stateEvent: Subscription | undefined;

    /**
     * 下拉选选中回调
     *
     * @param {*} data
     * @memberof AppUser
     */
    public userSelect(data: any) {
        if (Object.is(data, 'logout')) {
            const title: any = this.$t('components.appuser.surelogout');
            this.$Modal.confirm({
                title: title,
                onOk: () => {
                    this.logout();
                },
            });
        } else if (Object.is(data, 'updatepwd')) {
            let container: Subject<any> = this.$appmodal.openModal(
                {
                    viewname: 'app-update-password',
                    title: this.$t('components.appuser.changepwd') as string,
                    width: 500,
                    height: 400,
                },
                {},
                {},
            );
            this.stateEvent = container.subscribe((result: any) => {
                if (!result || !Object.is(result.ret, 'OK')) {
                    return;
                }
            });
        }
    }

    /**
     * vue  生命周期
     *
     * @memberof AppUser
     */
    public mounted() {
        let _user: any = {};
        if (this.$store.getters.getAppData()) {
            if (this.$store.getters.getAppData().context && this.$store.getters.getAppData().context.srfpersonname) {
                _user.name = this.$store.getters.getAppData().context.srfpersonname;
            } else {
                if (getCookie('ibzuaa-user')) {
                    let user: any = JSON.parse(getCookie('ibzuaa-user') as string);
                    if (user && user.personname) {
                        _user.name = user.personname;
                    }
                }
            }
            if (this.$store.getters.getAppData().context && this.$store.getters.getAppData().context.srfusericonpath) {
                _user.avatar = this.$store.getters.getAppData().context.srfusericonpath;
            }
        } else {
            if (getCookie('ibzuaa-user')) {
                let user: any = JSON.parse(getCookie('ibzuaa-user') as string);
                if (user && user.personname) {
                    _user.name = user.personname;
                }
            }
        }
        Object.assign(this.user, _user, {
            time: +new Date(),
        });
    }

    /**
     * 退出登录
     *
     * @memberof AppUser
     */
    public logout() {
        const get: Promise<any> = this.$http.get('/v7/logout');
        get.then((response: any) => {
            if (response && response.status === 200) {
                this.clearAppData();
                if((Environment as any).casLogoutUrl){
                    window.location.href = `${(Environment as any).casLogoutUrl}?service=${window.location.href}`;
                } else if (Environment.loginUrl) {
                    window.location.href = `${Environment.loginUrl}?redirect=${window.location.href}`;
                } else {
                    this.$router.push({ name: 'login' });
                }
            }
        }).catch((error: any) => {
            console.error(error);
        });
    }

    /**
     * 清除应用数据
     *
     * @private
     * @memberof AppUser
     */
    private clearAppData() {
        // 清除user、token
        let leftTime = new Date();
        leftTime.setTime(leftTime.getSeconds() - 1);
        clearCookie('ibzuaa-token', true);
        clearCookie('ibzuaa-expired',true);
        clearCookie('ibzuaa-user', true);
        // 清除应用级数据
        localStorage.removeItem('localdata');
        this.$store.commit('addAppData', {});
        this.$store.dispatch('authresource/commitAuthData', {});
        // 清除租户相关信息
        removeSessionStorage('activeOrgData');
        removeSessionStorage('srfdynaorgid');
        removeSessionStorage('dcsystem');
        removeSessionStorage('orgsData');
        // 重置路由缓存
        const navHistory: any = AppServiceBase.getInstance().getAppNavDataService();
        navHistory.reset();  
    }

    public destroyed() {
        if (this.stateEvent) {
            this.stateEvent.unsubscribe();
        }
    }
}
</script>