uniapp开发的app跳转到html页面,在html页面跳转回来app页面(web-view,h5)

需求:使用html开发的app想要使用微信授权登录,在最开始是想直接在h5页面调用wx.login,但是掉不起来微信客户端,将链接地址在微信客服端可以打开授权,找了一些资料,没有找到合适的解决方案,在加上后期可能使用微信支付,所以果断放弃,考虑其他方案(使用原生安卓开发Android Studio、使用uniapp开发),因为没写过原生安卓开发,使用就放在uniapp上。

 思路/解决代码:使用uniapp打包app开发,在pages/index/index页面使用web-view标签,连接到html页面,

uniapp
<web-view update-title="false" src="http://192.168.1.24:8001"></web-view>

这样就能顺利跳转到html页面在html页面点击按钮跳转回uniapp页面,下面文档有提到怎么跳回

uniapp文档:web-view | uni-app官网

当然这边也是直接将代码放在下面了这边跳转到地址是/pages/tabBar/component/component

目前是在app嵌套跳转到html,在html是在跳转回app

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport"
			content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>网络网页</title>
		<style type="text/css">
			.btn {
				display: block;
				margin: 20px auto;
				padding: 5px;
				background-color: #007aff;
				border: 0;
				color: #ffffff;
				height: 40px;
				width: 200px;
			}

			.btn-red {
				background-color: #dd524d;
			}

			.btn-yellow {
				background-color: #f0ad4e;
			}

			.desc {
				padding: 10px;
				color: #999999;
			}

			.post-message-section {
				visibility: hidden;
			}
		</style>
	</head>
	<body>
		<p class="desc">web-view 组件加载网络 html 示例。点击下列按钮,跳转至其它页面。</p>
		<div class="btn-list">
			<button class="btn" type="button" data-action="navigateTo">navigateTo</button>
			<button class="btn" type="button" data-action="redirectTo">redirectTo</button>
			<button class="btn" type="button" data-action="navigateBack">navigateBack</button>
			<button class="btn" type="button" data-action="reLaunch">reLaunch</button>
			<button class="btn" type="button" data-action="switchTab">switchTab</button>
		</div>
		<div class="post-message-section">
			<p class="desc">网页向应用发送消息,注意:小程序端应用会在此页面后退时接收到消息。</p>
			<div class="btn-list">
				<button class="btn btn-red" type="button" id="postMessage">postMessage</button>
			</div>
		</div>
		<script type="text/javascript">
			var userAgent = navigator.userAgent;
			if (userAgent.indexOf('AlipayClient') > -1) {
				// 支付宝小程序的 JS-SDK 防止 404 需要动态加载,如果不需要兼容支付宝小程序,则无需引用此 JS 文件。
				document.writeln('<script src="https://appx/web-view.min.js"' + '>' + '<' + '/' + 'script>');
			} else if (/QQ/i.test(userAgent) && /miniProgram/i.test(userAgent)) {
				// QQ 小程序
				document.write(
					'<script type="text/javascript" src="https://qqq.gtimg.cn/miniprogram/webview_jssdk/qqjssdk-1.0.0.js"><\/script>'
					);
			} else if (/miniProgram/i.test(userAgent) && /micromessenger/i.test(userAgent)) {
				// 微信小程序 JS-SDK 如果不需要兼容微信小程序,则无需引用此 JS 文件。
				document.write('<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"><\/script>');
			} else if (/toutiaomicroapp/i.test(userAgent)) {
				// 头条小程序 JS-SDK 如果不需要兼容头条小程序,则无需引用此 JS 文件。
				document.write(
					'<script type="text/javascript" src="https://s3.pstatp.com/toutiao/tmajssdk/jssdk-1.0.1.js"><\/script>');
			} else if (/swan/i.test(userAgent)) {
				// 百度小程序 JS-SDK 如果不需要兼容百度小程序,则无需引用此 JS 文件。
				document.write(
					'<script type="text/javascript" src="https://b.bdstatic.com/searchbox/icms/searchbox/js/swan-2.0.18.js"><\/script>'
					);
			} else if (/quickapp/i.test(userAgent)) {
				// quickapp
				document.write('<script type="text/javascript" src="https://quickapp/jssdk.webview.min.js"><\/script>');
			}
			if (!/toutiaomicroapp/i.test(userAgent)) {
				document.querySelector('.post-message-section').style.visibility = 'visible';
			}
		</script>
		<!-- uni 的 SDK -->
		<script type="text/javascript" src="https://unpkg.com/@dcloudio/uni-webview-js@0.0.1/index.js"></script>
		<script type="text/javascript">
			document.addEventListener('UniAppJSBridgeReady', function() {
				document.querySelector('.btn-list').addEventListener('click', function(evt) {
					var target = evt.target;
					if (target.tagName === 'BUTTON') {
						var action = target.getAttribute('data-action');
						switch (action) {
							case 'switchTab':
								uni.switchTab({
									url: '/pages/tabBar/component/component'
								});
								break;
							case 'reLaunch':
								uni.reLaunch({
									url: '/pages/tabBar/component/component'
								});
								break;
							case 'navigateBack':
								uni.navigateBack({
									delta: 1
								});
								break;
							default:
								uni[action]({
									url: '/pages/tabBar/component/component'
								});
								break;
						}
					}
				});
				document.getElementById('postMessage').addEventListener('click', function() {
					uni.postMessage({
						data: {
							action: 'message'
						}
					});
				});
			});
		</script>
	</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值