<template>
	<view class="loginPage">

		<view class="loginHeader">
			<view class="loginHeaderTitle">欢迎使用河道保洁系统</view>
			<view class="back" @click="back">返回主页</view>
		</view>
		<view class="loginBox">
			<u-field v-model="username" label-width=0 icon="account" placeholder="请填写登陆名">
			</u-field>
			<u-field v-model="password" label-width=0 icon="lock" :password="true" placeholder="请填写密码">
			</u-field>
		</view>

		<view class="loginFoot">
			<u-button :ripple="true" @click="loginBind" ripple-bg-color="#0385e5" type="primary">登陆</u-button>
		</view>
	</view>
</template>

<script>
	var util = require("../../utils/util.js");
	export default {
		data() {
			return {
				username: '',
				password: '',
				token: "",
				url: "",
				paraString: ""
			}
		},
		onLoad: function(options) {
			let userinfo = uni.getStorageSync('userInfo_token');
			this.paraString = window.location.href;
			let token =decodeURIComponent(this.getQueryVariable('token'))
			if (!util.isNull(token) && options.tp == "hdgl") {
				let url = util.HOST + '/api/mobile/auth/oss-login';
				this.token = token;
				this.url = url;
				uni.request({
					url: util.HOST + '/api/mobile/auth/oss-login',
					method: 'POST',
					data: {
						'token': token,
						'tp': options.tp
					},
					success: function(res) {
						console.log(!res.data.errcode)
						if (!res.data.errcode) {
							uni.setStorageSync('userInfo_token', res.data);
							uni.redirectTo({
								url: '/pages/index/index'
							});
						} else {

							util.alert(res.data.errmsg);
						}
					},
					fail: function(res) {
						console.log(res)
						util.alert(res);
					}
				});
			} else {

			}


		},
		methods: { //第二种
			getQueryVariable(variable) {
				var query = this.paraString;
				var rooms = query.split('?');
				for (var i = 0; i < rooms.length; i++) {
					var pair = rooms[i].split('=');
					if (pair[0] == variable) {
						return (pair[1].split('&'))[0]
					}
				}
				//或者拼接&后面的值
				var vars = query.split('&')
				for (var i = 0; i < vars.length; i++) {
					var pair = vars[i].split('=')
					if (pair[0] == variable) {
						return pair[1]
					}
				}
			},
			back: function() {
				window.android.finish();
			},
			loginBind: function() {
				let that = this;

				if (that.username == "") {
					util.alert("请输入登陆账号");
					return false;
				}

				if (that.password == "") {
					util.alert("请输入密码");
					return false;
				}

				util.request({
					api: '/api/mobile/auth/login',
					method: 'POST',
					data: {
						'username': that.username,
						'password': that.password
					},
					utilSuccess: function(res) {
						console.log(res)
						uni.setStorageSync('userInfo_token', res);
						uni.redirectTo({
							url: '/pages/index/index'
						});
					},
					utilFail: function(res) {
						console.log(res)
						util.alert(res);
					}
				});
			}

		}
	}
</script>

<style>
	.loginPage {}

	.loginFoot {
		padding: 40rpx;
		margin-top: 100rpx;
	}

	.loginBox {
		padding: 120rpx 40rpx;
		background: #FFFFFF;
		margin-top: -50rpx;
		background: #FFFFFF;
		border-radius: 50rpx;
	}

	.loginHeaderTitle {
		display: flex;
		font-size: 40rpx;
		padding: 80rpx 0;
		color: #fff;
	}

	.loginHeader {
		padding: 80rpx 40rpx;
		background: #0385e5;
		display: flex;
		justify-content: space-around;
	}

	.back {
		color: #FFFFFF;
	}
</style>
