优化设备控制,提交后,实时获取设备返回状态信息

This commit is contained in:
fengerli
2025-08-14 13:46:57 +08:00
parent d8edb9f31e
commit e56bbfe674
15 changed files with 1104 additions and 383 deletions

View File

@ -31,7 +31,7 @@
<block v-for="(item, index) in deviceList" :key="index" :ref="'swipeItem_' + index">
<uni-swipe-action-item :right-options="Options"
@click="handleSwipeClick($event, item, index)" class="device-card"
:style="{ border: item.communicationMode==0 && item.onlineStatus==0 ? '1px solid rgba(224, 52, 52, 1)' : 'none' }">
:style="{ border: item.communicationMode==0 && item.onlineStatus==0 && item.alarmStatus==1 ? '1px solid rgba(224, 52, 52, 1)' : 'none' }">
<view @click.stop="handleFile(item)">
<view class="device-header">
<view class="deviceIMG">
@ -54,7 +54,8 @@
</view>
</view>
<view class="device-callpolice"
v-if="item.communicationMode==0 && item.onlineStatus==0">报警中</view>
v-if="item.communicationMode==0 && item.onlineStatus==0 && item.alarmStatus==1">
报警中</view>
<view v-if="item.communicationMode==1">
<view class="device-status online">已连接</view>
<view class="device-status unline">未连接</view>
@ -144,6 +145,10 @@
deviceReName
} from '@/api/common/index.js'
export default {
onPullDownRefresh() {
// 执行下拉刷新时的操作,比如重新获取数据
this.onIntall();
},
data() {
return {
navBarHeight: 70 + uni.getSystemInfoSync().statusBarHeight,
@ -313,16 +318,18 @@
this.showTooltip = false; // 关闭弹窗
switch (item.action) {
case 'scan':
// uni.navigateTo({
// url: '/pages/common/scan/scan'
// });
// 扫一扫
uni.scanCode({
success: (res) => {
console.log('条码内容:' + res.result);
console.log('条码内容:', res);
// 清除之前的数据
this.previousScanResult = null;
// 处理新的扫码结果
const cleanedResult = res.result.trim();
console.log('扫码结果:', cleanedResult);
// 跳转并传递扫描结果
uni.navigateTo({
url: `/pages/common/qrcode/qrcode?deviceId=${encodeURIComponent(res.result)}`
url: `/pages/common/qrcode/qrcode?deviceId=${encodeURIComponent(cleanedResult)}`
});
},
fail: (err) => {
@ -438,7 +445,7 @@
},
success: (res) => {
res.eventChannel.emit('devicePolice', {
data: deviceType
data: currentTab
});
}
})
@ -455,7 +462,8 @@
},
success: (res) => {
res.eventChannel.emit('deviceSend', {
data: deviceType
//data: deviceType,
data: currentTab
});
}
})
@ -526,7 +534,12 @@
onIntall() {
this.page = 1;
this.finished = false;
this.getData(this.deviceType); // 重新加载第一页数据
const deviceType = this.activeTabInfo?.id === '' ? undefined : this.activeTabInfo?.id;
this.getData(deviceType); // 重新加载第一页数据
setTimeout(() => {
// 停止下拉刷新动画
uni.stopPullDownRefresh();
}, 800);
},
updateDeviceStatus(data) {
this.deviceList = this.deviceList

View File

@ -145,11 +145,9 @@
return false
}
try {
uni.showLoading({
title: '登录中...'
})
console.log('44444');
// 调用登录接口
const res = await login({
phonenumber: this.phone,
@ -157,7 +155,6 @@
tenantId: '894078' //租户ID
})
if (res.code == 200) {
console.log(res, 'ressss');
uni.hideLoading()
uni.setStorageSync('token', res.data.access_token) // 缓存token
uni.setStorageSync('clientID', res.data.client_id) // 缓存token
@ -170,19 +167,18 @@
})
} else {
uni.showToast({
title: res.msg,
title: res.msg || '服务器异常,请稍后重试',
icon: 'none'
})
}
} catch (error) {
uni.hideLoading()
console.log('捕获错误:', error);
uni.showToast({
title: error.msg,
title: error.msg || '登录失败',
icon: 'none'
});
uni.hideLoading()
}
},
// 跳转到协议页面
goToPage(type) {

View File

@ -5,17 +5,13 @@
<image src="/static/images/common/svg.png" class="svg"></image>
<view class="scanT">ID: {{ deviceId }}</view>
</view>
<!-- 连接中状态 -->
<view class="connecting-container" v-else>
<view class="device-info">
<view class="">
<image src="/static/images/common/svg.png" class="svg"></image>
</view>
<!-- <view>
<image src="/static/images/bip.6.png" class="bip"></image>
</view> -->
<text class="device-name">设备名{{deviceId}}</text>
<text class="device-model1">ID:{{deviceId}}</text>
</view>
@ -71,6 +67,7 @@
})
console.log(this.deviceId, 'deerer ere');
if (res.code == 200) {
this.isConnectNo = false
this.isSuccess = true
uni.hideLoading()

View File

@ -14,15 +14,13 @@
<image :src="item.devicePic" class="IMG" mode="aspectFit"></image>
</view>
<view class="device-name">
<view>设备:{{item.deviceName}}</view>
<view>设备:{{ item.deviceName }}</view>
<view class="ID">
<view class="ID">ID:{{item.deviceImei}}</view>
<view class="onlines"
v-if="item.onlineStatus==1">在线</view>
<view class="ID">ID:{{ item.deviceImei }}</view>
<view class="onlines" v-if="item.onlineStatus == 1">在线</view>
<!-- 离线状态 -->
<view class="unlines"
v-if="item.onlineStatus==0">离线</view>
<view>电量{{item.battery || '0'}}%</view>
<view class="unlines" v-if="item.onlineStatus == 0">离线</view>
<view>电量{{ item.battery || '0' }}%</view>
</view>
</view>
</view>
@ -32,14 +30,15 @@
<view class="ql-editor">编辑信息</view>
<view class="ql-input">
<textarea placeholder-style="color:rgba(255, 255, 255, 0.4)" placeholder="请输入内容" class="textarea"
v-model="messageToSend" :maxlength="20"/>
v-model="messageToSend" :maxlength="20" />
</view>
<button class="login-btn" @click="sendTextMessage">发送</button>
<button class="login-btn" @click.stop="sendTextMessage">发送</button>
</view>
</scroll-view>
<!-- 成功提示弹框 -->
<CustomPopup :show="showPopupFlag" :title="popupTitle" :message="popupMessage" icon="/static/images/common/sendSucc.png"
:confirm-text="popupConfirmText" :show-cancel="false" @confirm="onPopupConfirm" />
<CustomPopup :show="showPopupFlag" :title="popupTitle" :message="popupMessage"
icon="/static/images/common/sendSucc.png" :confirm-text="popupConfirmText" :show-cancel="false"
@confirm="onPopupConfirm" />
</view>
</template>
@ -49,12 +48,16 @@
deviceInfo,
} from '@/api/common/index.js'
import {
deviceSendMessage
deviceSendMessage,
deviceRealTimeStatus //设备状态
} from '@/api/6170/deviceControl.js'
import {
generateShortId
} from '@/utils/function.js';
export default {
components: {
CustomPopup
},
components: {
CustomPopup
},
data() {
return {
deviceList: [],
@ -62,10 +65,56 @@
showPopupFlag: false,
popupTitle: '',
popupMessage: '信息发送成功!',
popupConfirmText: '确认'
popupConfirmText: '确认',
isSending: false,
sendInfo: {}
}
},
methods: {
/**
* 获取设备状态(带自动轮询)
* @param {number} val - 功能模式
* @param {string} batchId - 批次ID
* @param {number} [maxRetries=30] - 最大重试次数约30秒
* @param {number} [interval=1000] - 轮询间隔(毫秒)
*/
async getdeviceSTatus(val, batchId, maxRetries = 30, interval = 2000) {
let retries = 0;
const checkStatus = async () => {
try {
const data = {
functionMode: val,
batchId: batchId,
typeName: this.sendInfo.typeName,
deviceImei: this.sendInfo.deviceImei
};
const res = await deviceRealTimeStatus(data);
if (res.code !== 200) {
throw new Error(res.msg || '请求失败');
}
switch (res.data.functionAccess) {
case 'OK':
return res; // 成功完成
case 'ACTIVE':
if (++retries >= maxRetries) {
throw new Error('操作超时');
}
await new Promise(r => setTimeout(r, interval));
return checkStatus(); // 继续轮询
case 'FAILED':
throw new Error('设备操作失败');
case 'TIMEOUT':
throw new Error('设备响应超时');
default:
throw new Error('未知状态');
}
} catch (error) {
throw error;
}
};
return checkStatus();
},
toggleSelect(index) {
this.deviceList[index].checked = !this.deviceList[index].checked
this.$forceUpdate()
@ -75,7 +124,7 @@
this.loading = true;
let data = {
pageNum: 1,
pageSize: 20,
pageSize: 50,
deviceType: deviceType
}
deviceInfo(data).then((res) => {
@ -93,9 +142,12 @@
});
},
// 发送文本消息
sendTextMessage() {
async sendTextMessage() {
// 防重复提交
if (this.isSending) return;
const selectedDevices = this.deviceList.filter(item => item.checked)
const deviceIds = selectedDevices.map(item => item.id);
const deviceImeiList = selectedDevices.map(item => item.deviceImei);
if (selectedDevices.length === 0) {
uni.showToast({
title: '请选择一个设备',
@ -105,25 +157,53 @@
}
if (!this.messageToSend) {
uni.showToast({
title: '请输入要发送的内容',
title: '请输入要发送的文字',
icon: 'none'
});
return;
}
let data = {
sendMsg: this.messageToSend,
deviceIds: deviceIds
}
deviceSendMessage(data).then((res) => {
if (res.code == 200) {
this.showPopupFlag = true
} else {
this.isSending = true;
let loadingShown = false;
try {
uni.showLoading({
title: '发送中...',
mask: true
});
loadingShown = true;
// 2. 准备请求数据
const batchId = generateShortId();
const data = {
sendMsg: this.messageToSend,
deviceIds: deviceIds,
batchId: batchId,
typeName: this.sendInfo.typeName,
batchId: batchId,
deviceImeiList: deviceImeiList
};
// 3.人员信息
const registerRes = await deviceSendMessage(data);
if (registerRes.code !== 200) {
uni.showToast({
title: res.msg,
title: registerRes.msg,
icon: 'none'
});
})
return
}
})
// 4. 获取设备状态
const statusRes = await this.getdeviceSTatus(2, batchId);
if (statusRes.data.functionAccess === 'OK') {
// 5. 显示成功弹窗
this.showPopupFlag = true
}
} catch (error) {
uni.showToast({
title: error.message,
icon: 'none'
});
} finally {
uni.hideLoading();
this.isSending = false;
}
},
onPopupConfirm() {
this.showPopupFlag = false
@ -137,7 +217,8 @@
// 监听 'deviceSend' 事件,获取传过来的数据
eventChannel.on('deviceSend', (data) => {
console.log('Received detail data:', data);
this.getData(data.data)
this.getData(data.data.id)
this.sendInfo = data.data
});
// 如果需要向调用页面返回数据,可以触发 'ack' 事件
eventChannel.emit('ack', {})