list.nvue 5.3 KB

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