<template>
	<view class="login_form_warp">
		<view class="login_form_item">
			<input class="login_form_input" v-model="form.username" placeholder="请输入账号" maxlength="20" />
		</view>
		<view class="login_form_item">
			<input class="login_form_input" v-model="form.password" type="password" placeholder="请输入密码" maxlength="20" />
		</view>
		<view class="login_form_item">
			<input class="login_form_input" v-model="form.verCode" placeholder="请输入图形验证码" maxlength="20" />
			<image :src="codeImg" @click="getVerCode" style="width: 100upx; height: 40upx;"></image>
		</view>
		<button type="primary" class="page-body-button" @click="doLogin">登录</button>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				form: {
					username: '',
					password: '',
					verCode: '',
					vercodeType: 5
				}, //登录form
				codeImg: '', //图形验证码
			}
		},
		onLoad() {
			this.getVerCode();
		},
		methods: {
			//获取图形验证码
			getVerCode() {
				this.$request1(getApp().globalData.baseUrl + '/api/common/captchaBase64?type=5&_t=' + new Date(), {}, 'GET').then(res => {
					if(res.code == 200) {
						this.codeImg = res.data.img;
					}
				}).catch(() => {
					console.log('vercode err')
				})
			},
			//登录
			doLogin() {
				if(this.form.username == '' || this.form.password == '') {
					uni.showToast({
						title: '用户名密码不能为空',
						icon: 'none'
					})
					return;
				} else if(this.form.verCode == '') {
					uni.showToast({
						title: '验证码不能为空',
						icon: 'none'
					})
					return;
				}
				uni.showLoading({
					title: '登录中...',
					mask: true
				})
				this.$request1(getApp().globalData.baseUrl + '/api/login/auth', this.form, 'POST').then(res => {
					uni.hideLoading();
					if(res.code == 200) {
						uni.setStorageSync('satoken', res.data.satoken);
					}
				}).catch(() => {
					uni.hideLoading();
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.login_form_warp{
		.login_form_item {
			display: flex;
			align-items: center;
			.login_form_input {
				padding: 8upx 10upx;
			}
		}
	}
	button {
		background-color: #007aff;
		color: #ffffff;
	}
</style>