<template>
	<view
		style="background: url(../../static/bg-login.png) no-repeat;background-size:100% 100%;height:100vh;background-color: #058cf5; position: fixed; width: 100%;">
		<!-- <view class="bottom-img">
			<image src="../../static/hg-bottom.png" mode=""></image>
		</view> -->
		<view class="login-box">
			<view class="login-title">
				<text>欢迎使用，</text>
				<text>唐山文旅集团安全信息化管理平台!</text>
			</view>
			<view class="login-content">
				<view class="input-row">
					<view class="title">
						<text class="">手机号码</text>
					</view>
					<view class="m-input-view">
						<input v-model="userName" class="m-input" placeholder-style="color: #5caceb;font-size: 34upx;"
							placeholder="请输入手机号..." />
					</view>
				</view>
				<view class="input-row mt30">
					<view class="title">
						<text class="">密码</text>
					</view>
					<view class="m-input-view">
						<input v-model="userPwd" class="m-input" placeholder-style="color: #5caceb;font-size: 34upx;"
							password type="text" placeholder="请输入密码..." />
					</view>
				</view>
			</view>
			<view class="btn-row">
				<button class="cu-btn bg-login margin-tb-sm lg" @click="$noMultipleClicks(login)">登 录</button>
			</view>
			<view class="bar-forget">
				<!-- 				<navigator class="title" url="/pages/login/forget/forget">
					忘记密码
				</navigator> -->
				<!-- 				<view class="title">
					注册
				</view> -->
			</view>
		</view>
	</view>
</template>

