当前位置: 首页 > article >正文

uniapp展示本地swf格式文件,实现交互

概览

uniapp打包的Android项目实现本地swf格式文件的展示,并且能够进行交互

需求分析

1、因为是打包的Android项目展示本地的swf文件,首先需要拿到这个本地的swf文件路径

2、如何在uniapp的vue页面中展示swf,因为没有直接展示swf文件的标签/组件,考虑使用web-view组件来进行展示swf文件

3、直接使用web-view组件无法展示swf,使用html先展示swf,然后把html链接放到web-view中进行展示

4、html中如何展示swf,把swf转为html来进行展示这个swf文件(使用开源的js实现,下载地址:https://download.csdn.net/download/ahualong1/89900202)

具体实现

1、flashPage.vue页面

<template>
	<view :style="{height: screenHeight + 'px'}">
		<!-- <titleViewVue :titleName="titleName" class="VideoTitleViewClass"></titleViewVue> -->
		<web-view :src="url" @message="handlePostMessage"></web-view>
	</view>
</template>

<script>
	import titleViewVue from '../../components/titleView/titleView.vue'
	import {uploadStudyTime} from '@/api/api.js'
	export default {
		components: {
			titleViewVue
		},
		data() {
			return {
				titleName: '',//标题
				SubjectId: '',
				videoSrc: '',//视频资源本地地址
				videoHeight: '',
				videoWidth: '100%',
				screenHeight: '',
				
				startTime: 0,
				fileUrl: '',
				url: ''
			}
		},
		onLoad(option) {
			this.startTime = Date.now()
			this.screenHeight = getApp().globalData.screenHeight;
			// 定义视频URL
			this.titleName = option.titleName?option.titleName:''
			this.SubjectId = option.SubjectId + ''
			this.fileUrl = option.url
			this.url = '/static/flash.html?filePath=' + plus.io.convertLocalFileSystemURL(this.fileUrl) + '&titleName=' + this.titleName
		},
		onUnload() {
			if(!getApp().globalData.isGuest){
				this.uploadSduyTimePage()
			} 
		},
		methods: {
			uploadSduyTimePage(){
				var upLoadTime = {
					"subject_id": this.SubjectId,
					"date": this.$utils.formatDateTime(),
					"time": Date.now() / 1000 - this.startTime / 1000
				}
				this.startTime = 0
				const arryUpload = []
				arryUpload[0] = upLoadTime
				uploadStudyTime({"data": arryUpload}).then(res=>{
					if(res.data.code == 200 && res.data.status == 'success'){
						
					}else{
						if(uni.getStorageSync('uploadTime')){
							arryUpload = []
							arryUpload = uni.getStorageSync('uploadTime')
							arryUpload.push(upLoadTime)
							uni.setStorageSync('uploadTime',arryUpload)
						}else{
							arryUpload = []
							arryUpload.push(upLoadTime)
							uni.setStorageSync('uploadTime',arryUpload)
						}
					}
				})
			},
			handlePostMessage(){
				uni.navigateBack()
			}
		}
	}
</script>

<style>

</style>

码中fileUrl 为uni.saveFile()保存到本地的路径,直接打开是无法展示的,需要使用plus的api:plus.io.convertLocalFileSystemURL(this.fileUrl) 将本地文件系统的URL转换为跨域可以访问的URL

2、flash.html的具体代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
	<head>
		<title>SWFObject 2 dynamic publishing example page</title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<script type="text/javascript" src="swf2js.js"></script>

	</head>
	<body>
		<div style="width: 100%;height: auto;display: flex;align-items: center;justify-content: space-between;flex-direction: row;padding: 8px;background-color: #87B5F9;">
			<img src="leftarrow.png" style="height: 20px;" id="backClickId" />
			<div style="flex: 1;display: flex;align-items: center;justify-content: center;">
				<div id="titleNameId" style="color: #FFFFFF;font-size: 20px;"></div>
			</div>
		</div>
	</body>
	<!-- 微信 JS-SDK 如果不需要兼容小程序,则无需引用此 JS 文件。 -->
	<!-- <script type="text/javascript" src="//res.wx.qq.com/open/js/jweixin-1.6.0.js"></script> -->
	<!-- uni 的 SDK,必须引用。 -->
	<script type="text/javascript" src="../hybrid/html/web/uni.webview.js">

	</script>
	<script type="text/javascript">
		document.getElementById('backClickId').addEventListener('click', function() {
			uni.postMessage({
				data: {
					"back": true
				}
			});
		});
		window.onload = function() {
			if(location.href.includes('filePath')){
				swf2js.load(getParameterByName('filePath', ''))
			}
			if (location.href.includes('titleName')) {
				document.getElementById('titleNameId').textContent = getParameterByName('titleName', '');
			} else {
				document.getElementById('titleNameId').textContent = '模拟训练';
			}
		};

		function getParameterByName(name, url) {
			if (!url) url = location.href;
			name = name.replace(/[\[\]]/g, '\\$&');
			var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
				results = regex.exec(url);
			if (!results) return null;
			if (!results[2]) return '';
			return decodeURIComponent(results[2].replace(/\+/g, ' '));
		}
	</script>
</html>

其中uni.postMessage 是web-view向uniapp.vue传递消息

document.getElementById('backClickId').addEventListener('click', function() {
			uni.postMessage({
				data: {
					"back": true
				}
			});
		});

swf2js.load(getParameterByName('filePath', ''))为加载本地swf格式的资源代码

需要引入<script type="text/javascript" src="swf2js.js"></script>

方法getParameterByName 是获取打开的链接获取参数的方法

运行项目到模拟器或真机进行展示swf,就OK了。

可以查看项目中 pages-> flashPage->flashPage.vue页面

项目地址:https://download.csdn.net/download/ahualong1/89900184

参考开源项目:GitCode - 全球开发者的开源社区,开源代码托管平台


http://www.kler.cn/news/360244.html

相关文章:

  • WPF开发之页面切换
  • 【氮化镓】低温对p-GaN HEMT迁移率、阈值电压和亚阈值摆幅的影响
  • 云安全 | AWS S3存储桶安全设计缺陷分析
  • 人工智能 | 阿里通义千问大模型
  • 面试篇!!!!!
  • 数据结构编程实践20讲(Python版)—20并查集
  • 超全!一文详解大模型的11种微调方法
  • 【数据结构】预备练习——文件读写
  • Java集合剖析4】LinkedList
  • 大模型生图安全疫苗注入——进阶解决方案与系统优化(DataWhale组队学习)
  • 【Flutter】状态管理:Provider状态管理
  • Nest.js 实战 (十五):前后端分离项目部署的最佳实践
  • 工业大模型:体系架构、关键技术与典型应用
  • 大数据之——Hadoop的HDFS、YARN、MapReduce
  • LLM应用实战: OpenAI多代理新作-Swarm
  • postgresql执行计划解读案例
  • 03_深入理解Linux:系统组成、内核版本及文件系统详解
  • jiehun_DEMO
  • without OpenSSL
  • 【ArcGIS微课1000例】0125:ArcGIS矢量化无法自动完成面解决方案