MyHeader.vue 4.3 KB
Newer Older
1
<template>
2 3 4 5 6 7 8 9
	<div class="layout-header-container d-flex justify-content-between align-items-center p-3">
		<!-- 左侧 logo 和 标题区域 -->
		<div class="layout-header-left d-flex align-items-center user-select-none">
			<!-- logo -->
			<img class="layout-header-left-img" src="../../assets/kwan.png" alt="" />
			<!-- 标题 -->
			<h4 class="layout-header-left-title ml-3">kwan的解忧杂货铺</h4>
		</div>
10
		<h2 class="layout-header-left-title ml-3" :style="{ color: textColor }">{{ randomAlgorithmic.poetryText }}</h2>
11 12 13 14 15 16 17
		<el-row>
			<el-col :inline="true" :span="24">
				<el-button class="el-button-header" type="success" round @click="myHome">我的主页</el-button>
				<el-button class="el-button-header" type="primary" round @click="logout">退出登录</el-button>
			</el-col>
		</el-row>
	</div>
18 19 20
</template>

<script>
21
import axios from 'axios'
22
export default {
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
	name: 'MyHeader',
	data() {
		return {
			randomAlgorithmic: {
				poetryText: '',
			},
			color: [
				'red',
				'blue',
				'green',
				'yellow',
				'purple',
				'orange',
				'pink',
				'brown',
				'cyan',
				'magenta',
				'maroon',
				'navy',
				'olive',
				'teal',
				'lime',
				'aqua',
				'silver',
				'gray',
				'black',
				'white',
				'indigo',
				'violet',
				'lavender',
				'coral',
				'gold',
				'salmon',
				'turquoise',
				'orchid',
				'khaki',
				'slategray',
				'thistle',
				'burlywood',
				'cadetblue',
				'chartreuse',
				'chocolate',
				'crimson',
				'darkblue',
				'darkcyan',
				'darkgoldenrod',
				'darkgray',
				'darkgreen',
				'darkkhaki',
				'darkmagenta',
				'darkolivegreen',
				'darkorange',
				'darkorchid',
				'darkred',
				'darksalmon',
				'darkseagreen',
				'darkslateblue',
				'darkslategray',
				'darkturquoise',
				'darkviolet',
				'deeppink',
				'deepskyblue',
				'dodgerblue',
				'firebrick',
				'forestgreen',
				'fuchsia',
				'gold',
				'greenyellow',
				'hotpink',
				'indianred',
				'lawngreen',
				'lightcoral',
				'lightgreen',
				'lightpink',
				'lightsalmon',
				'lightseagreen',
				'lightskyblue',
				'lightslategray',
				'lightsteelblue',
				'mediumaquamarine',
				'mediumblue',
				'mediumorchid',
				'mediumpurple',
				'mediumseagreen',
				'mediumslateblue',
				'mediumspringgreen',
				'mediumturquoise',
				'mediumvioletred',
				'midnightblue',
				'orangered',
				'palegoldenrod',
				'palegreen',
				'paleturquoise',
				'palevioletred',
				'peru',
				'powderblue',
				'rosybrown',
				'saddlebrown',
				'seagreen',
				'sienna',
				'skyblue',
				'slateblue',
				'springgreen',
				'tan',
				'thistle',
				'tomato',
				'turquoise',
				'violet',
			],
			textColor: 'black', // 初始字体颜色为黑色
			loading: false,
		}
	},
	watch: {
		'randomAlgorithmic.poetryText': {
			handler(newValue, oldValue) {
				if (newValue !== oldValue) {
					// 当值改变时,根据条件设置不同的字体颜色
					this.textColor = this.getRandomColor()
				}
			},
			deep: true,
		},
	},
	created() {
		//获取问题类型的枚举
		this.startInterviewRandomTimer()
	},
151 152 153 154
	destroyed() {
		//销毁的时候需要清理定时器
		this.stopInterviewRandomTimer()
	},
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
	methods: {
		logout() {
			// 1. 清空 token
			localStorage.removeItem('token')
			// 2. 跳转到登录页面
			this.$router.push('/login')
		},
		myHome() {
			// 新页面打开
			window.open('http://qinyingjie.top/')
		},
		async interviewRandom() {
			this.loading = true
			const { data: res } = await axios.get('http://120.79.36.53:8888/aphorismPoetry/random', { params: {} })
			if (res.code === 200) {
				this.randomAlgorithmic.poetryText = res.result.poetryText
			}
			this.loading = false
		},

		// 定时执行 interviewRandom 方法
		startInterviewRandomTimer() {
			this.interviewRandom() // 首次调用方法
			this.interviewRandomTimer = setInterval(() => {
				this.interviewRandom()
180
			}, 10000) // 5000 毫秒(5秒)为间隔
181 182 183 184 185 186 187 188 189 190 191 192
		},

		// 停止定时执行 interviewRandom 方法
		stopInterviewRandomTimer() {
			clearInterval(this.interviewRandomTimer)
		},
		getRandomColor() {
			// 从颜色数组中随机选取一个颜色
			const randomIndex = Math.floor(Math.random() * this.color.length)
			return this.color[randomIndex]
		},
	},
193
}
194 195 196 197
</script>

<style lang="less" scoped>
.layout-header-container {
198 199
	height: 60px;
	border-bottom: 1px solid #eaeaea;
200 201 202
}

.layout-header-left-img {
203
	height: 50px;
204
}
205
</style>