<template>
	<view class="app">
		<view class="title">
			账号登录
		</view>
		<view class="loginphone logincode">
			<u-input class="code phone" type="text" placeholder="身份证号" maxlength="18" v-model="phone" />
		</view>
		<view class="logincode loginphone" style="top: 40%;">
			<u-input class='code phone' placeholder="密码" maxlength="20" v-model="password" type="password"
				:password-icon="true" />
		</view>
		<view class="loginbtn" @tap='pwdlogin'>
			登录
		</view>
		<!-- <view class="switch" @tap='goforgotPassword' style="left:10%">
			忘记密码
		</view> -->
		<view class="switch" @tap='goregister' style="right:10%">
			没有账号,请先注册
		</view>
		<u-toast ref="uToast" />
	</view>
</template>
<script>
	import {
		info
	} from '@/api/user/index.js'
	import {
		login
	} from '../../api/login/index.js'
	export default {
		data() {
			return {
				phone: '',
				password: '',
			};
		},
		//获取到传值
		onLoad(options) {
			if (options.phone && options.password) {
				this.phone = options.phone
				this.password = options.password
			}
		},
		onShow() {
			if (uni.getStorageSync('examh5token')) {
				uni.switchTab({
					url: "/pages/examlist/examlist"
				})
				// info({
				// 	token: uni.getStorageSync('examh5token')
				// }).then(res => {
				// 	if (res.code == 0) {
				// 		uni.setStorageSync("examh5token", res.data.token)
				// 		uni.setStorageSync("examh5user", {
				// 			id: res.data.id,
				// 			phone: res.data.phone,
				// 			name: res.data.realName,
				// 			cardNo: res.data.userName
				// 		})
				// 	}
				// })
			}
		},
		methods: {
			pwdlogin() {
				login({
					username: this.phone,
					password: this.password
				}).then(res => {
					if (res.code == 0) {
						uni.setStorageSync("examh5token", res.data.token)
						uni.setStorageSync("examh5user", {
							id: res.data.id,
							phone: res.data.phone,
							name: res.data.realName,
							cardNo: res.data.userName
						})
						this.$refs.uToast.show({
							title: "登录成功!",
							type: 'success',
							duration: '1500'
						})
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/examlist/examlist',
							})
						}, 1500);
					} else {
						this.$refs.uToast.show({
							title: res.msg,
							type: 'error'
						})
					}
				})
			},
			//跳转注册页
			goregister() {
				uni.navigateTo({
					url: '/pages/register/register'
				})
			},
			//跳转忘记密码页面
			goforgotPassword() {
				uni.navigateTo({
					url: `/pages/forgotPassword/forgotPassword?phone=${this.phone}`
				})
			},
		},
	}
</script>

<style lang="scss">
	@import "./login.scss";
</style>