<template>
	<view>
		<web-view :webview-styles="webviewStyles" :src="url" v-if="iOS"></web-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				url: "",
				webviewStyles: {
					progress: {
						color: '#F07423'
					}
				},
				type: "",
				iOS: true
			};
		},
		onLoad(param) {
			this.type = param.type;
			let title = ""
			if (this.type == 1) {
				title = "用户使用协议"
			} else if (this.type == 2) {
				title = "隐私协议"
			} else if (this.type == 3) {
				title = "入驻协议"
			}
			uni.setNavigationBarTitle({
				title: title
			})
			this.webviewStyles.progress.color = getApp().globalData.color1;
			this.queryProtocolConfigView();
		},
		methods: {
			async queryProtocolConfigView() {
				let res = await this.$myRequest({
					url: "/project/queryProtocolConfigView",
					data: {},
				})
				if (res.data.success) {
					if (this.type = '1') { //使用协议
						this.url = res.data.single.userUseProtocol;
					} else if (this.type = '2') { //隐私协议
						this.url = res.data.single.userPrivacyProtocol;
					} else if (this.type = '3') { //业主注册协议
						this.url = res.data.single.ownerRegistProtocol;
					}
					let index = this.url.lastIndexOf('.');
					let ah = this.url.substring(index+1);
					if(ah.indexOf('pdf')>-1){ //是pdf
						 this.loadpdf();
					}
                   
				}
			},
			loadpdf() {
				uni.getSystemInfo({
					success: (res) => {
						console.log(res)
						if (res.system.includes('iOS')) {
							this.iOS = true;
						} else {
							this.iOS = false;
							// Android 需要下载后再打开
							uni.downloadFile({
								url: this.url,
								success: (res) => {
									const path = res.tempFilePath;
									uni.openDocument({
										filePath: path,
										fileType: 'pdf',
										success: (res) => {
											uni.navigateBack({
												delta: 1
											});
										},
										fail: (err) => {
											uni.showToast({
												title: '打开文件失败',
												icon: 'none',
												duration: 2000
											});
										}
									});
								},
								fail: (err) => {
									console.log(err);
									uni.showToast({
										title: '下载文件失败',
										icon: 'none',
										duration: 2000
									});
								}
							});
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss">

</style>
