分享设备相关的功能

This commit is contained in:
fengerli
2025-07-22 17:47:38 +08:00
parent 1663ae75e4
commit 864f7f58a0
19 changed files with 1360 additions and 307 deletions

View File

@ -1,214 +1,221 @@
<template>
<view>
<!-- 添加全局加载遮罩 -->
<view v-if="pageLoading" class="page-loading-mask">
</view>
<!-- 使用自定义导航栏 -->
<custom-navbar :title="navTitle" :showBack="true" color="#FFFFFF" rightIcon="/static/images/shape.png"
@right-click="shareUp"></custom-navbar>
<view class="device-detail-container" :style="{ paddingTop: navBarHeight + 'px' }">
<!-- 设备电量信息 -->
<view class="battery-section">
<view class="battery-sectionLeft">
<image :src="itemInfo.devicePic" class="bipImg"></image>
</view>
<view>
<view class="battery-v1">
<image src="/static/images/dl.png" class="dlIMG"></image>
<view>
<view class="battery-v2">90%</view>
<view class="battery-v3">电量</view>
</view>
<view v-show="!pageLoading">
<custom-navbar :title="navTitle" :showBack="true" color="#FFFFFF"
:rightIcon="isRightIconVisible ? '/static/images/shape.png' : ''"
@right-click="shareUp"></custom-navbar>
<view class="device-detail-container" :style="{ paddingTop: navBarHeight + 'px' }">
<!-- 设备电量信息 -->
<view class="battery-section">
<view class="battery-sectionLeft">
<image :src="deviceInfo.devicePic" class="bipImg"></image>
</view>
<view class="battery-v1">
<image src="/static/images/nz.png" class="dlIMG"></image>
<view>
<view class="battery-v2">1小时</view>
<view class="battery-v3">续航时间</view>
</view>
</view>
</view>
<view>
</view>
</view>
<!-- 设备基本信息 -->
<view class="info-card">
<view class="info-row">
<text class="info-label">IMEI号</text>
<text class="info-value">123456</text>
</view>
<view class="info-row">
<text class="info-label">设备状态</text>
<text class="info-value status-running">运行中</text>
</view>
<view class="info-row">
<text class="info-label">定位信息</text>
<view class="info-value status-running">
<view class="info-value status-running">114.72 30.28</view>
<view class="info-value status-running">深圳市龙华区富源晟</view>
</view>
</view>
</view>
<view class="info-row">
<text class="info-label">灯光亮度</text>
<text class="info-value status-running">{{ sliderValue }}%</text>
</view>
<!-- 灯光亮度控制 -->
<view class="control-card">
<slider :value="sliderValue" min="0" max="100" activeColor="rgb(187, 230, 0)"
backgroundColor="rgb(26, 26, 26)" :show-value="false" @changing="onSliderChanging"
@change="onSliderChanging" />
</view>
<!-- 灯光模式选择 -->
<view class="mode-section">
<view class="mode-buttons">
<view class="mode-v1">
<view class="mode-v2" @click="selectMode('main')">
<image src="/static/images/set.png" class="setIMG"></image>
<view>
<view class="battery-v1">
<image src="/static/images/dl.png" class="dlIMG"></image>
<view>
<view class="battery-v2">灯光模式</view>
<view class="mode-v3">{{ currentMainMode }}</view>
<view class="battery-v2">90%</view>
<view class="battery-v3">电量</view>
</view>
</view>
<view class="battery-v1">
<image src="/static/images/nz.png" class="dlIMG"></image>
<view>
<view class="battery-v2">1小时</view>
<view class="battery-v3">续航时间</view>
</view>
</view>
</view>
<view class="mode-v1">
<view class="mode-v2" @click="lasermode">
<image src="/static/images/jg.png" class="setIMG"></image>
<view>
<view class="battery-v2">激光模式</view>
<!--<view class="mode-v3">{{currentSecondaryMode}}</view> -->
<view>
</view>
</view>
<!-- 设备基本信息 -->
<view class="info-card">
<view class="info-row">
<text class="info-label">IMEI号</text>
<text class="info-value">{{deviceInfo.deviceImei}}</text>
</view>
<view class="info-row">
<text class="info-label">设备状态</text>
<text class="info-value status-running">运行中</text>
</view>
<view class="info-row">
<text class="info-label">定位信息</text>
<view class="info-value status-running">
<view class="info-value status-running">114.72 30.28</view>
<view class="info-value status-running">深圳市龙华区富源晟</view>
</view>
</view>
</view>
<view class="info-row">
<text class="info-label">灯光亮度</text>
<text class="info-value status-running">{{ sliderValue }}%</text>
</view>
<!-- 灯光亮度控制 -->
<view class="control-card">
<slider :value="sliderValue" min="0" max="100" activeColor="rgb(187, 230, 0)"
backgroundColor="rgb(26, 26, 26)" :show-value="false" @changing="onSliderChanging"
@change="onSliderChanging" />
</view>
<!-- 灯光模式选择 -->
<view class="mode-section">
<view class="mode-buttons">
<view class="mode-v1" v-if="hasPermission('1')">
<view class="mode-v2" @click="selectMode('main')">
<image src="/static/images/set.png" class="setIMG"></image>
<view>
<view class="battery-v2">灯光模式</view>
<view class="mode-v3">{{ currentMainMode }}</view>
</view>
</view>
</view>
</view>
<view class="mode-v1">
<view class="mode-v2" @click="uploadStartup">
<image src="/static/images/path7.png" class="setIMG"></image>
<view>
<view class="battery-v2">开机画面</view>
<view class="mode-v3">上传</view>
<view class="mode-v1" v-if="hasPermission('2')">
<view class="mode-v2" @click="lasermode">
<image src="/static/images/jg.png" class="setIMG"></image>
<view>
<view class="battery-v2">激光模式</view>
<!--<view class="mode-v3">{{currentSecondaryMode}}</view> -->
</view>
</view>
</view>
<view class="mode-v1" v-if="hasPermission('3')">
<view class="mode-v2" @click="uploadStartup">
<image src="/static/images/path7.png" class="setIMG"></image>
<view>
<view class="battery-v2">开机画面</view>
<view class="mode-v3">上传</view>
</view>
</view>
</view>
</view>
</view>
</view>
<!-- 人员信息登记 -->
<view class="form-section">
<view class="mode-buttons">
<view class="section-title">人员信息登记</view>
<!-- <button class="send-btn">发送</button> -->
<view class="right-icons">
<uni-icons @click="toggleForm" :type="isFormExpanded ? 'arrowup' : 'down'" size="20"
color="rgba(255, 255, 255, 0.87" class="toggle-icon" />
<!-- 人员信息登记 -->
<view class="form-section" v-if="hasPermission('4')">
<view class="mode-buttons">
<view class="section-title">人员信息登记</view>
<!-- <button class="send-btn">发送</button> -->
<view class="right-icons">
<uni-icons @click="toggleForm" :type="isFormExpanded ? 'arrowup' : 'down'" size="20"
color="rgba(255, 255, 255, 0.87" class="toggle-icon" />
</view>
</view>
</view>
<view class="form-content" v-if="isFormExpanded">
<button class="send-btn1" @click="sendPersonnelInfo">发送</button>
<view class="form-row">
<text class="form-label">单位</text>
<input class="form-input" placeholder="请输入单位" v-model="personnelInfo.unit" />
</view>
<view class="form-row">
<text class="form-label">姓名</text>
<input class="form-input" placeholder="请输入姓名" v-model="personnelInfo.name" />
</view>
<view class="form-row">
<text class="form-label">职位</text>
<input class="form-input" placeholder="请输入职位" v-model="personnelInfo.position" />
</view>
<view class="form-row">
<text class="form-label">ID</text>
<input class="form-input" placeholder="请输入ID号" v-model="personnelInfo.id" />
</view>
</view>
</view>
<!-- 人员信息登记 -->
<view class="form-section">
<view class="mode-buttons">
<view class="section-title">发送信息</view>
<button class="send-btn" @click="sendTextMessage">发送</button>
</view>
<view class="form-row">
<input class="form-input" placeholder="请输入文字" v-model="messageToSend" />
</view>
</view>
<!-- 产品信息 -->
<view class="section-title">产品信息</view>
<view class="mode-buttons">
<view class="mode_1" @click="productparams">
<image src="/static/images/cp.png" mode="" class="cpIMG"></image>
<view class="">产品参数</view>
</view>
<view class="mode_1" @click="operatingInst">
<image src="/static/images/sm.png" mode="" class="cpIMG"></image>
<view class="">操作说明</view>
</view>
<view class="mode_1" @click="operatingVideo">
<image src="/static/images/sp.png" mode="" class="cpIMG"></image>
<view class="">操作视频</view>
</view>
</view>
</view>
<!-- 弹框 -->
<view class="agreement-mask" v-if="lightModeA">
<!-- 协议弹窗 -->
<view class="agreement-popup" @click.stop>
<!-- 标题 -->
<view class="popup-title"> {{ popupTitle }}</view>
<view class="popup-content">
<view v-for="(item, index) in items" :key="index">
<view class="item" :class="{'selected': item.selected}" @click="onItemClick(index)">
<image :src="item.image" class="setIMG"></image>
{{item.text}}
<view class="form-content" v-if="isFormExpanded">
<button class="send-btn1" @click="sendPersonnelInfo">发送</button>
<view class="form-row">
<text class="form-label">单位</text>
<input class="form-input" placeholder="请输入单位" v-model="personnelInfo.unitName" />
</view>
<view class="form-row">
<text class="form-label">姓名</text>
<input class="form-input" placeholder="请输入姓名" v-model="personnelInfo.name" />
</view>
<view class="form-row">
<text class="form-label">职位</text>
<input class="form-input" placeholder="请输入职位" v-model="personnelInfo.position" />
</view>
<view class="form-row">
<text class="form-label">ID</text>
<input class="form-input" placeholder="请输入ID号" v-model="personnelInfo.code" />
</view>
</view>
</view>
<!-- 按钮组 -->
<view class="popup-buttons">
<button class="agree" @click="handleSumbit">确定</button>
</view>
</view>
</view>
<!-- 上传开机画面弹框 -->
<view class="agreement-mask" v-if="lightModeB">
<!-- 上传画面弹窗 -->
<view class="agreement-popupB">
<!-- 标题 -->
<view class="popup-title">上传开机画面</view>
<view class="popup-content">
<view class="example-body">
<uni-file-picker limit="1" class="custom-file-picker"></uni-file-picker>
<view class="example_title">点击上传图片</view>
<!-- 人员信息登记 -->
<view class="form-section" v-if="hasPermission('5')">
<view class="mode-buttons">
<view class="section-title">发送信息</view>
<button class="send-btn" @click="sendTextMessage">发送</button>
</view>
<view class="form-row">
<input class="form-input" placeholder="请输入文字" v-model="messageToSend" />
</view>
</view>
<!-- 按钮组 -->
<view class="popup-buttons">
<button class="agree" @click="handleupload">确定</button>
</view>
</view>
</view>
<!--===================== 激光提示框================== -->
<view class="agreement-mask" v-if="lightModeC">
<!-- 上传画面弹窗 -->
<view class="agreement-popupC">
<!-- 标题 -->
<view class="popup-title">确认开启激光模式</view>
<view class="popup-content">
<view class="popup-Title">
<view>注意事项</view>
<view>1.禁止直视光源或反射面</view>
<view>2.避免直射人或易燃物</view>
<view>3.需佩戴相应专业防护眼镜</view>
<!-- 产品信息 -->
<view v-if="hasPermission('6')">
<view class="section-title">产品信息</view>
<view class="mode-buttons">
<view class="mode_1" @click="productparams">
<image src="/static/images/cp.png" mode="" class="cpIMG"></image>
<view class="">产品参数</view>
</view>
<view class="mode_1" @click="operatingInst">
<image src="/static/images/sm.png" mode="" class="cpIMG"></image>
<view class="">操作说明</view>
</view>
<view class="mode_1" @click="operatingVideo">
<image src="/static/images/sp.png" mode="" class="cpIMG"></image>
<view class="">操作视频</view>
</view>
</view>
</view>
<!-- 按钮组 -->
<view class="popup-buttons">
<button class="btn disagree" @click="handleDisagree">取消</button>
<button class="btn agreeBtn" @click="handleBtn">确定</button>
</view>
<!-- 弹框 -->
<view class="agreement-mask" v-if="lightModeA">
<!-- 协议弹窗 -->
<view class="agreement-popup" @click.stop>
<!-- 标题 -->
<view class="popup-title"> {{ popupTitle }}</view>
<view class="popup-content">
<view v-for="(item, index) in items" :key="index">
<view class="item" :class="{'selected': item.selected}" @click="onItemClick(index)">
<image :src="item.image" class="setIMG"></image>
{{item.text}}
</view>
</view>
</view>
<!-- 按钮组 -->
<view class="popup-buttons">
<button class="agree" @click="handleSumbit">确定</button>
</view>
</view>
</view>
<!-- 上传开机画面弹框 -->
<view class="agreement-mask" v-if="lightModeB">
<!-- 上传画面弹窗 -->
<view class="agreement-popupB">
<!-- 标题 -->
<view class="popup-title">上传开机画面</view>
<view class="popup-content">
<view class="example-body">
<uni-file-picker limit="1" class="custom-file-picker"></uni-file-picker>
<view class="example_title">点击上传图片</view>
</view>
</view>
<!-- 按钮组 -->
<view class="popup-buttons">
<button class="agree" @click="handleupload">确定</button>
</view>
</view>
</view>
<!--===================== 激光提示框================== -->
<view class="agreement-mask" v-if="lightModeC">
<!-- 上传画面弹窗 -->
<view class="agreement-popupC">
<!-- 标题 -->
<view class="popup-title">确认开启激光模式</view>
<view class="popup-content">
<view class="popup-Title">
<view>注意事项</view>
<view>1.禁止直视光源或反射面</view>
<view>2.避免直射人或易燃物</view>
<view>3.需佩戴相应专业防护眼镜</view>
</view>
</view>
<!-- 按钮组 -->
<view class="popup-buttons">
<button class="btn disagree" @click="handleDisagree">取消</button>
<button class="btn agreeBtn" @click="handleBtn">确定</button>
</view>
</view>
</view>
</view>
@ -217,10 +224,20 @@
<script>
import MqttClient from '@/utils/mqtt.js';
import {
deviceDetail,
registerPersonInfo,
deviceSendMessage,
deviceShareId
} from '@/api/6170/deviceControl.js'
import {
getDeviceId
} from '../../../store/BLETools';
export default {
data() {
return {
pageLoading: true,
mainMode: 'string',
secondaryMode: 'string',
navBarHeight: 70 + uni.getSystemInfoSync().statusBarHeight,
@ -238,11 +255,22 @@
mqttClient: null,
messageToSend: '',
personnelInfo: {
unit: '',
unitName: '',
name: '',
position: '',
id: ''
}
code: '',
},
deviceInfo: {},
modeInstructions: {
'关灯': [1, 0, 0, 0, 0],
'强光': [1, 1, 0, 0, 0],
'弱光': [1, 2, 0, 0, 0],
'爆闪': [1, 3, 0, 0, 0],
'泛光': [1, 4, 0, 0, 0]
},
activePermissions: [], // 存储当前设备的权限数组
isSharedDevice: false, // 标记是否来自分享
isRightIconVisible: false
}
},
computed: {
@ -292,29 +320,32 @@
onItemClick(index) {
const selectedItem = this.items[index];
console.log(selectedItem.text, 'selectedItem.text');
if (selectedItem.text === '激光') {
this.lightModeC = true
this.selectedItemIndex = index; // 记录当前选择的索引
} else {
this.updateSelectedItem(index);
this.items = this.items.map((item, i) => ({
...item,
selected: i === index
}));
// 记录当前选中的 index但不发送
this.selectedItemIndex = index;
console.log(selectedItem.text, 'selectedItem.text');
}
},
updateSelectedItem(index) {
this.items = this.items.map((item, i) => ({
...item,
selected: i === index
}));
if (this.modeType === 'main') {
this.currentMainMode = this.items[index].text + '模式';
} else {
this.currentSecondaryMode = this.items[index].text + '模式';
}
this.showPopup = false; // 关闭弹窗
},
// 确认
// 灯光模式的确认
handleSumbit() {
this.lightModeA = false
if (this.selectedItemIndex === null) return;
const selectedItem = this.items[this.selectedItemIndex];
const instruction = this.modeInstructions[selectedItem.text];
if (instruction) {
const topic = `B/${this.itemInfo.deviceImei}`;
const message = JSON.stringify(instruction);
this.mqttClient.publish(topic, message);
console.log('已发送指令:', instruction);
}
this.lightModeA = false;
// this.lightModeA = false
},
// 上传开机画面
uploadStartup() {
@ -358,42 +389,87 @@
})
},
// 同意
// 激光确认框
handleBtn() {
this.lightModeC = false
this.updateSelectedItem(this.selectedItemIndex); // 使用记录的索引来更新选择项
this.lightModeA = false //关闭大弹框
},
// 不同意
//取消
handleDisagree() {
this.lightModeC = false
},
// 发送人员信息
sendPersonnelInfo() {
if (!this.mqttClient || !this.mqttClient.client.isConnected()) {
// if (!this.mqttClient || !this.mqttClient.client.isConnected()) {
// uni.showToast({
// title: 'MQTT未连接',
// icon: 'none'
// });
// return;
// }
// const topic = `device/command/${this.deviceID}/personnel`;
// const message = JSON.stringify(this.personnelInfo);
// this.mqttClient.publish(topic, message);
// uni.showToast({
// title: '人员信息已发送',
// icon: 'success'
// });
if (!this.personnelInfo.unitName) {
uni.showToast({
title: 'MQTT未连接',
title: '单位名称不能为空',
icon: 'none'
});
return;
}
const topic = `device/command/${this.deviceID}/personnel`;
const message = JSON.stringify(this.personnelInfo);
this.mqttClient.publish(topic, message);
uni.showToast({
title: '人员信息已发送',
icon: 'success'
});
if (!this.personnelInfo.name) {
uni.showToast({
title: '姓名不能为空',
icon: 'none'
});
return;
}
if (!this.personnelInfo.position) {
uni.showToast({
title: '职位不能为空',
icon: 'none'
});
return;
}
if (!this.personnelInfo.code) {
uni.showToast({
title: 'ID不能为空',
icon: 'none'
});
return;
}
uni.showLoading({
title: '发送中...',
mask: true
})
let data = {
code: this.personnelInfo.code,
name: this.personnelInfo.name,
position: this.personnelInfo.position,
unitName: this.personnelInfo.unitName,
deviceId: this.deviceID
}
registerPersonInfo(data).then((res) => {
if (res.code == 200) {
uni.hideLoading()
uni.showToast({
title: res.msg,
icon: 'none'
});
} else {
uni.showToast({
title: res.msg,
icon: 'none'
});
}
})
},
// 发送文本消息
sendTextMessage() {
if (!this.mqttClient || !this.mqttClient.client.isConnected()) {
uni.showToast({
title: 'MQTT未连接',
icon: 'none'
});
return;
}
if (!this.messageToSend) {
uni.showToast({
title: '请输入要发送的文字',
@ -401,29 +477,113 @@
});
return;
}
const topic = `device/command/${this.deviceID}/message`;
this.mqttClient.publish(topic, this.messageToSend);
uni.showToast({
title: '消息已发送',
icon: 'success'
});
uni.showLoading({
title: '发送中...',
mask: true
})
let data = {
sendMsg: this.messageToSend,
deviceIds: [this.deviceID]
}
deviceSendMessage(data).then((res) => {
if (res.code == 200) {
uni.hideLoading()
uni.showToast({
title: res.msg,
icon: 'none'
});
} else {
uni.showToast({
title: res.msg,
icon: 'none'
});
}
})
// const topic = `device/command/${this.deviceID}/message`;
// this.mqttClient.publish(topic, this.messageToSend);
// uni.showToast({
// title: '消息已发送',
// icon: 'success'
// });
},
// 统一处理返回方法
handleDeviceData(res, isFromShared = false) {
if (res.code == 200) {
// 最后关闭加载状态
this.pageLoading = false
this.deviceInfo = res.data
this.personnelInfo = {
unitName: res.data.personnelInfo?.unitName || '',
name: res.data.personnelInfo?.name || '',
code: res.data.personnelInfo?.code || '',
position: res.data.personnelInfo?.position || '',
}
// 将权限字符串转换为数组 ["1", "2"]
if (isFromShared) {
this.isSharedDevice = true
this.activePermissions = res.data.permission ?
res.data.permission.split(',') : []
} else {
this.isSharedDevice = false
this.activePermissions = [] // 非分享设备清空权限
}
this.messageToSend = res.data.sendMsg || ''
// 关闭加载中
uni.hideLoading()
}
},
// 检查权限的方法
hasPermission(permissionCode) {
// 如果还在加载中直接返回false
if (this.pageLoading) return false
// 如果不是分享设备,默认有全部权限
if (!this.isSharedDevice) return true
return this.activePermissions.includes(permissionCode)
},
// 获取设备详情(普通详情)
async fetchDeviceDetail(id) {
try {
const res = await deviceDetail(id)
this.handleDeviceData(res, false)
} catch (error) {
uni.showToast({
title: '获取详情失败',
icon: 'none'
})
}
},
// 获取分享设备详情
async fetchSharedDeviceDetail(id) {
try {
const res = await deviceShareId(id)
this.handleDeviceData(res, true)
} catch (error) {
uni.showToast({
title: '获取分享详情失败',
icon: 'none'
})
}
}
},
onLoad(options) {
const eventChannel = this.getOpenerEventChannel();
// 监听 'detailData' 事件,获取传过来的数据
uni.showLoading({
title: '加载中...'
})
eventChannel.on('deviceControl', (data) => {
this.itemInfo = data.data;
this.deviceID = data.data.id;
this.navTitle = data.data.deviceName;
console.log('Received detail data:', this.navTitle);
this.apiType = data.apiType
// 根据 apiType 设置右图标的显示状态
this.isRightIconVisible = this.apiType === 'listA';
// 初始化并连接MQTT
this.mqttClient = new MqttClient();
this.mqttClient.connect(() => {
console.log('MQTT 连接成功,开始订阅主题');
// 订阅来自设备的状态更新
const statusTopic = `device/status/${this.deviceID}`;
const statusTopic = `A/${this.itemInfo.deviceImei}`;
this.mqttClient.subscribe(statusTopic, (payload) => {
console.log(`收到来自 ${statusTopic} 的消息:`, payload);
uni.showModal({
@ -433,9 +593,16 @@
});
});
});
if (this.apiType === 'listA') {
this.fetchDeviceDetail(data.data.id)
} else {
this.fetchSharedDeviceDetail(data.data.deviceId)
}
});
// 如果需要向调用页面返回数据,可以触发 'ack' 事件
eventChannel.emit('ack', {})
},
onUnload() {
// 页面卸载时断开MQTT连接
@ -448,6 +615,24 @@
</script>
<style scoped>
.page-loading-mask {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #121212;
display: flex;
justify-content: center;
align-items: center;
z-index: 9999;
}
/* 优化权限控制区域的显示 */
.mode-section,
.form-section {
transition: opacity 0.3s ease;
}
.device-detail-container {
padding: 30rpx;
background: #121212;