uniapp中App端使用web-office-sdk

WebOffice 即在线文档预览编辑服务,可为接入方提供专业的文档处理能力。支持文字、表格、演示和 PDF 四大主流文档类型。支持多人协作编辑,内容实时同步。跳转至官方文档

一、sdk引入

<script charset="utf-8" src="js/web-office-sdk-solution-v2.0.4.umd.js"></script>

二、html页面实现

由于在app端没有window浏览器对象,所以得需要使用webview组件内嵌html来实现。
目录结果如下:
web-office.html文件目录
web-office.html代码:

<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>文档在线预览</title>
</head>
<body>
	<div></div>
</body>
<script charset="utf-8" src="js/web-office-sdk-solution-v2.0.4.umd.js"></script>
<script type="text/javascript">
	window.onload = function() {
		let query = getQuery('data')
		if(!query) {
			alert('参数不存在')
			return
		}
		query = JSON.parse(query)
		let wps = WebOfficeSDK.init({
			appId: '此处填写 WebOfficeSDK appId',
			officeType: query.type,
			fileId: query.file_id,
			token: query.token,
			customArgs:{
				handel_type: query.handel_type
			}
		})
		wps.ApiEvent.AddApiEventListener("fileOpen", (data) => {
			if (!data.success) {
				alert('文件打开失败');
			}
		});
		
		function getQuery(name) {
			let reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
			let r = window.location.search.substr(1).match(reg);
			if(r != null) {
				// 对参数值进行解码
				return decodeURIComponent(r[2]);
			}
			return null;
		}
	}
</script>
</html>

三、unipp vue页面

<web-view :src="src"></web-view>
async wpsInit(){
	const token = await tokenInvalidReturnInfo(3)
	let data = {
		file_id: this.params.fileId,
		type: this.params.type,
		handel_type: this.params.handelType,
		token: token
	}
	this.src = '/hybrid/html/web-office.html?data='+encodeURIComponent(JSON.stringify(data))
}

打开任意文档,将文档相关参数传入上面的vue页面即可

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值