list.nvue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 { getDeviceList, selectDevice } 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. },
  50. onPullDownRefresh() {
  51. uni.stopPullDownRefresh()
  52. this.init()
  53. },
  54. methods: {
  55. async init() {
  56. try {
  57. const data = await getDeviceList()
  58. this.list = (data || []).map(item => ({
  59. ...item,
  60. deviceId: item.deviceCode || item.deviceId,
  61. name: item.deviceName || item.name || item.deviceCode,
  62. RSSI: item.RSSI || ''
  63. }))
  64. uni.setStorageSync('deviceList', this.list)
  65. return
  66. } catch (e) {
  67. console.error('加载设备列表失败,使用本地缓存', e)
  68. }
  69. try {
  70. const data = uni.getStorageSync('deviceList')
  71. if (data && data.length > 0) {
  72. this.list = data
  73. } else {
  74. this.list = []
  75. }
  76. } catch (e) {
  77. this.list = []
  78. }
  79. },
  80. goAddDevice() {
  81. uni.navigateTo({
  82. url: '/pages/device/search/search'
  83. })
  84. },
  85. async goDeviceInfo(item) {
  86. try {
  87. const selected = await selectDevice({
  88. deviceCode: item.deviceCode || item.deviceId,
  89. groupId: item.groupId || null,
  90. profileId: item.profileId || null
  91. })
  92. const merged = {
  93. ...item,
  94. ...selected,
  95. deviceId: item.deviceId || selected.deviceCode,
  96. name: selected.deviceName || item.name
  97. }
  98. uni.setStorageSync('current_device', merged)
  99. if (selected.profile) {
  100. uni.setStorageSync('current_manage_user_id', selected.profile.profileId || selected.profile.id)
  101. uni.setStorageSync('current_manage_user', selected.profile)
  102. }
  103. } catch (e) {
  104. console.error('选择设备失败', e)
  105. return
  106. }
  107. uni.navigateTo({
  108. url: '/pages/device/deviceInfo/deviceInfo?name=' + encodeURIComponent(item.name || '') +
  109. '&deviceId=' + encodeURIComponent(item.deviceId || '') +
  110. '&deviceCode=' + encodeURIComponent(item.deviceCode || item.deviceId || '') +
  111. '&rssi=' + encodeURIComponent(item.RSSI || '')
  112. })
  113. }
  114. }
  115. }
  116. </script>
  117. <style>
  118. .page {
  119. flex: 1;
  120. position: relative;
  121. }
  122. .content-bg {
  123. position: absolute;
  124. left: 0;
  125. top: 0;
  126. width: 750rpx;
  127. height: 2000px;
  128. }
  129. .status-bar {
  130. background-color: rgba(0,0,0,0);
  131. }
  132. .custom-head {
  133. flex-direction: row;
  134. justify-content: space-between;
  135. align-items: center;
  136. height: 88rpx;
  137. padding-left: 54rpx;
  138. padding-right: 54rpx;
  139. }
  140. .head-placeholder {
  141. width: 36rpx;
  142. }
  143. .head-title {
  144. font-weight: bold;
  145. font-size: 32rpx;
  146. color: #545F71;
  147. }
  148. .head-add {
  149. width: 20px;
  150. height: 20px;
  151. border-width: 2px;
  152. border-style: solid;
  153. border-color: #545F71;
  154. border-radius: 20px;
  155. text-align: center;
  156. line-height: 16px;
  157. font-size: 20px;
  158. font-weight: bold;
  159. color: #545F71;
  160. }
  161. .section-title {
  162. font-size: 32rpx;
  163. color: #545F71;
  164. margin-left: 38rpx;
  165. margin-bottom: 38rpx;
  166. margin-top: 20rpx;
  167. }
  168. .device-list {
  169. flex-direction: row;
  170. flex-wrap: wrap;
  171. justify-content: space-between;
  172. padding-left: 38rpx;
  173. padding-right: 38rpx;
  174. }
  175. .device-card {
  176. width: 324rpx;
  177. height: 300rpx;
  178. background-color: #FFFFFF;
  179. border-radius: 24rpx;
  180. margin-bottom: 26rpx;
  181. align-items: center;
  182. padding-top: 40rpx;
  183. }
  184. .device-icon {
  185. width: 156rpx;
  186. height: 156rpx;
  187. }
  188. .device-name {
  189. margin-top: 36rpx;
  190. font-weight: bold;
  191. font-size: 28rpx;
  192. color: #545F71;
  193. }
  194. .device-status {
  195. font-size: 24rpx;
  196. color: #999999;
  197. margin-top: 10rpx;
  198. }
  199. .add-card {
  200. align-items: flex-start;
  201. padding-left: 40rpx;
  202. padding-top: 40rpx;
  203. }
  204. .add-title {
  205. font-weight: bold;
  206. font-size: 28rpx;
  207. color: #545F71;
  208. margin-bottom: 48rpx;
  209. }
  210. .add-icon-wrap {
  211. align-items: center;
  212. width: 244rpx;
  213. }
  214. .add-icon-circle {
  215. width: 60rpx;
  216. height: 60rpx;
  217. background-color: #C3DFDB;
  218. border-radius: 60rpx;
  219. justify-content: center;
  220. align-items: center;
  221. }
  222. .add-icon-text {
  223. font-size: 42rpx;
  224. color: #389588;
  225. font-weight: bold;
  226. margin-top: -4rpx;
  227. }
  228. .add-tip {
  229. text-align: center;
  230. margin-top: 8rpx;
  231. font-size: 24rpx;
  232. color: #999999;
  233. }
  234. </style>