<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<!-- <view class="header">
				<image src="../../static/logo.png"></image>
			</view> -->
			<!-- 主体表单 -->
			<view class="main mart90">
				<wInput v-model="phoneData" type="text" placeholder="用户名/电话" :focus="isFocus"></wInput>
				<wInput v-model="passData" type="password" placeholder="密码" isShowPass="true"></wInput>
			</view>
			<wButton class="wbutton" text="登 录" bgColor="#089bf5" :rotate="isRotate" @click="startLogin"></wButton>
		</view>
	</view>
</template>

<script>
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	import {
		userLogin,
		uniPush
	} from '../../Utils/Api.js';
	let app = getApp();
	export default {
		data() {
			return {
				phoneData: '', //用户/电话
				passData: '', //密码
				isRotate: false, //是否加载旋转
				isFocus: true // 是否聚焦
			};
		},
		components: {
			wInput,
			wButton,
		},
		mounted() {
			this.isLogin();
		},
		onShow() {
			

		},
		methods: {
			//判断缓存中是否登录过，直接登录
			isLogin() {
				// uni.getStorage({
				// 	key: 'payuser',
				// 	success: (res) => {
				// 		app.globalData.userInfo = res.data;
				// 		uni.getStorage({
				// 			key: "paytoken",
				// 			success: (res) => {
				// 				app.globalData.token = res.data;
				// 				uni.reLaunch({
				// 					url: '../tabBar/home/home'
				// 				})
				// 			}
				// 		})

				// 	}
				// });
				uni.getStorage({
					key: 'loginaccount',
					success: (res) => {
						this.phoneData = res.data
					}
				});
				uni.getStorage({
					key: 'loginpass',
					success: (res) => {
						this.passData = res.data
					}
				});
			},
			//登录
			startLogin(e) {
				uni.showLoading({
					title: '登录中'
				});
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length == "") {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '用户名不能为空'
					});
					return;
				}
				if (this.passData.length < 5) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码不正确'
					});
					return;
				}
				this.isRotate = true
				let params = {
					loginName: this.phoneData,
					password: this.passData
				}
				userLogin(params).then(res => {
					uni.hideLoading();
					this.isRotate = false;
					if (res.return_code == '000000' && res.return_data) {
						app.globalData.userInfo = res.return_data
							.object;
						app.globalData.token = res.return_data.uniqueCode;
						uni.setStorage({
							key: "loginaccount",
							data: this.phoneData
						})
						uni.setStorage({
							key: "loginpass",
							data: this.passData
						})
						uni.setStorage({
							key: "payuser",
							data: res.return_data
								.object
						})
						uni.setStorage({
							key: "paytoken",
							data: res.return_data.uniqueCode
						})
						uni.reLaunch({
							url: '../tabBar/home/home'
						})
					} else {
						uni.showToast({
							title: res.return_msg,
							icon: 'none',
							duration: 2000
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>