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 11 12
		<marquee ref="myMarquee" behavior="scroll" direction="left" :scrollamount="5" width="1000px" height="50px">
			<h1 class="layout-header-left-title ml-3" :style="{ color: textColor }">{{ randomAlgorithmic.poetryText }}</h1>
		</marquee>
13 14 15 16 17 18 19
		<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>
20 21 22
</template>

<script>
23
import axios from 'axios'
24
export default {
25 26 27 28 29 30
	name: 'MyHeader',
	data() {
		return {
			randomAlgorithmic: {
				poetryText: '',
			},
31 32
			textColor: 'skyblue', // 初始字体颜色为黑色
			loading: false,
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
			color: [
				'red',
				'blue',
				'green',
				'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',
			],
		}
	},
	watch: {
137 138 139
		'randomAlgorithmic.poetryText'(newVal, oldVal) {
			// 当值改变时,根据条件设置不同的字体颜色
			this.textColor = this.getRandomColor()
140 141 142 143 144 145
		},
	},
	created() {
		//获取问题类型的枚举
		this.startInterviewRandomTimer()
	},
146 147 148 149
	destroyed() {
		//销毁的时候需要清理定时器
		this.stopInterviewRandomTimer()
	},
150 151 152 153 154 155 156 157 158 159 160 161 162
	methods: {
		logout() {
			// 1. 清空 token
			localStorage.removeItem('token')
			// 2. 跳转到登录页面
			this.$router.push('/login')
		},
		myHome() {
			// 新页面打开
			window.open('http://qinyingjie.top/')
		},
		async interviewRandom() {
			this.loading = true
163
			const { data: res } = await axios.get('http://120.79.36.53:8888/aphorismPoetry/random')
164 165 166 167 168 169 170 171 172 173
			if (res.code === 200) {
				this.randomAlgorithmic.poetryText = res.result.poetryText
			}
			this.loading = false
		},
		// 定时执行 interviewRandom 方法
		startInterviewRandomTimer() {
			this.interviewRandom() // 首次调用方法
			this.interviewRandomTimer = setInterval(() => {
				this.interviewRandom()
174
			}, 15000) // 5000 毫秒(5秒)为间隔
175 176 177 178 179 180 181 182 183 184 185 186
		},

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

<style lang="less" scoped>
.layout-header-left-img {
192
	height: 50px;
193
}
194
</style>