uniapp 外接 html

<template>
	<view class="page">
		<web-view class="page" ref="shrimp" :src="url" @message="onMessage" @onPostMessage="onPostMessage"></web-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// url: "http://10.210.212.75:8080/",
				url: "https://ifarm-shrimp-dev.cpcti.com",
				username: '',
				id: 0,
				height: 20
			}
		},
		onLoad() {
			console.log('load');
			try {
				const value = uni.getStorageSync('token')
				const name = uni.getStorageSync('userName')
				const id = uni.getStorageSync('userCode')
				this.username = name
				this.id = id
				if (value) this.token = value
				const barHeight = uni.getSystemInfoSync().statusBarHeight;
				this.height = barHeight
			} catch (e) {}
		},
		mounted() {
			this.url = this.url + "?platform=shrimp&token=" + this.token + "&username=" + this.username + "&uid=" + this
				.id + '&safeArea=' + this.height;
			console.log(this.url);
		},
		methods: {
			// nvue使用
			onPostMessage(res) {
				// 接收信息
				console.log('postres---------', JSON.stringify(res));

				// const  s = "window.JsBridge.shrimp='122112'"
				// this.$refs.shrimp.evalJs(s)
				const {
					data
				} = res.detail;
				const params = data[0];
				console.log(params);
				if (params && typeof params === 'object') {
					const {
						type,
						callbackId
					} = params;
					this.swicthFu(type)
					// if (type === "EXITWEBPAGE") {
					// 	uni.navigateBack({
					// 		delta: 1
					// 	})
					// }
					const res = {
						callbackId,
						data: {
							a: 11
						}
					}
					// 返回
					const str = `JsBridge.native_call_h5(${JSON.stringify(res)})`
					console.log('sstt------', str)
					this.$refs.shrimp.evalJs(str)
				}
			},
			// 判断并响应
			swicthFu(type) {
				switch (type) {
					case "EXITWEBPAGE":
						uni.navigateBack({
							delta: 1
						});
						break;
					case 1:
						break;
					case 2:
						break;
					case 3:
						break;
					case 4:
						break;
					default:
						break;
				}
			},
			// vue使用
			onMessage(res) {
				console.log('res---------', JSON.stringify(res));
				// var currentWebview = this.$scope.$getAppWebview()
				// const wv = currentWebview.children();
				// console.log('wv-----',JSON.stringify(wv))
				const {
					data
				} = res.detail;
				const params = data[0];
				if (params && typeof params === 'object') {
					console.log('pppppp==-==', params)
					const {
						type,
						callbackId
					} = params;
					const res = {
						callbackId,
						data: {
							a: 11
						}
					}
					const str = `window.JsBridge.native_call_h5(${JSON.stringify(res)})`
					console.log('sstt------', str)

					const evalJs = this.$refs.webview.evalJs;
					const data = {};
					const owm = this.$refs.webview.ownerDocument;
					console.log('own-----', owm)
					Object.keys(this.$refs.webview).forEach((key) => {
						const val = this.$refs.webview[key];
						if (typeof val === 'string' || typeof val === 'number') {
							data[key] = val
						} else {
							data[key] = ''
						}

					})
					console.log('web------', JSON.stringify(data))
					evalJs(str)
				}
			}
		}

	}
</script>

<style>
	.page {
		flex: 1;
		flex-direction: column;
		background-color: #fff;
	}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值