detail.nvue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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">{{deviceCode}}</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. this.deviceCode=res.deviceCode
  194. }
  195. } catch (e) {
  196. console.log('获取设备详情失败', e)
  197. }
  198. },
  199. goBack() {
  200. uni.navigateBack()
  201. },
  202. _cleanListeners() {
  203. if (this._stopWatch) {
  204. this._stopWatch()
  205. this._stopWatch = null
  206. }
  207. },
  208. async connectDevice() {
  209. if (this.connecting || this.connected) return
  210. this.connecting = true
  211. const bleStore = useBleStore()
  212. try {
  213. await bleStore.init()
  214. await bleStore.connectDevice(this.deviceId)
  215. this.connected = true
  216. this.$refs.ayToast.success('连接成功')
  217. } catch (e) {
  218. this.$refs.ayToast.error('连接失败: ' + (e.message || ''))
  219. } finally {
  220. this.connecting = false
  221. }
  222. },
  223. async disconnectDevice() {
  224. const bleStore = useBleStore()
  225. try {
  226. await bleStore.disconnect()
  227. this.connected = false
  228. this.$refs.ayToast.success('已断开连接')
  229. } catch (e) {
  230. this.$refs.ayToast.error('断开失败')
  231. }
  232. },
  233. delDevice() {
  234. const bleStore = useBleStore()
  235. uni.showModal({
  236. title: '提示',
  237. content: '此操作将删除该设备,是否继续?',
  238. cancelText: '取消',
  239. confirmText: '继续',
  240. success: (res) => {
  241. if (res.confirm) {
  242. if (this.connected) {
  243. bleStore.disconnect().catch(() => {})
  244. }
  245. // 从本地存储中移除
  246. try {
  247. let deviceList = uni.getStorageSync('deviceList') || []
  248. deviceList = deviceList.filter(item => item.deviceId !== this.deviceId)
  249. uni.setStorageSync('deviceList', deviceList)
  250. } catch (e) {}
  251. this.$refs.ayToast.success('设备删除成功')
  252. setTimeout(() => {
  253. uni.navigateBack()
  254. }, 1500)
  255. }
  256. }
  257. })
  258. }
  259. }
  260. }
  261. </script>
  262. <style>
  263. .container {
  264. flex: 1;
  265. background-color: #FFFFFF;
  266. }
  267. .status-bar {
  268. background-color: #FFFFFF;
  269. }
  270. .customHead {
  271. width: 750rpx;
  272. background-color: #FFFFFF;
  273. flex-direction: row;
  274. justify-content: space-between;
  275. height: 88rpx;
  276. align-items: center;
  277. padding-left: 54rpx;
  278. padding-right: 54rpx;
  279. }
  280. .back-wrap {
  281. width: 50rpx;
  282. height: 50rpx;
  283. justify-content: center;
  284. align-items: center;
  285. }
  286. .backimg {
  287. width: 50rpx;
  288. height: 50rpx;
  289. }
  290. .head-title {
  291. font-weight: bold;
  292. font-size: 32rpx;
  293. color: #545F71;
  294. }
  295. .plus {
  296. width: 50rpx;
  297. height: 50rpx;
  298. }
  299. .list {
  300. flex: 1;
  301. }
  302. .connect-status {
  303. flex-direction: row;
  304. align-items: center;
  305. margin-left: 38rpx;
  306. margin-right: 38rpx;
  307. margin-top: 30rpx;
  308. margin-bottom: 10rpx;
  309. padding-top: 24rpx;
  310. padding-bottom: 24rpx;
  311. padding-left: 30rpx;
  312. padding-right: 30rpx;
  313. background-color: #F8F9FB;
  314. border-radius: 12rpx;
  315. }
  316. .status-dot {
  317. width: 16rpx;
  318. height: 16rpx;
  319. border-radius: 16rpx;
  320. background-color: #CCCCCC;
  321. margin-right: 12rpx;
  322. }
  323. .status-text {
  324. font-size: 28rpx;
  325. color: #545F71;
  326. flex: 1;
  327. }
  328. .connect-btn {
  329. font-size: 26rpx;
  330. color: #389588;
  331. padding-top: 8rpx;
  332. padding-bottom: 8rpx;
  333. padding-left: 24rpx;
  334. padding-right: 24rpx;
  335. border-width: 1px;
  336. border-style: solid;
  337. border-color: #389588;
  338. border-radius: 24rpx;
  339. }
  340. .txt {
  341. margin-top: 50rpx;
  342. margin-left: 38rpx;
  343. margin-right: 38rpx;
  344. font-size: 28rpx;
  345. color: #9BA5B7;
  346. }
  347. .lastItem {
  348. border-bottom-width: 0;
  349. }
  350. .menuitem {
  351. margin-left: 38rpx;
  352. margin-right: 38rpx;
  353. flex-direction: row;
  354. align-items: center;
  355. justify-content: space-between;
  356. height: 104rpx;
  357. font-size: 32rpx;
  358. color: #545F71;
  359. border-bottom-width: 1px;
  360. border-bottom-style: solid;
  361. border-bottom-color: #EEF1F4;
  362. }
  363. .gray {
  364. font-size: 28rpx;
  365. color: #9BA5B7;
  366. padding-right: 20rpx;
  367. }
  368. .device-id-text {
  369. font-size: 22rpx;
  370. }
  371. .signal-value-wrap {
  372. flex-direction: row;
  373. align-items: center;
  374. padding-right: 20rpx;
  375. }
  376. .signal-wrap {
  377. flex-direction: row;
  378. align-items: flex-end;
  379. margin-right: 12rpx;
  380. height: 38rpx;
  381. padding-bottom: 4rpx;
  382. }
  383. .signal-bar {
  384. width: 8rpx;
  385. margin-left: 4rpx;
  386. border-radius: 4rpx;
  387. background-color: #ddd;
  388. }
  389. .signal-bar1 {
  390. height: 14rpx;
  391. }
  392. .signal-bar2 {
  393. height: 22rpx;
  394. }
  395. .signal-bar3 {
  396. height: 30rpx;
  397. }
  398. .signal-bar4 {
  399. height: 38rpx;
  400. }
  401. .signal-active {
  402. background-color: #389588;
  403. }
  404. .arrowright {
  405. width: 40rpx;
  406. height: 40rpx;
  407. }
  408. .menuitem-address {
  409. height: auto;
  410. min-height: 104rpx;
  411. padding-top: 24rpx;
  412. padding-bottom: 24rpx;
  413. }
  414. .address-wrap {
  415. flex: 1;
  416. margin-left: 20rpx;
  417. }
  418. .address-text {
  419. font-size: 28rpx;
  420. color: #9BA5B7;
  421. text-align: right;
  422. lines: 0;
  423. }
  424. .delbtn {
  425. width: 654rpx;
  426. height: 96rpx;
  427. background-color: #F3F3F3;
  428. border-radius: 12rpx;
  429. margin-top: 60rpx;
  430. margin-left: 48rpx;
  431. margin-right: 48rpx;
  432. justify-content: center;
  433. align-items: center;
  434. }
  435. .delbtn-text {
  436. font-size: 32rpx;
  437. color: #F65252;
  438. }
  439. </style>