| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <template>
- <view class="page">
- <image class="content-bg" src="/static/device/devicebg.png" mode="scaleToFill"></image>
- <view class="status-bar" :style="{height: statusBarHeight + 'px'}"></view>
- <view class="custom-head">
- <text class="head-placeholder"> </text>
- <text class="head-title">我的设备</text>
- <text class="head-add" @click="goAddDevice">+</text>
- </view>
- <text class="section-title">已添加设备</text>
- <view class="device-list">
- <!-- 设备卡片 -->
- <view class="device-card" v-for="(item, index) in list" :key="index" @click="goDeviceInfo(item)">
- <image class="device-icon" src="/static/device/device1.png" mode="heightFix"></image>
- <text class="device-name">{{item.name}}</text>
- <text class="device-status"></text>
- </view>
- <!-- 添加设备卡片 -->
- <view class="device-card add-card" @click="goAddDevice">
- <text class="add-title">添加设备</text>
- <view class="add-icon-wrap">
- <view class="add-icon-circle">
- <text class="add-icon-text">+</text>
- </view>
- <text class="add-tip">去添加</text>
- </view>
- </view>
- </view>
- <ay-toast ref="ayToast" />
- </view>
- </template>
- <script>
- import ayToast from '@/components/ay-toast/ay-toast.nvue'
- import { useBleStore } from '@/stores/ble'
- export default {
- components: {
- ayToast
- },
- data() {
- return {
- statusBarHeight: 44,
- list: []
- }
- },
- onShow() {
- const sysInfo = uni.getSystemInfoSync()
- this.statusBarHeight = sysInfo.statusBarHeight || 44
- this.init()
- this.updateConnectedDeviceRSSI()
- },
- onPullDownRefresh() {
- uni.stopPullDownRefresh()
- this.init()
- },
- methods: {
- init() {
- // 从本地存储读取设备列表
- try {
- const data = uni.getStorageSync('deviceList')
- if (data && data.length > 0) {
- this.list = data
- } else {
- this.list = []
- }
- } catch (e) {
- this.list = []
- }
- },
- goAddDevice() {
- uni.navigateTo({
- url: '/pages/device/search/search'
- })
- },
- goDeviceInfo(item) {
- uni.navigateTo({
- url: '/pages/device/deviceInfo/deviceInfo?name=' + encodeURIComponent(item.name || '') + '&deviceId=' + encodeURIComponent(item.deviceId || '') + '&rssi=' + encodeURIComponent(item.RSSI || '')
- })
- },
- /** 如果设备已连接,实时获取RSSI并更新缓存 */
- updateConnectedDeviceRSSI() {
- const bleStore = useBleStore()
- if (!bleStore.linked || !bleStore.device || !bleStore.device.deviceId) return
- const connectedDeviceId = bleStore.device.deviceId
- uni.getBLEDeviceRSSI({
- deviceId: connectedDeviceId,
- success: (res) => {
- console.log('实时RSSI更新:', res.RSSI)
- // 更新列表中对应设备的RSSI
- const idx = this.list.findIndex(item => item.deviceId === connectedDeviceId)
- if (idx !== -1) {
- this.list[idx].RSSI = res.RSSI
- // 同步更新到本地缓存
- try {
- uni.setStorageSync('deviceList', this.list)
- } catch (e) {}
- }
- },
- fail: (err) => {
- console.log('获取RSSI失败:', err)
- }
- })
- }
- }
- }
- </script>
- <style>
- .page {
- flex: 1;
- position: relative;
- }
- .content-bg {
- position: absolute;
- left: 0;
- top: 0;
- width: 750rpx;
- height: 2000px;
- }
- .status-bar {
- background-color: rgba(0,0,0,0);
- }
- .custom-head {
- flex-direction: row;
- justify-content: space-between;
- align-items: center;
- height: 88rpx;
- padding-left: 54rpx;
- padding-right: 54rpx;
- }
- .head-placeholder {
- width: 36rpx;
- }
- .head-title {
- font-weight: bold;
- font-size: 32rpx;
- color: #545F71;
- }
- .head-add {
- width: 20px;
- height: 20px;
- border-width: 2px;
- border-style: solid;
- border-color: #545F71;
- border-radius: 20px;
- text-align: center;
- line-height: 16px;
- font-size: 20px;
- font-weight: bold;
- color: #545F71;
- }
- .section-title {
- font-size: 32rpx;
- color: #545F71;
- margin-left: 38rpx;
- margin-bottom: 38rpx;
- margin-top: 20rpx;
- }
- .device-list {
- flex-direction: row;
- flex-wrap: wrap;
- justify-content: space-between;
- padding-left: 38rpx;
- padding-right: 38rpx;
- }
- .device-card {
- width: 324rpx;
- height: 300rpx;
- background-color: #FFFFFF;
- border-radius: 24rpx;
- margin-bottom: 26rpx;
- align-items: center;
- padding-top: 40rpx;
- }
- .device-icon {
- width: 156rpx;
- height: 156rpx;
- }
- .device-name {
- margin-top: 36rpx;
- font-weight: bold;
- font-size: 28rpx;
- color: #545F71;
- }
- .device-status {
- font-size: 24rpx;
- color: #999999;
- margin-top: 10rpx;
- }
- .add-card {
- align-items: flex-start;
- padding-left: 40rpx;
- padding-top: 40rpx;
- }
- .add-title {
- font-weight: bold;
- font-size: 28rpx;
- color: #545F71;
- margin-bottom: 48rpx;
- }
- .add-icon-wrap {
- align-items: center;
- width: 244rpx;
- }
- .add-icon-circle {
- width: 60rpx;
- height: 60rpx;
- background-color: #C3DFDB;
- border-radius: 60rpx;
- justify-content: center;
- align-items: center;
- }
- .add-icon-text {
- font-size: 42rpx;
- color: #389588;
- font-weight: bold;
- margin-top: -4rpx;
- }
- .add-tip {
- text-align: center;
- margin-top: 8rpx;
- font-size: 24rpx;
- color: #999999;
- }
- </style>
|