<!--认证页面-->
<template>
	<view id="ssoCertification">
		登录中请稍后
	</view>
</template>

<script>
	import {
		config
	} from '@/api/request/config.js'
	import {
		authGetAppletUser,
		configInfo,
		getConfigByType,
		systemAppletRolePermission,
		securityDataStatisticsGetUserIdentity
	} from '@/pages/api/index.js'
	import {
		Encrypt,
		Decrypt
	} from '@/utils/secret.js'
	export default {
		name: "ssoCertification",
		data() {
			return {
				
			}
		},
		onLoad(option) {
			if(option.token){
				uni.setStorageSync('token', option.token);
				//获取登录人信息接口
				this.authGetAppletUser();
			}else{
				uni.showToast({
					mask: true,
					icon: "none",
					position: "center",
					title: '数据异常,请稍候再试!',
					duration: 2000
				});
				setTimeout(function() {
					uni.redirectTo({
						url: '/pages/views/login/login',
					});
				}, 2000);
				
			}
		},
		mounted() {

		},
		methods: {
			async authGetAppletUser(){
				const {
					data
				} = await authGetAppletUser();
				if (data.code == 200) {
					uni.setStorageSync('dataBoardType', false);
					uni.setStorageSync('userId', data.data.userId);
					uni.setStorageSync('isInitPwd', true);
					// userType 0-系统 1-教职工 2-学生 3-大屏
					uni.setStorageSync('userType', data.data.userType == 0 || data.data.userType == 1 ? '1' :
						(data.data.userType == 2 ? '2' : (data.data.userType == 3 ? '3' : 'none')));
					//等待配置与字段获取到后跳转
					Promise.all([
						//查询公共配置
						this.getConfigInfo(),
						//获取开发配置
						this.getConfigByType(),
						//获取权限字段
						this.systemAppletRolePermission()
						//查询身份
						// this.securityDataStatisticsGetUserIdentity()
					]).then((result) => {
						if (uni.getStorageSync('codeData')) {
							uni.redirectTo({
								url: '/pages/views/saoCode/saoCode',
							});
						} else if (uni.getStorageSync('warningId')) {
							uni.redirectTo({
								url: '/pages_basics/views/earlyWarningManage/earlyWarningDetail',
							});
						} else {
							uni.redirectTo({
								url: '/pages/views/home/home',
							});
						}
					}).catch((error) => {
						uni.showToast({
							mask: true,
							icon: "none",
							position: "center",
							title: '数据异常,请稍候再试!',
							duration: 2000
						});
						setTimeout(function() {
							uni.redirectTo({
								url: '/pages/views/login/login',
							});
						}, 2000);
					})
				}else{
					uni.redirectTo({
						url: '/pages/views/home/home',
					});
				}
			},
			
			//查询公共配置
			async getConfigInfo() {
				const {
					data
				} = await configInfo({
					type: '1,2,4'
				});
				if (data.code == 200) {
					let list = JSON.parse(data.data)
					let newData = {};
					list.forEach((item) => {
						let obj = JSON.parse(item.configValue)
						newData = {
							...newData,
							...obj
						}
					})
					uni.setStorageSync('circularLogo', config.base_url + newData.circularLogo)
					uni.setStorageSync('rectangleLogo', config.base_url + newData.rectangleLogo)
					uni.setStorageSync('videoCover', config.base_url + newData.videoCover)
					this.$set(this, 'loginBanner', config.base_url + newData.loginBanner);
					uni.setStorageSync('loginBanner', config.base_url + newData.loginBanner)
					this.$set(this, 'supplierType', newData.supplier);
					uni.setStorageSync('supplierType', newData.supplier)
					uni.setStorageSync('homepageBanner', config.base_url + newData.homepageBanner)
				}
			},
			//获取开发配置
			async getConfigByType() {
				const {
					data
				} = await getConfigByType({
					category: 2,
					configType: 5
				});
				if (data.code == 200) {
					let obj = JSON.parse(data.data.configValue)
					//文件预览地址
					uni.setStorageSync('filePreviewUrl', 'https://' + obj.fileExtranetUrl)
					//小程序视频地址
					uni.setStorageSync('cameraExtranetAgent', 'https://' + obj.cameraExtranetAgent)
					//MQTT地址
					uni.setStorageSync('mqttUrl', Decrypt(obj.mqttExtranetUrl))
					//MQTT地址-内网
					uni.setStorageSync('mqttIntranetUrl', Decrypt(obj.mqttIntranetUrl))
					//MQTT账号
					uni.setStorageSync('mqttUser', Decrypt(obj.mqttExtranetUser))
					//MQTT密码
					uni.setStorageSync('mqttPassword', Decrypt(obj.mqttExtranetPassword))
					//文件浏览环境
					uni.setStorageSync('fileBrowseEnvironment','http://'+Decrypt(obj.fileBrowseEnvironment))
					uni.setStorageSync('fileBrowseEnvironmentExtranet','https://'+Decrypt(obj.fileBrowseEnvironmentExtranet))
				}
			},
			//获取权限字段
			async systemAppletRolePermission() {
				let self = this;
				const {
					data
				} = await systemAppletRolePermission();
				if (data.code == 200) {
					uni.setStorageSync('permissions', data.data.data)
					uni.setStorageSync('controlsRestrict', data.data.roleKeys ? data.data.roleKeys : [])
					uni.setStorageSync('user', data.data.userInfo)
				}
			},
			//查询身份
			async securityDataStatisticsGetUserIdentity(routeUrl) {
				let self = this;
				const {
					data
				} = await securityDataStatisticsGetUserIdentity();
				if(data.schoolAdmin){
					//校级管理员
					uni.setStorageSync('identityData',{
						type:'schoolAdmin'
					});
				}else if(data.collegeAdmin){
					//院级管理员
					uni.setStorageSync('identityData',{
						type:'collegeAdmin'
					});
				}else if(data.schoolGroup){
					//校级督导组
					uni.setStorageSync('identityData',{
						type:'schoolGroup',
						groupIds:data.groupIds
					});
				}else if(data.collegeGroup){
					//院级督导组
					uni.setStorageSync('identityData',{
						type:'collegeGroup',
						groupIds:data.groupIds
					});
				}else{
					uni.removeStorageSync('identityData');
				}
			},
		},
	}
</script>

<style lang="stylus" scoped>
	#ssoCertification {
		width:100%;
		height:100%;
		overflow:scroll;
		text-align: center;
		line-height:200rpx;
	}
</style>