<template>
	<view>
		<web-view v-if="src" :src="src"  @message="onMessage"></web-view>
	</view>
</template>

<script>
import { parse, urlParam } from '@/utils/param';

export default {
	data: function() {
		return {
			src: ''
		}
	},
	onLoad: function(option) {
		let query = {};
		try {
			query = JSON.parse(option.query || {}) || {};
		} catch (e) {
			query = {};
		}
		const url = `${option.url}${urlParam(query)}`;
		this.src = option.encode ? encodeURI(url) : url;
	},
	methods: {
		onMessage(e) {
			const { type, data, message } = e.detail.data[0] || {};
			if (type === 'back') {
				uni.navigateBack();
			} else if (type === 'select') {
				uni.$emit('webview-select', data);
				// uni.navigateBack();
			} else if (type === 'toast') {
				uni.showToast({ title: message, icon: 'none' });
			}
		}
	}
}
</script>
