<template>
	<view class="content">
		<view class="head">
			<text>登录</text>
		</view>
		<view class="login-wrap">
			<view class="username item">
				<text>账号：</text>
				<uni-easyinput :inputBorder="false" class="login_input" type="text" v-model="username" placeholder="请输入账号" />
			</view>
			<view class="password item">
				<text>密码：</text>
				<uni-easyinput 
					v-model="password"
					:inputBorder="false"
					 class="login_input" 
					 type="password"  
					 placeholder="请输入密码">
				</uni-easyinput>
			</view>
			<button type="primary" @click="loginSubmit" class="submit-BT">登录</button>
		</view>
	</view>
</template>

<script>
	import store from '@/store/index.js'
	import { login } from '@/api/system/user.js';
	import { setToken } from '@/utils/auth.js';	
	import { wxLogin} from '@/api/openApi.js'
	export default {
		data() {
			return {
				username: "",
				password: "",
			}
		},
		onLoad({username,password}) {
			if(username){
				this.username=username;
				this.password=password;
			}
		},
		methods: {		
			loginSubmit() {	
				let username = (this.username).trim();
				let password = (this.password).trim();
				if (username.length < 1) {
					uni.showToast({
						title: '请填写账号',
						icon: "error"
					})
					return;
				}
				if (password.length < 1) {
					uni.showToast({
						title: '请填写密码',
						icon: "error"
					})
					return;
				}
				// #ifndef MP-WEIXIN
					login({
						username,
						password
					}).then((res) => {
						let token = res.data.accessToken;
						setToken(token);							
						uni.setStorageSync('accountInfo', res.data.user);
						uni.setStorageSync('isLogin', true);
						store.commit('setToken', token);
						uni.reLaunch({
							url: '/pages/index/index'
						})				
					})				
				// #endif
				//#ifdef MP-WEIXIN
					uni.login({
						provider: 'weixin',
						success: function (loginRes) {
							let code=loginRes.code;
							wxLogin(code).then((resp)=>{
								const { code, data, msg } = resp
								login({
									username,
									password
								}).then((res) => {
									let token = res.data.accessToken;
									setToken(token);
									uni.setStorageSync('unionid',data.unionid)								
									uni.setStorageSync('accountInfo', res.data.user);
									uni.setStorageSync('isLogin', true);
									store.commit('setToken', token);
									uni.reLaunch({
										url: '/pages/index/index'
									})				
								})
							})
						}						
					})
				//#endif					
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		.head {
			font-size: 50upx;
			text-align: center;
			padding: 40upx 0;
			letter-spacing: 2upx;
		}

		.login-wrap {
			width: 90%;
			margin: 50upx auto 0;
			box-sizing: border-box;
			padding: 20upx;
			border-radius: 8upx;

			.item {
				display: flex;
				justify-content: flex-start;
				align-items: center;
				height: 100upx;
				font-size: 32upx;
				line-height: 100upx;
				input {
					font-size: 32upx;
					height: 100upx;
					border: 0;
				}

				.login_input {
					width: 580upx;
					border: none;
				}
			}



			.tip {
				text-align: right;
				padding: 18upx 0;
				font-size: 28upx;
			}

			.submit-BT {
				border-radius: 16upx;
				margin-top: 50upx;
				background-color:#3384FF;
			}
		}
	}
</style>
