<template>
	<div class="body">		
		<uni-forms ref="form" label-position="top" :rules="rules" :model="form">
			<uni-forms-item label="账号" name="username" clearable>
				<uni-easyinput v-model="form.username" placeholder="请输入账号" trim></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item label="密码" name="password">
				<uni-easyinput v-model="form.password" type="password" placeholder="请输入密码" trim></uni-easyinput>
			</uni-forms-item>
		</uni-forms>	
		<button class="login-BT" type="default" @click="submit">登录</button>
	</div>
</template>

<script>
	import { login } from '@/api/user';
	import { setToken } from '@/libs/auth';	
	export default {
		data() {
			return {
				rules:{
					username:{
						rules:[
							{
								required: true,
								errorMessage: '请填写账号'
							}					
						],
						validateTrigger:'submit'
					},
					password:{
						rules:[
							{
								required: true,
								errorMessage: '请填写密码',
							}
						],
						validateTrigger:'submit'
					}					
				},
				form:{
					username:"",
					password:""
				}
			}
		},
		methods: {
			submit(){
				this.$refs.form.validate().then(res=>{
					login(this.form).then((res) => {
						let token = res.data.accessToken;
						const userId=res.data.user.userId;
						setToken(token);
						uni.setStorageSync('accountInfo', res.data.user);
						let info=uni.getSystemInfoSync();
						uni.switchTab({
							url: '/pages/index/index'
						})			
					}).catch((res)=>{
						console.log(res)
					})
				}).catch(err =>{
					console.log('表单错误信息：', err);
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.body{
	padding:224upx 90upx 0;
	.login-BT{
		margin-top:110upx;
		background: #8DA6FF;
		border-radius: 6px;
		color: #fff;
	}
}
</style>
