<template>
	<view>
		<!--<view class="l_top"><image src="" mode="aspectFill"></image></view>-->
		<view class="bg">
			<view class="login">
				<view class="form_group">
					<view class="lable"><text class="icon">&#xe6a0;</text></view>
					<input type="text" placeholder="邮箱/名字/工号/手机号" v-model="item.username" />
				</view>
				<view class="form_group">
					<view class="lable"><text class="icon">&#xe728;</text></view>
					<input type="password" placeholder="密码" v-model="item.password" />
				</view>
				<view class="form_group" v-if="checkcodeImg !=''">
					<view class="lable" style="width: 25%;">
						<image :src=checkcodeImg style="height: 70rpx;"></image>
					</view>
					<input type="text" placeholder="验证码" v-model="item.checkcode" />
				</view>
				<button class="btn" @click="login()">登陆</button>
				<navigator url="/pages/find_pass" class="pass" hover-class="none">找回密码</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				item: {
					domainName: '柳工',
					remember: '0',
					debug: 'false',
					checkcode: '',
					language: 'CN',
					url: '/obpm'
				},
				uitem: {},
				checkcodeImg: ''
			};
		},
		onLoad(e) {


			//刷新
			uni.$on('index', res => {
				//this.refresh();
				uni.showLoading({
					title: '加载中',
					mask: true
				});
			});

		},

		mounted() {
			//自动登陆

			if (this.hasLogin()) {
				//uni.redirectTo({ url: '/pages/index/index' ,


				//	});

			}
		},
		methods: {
			//登陆
			login() {
				let rule = [{
					name: 'username',
					checkType: 'notnull',
					errorMsg: '请输入账号'
				}, {
					name: 'password',
					checkType: 'notnull',
					errorMsg: '请输入密码'
				}];
				//if (!vm.verify.check(this.item, rule)) {
				//	uni.showModal({ content: this.verify.error, showCancel: false });
				//	return false;
				//	}
				this.item.password = window.btoa(this.item.password);
				var p1 = this.item.password.slice(2);
				var p2 = this.item.password.substring(0, 2)
				this.item.password = p1 + p2;
				//console.log("this.item："+this.item);
				this.http.request({
					url: this.http.urls.login,
					data: this.item,
					method: 'post',
					success: res => {
						console.log("gfgh===" + JSON.stringify(res.data));
						if (res.data.resultCode == 1) {

							uni.removeStorageSync('user');
							uni.setStorageSync('user', res.data);
							this.myprofile();

						} else {
							this.checkcodeImg = res.data.checkcodeImg;
						}
					}
				});
			}, //登陆
			myprofile() {

				this.http.request({
					url: this.http.urls.myprofile,
					data: this.item,
					success: res => {

						if (res.data.errmsg == 'ok') {
							var accessToken = this.getUser().accessToken;
							res.data.data.accessToken = accessToken;
							uni.removeStorageSync('user');
							//console.log("gfgh==="+JSON.stringify(res.data.data));
							uni.setStorageSync('user', res.data.data);
							uni.$emit('index');
							uni.redirectTo({
								url: '/',

							});
							console.log("res.data.resultCode===" + res.data.errcode);
						}
					}
				});
			},

		}
	};
</script>
<style lang="scss">
	.bg {
		padding: 15px;

		image {
			width: 100%;
			height: 10px;
			border-radius: 5px;
		}

		.login {
			.form_group {
				border: 1px solid $line;
				border-radius: 5px;
				margin-top: 10px;

				.lable {
					width: 10px;
				}

				input {
					border: 0px;
				}
			}

			.btn {
				margin-top: 25px;
				width: 82%;
			}

			.pass {
				width: 90%;
				text-align: right;
				padding-top: 20px;
				//color: $cor;
			}
		}
	}
</style>
