auth.js 2.65 KB
/**
 * 组件描述: 用户登陆提示
 * 其中包含
 * 1.用户登陆提示
 * 2.五项基本信息验证
 */

import {
	mapState
} from 'vuex';

export default {
	props: {
		// 模型;auth、suggest
		model: {
			type: String,
			default: "auth"
		},
		checkProfile: {
			type: Boolean,
			default: false
		},
		// m1 、m2、m3
		tipModel: {
			type: String,
			default: "m3"
		}
	},
	data() {
		return {
			key: 'value',
			showProfileInfo: false,
			showUnAuth: false,
			showSuggest: true,
			tips: {

			}
		}
	},
	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() {
			let b1 = this.model == "auth" && (this.showProfileInfo || this.showUnAuth);
			let b2 = this.model == "suggest" && this.showSuggest;
			return b1 || b2;
		}
	},
	methods: {
		// 检测用户状态,触发用户登陆/登出
		initData() {
			this.tips = this.i18n.customService.unauth[this.tipModel];
			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.showProfileInfo = false;
			this.$emit("onLogout", {});
		},
		// 前往登陆(带重定向)
		gotoLoginPage() {
			let callback = this.$route.path;
			let separator = "?";
			for (let key in this.$route.query) {
				callback += separator + key + "=" + this.$route.query[key];
				separator = "&";
			}
			this.$router.push({
				path: "/login?callback=" + callback
			});
		},
		// 前往注册页面
		gotoRegisterPage() {
			this.$router.push({
				path: "/register"
			});
		},
		gotoInformationPage() {
			let c = this.$route.fullPath;
			this.$router.push({
				path: "/infomation/improve",
				query: {
					c: c,
					a: 1
				}
			});
		},
		noAuth() {
			this.$store.commit("SET_USER_INFO", null);
			this.initData();
			this.$emit("onLogout", {});
		}
	},
	mounted() {
		this.initData();
	},
	created() {
		this.$root.eventBus.$on("langChange", () => {
			try {
				this.tips = this.i18n.customService.unauth[this.tipModel];
			} catch (e) {}
		});
	},
	watch: {
		userInfo(val) {
			if (val && val.name) {
				this.loginAction();
			} else {
				this.logoutAction();
			}
		}
	},
}