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
	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',
				'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',
			],
131
			textColor: 'skyblue', // 初始字体颜色为黑色
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
			loading: false,
		}
	},
	watch: {
		'randomAlgorithmic.poetryText': {
			handler(newValue, oldValue) {
				if (newValue !== oldValue) {
					// 当值改变时,根据条件设置不同的字体颜色
					this.textColor = this.getRandomColor()
				}
			},
			deep: true,
		},
	},
	created() {
		//获取问题类型的枚举
		this.startInterviewRandomTimer()
	},
150 151 152 153
	destroyed() {
		//销毁的时候需要清理定时器
		this.stopInterviewRandomTimer()
	},
154 155 156 157 158 159 160 161 162 163 164 165 166
	methods: {
		logout() {
			// 1. 清空 token
			localStorage.removeItem('token')
			// 2. 跳转到登录页面
			this.$router.push('/login')
		},
		myHome() {
			// 新页面打开
			window.open('http://qinyingjie.top/')
		},
		async interviewRandom() {
			this.loading = true
167
			const { data: res } = await axios.get('http://localhost:8888/aphorismPoetry/random', { params: {} })
168 169 170 171 172 173 174 175 176 177 178
			if (res.code === 200) {
				this.randomAlgorithmic.poetryText = res.result.poetryText
			}
			this.loading = false
		},

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

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

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

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