list.nvue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <template>
  2. <view class="page">
  3. <image class="content-bg" src="/static/device/devicebg.png" mode="scaleToFill"></image>
  4. <view class="status-bar" :style="{height: statusBarHeight + 'px'}"></view>
  5. <view class="custom-head">
  6. <text class="head-placeholder"> </text>
  7. <text class="head-title">我的设备</text>
  8. <text class="head-add" @click="goAddDevice">+</text>
  9. </view>
  10. <text class="section-title">已添加设备</text>
  11. <view class="device-list">
  12. <!-- 设备卡片 -->
  13. <view class="device-card" v-for="(item, index) in list" :key="index" @click="goDeviceInfo(item)">
  14. <image class="device-icon" src="/static/device/device1.png" mode="heightFix"></image>
  15. <text class="device-name">{{item.name}}</text>
  16. <text class="device-status"></text>
  17. </view>
  18. <!-- 添加设备卡片 -->
  19. <view class="device-card add-card" @click="goAddDevice">
  20. <text class="add-title">添加设备</text>
  21. <view class="add-icon-wrap">
  22. <view class="add-icon-circle">
  23. <text class="add-icon-text">+</text>
  24. </view>
  25. <text class="add-tip">去添加</text>
  26. </view>
  27. </view>
  28. </view>
  29. <ay-toast ref="ayToast" />
  30. </view>
  31. </template>
  32. <script>
  33. import ayToast from '@/components/ay-toast/ay-toast.nvue'
  34. import { useBleStore } from '@/stores/ble'
  35. export default {
  36. components: {
  37. ayToast
  38. },
  39. data() {
  40. return {
  41. statusBarHeight: 44,
  42. list: []
  43. }
  44. },
  45. onShow() {
  46. const sysInfo = uni.getSystemInfoSync()
  47. this.statusBarHeight = sysInfo.statusBarHeight || 44
  48. this.init()
  49. this.updateConnectedDeviceRSSI()
  50. },
  51. onPullDownRefresh() {
  52. uni.stopPullDownRefresh()
  53. this.init()
  54. },
  55. methods: {
  56. init() {
  57. // 从本地存储读取设备列表
  58. try {
  59. const data = uni.getStorageSync('deviceList')
  60. if (data && data.length > 0) {
  61. this.list = data
  62. } else {
  63. this.list = []
  64. }
  65. } catch (e) {
  66. this.list = []
  67. }
  68. },
  69. goAddDevice() {
  70. uni.navigateTo({
  71. url: '/pages/device/search/search'
  72. })
  73. },
  74. goDeviceInfo(item) {
  75. uni.navigateTo({
  76. url: '/pages/device/deviceInfo/deviceInfo?name=' + encodeURIComponent(item.name || '') + '&deviceId=' + encodeURIComponent(item.deviceId || '') + '&rssi=' + encodeURIComponent(item.RSSI || '')
  77. })
  78. },
  79. /** 如果设备已连接,实时获取RSSI并更新缓存 */
  80. updateConnectedDeviceRSSI() {
  81. const bleStore = useBleStore()
  82. if (!bleStore.linked || !bleStore.device || !bleStore.device.deviceId) return
  83. const connectedDeviceId = bleStore.device.deviceId
  84. uni.getBLEDeviceRSSI({
  85. deviceId: connectedDeviceId,
  86. success: (res) => {
  87. console.log('实时RSSI更新:', res.RSSI)
  88. // 更新列表中对应设备的RSSI
  89. const idx = this.list.findIndex(item => item.deviceId === connectedDeviceId)
  90. if (idx !== -1) {
  91. this.list[idx].RSSI = res.RSSI
  92. // 同步更新到本地缓存
  93. try {
  94. uni.setStorageSync('deviceList', this.list)
  95. } catch (e) {}
  96. }
  97. },
  98. fail: (err) => {
  99. console.log('获取RSSI失败:', err)
  100. }
  101. })
  102. }
  103. }
  104. }
  105. </script>
  106. <style>
  107. .page {
  108. flex: 1;
  109. position: relative;
  110. }
  111. .content-bg {
  112. position: absolute;
  113. left: 0;
  114. top: 0;
  115. width: 750rpx;
  116. height: 2000px;
  117. }
  118. .status-bar {
  119. background-color: rgba(0,0,0,0);
  120. }
  121. .custom-head {
  122. flex-direction: row;
  123. justify-content: space-between;
  124. align-items: center;
  125. height: 88rpx;
  126. padding-left: 54rpx;
  127. padding-right: 54rpx;
  128. }
  129. .head-placeholder {
  130. width: 36rpx;
  131. }
  132. .head-title {
  133. font-weight: bold;
  134. font-size: 32rpx;
  135. color: #545F71;
  136. }
  137. .head-add {
  138. width: 20px;
  139. height: 20px;
  140. border-width: 2px;
  141. border-style: solid;
  142. border-color: #545F71;
  143. border-radius: 20px;
  144. text-align: center;
  145. line-height: 16px;
  146. font-size: 20px;
  147. font-weight: bold;
  148. color: #545F71;
  149. }
  150. .section-title {
  151. font-size: 32rpx;
  152. color: #545F71;
  153. margin-left: 38rpx;
  154. margin-bottom: 38rpx;
  155. margin-top: 20rpx;
  156. }
  157. .device-list {
  158. flex-direction: row;
  159. flex-wrap: wrap;
  160. justify-content: space-between;
  161. padding-left: 38rpx;
  162. padding-right: 38rpx;
  163. }
  164. .device-card {
  165. width: 324rpx;
  166. height: 300rpx;
  167. background-color: #FFFFFF;
  168. border-radius: 24rpx;
  169. margin-bottom: 26rpx;
  170. align-items: center;
  171. padding-top: 40rpx;
  172. }
  173. .device-icon {
  174. width: 156rpx;
  175. height: 156rpx;
  176. }
  177. .device-name {
  178. margin-top: 36rpx;
  179. font-weight: bold;
  180. font-size: 28rpx;
  181. color: #545F71;
  182. }
  183. .device-status {
  184. font-size: 24rpx;
  185. color: #999999;
  186. margin-top: 10rpx;
  187. }
  188. .add-card {
  189. align-items: flex-start;
  190. padding-left: 40rpx;
  191. padding-top: 40rpx;
  192. }
  193. .add-title {
  194. font-weight: bold;
  195. font-size: 28rpx;
  196. color: #545F71;
  197. margin-bottom: 48rpx;
  198. }
  199. .add-icon-wrap {
  200. align-items: center;
  201. width: 244rpx;
  202. }
  203. .add-icon-circle {
  204. width: 60rpx;
  205. height: 60rpx;
  206. background-color: #C3DFDB;
  207. border-radius: 60rpx;
  208. justify-content: center;
  209. align-items: center;
  210. }
  211. .add-icon-text {
  212. font-size: 42rpx;
  213. color: #389588;
  214. font-weight: bold;
  215. margin-top: -4rpx;
  216. }
  217. .add-tip {
  218. text-align: center;
  219. margin-top: 8rpx;
  220. font-size: 24rpx;
  221. color: #999999;
  222. }
  223. </style>