<script>
	import {
		basePath,
		setloginUserId,
		setCorpinfoId,
		setloginUser,
		setDeptId,
		setDeptLevel,
		corpinfoId,
		setBasePath,
		setIsRest,
		isRest,
		loginUser
	} from '@/common/tool.js';
	export default {
		data() {
			return {
				noClick: true,
				userName: '',
				userPwd: '',

				oldUserName: '', // 判断是否新账号使用
				oldUserPwd: '',
			}
		},
		onLoad() {
			var _this = this
			// this.getUrlVersion();
			uni.getStorage({
				key: 'USER',
				success: function(res) {
					if (res.data && res.data.USER_ID) {
						setloginUserId(res.data.USER_ID);
						setCorpinfoId(res.data.CORPINFO_ID);
						setDeptId(res.data.DEPARTMENT_ID);
						setDeptLevel(res.data.DEPARTMENT_LEVEL);
						// 记住账号密码功能。因不知道密码。所以用user_id 前10位做临时密码（oldUserPwd）
						// 点击登陆按钮后，如果oldUserPwd与userPwd相同。oldUserName与userName相同。说明用户直接点的登录，可以直接登录
						// 如果上述两组数据。任一组不相同。则用户修改了初始化的账号密码。需走正常登录流程
						_this.userName = res.data.USERNAME
						_this.oldUserName = res.data.USERNAME
						var temPwd = '60d68d30d7'; // 若user_id 不足10 位。则用此临时密码
						if (res.data.USER_ID.length > 10) {
							temPwd = res.data.USER_ID.substring(0, 10)
						}
						_this.userPwd = temPwd
						_this.oldUserPwd = temPwd
						setloginUser(res.data);
						// 查看是否离岗
						uni.request({
							url: basePath + '/app/offduty/isRest',
							method: 'POST',
							dataType: 'json',
							header: {
								'Content-type': 'application/x-www-form-urlencoded'
							},
							data: {
								CORPINFO_ID: corpinfoId,
								USER_ID: loginUser.USER_ID,
							},
							success: (res1) => {
								if (res1.data.ISREST != null) {
									setIsRest(res1.data.ISREST);
								} else {
									setIsRest(res.data.ISREST);
								}


							},
							fail: (res2) => {
								uni.showToast({
									icon: 'none',
									title: "服务器正在升级，请稍后再试。",
									duration: 2000
								});
							}
						});
						// uni.navigateTo({
						// 	url: '/pages/index/index',
						// });
					}
				}
			});
		},
		methods: {
			userNameinp(e) {
				this.userName = e.detail.value
			},
			userPwdinp(e) {
				this.userPwd = e.detail.value
			},
			login() {

				var _this = this;
				console.info(basePath + '/admin/check')
				var userName = _this.userName;
				var userPwd = _this.userPwd;
				var keydataVal = 'qdkjchina' + userName + ',qd,' + userPwd;


				var myreg = /^(((13[0-9]{1})|159)+\d{8})$/;

				if (userName == '') {
					uni.showToast({
						icon: 'none',
						title: '请输入用户名',
						duration: 2000
					});
					return;
				} else if (userName.length != 11 && !myreg.test(userName)) {
					uni.showToast({
						icon: 'none',
						title: '手机号格式不正确',
						duration: 2000
					});
					return;
				}
				if (userPwd == '') {
					uni.showToast({
						title: '密码不能为空',
						icon: 'none',
						duration: 2000
					});
					return;
				}

				// 点击登陆按钮后，如果oldUserPwd与userPwd相同。oldUserName与userName相同。说明用户直接点的登录，可以直接登录
				// 如果上述两组数据。任一组不相同。则用户修改了初始化的账号密码。需走正常登录流程
				if (_this.oldUserName == _this.userName && _this.oldUserPwd == _this.userPwd) {
					uni.navigateTo({
						url: '/pages/index/index'
					});
				} else {


					uni.showLoading({
						mask: true,
						title: '请稍候'
					})
					uni.request({
						url: basePath + '/admin/check',
						method: 'POST',
						dataType: 'json',
						header: {
							'Content-type': 'application/x-www-form-urlencoded'
						},
						data: {
							KEYDATA: keydataVal,
							SOURCE: '2',
							tm: new Date().getTime(),
						},
						success: (res) => {

							var result = res.data.result;
							if ("success" == result) {
								// 登录成功后，若有之前记住的账号密码。则进行消除
								if (_this.oldUserName && _this.oldUserName != null &&
									_this.oldUserName != undefined &&
									_this.oldUserName != 'undefined' &&
									_this.oldUserName != '') {
									setloginUserId(null);
									setCorpinfoId(null);
									setDeptId(null);
									setloginUser(null);
									setIsRest(null);
									uni.removeStorage({
										key: 'USER'
									});
								}
								uni.hideLoading();
								uni.showToast({
									title: "登录成功",
									duration: 1000
								});
								setloginUserId(res.data.USER_ID);
								setCorpinfoId(res.data.CORPINFO_ID);
								setDeptId(res.data.DEPARTMENT_ID);
								setDeptLevel(res.data.DEPARTMENT_LEVEL);
								setIsRest(res.data.ISREST);
								setloginUser(res.data);
								uni.setStorage({
									key: 'USER',
									data: res.data
								});
								uni.navigateTo({
									url: '/pages/index/index'
								});

							} else if ("usererror" == result) {
								uni.showToast({
									icon: 'none',
									title: "用户名或密码有误",
									duration: 2000
								});
							} else if ("error" == result) {
								uni.showToast({
									icon: 'none',
									title: "缺少参数",
									duration: 2000
								});
							} else if ("invalid" == result) {
								uni.showToast({
									icon: 'none',
									title: "账号已被冻结，请联系管理员",
									duration: 2000
								});
							} else if ("exception" == result) {
								uni.showToast({
									icon: 'none',
									title: res.data.exception,
									duration: 2000
								});
							} else {
								uni.showToast({
									icon: 'none',
									title: res.data.msg,
									duration: 2000
								});
							}

						},
						fail: (res) => {
							uni.showToast({
								icon: 'none',
								title: "服务器正在升级，请稍后再试。",
								duration: 2000
							});
						}
					})
				}
			},
			getUrlVersion() {
				plus.runtime.getProperty(plus.runtime.appid, function(inf) {
					var _this = this;
					//  路径
					uni.request({
						url: basePath + '/app/versionurl/goUrlByVersion',
						method: 'POST',
						dataType: 'json',
						header: {
							'Content-type': 'application/x-www-form-urlencoded'
						},
						data: {
							VERSION: inf.version,
						},
						success: (res) => {
							if (res.data.pd != null) {
								console.info(inf.version);
								setBasePath(res.data.pd.URL);
							}
						},
						fail: (res) => {
							uni.showToast({
								icon: 'none',
								title: "服务器正在升级，请稍后再试。",
								duration: 2000
							});
						}
					});
				});
			}
		}
	}
</script>

<style>
	.bottom-img {
		height: 298upx;
		width: 100%;
		position: fixed;
		bottom: 0;
	}

	.bottom-img image {
		width: 100%;
		height: 100%;
	}

	.bg-login {
		background-color: #72b0ed;
		color: #1f63ae;
		font-weight: bold;
	}

	.btn-row {
		display: flex;
		flex-direction: column;
	}

	.login-box {
		padding: 105upx 55upx;
	}

	.login-img {
		width: 90upx;
		height: 90upx;
		margin-bottom: 50upx;
	}

	.login-img image {
		width: 100%;
		height: 100%;
	}

	.login-title {
		color: #fff;
		font-size: 44upx;
		font-weight: 500;
		display: flex;
		flex-direction: column;
		padding-top: 46upx;
		line-height: 1.6;
	}

	.login-content {
		margin-top: 100upx;
		margin-bottom: 50upx;
	}

	.input-row .title {
		color: #8ee6ff;
		margin-bottom: 10upx;
	}

	.bar-forget {
		display: flex;
		padding-top: 30upx;
		justify-content: space-between;
		color: #8ee6ff;
	}

	.m-input-view {}

	.m-input-view .m-input {
		height: 80upx;
		line-height: 1;
		color: #fff;
		font-size: 40upx;
		border-bottom: 1upx solid #62abf7;
	}
</style>
