<template>
	<view class="pw-login-wrap">
		<!-- <view v-if="!buildType" style="text-align: center;color: #999999;padding-top: 40px;">{{ envTips }}</view> -->
		<view class="logo">
			<u-image src="/static/logo.jpg" width="140" height="140" ></u-image>
		</view>
		<form class="login-form" @submit="formSubmit">
			<view class="form-item">
				<input v-model="form.loginName" class="item-inp" name="loginName" type="text" maxlength="30" placeholder="请输入用户名" />
			</view>
			<view class="form-item">
				<input v-model="form.password" class="item-inp" name="password" password type="text" maxlength="12" placeholder="请输入密码" />
			</view>
			<view class="flex-box" style="margin-top: 60upx;">
				<view class="remember-pass">
					<u-checkbox v-model="isRemember" value="true" :size="30" :label-size="26" active-color="#07c160" class="login-form-checkbox" @change="rememberChange">记住密码</u-checkbox>
				</view>
			</view>
			<view class="form-btn-con">
				<button class="form-btn" form-type="submit">登录</button>
			</view>
		</form>
	</view>
</template>

<script>
import market from "@/js_sdk/dc-market/market.js"
import { checkVersionToLoadUpdate, setStorageForAppVersion,clzConfirm } from '@/libs/tools'
import { isvalidPhone } from '@/libs/validate.js';
import { loginPhone } from '@/api/user.js';
import { getSysVersion } from '@/api/data.js'
export default {
	data() {
		return {
			form: {
				loginName: this.$store.state.vuex_user.account,
				password: this.$store.state.vuex_user.password
			},
			isRemember: true, //  是否记住密码
			envTips: '',
			buildType: '',
		};
	},
	mounted() {
		this.isRemember = this.$store.state.vuex_isRemember || true;
		this.envTips = getApp().globalData.envTips;
		this.buildType = getApp().globalData.buildType;
	},
	onShow() {
	},
	methods: {
		//  获取微信login  code
		getWechatCode(){
			const _this = this
			wx.login({
			  success (res) {
			    if (res.code) {
					//发起网络请求
					let formData = JSON.parse(JSON.stringify(_this.form))
					formData.code = res.code
					_this.submitForm(formData)
			    } else {
					console.log('登录失败，点击“登录”重试！' + res.errMsg)
			    }
			  }
			})
		},
		//  表单提交
		formSubmit() {
			const _this = this;
			//  表单校验
			if (_this.form.loginName == '') {
				uni.showToast({ icon: 'none', title: '请输入用户名' });
				return;
			}
			if (_this.form.password == '') {
				uni.showToast({ icon: 'none', title: '请输入密码' });
				return;
			}
			if (_this.form.password.length < 6 || _this.form.password.length > 12) {
				uni.showToast({ icon: 'none', title: '请输入6-12位密码' });
				return;
			}
			uni.showLoading({
				title: '正在登录...'
			});
			// #ifdef MP-WEIXIN
				//  获取微信login   code
				this.getWechatCode()
			// #endif
			// #ifndef MP-WEIXIN
				this.submitForm(_this.form)
			// #endif
		},
		submitForm(formData){
			const _this = this;
			//  登录
			loginPhone(formData).then(res => {
				uni.hideLoading();
				if (res.status == 200) {
					_this.$u.vuex('vuex_token', res.data);
					getApp().globalData.token = res.data;
					//登录成功将用户名密码存储到用户本地
					if (_this.isRemember) {
						//用户勾选“记住密码”
						_this.$u.vuex('vuex_user.account', _this.form.loginName);
						_this.$u.vuex('vuex_user.password', _this.form.password);
					} else {
						//用户没有勾选“记住密码”
						_this.$u.vuex('vuex_user.account', '');
						_this.$u.vuex('vuex_user.password', '');
						_this.form.loginName = '';
						_this.form.password = '';
					}
					uni.setStorageSync('loginTimeOut', 'NO');
					_this.toMain();
				} else {
					uni.showToast({ icon: 'none', title: res.message });
				}
			});
		},
		toMain() {
			// 跳转到首页
			uni.switchTab({
				url: '/pages/index/index'
			});
		},
		//  忘记密码
		forgetPass() {
			uni.navigateTo({
				url: '/pages/login/forget-pass?username=' + this.form.loginName
			});
		},
		//  判断是否记住密码
		rememberChange(e) {
			this.isRemember = e.value;
		},
		toYsPage() {
			uni.navigateTo({
				url: '/pages/agreement/agreement'
			});
		}
	}
};
</script>
<style scoped lang="scss">
@import './login.scss';
</style>
