<template>
	<view class="wrap">
		<image :src="logoImg" mode="" style="width: 200rpx;height: 200rpx;"></image>
		<view class="mainText">海航技术移动生产门户平台</view>
		<span>HNAT Aircraft Maintenance and Aviation Material</span>
		<text class="subText">Management System</text>
		<image :src="loadingImg" mode="" style="width: 100rpx;height: 100rpx;"></image>
		<text class="rateNum subText">{{ rate }}</text>
	</view>
</template>

<script>
	import apiMapper from '../../apiMapper/index';
	import {
		request,
		loginRequest,
		ipConfig
	} from '../../utils/request';
	import {
		deepCloneNew
	} from '../../common/index.js';
	
	export default {
		data() {
			return {
				loadingImg: '../../static/img/login/loading.gif',
				logoImg: '../../static/img/login/logo.png',
				rate: 0,
				ticket: ''
			};
		},
		onLoad(option) {
			console.log(option)
			this.ticket = typeof(option.ticket) == 'undefined' ? '' : option.ticket;
			this.casLogin()
		},
		onReady() {},
		mounted() {},
		methods: {
			casLogin() {
				let vm = this;
				if (this.ticket) {
					// 调用单点登录的接口
					// console.log(data);
					this.rate = '50%'
					loginRequest({
							method: 'GET',
							url: 'cas-login',
							data: {
								ticket: this.ticket,
								callbackUrl: encodeURIComponent(location.origin + apiMapper['iam-h5'])
							}
						})
						.then(res => {
							uni.setStorageSync('loginType', 'iam');
							uni.setStorageSync('access_token', res.access_token);
							uni.setStorageSync('refresh_token', res.refresh_token);
							uni.setStorageSync('license', res.license);
							uni.setStorageSync('username', res.username);
							uni.setStorageSync('userid', res.user_id);
							uni.setStorageSync('loginDate', new Date().getTime());
							// 获取内部账号信息
							this.getAppInfo();
							// 获取授权打印机列表
							this.getAuthPrinter();
							// 获取人员属性
							this.getUserAttributesFun(res.username);
							this.getUserInfo();
							this.rate = '90%'

							setTimeout(() => {
								vm.rate = '100%';
								// 登录成功后隐藏登录界面
								vm.showIamHref = false;
								// app无window对象
								if (typeof(window) != 'undefined') {
									uni.redirectTo({
										url: uni.getStorageSync('preUrl')
									});
									// uni.removeStorageSync('preUrl')
								} else {
									uni.redirectTo({
										url: '/pages/home/homeIndex'
									});
								};
							}, 300);
						})
						.catch(err => {
							uni.showToast({
								title: err.data.msg,
								icon: 'none'
							});
							console.log(err);
						});
				} else {
					uni.redirectTo({
						url: '/pages/login/index'
					})
				}
			},
			// 获取内部账号信息
			getAppInfo() {
				request({
						url: 'appInfo',
						method: 'get'
					})
					.then(res => {
						if (res.code == 0) {
							uni.setStorageSync('userAppId', res.data.id);
						}
					})
					.catch(err => {});
			},
			getUserInfo() {
				request({
						url: 'getUserInfo',
						method: 'get'
					})
					.then(res => {
						if (res.code == 0) {
							let permissions = res.data.permissions || [];
							const list = {};
							for (let i = 0; i < permissions.length; i++) {
								list[permissions[i]] = true;
							}
							uni.setStorageSync('permissions', list);
							uni.setStorageSync('phone', res.data.sysUser.phone);
						}
					})
					.catch(err => {});
			},
			// 获取人员属性
			getUserAttributesFun(username) {
				let vm = this;
				request({
						method: 'GET',
						url: 'getUserAttributes',
						data: {
							userName: username
						}
					})
					.then(res => {
						if (res.code == 0) {
							if (!!res.data && res.data.length > 0) {
								uni.setStorageSync('deptName', res.data[0].orgName);
								// 设置工厂
								uni.setStorageSync('factoryList', res.data[0].factoryList);
								if (!!res.data[0].defaultFactory) {
									this.setDefaultFactory(res.data[0].defaultFactory);
								} else {
									uni.setStorageSync('userFactory', '');
									uni.setStorageSync('userFactoryId', '');
								}
								// 设置仓库
								if (!!res.data[0].warehouseList) {
									this.setWarehouseList(res.data[0].warehouseList, res.data[0].defaultWarehouse);
								}
								// 设置航站
								uni.setStorageSync('terminalList', res.data[0].terminalList);
								if (!!res.data[0].defaultTerminal) {
									uni.setStorageSync('userTerminal', res.data[0].defaultTerminal.tag);
									uni.setStorageSync('userTerminalValue', res.data[0].defaultTerminal.value);
								} else {
									uni.setStorageSync('userTerminal', '');
									uni.setStorageSync('userTerminalValue', '');
								}
								// 设置库存地
								uni.setStorageSync('stockLocationList', res.data[0].stockLocationList);
								if (!!res.data[0].defaultStockLocation) {
									uni.setStorageSync('userStock', res.data[0].defaultStockLocation.tag);
									uni.setStorageSync('userStockValue', res.data[0].defaultStockLocation.value);
								} else {
									uni.setStorageSync('userStock', '');
									uni.setStorageSync('userStockValue', '');
								}
							}
						}
					})
					.catch(res => {
						console.log(res);
					});
			},
			// 获取打印机
			getAuthPrinter() {
				request({
						url: 'auth_printer',
						method: 'get'
					})
					.then(res => {
						if (res.code == 0) {
							uni.setStorageSync('userPrinterList', res.data);
							let lists = deepCloneNew(res.data);
							let blnDefaultPrinter = false;
							lists.forEach((item, index) => {
								if (item.defaultPrinter == true) {
									uni.setStorageSync('userDefaultPrinter', item);
									blnDefaultPrinter = true;
								}
							});
							if (!blnDefaultPrinter) {
								uni.setStorageSync('userDefaultPrinter', '');
							}
						}
					})
					.catch(err => {
						console.log(err);
					});
			},
			// 设置默认工厂
			setDefaultFactory(defaultFactory) {
				let vm = this;
				request({
						method: 'GET',
						url: 'getFactoryDict'
					})
					.then(res => {
						if (res.code == 0) {
							let ele = res.data.find(item => item.id == defaultFactory);
							if (!!ele) {
								uni.setStorageSync('userFactory', ele.label);
								uni.setStorageSync('userFactoryId', defaultFactory);
							}
						}
					})
					.catch(res => {
						console.log(res);
					});
			},
			// 设置默认仓库
			setWarehouseList(datas, defaultWarehouse) {
				let vm = this;
				request({
						method: 'GET',
						url: 'getWarehouseDict'
					})
					.then(res => {
						if (res.code == 0) {
							let warehouseList = [];
							datas.forEach(element => {
								let temp = res.data.find(item => item.id == element);
								warehouseList.push(temp);
							});
							uni.setStorageSync('warehouseList', warehouseList);
							if (!!defaultWarehouse) {
								let ele = res.data.find(item => item.id == defaultWarehouse);
								if (!!ele) {
									uni.setStorageSync('userWarehouse', ele.label);
									uni.setStorageSync('userWarehouseId', defaultWarehouse);
								}
							} else {
								uni.setStorageSync('userWarehouse', '');
								uni.setStorageSync('userWarehouseId', '');
							}
						}
					})
					.catch(res => {
						console.log(res);
					});
			}
		}
	};
</script>

<style lang="less">
	.wrap {
		width: 100%;
		height: 100%;
		box-sizing: border-box;
		padding-top: 11.5rem;
		text-align: center;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background: #ffffff;

		.mainText {
			vertical-align: middle;
			display: inline-block;
			font-size: 32rpx;
			font-weight: bold;
			margin-top: 40rpx;
			color: #166ce5;
		}

		.subText {
			display: inline-block;
			width: 100%;
			text-align: center;
			margin-bottom: 5px;
			font-size: 12px;
			color: #999999;
		}

		.rateNum {
			font-size: 14px;
			margin-top: 10px;
		}
	}
</style>
