detail.nvue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. <template>
  2. <view class="container">
  3. <view class="status-bar" :style="{height: statusBarHeight + 'px'}"></view>
  4. <view class="customHead">
  5. <view class="back-wrap" @click="goBack">
  6. <image src="/static/public/backBlack.png" mode="aspectFill" class="backimg"></image>
  7. </view>
  8. <text class="head-title">设备详情</text>
  9. <view class="plus"></view>
  10. </view>
  11. <scroll-view class="list" scroll-y="true">
  12. <!-- 连接状态 -->
  13. <view class="connect-status">
  14. <view class="status-dot" :style="{backgroundColor: connected ? '#389588' : '#CCCCCC'}"></view>
  15. <text class="status-text">{{connected ? '已连接' : '未连接'}}</text>
  16. <!-- <text class="connect-btn" v-if="!connected && !connecting" @click="connectDevice">连接设备</text>
  17. <text class="connect-btn" v-if="connecting">连接中...</text> -->
  18. </view>
  19. <text class="txt">通用设置</text>
  20. <view class="menuitem">
  21. <text>设备名称</text>
  22. <text class="gray">{{name}}</text>
  23. </view>
  24. <view class="menuitem">
  25. <text>设备编号</text>
  26. <text class="gray device-id-text">{{deviceId}}</text>
  27. </view>
  28. <view class="menuitem">
  29. <text>设备型号</text>
  30. <text class="gray">{{deviceModel || '未知'}}</text>
  31. </view>
  32. <view class="menuitem">
  33. <text>序列号</text>
  34. <text class="gray">{{serialNo || '未知'}}</text>
  35. </view>
  36. <view class="menuitem">
  37. <text>信号强度</text>
  38. <view class="signal-value-wrap">
  39. <view class="signal-wrap">
  40. <view class="signal-bar signal-bar1" :class="{'signal-active': signalLevel >= 1}"></view>
  41. <view class="signal-bar signal-bar2" :class="{'signal-active': signalLevel >= 2}"></view>
  42. <view class="signal-bar signal-bar3" :class="{'signal-active': signalLevel >= 3}"></view>
  43. <view class="signal-bar signal-bar4" :class="{'signal-active': signalLevel >= 4}"></view>
  44. </view>
  45. <text class="gray" style="padding-right: 0;">{{rssiText}}</text>
  46. </view>
  47. </view>
  48. <view class="menuitem">
  49. <text>固件版本</text>
  50. <text class="gray">{{firmwareVersion || '未获取'}}</text>
  51. </view>
  52. <view class="menuitem">
  53. <text>连接状态</text>
  54. <text class="gray">{{connected ? '已连接' : '未连接'}}</text>
  55. </view>
  56. <view class="menuitem">
  57. <text>在线状态</text>
  58. <text class="gray">{{onlineStatus === 1 ? '在线' : '离线'}}</text>
  59. </view>
  60. <view class="menuitem lastItem menuitem-address">
  61. <text>设备地址</text>
  62. <view class="address-wrap">
  63. <text class="gray address-text">{{address || '未设置'}}</text>
  64. </view>
  65. </view>
  66. <text class="txt">设备操作</text>
  67. <view class="menuitem" @click="disconnectDevice" v-if="connected">
  68. <text>断开连接</text>
  69. <image class="arrowright" src="/static/my/arrowright.png" mode="aspectFill"></image>
  70. </view>
  71. <view class="menuitem lastItem">
  72. <text>帮助与反馈</text>
  73. <image class="arrowright" src="/static/my/arrowright.png" mode="aspectFill"></image>
  74. </view>
  75. <view class="delbtn" @click="delDevice">
  76. <text class="delbtn-text">删除设备</text>
  77. </view>
  78. </scroll-view>
  79. <ay-toast ref="ayToast" />
  80. </view>
  81. </template>
  82. <script>
  83. import { useBleStore } from '@/stores/ble'
  84. import { BLE_STATE } from '@/utils/ble/constants.js'
  85. import { getDeviceDetail } from '@/utils/api/device.js'
  86. import ayToast from '@/components/ay-toast/ay-toast.nvue'
  87. export default {
  88. components: {
  89. ayToast
  90. },
  91. data() {
  92. return {
  93. statusBarHeight: 44,
  94. name: '',
  95. deviceId: '',
  96. deviceCode: '',
  97. deviceModel: '',
  98. serialNo: '',
  99. onlineStatus: 0,
  100. address: '',
  101. rssi: '',
  102. id:'',
  103. connected: false,
  104. connecting: false,
  105. firmwareVersion: '',
  106. _unbinders: []
  107. }
  108. },
  109. computed: {
  110. rssiText() {
  111. console.log("信号强度",this.rssi)
  112. if (!this.rssi) return '未知'
  113. const val = Number(this.rssi)
  114. if (val >= -50) return '非常强'
  115. if (val >= -65) return '强'
  116. if (val >= -80) return '中'
  117. return '弱'
  118. },
  119. signalLevel() {
  120. if (!this.rssi) return 0
  121. const val = Number(this.rssi)
  122. if (val >= -50) return 4
  123. if (val >= -65) return 3
  124. if (val >= -80) return 2
  125. if (val >= -95) return 1
  126. return 1
  127. }
  128. },
  129. onLoad(options) {
  130. const sysInfo = uni.getSystemInfoSync()
  131. this.statusBarHeight = sysInfo.statusBarHeight || 44
  132. this.name = decodeURIComponent(options.name || '')
  133. this.deviceId = decodeURIComponent(options.deviceId || '')
  134. this.rssi = decodeURIComponent(options.rssi || '')
  135. this.id = decodeURIComponent(options.id || '')
  136. // 获取设备详情
  137. if (this.id) {
  138. this.fetchDeviceDetail()
  139. }
  140. },
  141. async onShow() {
  142. const bleStore = useBleStore()
  143. // 用bleState精确判断连接状态
  144. this.connected = (bleStore.bleState === BLE_STATE.READY_COMM)
  145. console.log("连接状态", this.connected, "bleState:", bleStore.bleState)
  146. // 已连接时获取实时RSSI(使用store中实际连接的deviceId)
  147. const connectedId = bleStore.device && bleStore.device.deviceId
  148. if (this.connected && connectedId) {
  149. console.log("获取RSSI使用deviceId:", connectedId, "页面deviceId:", this.deviceId)
  150. uni.getBLEDeviceRSSI({
  151. deviceId: connectedId,
  152. success: (res) => {
  153. console.log("实时信号强度", res.RSSI)
  154. this.rssi = res.RSSI
  155. },
  156. fail: (err) => {
  157. console.log("获取RSSI失败", err)
  158. // 连接实际已断开,同步状态
  159. if (err.code === 10004) {
  160. this.connected = false
  161. }
  162. }
  163. })
  164. }
  165. // 监听store状态变化(使用$watch)
  166. this._stopWatch = this.$watch(
  167. () => bleStore.bleState,
  168. (s) => {
  169. this.connected = (s === BLE_STATE.READY_COMM)
  170. if (s === BLE_STATE.DISCONNECTED) {
  171. this.connecting = false
  172. }
  173. }
  174. )
  175. },
  176. onHide() {
  177. this._cleanListeners()
  178. },
  179. onUnload() {
  180. this._cleanListeners()
  181. },
  182. methods: {
  183. async fetchDeviceDetail() {
  184. try {
  185. const res = await getDeviceDetail(this.id)
  186. if (res) {
  187. this.name = res.deviceName || this.name
  188. this.deviceModel = res.deviceModel || ''
  189. this.serialNo = res.serialNo || ''
  190. this.firmwareVersion = res.firmwareVersion || this.firmwareVersion
  191. this.onlineStatus = res.onlineStatus || 0
  192. this.address = res.address || ''
  193. }
  194. } catch (e) {
  195. console.log('获取设备详情失败', e)
  196. }
  197. },
  198. goBack() {
  199. uni.navigateBack()
  200. },
  201. _cleanListeners() {
  202. if (this._stopWatch) {
  203. this._stopWatch()
  204. this._stopWatch = null
  205. }
  206. },
  207. async connectDevice() {
  208. if (this.connecting || this.connected) return
  209. this.connecting = true
  210. const bleStore = useBleStore()
  211. try {
  212. await bleStore.init()
  213. await bleStore.connectDevice(this.deviceId)
  214. this.connected = true
  215. this.$refs.ayToast.success('连接成功')
  216. } catch (e) {
  217. this.$refs.ayToast.error('连接失败: ' + (e.message || ''))
  218. } finally {
  219. this.connecting = false
  220. }
  221. },
  222. async disconnectDevice() {
  223. const bleStore = useBleStore()
  224. try {
  225. await bleStore.disconnect()
  226. this.connected = false
  227. this.$refs.ayToast.success('已断开连接')
  228. } catch (e) {
  229. this.$refs.ayToast.error('断开失败')
  230. }
  231. },
  232. delDevice() {
  233. const bleStore = useBleStore()
  234. uni.showModal({
  235. title: '提示',
  236. content: '此操作将删除该设备,是否继续?',
  237. cancelText: '取消',
  238. confirmText: '继续',
  239. success: (res) => {
  240. if (res.confirm) {
  241. if (this.connected) {
  242. bleStore.disconnect().catch(() => {})
  243. }
  244. // 从本地存储中移除
  245. try {
  246. let deviceList = uni.getStorageSync('deviceList') || []
  247. deviceList = deviceList.filter(item => item.deviceId !== this.deviceId)
  248. uni.setStorageSync('deviceList', deviceList)
  249. } catch (e) {}
  250. this.$refs.ayToast.success('设备删除成功')
  251. setTimeout(() => {
  252. uni.navigateBack()
  253. }, 1500)
  254. }
  255. }
  256. })
  257. }
  258. }
  259. }
  260. </script>
  261. <style>
  262. .container {
  263. flex: 1;
  264. background-color: #FFFFFF;
  265. }
  266. .status-bar {
  267. background-color: #FFFFFF;
  268. }
  269. .customHead {
  270. width: 750rpx;
  271. background-color: #FFFFFF;
  272. flex-direction: row;
  273. justify-content: space-between;
  274. height: 88rpx;
  275. align-items: center;
  276. padding-left: 54rpx;
  277. padding-right: 54rpx;
  278. }
  279. .back-wrap {
  280. width: 50rpx;
  281. height: 50rpx;
  282. justify-content: center;
  283. align-items: center;
  284. }
  285. .backimg {
  286. width: 50rpx;
  287. height: 50rpx;
  288. }
  289. .head-title {
  290. font-weight: bold;
  291. font-size: 32rpx;
  292. color: #545F71;
  293. }
  294. .plus {
  295. width: 50rpx;
  296. height: 50rpx;
  297. }
  298. .list {
  299. flex: 1;
  300. }
  301. .connect-status {
  302. flex-direction: row;
  303. align-items: center;
  304. margin-left: 38rpx;
  305. margin-right: 38rpx;
  306. margin-top: 30rpx;
  307. margin-bottom: 10rpx;
  308. padding-top: 24rpx;
  309. padding-bottom: 24rpx;
  310. padding-left: 30rpx;
  311. padding-right: 30rpx;
  312. background-color: #F8F9FB;
  313. border-radius: 12rpx;
  314. }
  315. .status-dot {
  316. width: 16rpx;
  317. height: 16rpx;
  318. border-radius: 16rpx;
  319. background-color: #CCCCCC;
  320. margin-right: 12rpx;
  321. }
  322. .status-text {
  323. font-size: 28rpx;
  324. color: #545F71;
  325. flex: 1;
  326. }
  327. .connect-btn {
  328. font-size: 26rpx;
  329. color: #389588;
  330. padding-top: 8rpx;
  331. padding-bottom: 8rpx;
  332. padding-left: 24rpx;
  333. padding-right: 24rpx;
  334. border-width: 1px;
  335. border-style: solid;
  336. border-color: #389588;
  337. border-radius: 24rpx;
  338. }
  339. .txt {
  340. margin-top: 50rpx;
  341. margin-left: 38rpx;
  342. margin-right: 38rpx;
  343. font-size: 28rpx;
  344. color: #9BA5B7;
  345. }
  346. .lastItem {
  347. border-bottom-width: 0;
  348. }
  349. .menuitem {
  350. margin-left: 38rpx;
  351. margin-right: 38rpx;
  352. flex-direction: row;
  353. align-items: center;
  354. justify-content: space-between;
  355. height: 104rpx;
  356. font-size: 32rpx;
  357. color: #545F71;
  358. border-bottom-width: 1px;
  359. border-bottom-style: solid;
  360. border-bottom-color: #EEF1F4;
  361. }
  362. .gray {
  363. font-size: 28rpx;
  364. color: #9BA5B7;
  365. padding-right: 20rpx;
  366. }
  367. .device-id-text {
  368. font-size: 22rpx;
  369. }
  370. .signal-value-wrap {
  371. flex-direction: row;
  372. align-items: center;
  373. padding-right: 20rpx;
  374. }
  375. .signal-wrap {
  376. flex-direction: row;
  377. align-items: flex-end;
  378. margin-right: 12rpx;
  379. height: 38rpx;
  380. padding-bottom: 4rpx;
  381. }
  382. .signal-bar {
  383. width: 8rpx;
  384. margin-left: 4rpx;
  385. border-radius: 4rpx;
  386. background-color: #ddd;
  387. }
  388. .signal-bar1 {
  389. height: 14rpx;
  390. }
  391. .signal-bar2 {
  392. height: 22rpx;
  393. }
  394. .signal-bar3 {
  395. height: 30rpx;
  396. }
  397. .signal-bar4 {
  398. height: 38rpx;
  399. }
  400. .signal-active {
  401. background-color: #389588;
  402. }
  403. .arrowright {
  404. width: 40rpx;
  405. height: 40rpx;
  406. }
  407. .menuitem-address {
  408. height: auto;
  409. min-height: 104rpx;
  410. padding-top: 24rpx;
  411. padding-bottom: 24rpx;
  412. }
  413. .address-wrap {
  414. flex: 1;
  415. margin-left: 20rpx;
  416. }
  417. .address-text {
  418. font-size: 28rpx;
  419. color: #9BA5B7;
  420. text-align: right;
  421. lines: 0;
  422. }
  423. .delbtn {
  424. width: 654rpx;
  425. height: 96rpx;
  426. background-color: #F3F3F3;
  427. border-radius: 12rpx;
  428. margin-top: 60rpx;
  429. margin-left: 48rpx;
  430. margin-right: 48rpx;
  431. justify-content: center;
  432. align-items: center;
  433. }
  434. .delbtn-text {
  435. font-size: 32rpx;
  436. color: #F65252;
  437. }
  438. </style>