UserRealName.vue 1.54 KB
<template>
    <template v-if="userName==='系统默认'">
        系统默认
    </template>
    <template v-else>
        {{ user?.realName ?? userName }}
    </template>
</template>

<script lang="ts">
import {Component, Prop, Vue, Watch} from "vue-facing-decorator";
import {User} from "lib/src/entity/security/entity/User";
import {Getter, Mutation} from 'vuex-facing-decorator';
import {SecurityController} from "@/api/SecurityController";

@Component({
    components: {}
})
export default class UserRealName extends Vue {

    @Prop({type: String})
    userName: string;

    user: User = null;

    @Getter getUserByName;

    @Mutation cacheUser;

    created(): void {
        this.user = this.getUserByName(this.userName);
        if (!this.user && this.userName) {
            SecurityController.getUserByUserName(this.axios, this.userName).then(res => {
                this.user = res.data;
                if (this.user) {
                    this.cacheUser(this.user);
                }
            });
        }
    }

    @Watch('userName', {deep: true, immediate: true})
    watchUserName(value: string): void {
        if (value) {
            this.user = this.getUserByName(value);
            if (!this.user) {
                SecurityController.getUserByUserName(this.axios, value).then(res => {
                    this.user = res.data;
                    if (this.user) {
                        this.cacheUser(this.user);
                    }
                });
            }
        }
    }
}
</script>

<style lang="scss" scoped>
</style>