<template>
	<view class="app">
		<view class="title">
			账号登录
		</view>
		<view class="loginphone">
			<input class="phone" type="text" placeholder="用户名" v-model="info.username" />
		</view>
		<view class="logincode loginphone" style="top: 40%;">
			<u-input class='code phone' placeholder="密码" maxlength="10" v-model="info.password" type="password"
				:border="true" :password-icon="true" />
		</view>
		<view class="loginbtn" @tap='logininfo'>
			登录
		</view>
		<u-checkbox-group @change="checkboxGroupChange" class='checkbox'>
			<u-checkbox @change="checkboxChange" v-model="item.checked" v-for="(item, index) in list" :key="index"
				:name="item.name">{{item.name}}</u-checkbox>
		</u-checkbox-group>
		<u-toast ref="uToast" />
	</view>
</template>
<script>
	import {
		login
	} from '@/api/login/index.js'
	export default {
		data() {
			return {
				list: [{
					name: '记住密码',
					checked: false,
					disabled: false
				}],
				info: {
					username: '',
					password: '',
					// username: 'admin',
					// password: '1852753',
					mobileLogin: true,
					timer: null,
				},
			};
		},
		onShow() {
			const that = this
			try {
				const value = uni.getStorageSync('username');
				const value2 = uni.getStorageSync('password');
				if (value && value2) {
					this.list[0].checked = true
					that.info.username = value
					that.info.password = value2
				}
			} catch (e) {}
		},
		//获取到传值
		onLoad(options) {},
		methods: {
			logininfo() {
				// let formdata = new FormData()
				// formdata.append('username', this.info.username);
				// formdata.append('password', this.info.password);
				// formdata.append('mobileLogin', this.info.mobileLogin);
				// console.log(formdata)
				login(this.info.username, this.info.password, this.info.mobileLogin).then(res => {
					console.log(res)
					if (res.success) {
						if (this.list[0].checked) {
							uni.setStorageSync("username", this.info.username)
							uni.setStorageSync("password", this.info.password)
						} else {
							uni.removeStorageSync('username')
							uni.removeStorageSync('password')
						}
						var that = this
						if (res.body.rose == 'qxzh' || res.body.rose == 'dzswszfzd' || res.body.rose == 'admin') {
							uni.setStorageSync("token", res.body.JSESSIONID)
							uni.setStorageSync("rose", res.body.rose)
							if (res.errorCode == '0' && res.msg == "登录成功!") {
								this.$refs.uToast.show({
									title: res.msg,
									type: 'success',
									duration: 1500
								})

							}

							if (this.timer) {
								clearTimeout(this.timer)
							}
							this.timer = setTimeout(e => {
								uni.reLaunch({
									url: '/pages/homepage/index'
								})
							}, 1500)
						} else {
							this.$refs.uToast.show({
								title: '账号无权限',
								type: 'error',
								duration: 1500
							})
						}
					} else {
						if (res.errorCode == '0' && res.msg == "用户账号密码错误（或登录超时）, 请重试.") {
							this.$refs.uToast.show({
								title: res.msg,
								type: 'error',
								duration: 1500
							})

						} else {
							this.$refs.uToast.show({
								title: res.msg,
								type: 'error',
								duration: 1500
							})

						}

					}

				})
			},
			checkboxChange(e) {},
			// 选中任一checkbox时，由checkbox-group触发
			checkboxGroupChange(e) {},
		},
	}
</script>

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