Blame view

src/components/home/header/header.js 2.96 KB
simon committed
1 2 3
import {
	mapState
} from 'vuex'
simon committed
4 5 6 7

import {
	getObjByListKeyValue
} from '@utils/utils.js'
simon committed
8
import VDropdown from '@components/home/dropdown/dropdown.vue'
simon committed
9
import MDropdown from '@components/home/m-dropdown/m-dropdown.vue'
simon committed
10 11 12 13

export default {
	name: 'VHeader',
	components: {
simon committed
14 15
		VDropdown,
		MDropdown
simon committed
16
	},
simon committed
17 18 19 20 21
	computed: {
		i18n() {
			return this.$i18n.messages && this.$i18n.locale ? this.$i18n.messages[this.$i18n.locale] : {};
		}
	},
simon committed
22 23
	data() {
		return {
simon committed
24
			dropdownMVisible: false,
simon committed
25
			loginMVisible: false,
simon committed
26
			maxClientWidth: 1200,
simon committed
27 28
			navList: [],
			loginData: {},
simon committed
29 30
			langData: {
				name: "繁",
simon committed
31
				path: "",
simon committed
32
				list: [{
33 34 35 36 37 38 39 40 41 42 43 44 45 46
					name: "繁",
					path: "",
					value: "tc"
				},
				{
					name: "简",
					path: "",
					value: "zh"
				},
				{
					name: "EN",
					path: "",
					value: "en"
				},
simon committed
47 48 49 50 51 52
				]
			}
		}
	},
	computed: {
		...mapState({
53 54
			isSmallScreen: state => state.isSmallScreen,
			userInfo: state => state.userInfo
simon committed
55 56 57 58 59 60 61 62
		})
	},
	methods: {
		toIndex() {
			this.$router.push({
				path: "/"
			})
		},
simon committed
63 64 65 66 67 68 69
		onShowDropdown(evtStr) {
			this.dropdownMVisible = false;
			this.loginMVisible = false;
			if (evtStr == 'nav') {
				this.dropdownMVisible = true;
			} else if (evtStr == 'login') {
				this.loginMVisible = true;
70
			} else { }
simon committed
71
		},
simon committed
72 73 74 75 76 77 78
		checkIsSmallScreen() {
			const self = this;
			if (document.body.clientWidth > self.maxClientWidth) {
				self.$store.commit('IS_SMALL_SCREEN', false)
			} else {
				self.$store.commit('IS_SMALL_SCREEN', true)
			}
simon committed
79 80 81
		},
		initData() {
			let i18n = this.$i18n.messages[this.$i18n.locale] || {};
82 83
			// this.loginData = i18n.nav.loginData;
			this._buildLoginMenu();
simon committed
84 85 86
			this.navList = i18n.nav.navList;
			let curLang = getObjByListKeyValue(this.$i18n.locale, "value", this.langData.list)
			this.langData.name = curLang.name;
87 88 89 90 91 92 93 94
		},
		_buildLoginMenu() {
			// 构建登录页
			let i18n = this.$i18n.messages[this.$i18n.locale] || {};
			let menuData = JSON.parse(JSON.stringify(i18n.nav.loginData));
			let list = [];
			if (this.userInfo && this.userInfo.name) {
				menuData.name = this.userInfo.name;
simon committed
95 96 97
				if (this.$i18n.locale == "en" && this.userInfo.nameEn) {
					menuData.name = this.userInfo.nameEn;
				}
98 99 100 101 102 103 104 105 106 107 108 109 110 111
				menuData.list.forEach(element => {
					if (element.type == "auth") {
						list.push(element);
					}
				});
			} else {
				menuData.list.forEach(element => {
					if (element.type == "noAuth") {
						list.push(element);
					}
				});
			}
			menuData.list = list;
			this.$set(this, 'loginData', menuData);
joe committed
112
			// console.log(this.userInfo);
simon committed
113 114
		}
	},
simon committed
115 116 117
	beforeDestroy() {
		window.removeEventListener('resize', this.checkIsSmallScreen(), false)
	},
simon committed
118
	mounted() {
simon committed
119 120 121 122 123 124
		// const self = this;
		// self.checkIsSmallScreen();
		// document.body.onresize = () => {
		// 	self.checkIsSmallScreen();
		// }
		window.addEventListener('resize', () => this.checkIsSmallScreen(), false);
125 126 127
		this.$root.eventBus.$on("langChange", () => {
			this.initData();
		});
simon committed
128
	},
129 130 131 132 133
	watch: {
		userInfo(val) {
			this._buildLoginMenu();
		}
	},
simon committed
134 135 136
	created() {
		this.initData();
	},
simon committed
137
}