<!-- 登录 -->
<template>
	<view class="login">
		<view class="logoPage">
			<image src="../static/img/logo.png" class="imglogo" mode=""></image>
			<view class="logo_from">
				<view class="item">
					<text class="loglabe">账号</text>
					<u-input placeholder="请输入账号" border="bottom" clearable class="loginput" v-model="userName"
						:focus='startfocus == "zh"'></u-input>
				</view>
				<view class="item">
					<text class="loglabe">密码</text>
					<u-input placeholder="请输入密码" border="bottom" clearable class="loginput" v-model="password"
						 :password="passwordtag">
						<template slot="suffix">
							<u-icon :name="passwordtag ? 'eye': 'eye-fill'" size="24" @click="handleShowPass"></u-icon>
						</template>
					</u-input>
				</view>
				<view class="dlbt" @click="loginfn">登录</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				smjg: '',
				startfocus: 'zh', // 光标自动聚焦
				userName: '', // 賬戶
				password: '', // 密碼
				passwordtag: true,
			}
		},
		onLoad(option) {
			this.$broadcastScan.init(this.getScancode);
			// 缓存设备的像素比用来区分普通安卓还是pda
			uni.setStorageSync('devicePixelRatio', uni.getDeviceInfo().devicePixelRatio <= 1.5 ? 'pad' : 'normalAnroid')
		},
		onUnload() {
			this.$broadcastScan.stop();
		},
		onHide() {
			this.$broadcastScan.stop();
		},
		onShow() {
			this.$broadcastScan.start();
		},
		mounted() {

		},
		methods: {
			handleShowPass() {
				this.passwordtag = !this.passwordtag
			},
			bule(val) {
				this.searchText = val.target.value
				console.log('看看我在键盘模式下的触发bule', val.target.value)
			},
			submit(val) {
				console.log('看看我在键盘模式下的触发', val.target.value)
			},
			getScancode(code) {
				// 有些PDA会自带换行符，trim函数处理下
				code = code.trim()
				this.v1 = code
				this.searchText = code
				//code就是扫描获取的值
				console.log(code)
			},
			// 登录
			loginfn() {
				if (!this.userName) {
					uni.$u.toast('请输入账户名')
					return
				}
				if (!this.password) {
					uni.$u.toast('请输入密码')
					return
				}
				this.$api.post('/Login/LoginInPwd', {
					userName: this.userName,
					password: this.password
				}).then(res => {
					if (res.status == 200) {
						uni.setStorageSync('token', res.data.tokenInfo.tokenType + " " + res.data.tokenInfo.token)
						uni.setStorageSync('userInfo', res.data.userInfo)
						uni.navigateTo({
							url: "/pages/index"
						})
					} else {
						uni.$u.toast(res.message)
					}
					console.log("登录", res)
				})
			}
		}
	}
</script>
<style lang="scss">
	@import "@/static/public.scss";
</style>