<template>
	<view class="web-box">
		<navbar ref="navbar" :config="config" backColor="#1A1A1A"></navbar>
		<!-- :fullscreen="false" -->
		<web-view id="web-view" :src="path" @message="onMessage" @error="onError()" />
	</view>
</template>

<script>
	let app = getApp();
	export default {
		data() {
			return {
				showWeb: false,
				path: '',
				title: '',
				navKey: 1,
				// path: 'https://www.baidu.com/',
				webHeight: 0,
				webviewStyles: {
					height: '180px',
					backgroundColor: 'red'
				},
				config: {
					back: true,
					web: true,
					closePath: '',
					title: '',
					color: 'black',
					backgroundColor: [1, '#fff'],
					statusBarFontColor: 'black'
				},
			}
		},
		onReady() {

			// #ifdef APP
			const currentWebview = this.$scope.$getAppWebview()
			// this.pageTitle = uni.getAppBaseInfo().appName
			setTimeout(() =>{
				const {
					statusBarHeight,
					screenHeight,
					navHeight
				} = app.globalData;
				const top = statusBarHeight + navHeight
				const wv = currentWebview.children()[0]
				wv.setStyle({
					top: top,
					bottom: 0
				})
				wv.addEventListener('titleUpdate', e => {
					if (e.title) {
						this.$set(this.config, 'title', e.title || this.title || '');
						// this.navKey = Math.random()
						this.$refs.navbar.init()
					}
				})
			}, 10); //如果是页面初始化调用时，需要延时一下
			// #endif
		},
		onLoad(opt) {
			this.path = opt.path
			this.title = opt.title
			 
		},
		onShow() {

		},

		methods: {
			onError() {
				// console.log('页面加载失败')
				uni.redirectTo({
					url: '/pages/web-view/web-error'
				});
			},
			onMessage(e) {
				if (e.detail.data) {
					let {
						action
					} = e.detail.data[0];
					switch (action) {
						case "auth":
							this.getCheck(e.detail.data)
							break;
					}
				}
			},
			getCheck(data) {
				this.$yghttp.post('/auth/before/check', data[0]).then(res => {
					// console.log(res, 33333333)
					if (res.code == 200) {
						if (res.data.needConfirmAuth) {
							uni.setStorageSync('authInfo', data[0]);
							uni.setStorageSync('checkInfo', res.data);
							uni.navigateTo({
								url: '/pages/login/service-auth'
							})
						} else {
							uni.redirectTo({
								url: `/pages/web-view/Apps?path=${encodeURIComponent(res.data.redirect)}`
							});
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>