<template>
	<view class="login">
		<Logo></Logo>
		<view class="form">
			<van-field :value="username" name="用户名" label="用户名" placeholder="请输入用户名" @change="onUser" />
			<van-field :value="password" type="password" name="密码" label="密码" placeholder="请输入密码" @change="onWord" />
			<view class="btn">
				<van-button type="danger" block square form-type="submit" @click="goLogin">登录</van-button>
			</view>
		</view>
		<view class="register">
			<navigator url="/pages/register/register">没有账号去注册</navigator>
		</view>
	</view>
</template>


<script>
	import {
		toLogin
	} from '../../api/user.js';
	import {
		encrypto
	} from '@/utils/cryptojs.js'
	export default {
		data() {
			return {
				username: "",
				password: ""
			}
		},
		methods: {
			onUser(event) {
				this.username = event.detail;
			},
			onWord(event) {
				this.password = event.detail;
			},
			async goLogin() {
				const result = await toLogin({
					userName: this.username,
					passWord: encrypto(this.password)
				})
				if (result.accessToken) {
					uni.setStorageSync("token", result.accessToken);
					uni.switchTab({
						url: "/pages/home/home"
					})
				}
			}
		}
	}
</script>

<style lang="less">
	.form {
		width: 80%;
		margin: 50rpx auto;

		.btn {
			margin-top: 40rpx;
		}
	}

	.register {
		color: #00f;
		width: 209rpx;
		margin: 0 auto;
	}
</style>
