index.vue 7.8 KB
Newer Older
1
<template>
郝先瑞 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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
	<div class="login-container">
		<el-form
			ref="loginFormRef"
			:model="loginForm"
			:rules="loginRules"
			class="login-form"
			auto-complete="on"
			label-position="left"
		>
			<div class="title-container">
				<h3 class="title">{{ $t('login.title') }}</h3>
				<lang-select class="set-language" />
			</div>

			<el-form-item prop="username">
				<span class="svg-container">
					<svg-icon icon-class="user" />
				</span>
				<el-input
					ref="username"
					v-model="loginForm.username"
					:placeholder="$t('login.username')"
					name="username"
					type="text"
					tabindex="1"
					auto-complete="on"
				/>
			</el-form-item>

			<el-tooltip
				:disabled="capslockTooltipDisabled"
				content="Caps lock is On"
				placement="right"
			>
				<el-form-item prop="password">
					<span class="svg-container">
						<svg-icon icon-class="password" />
					</span>
					<el-input
						ref="passwordRef"
						:key="passwordType"
						v-model="loginForm.password"
						:type="passwordType"
						placeholder="Password"
						name="password"
						tabindex="2"
						auto-complete="on"
						@keyup="checkCapslock"
						@blur="capslockTooltipDisabled = true"
						@keyup.enter="handleLogin"
					/>
					<span class="show-pwd" @click="showPwd">
						<svg-icon
							:icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
						/>
					</span>
				</el-form-item>
			</el-tooltip>

			<!-- 验证码 -->
			<el-form-item prop="code">
				<span class="svg-container">
					<svg-icon icon-class="validCode" />
				</span>
				<el-input
					v-model="loginForm.code"
					auto-complete="off"
					:placeholder="$t('login.code')"
					style="width: 65%"
					@keyup.enter="handleLogin"
				/>

				<div class="captcha">
					<img
						:src="captchaBase64"
						@click="handleCaptchaGenerate"
						height="38px"
					/>
				</div>
			</el-form-item>

			<el-button
				size="default"
				:loading="loading"
				type="primary"
				style="width: 100%; margin-bottom: 30px"
				@click.prevent="handleLogin"
				>{{ $t('login.login') }}
			</el-button>

			<div class="tips">
				<span style="margin-right: 20px"
					>{{ $t('login.username') }}: admin</span
				>
				<span> {{ $t('login.password') }}: 123456</span>
			</div>
		</el-form>

		<div v-if="showCopyright == true" class="copyright">
			<p>{{ $t('login.copyright') }}</p>
			<p>{{ $t('login.icp') }}</p>
		</div>
	</div>
105 106
</template>

107
<script setup lang="ts">
郝先瑞 已提交
108
import { onMounted, reactive, ref, toRefs, watch, nextTick } from 'vue';
109 110

// 组件依赖
郝先瑞 已提交
111 112 113 114
import { ElForm, ElInput } from 'element-plus';
import router from '@/router';
import LangSelect from '@/components/LangSelect/index.vue';
import SvgIcon from '@/components/SvgIcon/index.vue';
115 116

// 状态管理依赖
郝先瑞 已提交
117
import useStore from '@/store';
118 119

// API依赖
郝先瑞 已提交
120 121 122
import { getCaptcha } from '@/api/login';
import { useRoute } from 'vue-router';
import { LoginFormData } from '@/types';
123

郝先瑞 已提交
124
const { user } = useStore();
125 126
const route = useRoute();

郝先瑞 已提交
127 128
const loginFormRef = ref(ElForm);
const passwordRef = ref(ElInput);
129 130

const state = reactive({
郝先瑞 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	redirect: '',
	loginForm: {
		username: 'admin',
		password: '123456',
		code: '',
		uuid: ''
	} as LoginFormData,
	loginRules: {
		username: [{ required: true, trigger: 'blur' }],
		password: [{ required: true, trigger: 'blur', validator: validatePassword }]
	},
	loading: false,
	passwordType: 'password',
	captchaBase64: '',
	// 大写提示禁用
	capslockTooltipDisabled: true,
	otherQuery: {},
	clientHeight: document.documentElement.clientHeight,
	showCopyright: true
郝先瑞 已提交
150
});
151 152

function validatePassword(rule: any, value: any, callback: any) {
郝先瑞 已提交
153 154 155 156 157
	if (value.length < 6) {
		callback(new Error('The password can not be less than 6 digits'));
	} else {
		callback();
	}
158 159
}

160
const {
郝先瑞 已提交
161 162 163 164 165 166 167
	loginForm,
	loginRules,
	loading,
	passwordType,
	captchaBase64,
	capslockTooltipDisabled,
	showCopyright
郝先瑞 已提交
168
} = toRefs(state);
169 170

function checkCapslock(e: any) {
郝先瑞 已提交
171 172 173
	const { key } = e;
	state.capslockTooltipDisabled =
		key && key.length === 1 && key >= 'A' && key <= 'Z';
174 175 176
}

