auth.js 1.91 KB
import {
    mapState
} from 'vuex';

export default {
    props: {
        checkProfile: false,
    },
    data() {
        return {
            key: 'value',
            showProfileInfo: false,
            showUnAuth: false
        }
    },
    components: {},
    computed: {
        ...mapState({
            userInfo: state => state.userInfo
        }),
        locale() {
            return this.$i18n.locale || 'tc';
        },
        i18n() {
            return this.$i18n.messages && this.$i18n.locale ? this.$i18n.messages[this.$i18n.locale] : {};
        },
        showPanel(){
            return this.showProfileInfo || this.showUnAuth;
        }
    },
    methods: {
        initData() {
            if (this.userInfo && this.userInfo.name) {
                this.loginAction();
            } else {
                this.logoutAction();
            }
        },
        loginAction() {
            this.showUnAuth = false;
            if (this.checkProfile) {
                if (this.userInfo.hadFullInfo == 1) {
                    this.$emit("onLogin", this.userInfo);
                } else {
                    this.showProfileInfo = true;
                }
            } else {
                this.$emit("onLogin", this.userInfo);
            }
        },
        logoutAction() {
            this.showUnAuth = true;
            this.$emit("onLogout", {});
        },
        gotoLoginPage() {
            this.$router.push({ path: "/login" });
        },
        gotoRegisterPage() {
            this.$router.push({ path: "/register" });
        },
        gotoInformationPage(){
            this.$router.push({ path: "/infomation/improve" });
        }
    },
    mounted() {
        this.initData();
    },
    created() { },
    watch: {
        userInfo(val) {
            if (val && val.name) {
                this.loginAction();
            } else {
                this.logoutAction();
            }
        }
    },
}