<template>
	<view class="container">
		<web-view :src="pageUrl" v-if="pageUrl" update-title allow="fullscreen" @message="handleMessage"></web-view>
		<image src="@/static/images/course_expiration_img.png" mode="aspectFill"></image>
		<view>{{msg}}</view>
	</view>
</template>

<script>
	import {getRealLink,updateSopAppLink} from "@/api/courseAnswer.js"
	export default {
		data() {
			return {
				pageUrl:"",
				msg: '加载中...',
				userInfo: {}
			}
		},
		onLoad(option) {
			this.userInfo = uni.getStorageSync("userInfo") ? JSON.parse(uni.getStorageSync("userInfo")) : {};
			let link = option.link
			if (link) {
				this.getLink(link)
				this.getCourseRead(link)
			} else {
				this.msg = "链接地址空的"
				uni.showToast({
					title: "链接地址空的",
					icon: 'error'
				})
			}
		},
		onUnload() {
			// #ifdef APP-PLUS
			plus.navigator.setFullscreen(false);
			plus.screen.lockOrientation('portrait-primary');
			// #endif
		},
		onBackPress() {
			// #ifdef APP-PLUS
			// 返回时退出全屏
			plus.navigator.setFullscreen(false);
			plus.screen.lockOrientation('portrait-primary');
			// #endif
		},
		methods: {
			getCourseRead(id) {
				updateSopAppLink(id).then(res=>{
					if(res.code == 200) {
						this.$updateMsgDot()
					}
				})
			},
			handleMessage(event) {
				const info = event.detail.data[0]
				// #ifdef APP-PLUS
				if(info.login != 1) {
					// 退出登录
					let IMUserID = uni.getStorageSync('IMUserID');
					//Igexin.unbindAlias(IMUserID);
					uni.removeStorage({key: "IMUserID"});
					uni.setStorageSync("AppToken",null);
										
					uni.removeStorage({ key: 'IMToken' });
					uni.removeStorageSync("onLaunch",null)
					uni.removeStorageSync("imUnread",null);
					uni.removeStorageSync("companyUser");
					uni.removeStorageSync("CompanyUserToken");
					
					uni.$emit("refreshUserInfo");
					let pages = getCurrentPages();
					
					let url = pages[ pages.length - 1]; //当前页页面实例
					//如果登录界面已打开,自动关闭
					if(url!=undefined&&url.route=="pages/auth/loginIndex"){
						return;
					}
					uni.navigateTo({
						url: '/pages/auth/loginIndex'
					});
					return
				}
				if (info&&info.isFullscreen) {
					plus.screen.lockOrientation('landscape-primary');
				} else {
					plus.screen.lockOrientation('portrait-primary');
				}
				if(info&&info.productId) {
					uni.navigateTo({
						url: '/pages/user/integral/integralGoodsDetails?goodsId='+info.productId
					})
				}
				console.log("event===info===",info)
				if(info&&info.pagesUrl) {
					uni.navigateTo({
						url: info.pagesUrl
					})
				}
				// #endif
			},
			getLink(sortLink) {
				this.msg = '加载中...'
				const userId = this.userInfo.userId || ''
				getRealLink({sortLink: sortLink}).then(res => {
					if (res.code == 200) {
						// 如果响应中包含真实链接，则跳转到真实链接
					    let pageUrl= res.realLink + "&userId="+userId+"&source=app&time=" + new Date().getTime();
						this.pageUrl = pageUrl;
					} else {
						this.msg = '课程已过期或链接无效'
						uni.showModal({
							title: '提示',
							content: '课程已过期或链接无效',
							showCancel: false,
							success: function(res) {
								if (res.confirm) {
									console.log('用户点击确定');
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					}
				}).catch(err => {
					this.msg = '发生错误，请稍后再试'
					uni.showToast({
						title: '发生错误，请稍后再试',
						icon: 'none'
					});
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.container {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-bottom: 88rpx;
		justify-content: center;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 32rpx;
		color: #757575;
		line-height: 48rpx;
		text-align: center;

		image {
			width: 428rpx;
			height: 360rpx;
			margin-bottom: 30rpx;
		}
	}
</style>