# Seal-UniPlugin-UTS-Demo **Repository Path**: twofloor/Seal-UniPlugin-UTS-Demo ## Basic Information - **Project Name**: Seal-UniPlugin-UTS-Demo - **Description**: 跨平台Office文档、图片在线预览,音视频播放UTS插件seal-office-online-uts【非X5离线、组件嵌入、水印、WPS预览编辑】 - **Primary Language**: CSS - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 1 - **Created**: 2024-06-09 - **Last Updated**: 2026-04-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README [Github访问](https://github.com/silianpan/Seal-UniPlugin-UTS-Demo) [国内Gitee访问](https://gitee.com/twofloor/Seal-UniPlugin-UTS-Demo)
## 一、注意事项
* ##### 本插件不需要其他任何配置,也不需要依赖其他任何库,不要在manifest.json->App模块配置中勾选Android X5 Webview`
* ##### 不要同时使用其他同类文档预览插件,否则,可能会引起包冲突
* ##### url或参数中是否含有中文字符串,请使用encodeURIComponent**或**encodeURI进行转码(新版可以不用转码,支持中文),详细说明见下面“问题一”
## 二、问题解决
### 问题一(新版可以不用转码,支持中文):
> 插件安装成功,但是文档预览失败。
解决方案:检查文档url或参数中是否含有**中文字符串**,如果有,请使用**encodeURIComponent**或**encodeURI**进行转码。
如果参数有http://链接,使用**encodeURIComponent**;如果要对整个url进行转码,使用**encodeURI**
> encodeURIComponent和encodeURI区别
>
> 它们都是编码URL,唯一区别就是编码的字符范围,其中
>
> encodeURI方法***不会***对下列字符编码 **ASCII字母 数字 ~!@#$&\*()=:/,;?+'**
>
> encodeURIComponent方法***不会***对下列字符编码 **ASCII字母 数字 ~!\*()'**
>
> 所以encodeURIComponent比encodeURI编码的范围更大。
## 三、快速上手
Github克隆([demo工程地址](https://github.com/silianpan/Seal-UniPlugin-UTS-Demo) )或在右上角直接下载示例工程
安装开发工具:[HBuilderX](https://www.dcloud.io/hbuilderx.html)
### Step1. 下载demo工程,使用HBuilderX打开
### Step2. 添加本文插件
插件名称:[seal-office-online-uts](https://ext.dcloud.net.cn/plugin?id=12669)
点击右上角`试用`或者`购买`,添加插件
### Step3. 使用插件
* 在vue或nvue组件中,导入插件
```js
import * as UTSSealOfficeOnline from '../../uni_modules/seal-office-online-uts'
```
* **initEngine**方法:插件首次初始化(在应用启动时进行调用)
* **openFile**方法(推荐):支持Android和IOS,预览Office文件,支持如下格式:pdf、txt、doc、docx、xls、xlsx、ppt、pptx、epub等近50种类型文件,同时支持常见的音视频格式。
* **openFileImage**方法(推荐):支持Android(IOS端采用openFile方法),预览图片(jpg、png、jpeg、bmp等)。
* **openFileVideo**方法(推荐):播放音视频(mp3、flac、wma、wav、mp4、mkv等)。
* **openFileWPS**方法(推荐):采用本机WPS客户端预览或编辑文档,支持pdf、txt、doc、xls、ppt等多种文件格式。
* **checkWps**方法:检查本机WPS客户端是否已经安装。
**接口使用方法,参考如下章节《四、使用方法》**
### Step5. 调试
* 制作自定义调试基座:在开发工具中点击“运行到手机或模拟器-》制作自定义调试基座”
* 选择自定义调试基座:然后“运行到手机或模拟器-》基座运行选择-》自定义调试基座”
* 连接真机,运行到手机或模拟器-》运行到Android App基座,进行运行调试
## 四、使用方法
### 1、插件首次初始化(在应用启动时进行调用)
使用接口:initEngine
```javascript
uni.showLoading({
title: '插件首次初始化中'
});
UTSSealOfficeOnline.initEngine((code, msg) => {
if (code === 1) {
this.initPluginFirstSuccess = true;
uni.showToast({
title: '插件首次初始化成功',
duration: 2000
});
} else {
this.initPluginFirstSuccess = false;
}
uni.hideLoading();
});
```
### 2、打开Office文档
使用接口:openFIle,参数参考章节《五、openFile接口参数说明》。
支持wps、doc、docx、xls、xlsx、csv、ppt、pptx、txt、properties、log、Log、ini、lua、conf、m、cpp、java、h、xml、html、 htm等常见文件格式。
```javascript
/**
* 打开文档,非腾讯TBS,无内核加载,真正离线
* @param {Object} fileUrl 文档url
*/
openFile(fileUrl, otherOptions) {
UTSSealOfficeOnline.openFile({
waterMarkText: '水印水印换行',
url: fileUrl,
...otherOptions
}, (code, msg) => {
console.log('openFile', code, msg);
});
}
```
### 3、WPS预览或编辑
使用接口:openFileWPS,参数参考章节《六、openFileWPS接口参数说明》。
```javascript
/**
* WPS预览或编辑文档
* @param {String} fileUrl 文档url
* @param {String} openMode 打开模式
* openMode取值:
* Normal:正常模式,正常打开,WPS默认打开方式
* ReadOnly:只读模式,以只读的方式打开,WPS会隐藏编辑按钮
* EditMode:编辑模式,可对文档进行编辑
* ReadMode:阅读器模式,支持左右翻页,仅Word、TXT文档支持
* SaveOnly:另存模式(打开文件,另存,关闭),仅Word、TXT文档支持
*/
openFileWPS(fileUrl, openMode) {
UTSSealOfficeOnline.openFileWPS({
url: fileUrl,
openMode
},
(code, msg) => {
console.log('openFileWPS', code, msg);
});
}
```
### 4、检查本机是否安装WPS客户端
##### 4.1、方法一(推荐)、使用接口:checkWps,无参数。
> 支持平台:Android、IOS
注意:IOS配置应用访问白名单:**manifest.json**—》App常用其他设置—》IOS设置—》应用访问白名单,
添加:**KingsoftOfficeApp,KingsoftOfficeAppEnterprise,WPSOfficeApi**
```javascript
/**
* 检查WPS客户端是否已经安装
* 返回结果:true or false
*/
checkWps() {
const checkWps = UTSSealOfficeOnline.checkWps();
console.log('checkWps', checkWps);
},
```
##### 4.2、方法二、直接调用*plus.runtime.isApplicationExist*
参考链接:https://www.cnblogs.com/goloving/p/14384105.html
```js
// 判断第三方程序(WPS) 是否安装
function checkApp() {
if (plus.runtime.isApplicationExist({pname:'cn.wps.moffice_eng', action:'KingsoftOfficeApp://'})) {
console.log("WPS应用已安装");
} else {
console.log("WPS应用未安装");
}
}
```
### 5、图片预览
使用接口:openFile(IOS)、openFileImage(Android),参数参考章节《五、openFile接口参数说明》
> 支持平台:Android、IOS
>
> 支持jpg、png、jpeg、bmp等
```javascript
/**
* 图片预览,jpg、png、jpeg、bmp等
* @param {Object} fileUrl 图片url
* @param {Object} imageCurrentIndex 当前图片位置下标,从0开始
*/
openFileImage(fileUrl, imageCurrentIndex) {
if (this.platform === 'android') {
UTSSealOfficeOnline.openFileImage({
imageUrls: JSON.parse(JSON.stringify(this.imageList)),
imageCurrentIndex, // 当前点击图片在imageUrls中的下标,从0开始,默认为0
imageIndexType: 'number', // 图片底部指示器类型,默认为'dot',可选:'number':数字;'dot':点
isSaveImg: true,
},
(code, msg) => {
console.log('openFileImage', code, msg);
});
} else if (this.platform === 'ios') {
UTSSealOfficeOnline.openFile({
url: fileUrl,
title: 'IOS图片预览',
topBarBgColor: '#3394EC'
});
}
}
```
### 6、音视频播放
使用接口:openFile,参数参考章节《五、openFile接口参数说明》
> 支持平台:Android、IOS
>
> 支持mp4, flv, avi, 3gp, webm, ts, ogv, m3u8, asf, wmv, rm, rmvb, mov, mkv等18种视频格式,以及mp3,wav,wma,flac等音频格式
```javascript
/**
* 视频播放,支持市面上几乎所有的音视频格式,包括mp4, flv, avi, 3gp, webm, ts, ogv, m3u8, asf, wmv, rm, rmvb, mov, mkv等18种视频格式,以及mp3,wav,wma,flac等音频格式
* 功能包括:播放、暂停、重播、全屏播放、直播等
* 支持Android和IOS
* @param {String} fileUrl 音视频url
*/
openFileVideo(fileUrl) {
if (this.platform === 'android') {
UTSSealOfficeOnline.openFileVideo({
videoUrl: fileUrl,
isLive: true,
title: '音视频播放标题',
isTopBar: true,
isBackArrow: false,
topBarBgColor: '#F77234',
topBarTextColor: '#FCF26B',
topBarTextLength: 12
},
(code, msg) => {
console.log('openFileVideo', code, msg);
});
} else if (this.platform === 'ios') {
UTSSealOfficeOnline.openFile({
videoUrl: fileUrl
});
}
}
```
## 五、openFile接口参数说明
支持打开在线文档,本地文档
> 支持平台:Android、IOS
| 参数名 | 说明 | 类型 | 是否必填 | 默认值 | 可选值 |
| ------------------ | ------------------------------------------------------------ | ------------- | -------- | ----------------- | ----------------------- |
| url | 支持如下三种地址方式:
* 预览docx
* 预览pptx
* 预览xlsx
* 预览pdf
* 预览txt
* 预览mp4
* 预览jpg
### 2、IOS
* 嵌入预览
* 预览docx
* 预览pptx
* 预览xlsx
* 预览pdf
* 预览txt
* 预览mp4
* 预览jpg
转载请注明:[我的技术分享](http://silianpan.cn) » [跨平台Office文档预览原生插件,非腾讯X5,支持离线,稳定高可用](http://silianpan.cn/archives/filepreviewuninative)