<script>
	// 引入检查更新
	//import checkupdate from "@/uni_modules/uni-upgrade-center-app/utils/check-update.js"
	//引进检查版本
	// import callCheckVersion from '@/uni_modules/uni-upgrade-center-app/utils/call-check-version';
	//#ifdef APP-PLUS
    // const jpushModule = uni.requireNativePlugin('JG-JPush');
	//#endif
	export default {
		async onLaunch() {
			
			//#ifdef H5
			
			var token= uni.getStorageSync("token");
			if(token){
				uni.redirectTo({
					url: '/pages/index/index'
				});
				return false;
			}
			
			const queryString = window.location.search;
			const searchParams = new URLSearchParams(queryString);
			var ticket = searchParams.get('ticket');
			console.log("ticket",ticket);
			if(!ticket){
				const query = window.location.hash.split('?')[1]; // 获取hash后面的查询字符串
				const params = new URLSearchParams(query); // 创建一个URLSearchParams对象
				ticket=params.get('ticket');
			}

      if (!ticket){
        this.closeWindow()
      }
			// alert(ticket)
			if(ticket){
				uni.showLoading({
					title: '登录跳转中...'
				});
				
				// const {data: res} = await uni.$http.post('/loginBySSO?ticket='+ticket);
				// if(res.success){
				// 	if(res.data){
				// 		console.log(res.data)
				// 		uni.setStorageSync('expires', res.data.expires);
				// 		uni.setStorageSync('token', res.data.token);
				// 		uni.setStorageSync('user', JSON.stringify(res.data.user));
				// 		uni.setStorageSync('menu', JSON.stringify(res.data.btns)); 
				// 		this.getdept();
				// 		// 跳转 首页
				// 		uni.switchTab({
				// 			url: '/pages/index/index'
				// 		});
				// 	}
				// 	else{
				// 		uni.showModal({
				// 			title: '提示',
				// 			content: '获取登录token失败，请重新登录后进入！',
				// 			cancelText: '取消',
				// 			confirmText: '确定',
				// 			success: ress => {
				// 			}
				// 		});
				// 	}
				// }
				// else{
				// 	uni.showModal({
				// 		title: '提示',
				// 		content: '获取登录token失败，请重新登录后进入！',
				// 		cancelText: '取消',
				// 		confirmText: '确定',
				// 		success: ress => {
				// 		}
				// 	});
				// }
				uni.request({
					header: {
					  "Content-Type": "application/json",
					  "Accept": "application/json"
					},
					url: 'https://pallet.dsic.cn/api/api/loginBySSO?ticket='+ticket,
					data:{
						"ticket":ticket
					},
					method: "POST",
					success: res => {
            uni.hideLoading()
						if(res.data){
							var data=res.data;
							if(data.data){
								var datas=data.data;
								uni.setStorageSync('expires', datas.expires);
								uni.setStorageSync('token', datas.token);
								uni.setStorageSync('user', JSON.stringify(datas.user));
								uni.setStorageSync('menu', JSON.stringify(datas.user.btns)); 
								this.getdept();
								console.log('1');
								// 跳转 首页
								uni.redirectTo({
									url: '/pages/index/index'
								});
							}else{
								uni.showModal({
									title: '提示',
									content: '登录失败，请重新登录后进入！',
									cancelText: '取消',
									confirmText: '确定',
									success: ress => {
                    this.closeWindow()
									}
								});
							}
						}else{
							uni.showModal({
								title: '提示',
								content: '登录失败，请重新登录后进入！',
								cancelText: '取消',
								confirmText: '确定',
								success: ress => {
                  this.closeWindow()
								}
							});
						}
					},
					fail: err => {
						console.log("err",err);
						uni.showModal({
							title: '提示',
							content: '登录请求失败，请重新登录后进入！',
							cancelText: '取消',
							confirmText: '确定',
							success: ress => {
                this.closeWindow()
							}
						});
					}
				});
				 
			}
			//#endif
			
			// 判断版本更新 需要真机进行测试
			//checkupdate();
			
			//#ifdef APP-PLUS
			
			// // 消息推送
			// jpushModule.setLoggerEnable(true);
			// jpushModule.initJPushService()
			// jpushModule.addConnectEventListener(result=>{
			// 	let connectEnable = result.connectEnable
			// 	console.log("jpush连接", connectEnable)
			// });
			
			// jpushModule.addTagAliasListener(result => {
			// 	let code = result.code
			// 	let sequence = result.sequence
			// 	let tags = result.tags
			// 	let tag = result.tag
			// 	let tagEnable = result.tagEnable
			// 	let alias = result.alias
			// 	console.log(alias, '别名')
			// });
			
			// // 通知事件回调
			// jpushModule.addNotificationListener(result => {
			// 	let notificationEventType = result.notificationEventType
			// 	let messageID = result.messageID
			// 	let title = result.title
			// 	let content = result.content
			// 	let extras = result.extras
			// 	console.log("通知", result)
			// 	// 点击事件
			// 	if (notificationEventType == 'notificationOpened') {
			// 		uni.reLaunch({
			// 			url: `/pages/message/newMsg`
			// 		});
			// 	}
			// })
			// jpushModule.getRegistrationID(result => {
			// 	console.log("注册ID", result.registerID)
			// 	if (result.registerID) {
			// 		uni.setStorageSync("register_id", result.registerID)
			// 	}
			// })
			// jpushModule.addCustomMessageListener(result => {
			// 	let messageID = result.messageID
			// 	let content = result.content
			// 	let extras = result.extras
			// 	console.log("自定义消息", result)
			// })
			
			//#endif
		},
		methods: { 
			async getdept(){
				var user= JSON.parse(uni.getStorageSync("user"));
				var bmbm=user.bmbm?user.bmbm:""; 
				const {data: res} = await uni.$http.get('/getCommonData');
				if(res.success){
					if(res.data){
						// 缓存部门
						uni.setStorageSync('commondata', JSON.stringify(res.data));
						if(res.data.depts){
							res.data.depts.forEach((item, ii) => {
								if(item.departCode==bmbm){
									uni.setStorageSync('bmmc', item.departName);
								}
							});
						}
					}
				}
			},
			closeWindow(){
        if (window.plus) { // 确保在App环境中运行
          const main = plus.android.runtimeMainActivity(); // Android方式调用退出
          main.finish(); // Android方式调用退出
          // 或者使用iOS方式的调用方式...
        } else {
          console.log('Not in App environment.'); // 非App环境提示信息
          setTimeout(function() {
            uni.redirectTo({
              url: '/pages/login/index'
            });
          }, 2000);
        }
      }
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "uview-ui/index.scss";
	@import "common/demo.scss";
	@import 'colorui/main.css';
	@import 'colorui/icon.css';
	body{
		font-family: 'FZLTZHUNHJW--GB1-0';
	}
	.selright {
	  height: 100%;
	  background: url("@/static/images/xiayiji.png") no-repeat center right;
	  background-size: auto 80%;
	  padding-right: 50rpx !important;
	  text-align: right !important;
	  display: flex;
	  flex: 1;
	  align-items: flex-end;
	  justify-content: flex-end;
	}
	.rightc {
	  height: 100%;
	  padding-right: 10rpx !important;
	  text-align: right !important;
	  display: flex;
	  flex: 1;
	  align-items: flex-end;
	  justify-content: flex-end;
	}
	.wrapnob{
		margin: 0;
		padding: 0;
		width: 100%;
		position: relative;
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
	}
	.wrap{
		margin: 0;
		padding: 0;
		width: 100%;
		position: relative;
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top) - var(--window-bottom));
		/* #endif */
		overflow-y: auto;
	}
	.page {
		position: relative;
		width: 100%;
		padding: 0;
		margin-top: 0;
		overflow: hidden;
		.back{
			width: 100%;
			height: calc(100vh - var(--window-top)) !important;
			position: absolute;
			top:0;
			left:0;
			background: url('@/static/images/pagebackimg.jpg') no-repeat top center;
			background-size: 100% 100%;
		}
	}
	.page-box{
		padding-bottom:20rpx;
	}
	.imgload{
		background-image: url('/static/images/tx.png');
		background-size: 66% auto;
		background-position: center center; 
		background-repeat: no-repeat;
	}
	::v-deep  .uni-system-preview-image{
		z-index: 9999999;
	}
</style>