Vue H5项目,怎么引入uni.webview sdk,调用uni postMessage实现手机扫描功能

前言

目前公司Vue H5项目,用webview打包成APP,现产品提出这样打包出来的app运行较慢,需要用uniapp方式(即使用HBuilder编辑器来打包H5)来打包,那需要的基座就不是安卓的基座而是uniapp的基座,而H5项目实现手机扫描功能就需要调用uniapp的基座的方法。

具体步骤

一、Uniapp Webview 源码

html 复制代码
<template>
	<view>
		<web-view :webview-styles="webviewStyles" :src="src" @message="showMessage"></web-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				webviewStyles: {
					progress: {
						color: '#FF3333'
					}
				},
				src:'http://******/', // H5项目地址
				qrCodeWv: null
			}
		},
		onReady() {
			// #ifdef APP-PLUS
			let currentWebview = this.$scope.$getAppWebview()
			setTimeout(() => {
				this.wv = currentWebview.children()[0]
				this.qrCodeWv = currentWebview.children()[0]
				this.wv.setStyle({scalable:true})
			},1000)
			// #endif
		},
		methods: {
			showMessage(event) {
				if(event.detail.data && event.detail.data.length >0){
					let dataInfo = event.detail.data[0]
					console.log(dataInfo)
					let type = dataInfo.type
					if(type==='scan') {
						this.startScanCode()
					}
				}
			},
			startScanCode() {
				const self = this 
				uni.scanCode({
					onlyFromCamera: false,
					scanType: ['qrCode'],
					success: function(res) {
						setTimeout(() => {
							const result = res.result.replace(/'/g,'"')
							self.qrCodeWv.evalJS(`appScanCodeResult('${result}')`)
						})
					},
					complete: function(args){
						console.log(args)
					}
				})
			}
		}
	}
</script>

二、H5 Vue项目引入js

1、在public新建js文件夹uni.webview.1.5.4.js文件,其源码地址

2、index.html 引入 public/js 下文件

html 复制代码
<script src="<%= BASE_URL %>js/uni.webview.1.5.4.js"></script>

3、main.js 定义回调方法和对象

js 复制代码
window.appScanCodeResult = function (val) {
    window.appScanCodeResultString = val
    window.dispatchEvent(new CustomEvent("scanCodeResult"))
}

4、Vue扫码页面代码

js 复制代码
created() {
   window.addEventListener("scanCodeResult", this.handleAppScanCode, false) 
    },
onBeforeDestroy() {
   window.removeEventListener("scanCodeResult", this.handleAppScanCode)
   },
methods: {
   handleAppScanCode() {
     const result = window.appScanCodeResultString
     console.log('扫码返回值---result',result)
   },
   // 点击扫码按钮
   saoCode() {
    uni.postMessage({
         data: {
             type: "scan"
          }
       })
   }
}

相关文章

基于ElementUi再次封装基础组件文档


基于ant-design-vue再次封装基础组件文档


vue3+ts基于Element-plus再次封装基础组件文档

相关推荐
JIngJaneIL30 分钟前
基于java+ vue助农电商系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
q_191328469535 分钟前
基于Springboot+MySQL+RuoYi的会议室预约管理系统
java·vue.js·spring boot·后端·mysql·若依·计算机毕业设计
想学后端的前端工程师40 分钟前
【Java集合框架深度解析:从入门到精通-后端技术栈】
前端·javascript·vue.js
小鑫同学1 小时前
vue-pdf-interactor 技术白皮书:为现代 Web 应用注入交互式 PDF 能力
前端·vue.js·github
布茹 ei ai2 小时前
城市天气查询系统 (City Weather Dashboard)
javascript·vue.js·html·css3·开源软件·天气预报
全栈老石2 小时前
从硬编码到 Schema 推断:前端表单开发的工程化转型
前端·vue.js·架构
VX:Fegn08952 小时前
计算机毕业设计|基于springboot + vue乡村振兴服务系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
期待のcode4 小时前
验证码实现
java·vue.js
韭菜炒大葱4 小时前
现代前端开发工程化:Vue3 + Vite 带你从 0 到 1 搭建 Vue3 项目🚀
前端·vue.js·vite
老华带你飞4 小时前
志愿者服务管理|基于springboot 志愿者服务管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·spring