<template>
	<view class="container">
		<view class="loginTitle">Welcome云杉矿业</view>
		<view class="loginText">请使用您的账号登录</view>
		<view class="login-box">
			<view class="username">账号</view>
			<input class="input-box" type="number" v-model="phone" placeholder="请输入账号" />
		</view>
		<view class="login-box">
			<view class="username">密码</view>
			<input class="input-box" type="password" v-model="password" placeholder="请输入密码" />
		</view>
			<view class="forget" @click="navTo('/pages/public/forget')">忘记密码</view>
		<view class="login" @click="toLogin">登录</view>
		<view class="login-tip">还没有账号？<text class="register" @click="navTo('/pages/public/register')">立即注册</text></view>
	</view>
</template>

<script>
import { mapMutations,mapState } from 'vuex';
import { login } from '@/api/login.js';
import { getUserInfo } from '@/api/user.js';
export default {
	data() {
		return {
			phone: '',
			password: '',
		};
	},
	onLoad() {
		let obj = this
		uni.getStorage({
		    key: 'user_name',
		    success: function (res) {
		        obj.phone = res.data
		    }
		});
		uni.getStorage({
		    key: 'passWord',
		    success: function (res) {
		        obj.password = res.data
		    }
		});
		console.log(this.userInfo,'userInfo');
	},
	methods: {
		...mapMutations('user', ['setUserInfo', 'login']),
		...mapState('user',['userInfo']),
		//登录
		async toLogin() {
			let obj = this;
			obj.logining = true;
			if (obj.phone == '') {
				obj.$api.msg('请输入手机号');
				return;
			}
			if (obj.password == '') {
				obj.$api.msg('请输入密码');
				return;
			}
			login({
				account: obj.phone,
				password: obj.password
			})
				.then(function(e) {
					
					uni.setStorageSync('token', e.data.token);
					getUserInfo({}).then(e => {
						obj.login();
						// 保存返回用户数据
						obj.setUserInfo(e.data);
						
						// 保存用户账号
						uni.setStorage({
						    key: 'user_name',
						    data: obj.phone
						});
						//保存密码
						uni.setStorage({
						    key: 'passWord',
						    data: obj.password
						});
					
						uni.switchTab({
							url:'/pages/index/index'
						})
					});
				})
				.catch(function(e) {
					console.log(e);
				});
		},
		navTo(url) {
			uni.navigateTo({
				url
			});
		},
		// 后退
		navBack() {
			uni.navigateBack();
		}
	}
};
</script>

<style lang="scss">
page {
		min-height: 100%;
		background-color: #ffffff;
		.container {
			width: 100%;
			padding: 30% 60rpx 0;
		}
	}
.loginTitle {
	font-weight: bold;
	color: #333333;
	font-size: 58rpx;
	padding-bottom: 34rpx;
}
.loginText {
	font-weight: 500;
	color: #333333;
	font-size: 34rpx;
	padding-bottom: 34rpx;
}
.login-box{
	padding-top: 90rpx;
	.username{
		padding-bottom: 30rpx;
		font-weight: 500;
		color: #333333;
		font-size: 32rpx;
	}
}

.forget{
	text-align: right;
	margin: 70rpx 0rpx;
	font-weight: 500;
	color: #333333;
	font-size: 28rpx;
}
.login{
	background-color: #141E47;
	margin-top: 20rpx;
	color: #FFFFFF;
	text-align: center;
	padding: 26rpx 0rpx;
	border-radius: 50rpx;
}
.login-tip{
	padding: 60rpx 0rpx;
	text-align: center;
	font-weight: 500;
	color: #333333;
	font-size: 28rpx;
	.register{
		color: #FF6600;
	}
}
// /* input 样式 */
// .input-placeholder {
// 	color: #ffffff;
// }

// .placeholder {
// 	color: #ffffff;
// }

</style>
