ry_app/package_a/monitor/monitor.nvue

184 lines
4.3 KiB
Plaintext
Raw Normal View History

2026-04-05 23:25:01 +08:00
<template>
<view class="monitor-container">
<text class="header">实时监控</text>
<!-- 这里使用我们封装好的原生插件组件 ry-ipc-video -->
<view class="video-wrapper">
<!-- #ifdef APP-PLUS -->
<ry-ipc-video
ref="ipcVideo"
class="ipc-video"
:uid="deviceUid"
@onStatus="onVideoStatusChange">
</ry-ipc-video>
<!-- #endif -->
<!-- 非App端提示 -->
<!-- #ifndef APP-PLUS -->
<view class="not-support-tips">
<text class="not-support-tips-text">当前环境不支持查看监控请使用App</text>
</view>
<!-- #endif -->
</view>
<view class="status-text-wrap" v-if="statusMsg">
<text class="status-text">状态:{{ statusMsg }}</text>
</view>
<view class="controls">
<button class="btn btn-primary" @click="startPlay"><text class="btn-text">播放</text></button>
<button class="btn btn-warn" @click="stopPlay"><text class="btn-text">停止</text></button>
</view>
</view>
</template>
<script>
export default {
data() {
return {
deviceUid: 'HLTY020667DHKCD', // 默认使用你 Demo 里的测试 UID
isPlaying: false,
statusMsg: '等待操作'
}
},
onLoad(options) {
if (options && options.uid) {
this.deviceUid = options.uid;
}
},
onUnload() {
// 页面卸载时停止播放,释放资源
this.stopPlay();
},
methods: {
startPlay() {
// #ifdef APP-PLUS
if (this.$refs.ipcVideo) {
this.statusMsg = '正在发起播放请求...';
console.log('组件对象:', this.$refs.ipcVideo);
// 在 nvue 中调用组件的方法,有时可能需要通过 evalJS但绝大多数情况可以直接调
if (typeof this.$refs.ipcVideo.start === 'function') {
this.$refs.ipcVideo.start();
} else {
console.error('start 方法不存在,可能插件注册失败或版本未生效');
uni.showToast({ title: '插件方法未找到', icon: 'none' });
}
} else {
uni.showToast({ title: '插件未加载成功', icon: 'none' });
}
// #endif
// #ifndef APP-PLUS
uni.showToast({ title: '仅App端支持', icon: 'none' });
// #endif
},
stopPlay() {
// #ifdef APP-PLUS
if (this.$refs.ipcVideo) {
if (typeof this.$refs.ipcVideo.stop === 'function') {
this.$refs.ipcVideo.stop();
}
this.statusMsg = '已发送停止指令';
}
// #endif
},
onVideoStatusChange(e) {
console.log("视频状态改变:", e.detail);
// 根据我们在 Java 封装里写的 fireEventStatus 传回的 msg 显示
if (e.detail) {
// 提取参数
const status = e.detail.status;
const msg = e.detail.msg;
const msgParam = e.detail.msgParam;
// 更新页面显示的文字状态
if (msg) {
this.statusMsg = msg;
}
// 处理特定的连接错误状态
if (status === 'error' || msgParam === 7 || msgParam === 14) {
uni.showToast({
title: '摄像机离线,请检查设备网络',
icon: 'none',
duration: 3000
});
// 可以在这里做一些重置操作
this.isPlaying = false;
} else if (status === 'playing') {
this.isPlaying = true;
}
}
}
}
}
</script>
<style>
/* nvue 样式必须是纯 CSS/flex不支持 scss 嵌套和某些普通 css 属性 */
.monitor-container {
flex: 1;
flex-direction: column;
background-color: #f5f5f5;
}
.header {
font-size: 32rpx;
font-weight: bold;
text-align: center;
padding: 20rpx;
background-color: #ffffff;
}
.video-wrapper {
width: 750rpx;
height: 450rpx;
background-color: #000000;
flex-direction: column;
justify-content: center;
align-items: center;
}
.ipc-video {
width: 750rpx;
height: 450rpx;
}
.not-support-tips {
align-items: center;
justify-content: center;
}
.not-support-tips-text {
color: #ffffff;
font-size: 28rpx;
}
.status-text-wrap {
align-items: center;
margin-top: 20rpx;
}
.status-text {
text-align: center;
font-size: 26rpx;
color: #666666;
}
.controls {
flex-direction: row;
justify-content: space-around;
padding-top: 40rpx;
padding-bottom: 40rpx;
}
.btn {
width: 300rpx;
height: 80rpx;
border-radius: 10rpx;
align-items: center;
justify-content: center;
}
.btn-primary {
background-color: #007aff;
}
.btn-warn {
background-color: #e64340;
}
.btn-text {
color: #ffffff;
font-size: 30rpx;
}
</style>