function showPwd() {
郝先瑞 已提交
177 178 179 180 181 182 183 184
	if (state.passwordType === 'password') {
		state.passwordType = '';
	} else {
		state.passwordType = 'password';
	}
	nextTick(() => {
		passwordRef.value.focus();
	});
185 186 187
}

function handleLogin() {
郝先瑞 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
	loginFormRef.value.validate((valid: boolean) => {
		if (valid) {
			state.loading = true;
			user
				.login(state.loginForm)
				.then(() => {
					router.push({ path: state.redirect || '/', query: state.otherQuery });
					state.loading = false;
				})
				.catch(() => {
					state.loading = false;
					handleCaptchaGenerate();
				});
		} else {
			return false;
		}
	});
205 206 207 208
}

// 获取验证码
function handleCaptchaGenerate() {
郝先瑞 已提交
209 210 211 212 213
	getCaptcha().then(({ data }) => {
		const { img, uuid } = data;
		state.captchaBase64 = 'data:image/gif;base64,' + img;
		state.loginForm.uuid = uuid;
	});
214 215
}

郝先瑞 已提交
216
watch(
郝先瑞 已提交
217 218 219 220 221 222 223 224 225 226 227
	route,
	() => {
		const query = route.query;
		if (query) {
			state.redirect = query.redirect as string;
			state.otherQuery = getOtherQuery(query);
		}
	},
	{
		immediate: true
	}
郝先瑞 已提交
228
);
229 230

function getOtherQuery(query: any) {
郝先瑞 已提交
231 232 233 234 235 236
	return Object.keys(query).reduce((acc: any, cur: any) => {
		if (cur !== 'redirect') {
			acc[cur] = query[cur];
		}
		return acc;
	}, {});
237
}
238 239

onMounted(() => {
郝先瑞 已提交
240 241 242 243 244 245 246 247
	handleCaptchaGenerate();
	window.onresize = () => {
		if (state.clientHeight > document.documentElement.clientHeight) {
			state.showCopyright = false;
		} else {
			state.showCopyright = true;
		}
	};
郝先瑞 已提交
248
});
249 250 251 252 253 254
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

255 256
$bg: #283443;
$light_gray: #fff;
257 258 259 260
$cursor: #fff;

/* reset element-ui css */
.login-container {
郝先瑞 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
	.title-container {
		position: relative;

		.title {
			font-size: 26px;
			color: $light_gray;
			margin: 0px auto 40px auto;
			text-align: center;
			font-weight: bold;
		}

		.set-language {
			color: #fff;
			position: absolute;
			top: 3px;
			font-size: 18px;
			right: 0px;
			cursor: pointer;
		}
	}

	.el-input {
		display: inline-block;
		height: 47px;
		width: 85%;
		.el-input__wrapper {
			padding: 0;
			background: transparent;
			box-shadow: none;
			.el-input__inner {
				background: transparent;
				border: 0px;
				-webkit-appearance: none;
				border-radius: 0px;
				padding: 12px 5px 12px 15px;
				color: $light_gray;
				height: 47px;
				caret-color: $cursor;

				&:-webkit-autofill {
					box-shadow: 0 0 0px 1000px $bg inset !important;
					-webkit-text-fill-color: $cursor !important;
				}
			}
		}
	}

	.el-input__inner {
		&:hover {
			border-color: var(--el-input-hover-border, var(--el-border-color-hover));
			box-shadow: none;
		}

		box-shadow: none;
	}

	.el-form-item {
		border: 1px solid rgba(255, 255, 255, 0.1);
		background: rgba(0, 0, 0, 0.1);
		border-radius: 5px;
		color: #454545;
	}

	.copyright {
		width: 100%;
		position: absolute;
		bottom: 0;
		font-size: 12px;
		text-align: center;
		color: #cccccc;
	}
332 333 334 335
}
</style>

<style lang="scss" scoped>
336 337 338
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
339 340

.login-container {
郝先瑞 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
	min-height: 100%;
	width: 100%;
	background-color: $bg;
	overflow: hidden;

	.login-form {
		position: relative;
		width: 520px;
		max-width: 100%;
		padding: 160px 35px 0;
		margin: 0 auto;
		overflow: hidden;
	}

	.tips {
		font-size: 14px;
		color: #fff;
		margin-bottom: 10px;

		span {
			&:first-of-type {
				margin-right: 16px;
			}
		}
	}

	.svg-container {
		padding: 6px 5px 6px 15px;
		color: $dark_gray;
		vertical-align: middle;
		width: 30px;
		display: inline-block;
	}

	.title-container {
		position: relative;

		.title {
			font-size: 26px;
			color: $light_gray;
			margin: 0px auto 40px auto;
			text-align: center;
			font-weight: bold;
		}
	}

	.show-pwd {
		position: absolute;
		right: 10px;
		top: 7px;
		font-size: 16px;
		color: $dark_gray;
		cursor: pointer;
		user-select: none;
	}

	.captcha {
		position: absolute;
		right: 0;
		top: 0;

		img {
			height: 52px;
			cursor: pointer;
			vertical-align: middle;
		}
	}
408 409
}
</style>